feat: add commandapi

This commit is contained in:
darksaid98 committed 2023-10-25 04:26:01 +02:00
1 parent 95e51559b4
commit 1cf01d691f
6 files changed
+200 -26

No files matched your search

@@ -29,6 +29,10 @@ public class GunnerEquipment implements Cloneable {
return (GunnerEquipment) super.clone();
}
public boolean equals(String EquipmentId) {
return this.EquipmentId.equals(EquipmentId);
}
public Boolean Enabled = true;
public UUID EntityId;
public Entity Entity;
@@ -38,6 +42,7 @@ public class GunnerEquipment implements Cloneable {
public String WorldName;
public String EquipmentName = "Peckle Gun";
public String EquipmentId = this.EquipmentName.replaceAll(" ", "_").toLowerCase();
public int XOffset = 7;
public int YOffset = 0;
public int PitchOffset = 0;
@@ -8,6 +8,7 @@ import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import com.github.alathra.siegeengines.command.CommandHandler;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@@ -52,34 +53,19 @@ public class SiegeEngines extends JavaPlugin {
public static MetadataValueAdapter metadata;
public static Random random = new Random();
private static String Path;
private CommandHandler commandHandler;
//public static Towny towny;
@SuppressWarnings("deprecation")
@Override
public void onDisable() {
for (GunnerEquipment equip : equipment.values()) {
if (equip.Entity != null) {
ItemStack item = new ItemStack(Material.CARVED_PUMPKIN);
ItemMeta meta = item.getItemMeta();
equip.AmmoHolder = new EquipmentMagazine();
meta.setCustomModelData(equip.ReadyModelNumber);
meta.setDisplayName("§e" + equip.EquipmentName + " Item");
List<String> Lore = new ArrayList<String>();
Lore.add("§ePlace as a block to spawn a " + equip.EquipmentName + " or put on an Armor Stand.");
meta.setLore(Lore);
item.setItemMeta(meta);
((LivingEntity) equip.Entity).getEquipment().setHelmet(item);
}
}
public void onLoad() {
plugin = this;
commandHandler = new CommandHandler(this);
commandHandler.onLoad();
}
@SuppressWarnings("deprecation")
@Override
public void onEnable() {
plugin = this;
Path = this.getDataFolder().getAbsolutePath();
this.getCommand("SiegeEngines").setExecutor(new GunnersCommand());
getServer().getPluginManager().registerEvents(new RotationHandler(), this);
@@ -98,6 +84,29 @@ public class SiegeEngines extends JavaPlugin {
System.out.println("§eWeapon Projectile ItemStacks : " + proj);
}
}
commandHandler.onEnable();
}
@SuppressWarnings("deprecation")
@Override
public void onDisable() {
commandHandler.onDisable();
for (GunnerEquipment equip : equipment.values()) {
if (equip.Entity != null) {
ItemStack item = new ItemStack(Material.CARVED_PUMPKIN);
ItemMeta meta = item.getItemMeta();
equip.AmmoHolder = new EquipmentMagazine();
meta.setCustomModelData(equip.ReadyModelNumber);
meta.setDisplayName("§e" + equip.EquipmentName + " Item");
List<String> Lore = new ArrayList<String>();
Lore.add("§ePlace as a block to spawn a " + equip.EquipmentName + " or put on an Armor Stand.");
meta.setLore(Lore);
item.setItemMeta(meta);
((LivingEntity) equip.Entity).getEquipment().setHelmet(item);
}
}
}
public static FixedMetadataValue addMetaDataValue(Object value) {
@@ -215,6 +224,7 @@ public class SiegeEngines extends JavaPlugin {
if (fuelVelocityMod == null)
fuelVelocityMod = 0.95f;
equip.EquipmentName = name;
equip.EquipmentId = equip.EquipmentName.replaceAll(" ", "_").toLowerCase();
equip.XOffset = XOffset;
equip.YOffset = YOffset;
equip.RotateStandHead = true;
@@ -0,0 +1,31 @@
package com.github.alathra.siegeengines.command;
import com.github.alathra.siegeengines.SiegeEngines;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
/**
* A class to handle registration of commands.
*/
public class CommandHandler {
private final SiegeEngines instance;
public CommandHandler(SiegeEngines instance) {
this.instance = instance;
}
public void onLoad() {
CommandAPI.onLoad(new CommandAPIBukkitConfig(instance).shouldHookPaperReload(true).silentLogs(true));
}
public void onEnable() {
CommandAPI.onEnable();
// Register commands here
new SiegeEnginesCommand();
}
public void onDisable() {
CommandAPI.onDisable();
}
}
@@ -0,0 +1,98 @@
package com.github.alathra.siegeengines.command;
import com.github.alathra.siegeengines.GunnerEquipment;
import com.github.alathra.siegeengines.projectile.GunnersProjectile;
import com.github.milkdrinkers.colorparser.ColorParser;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.ArgumentSuggestions;
import dev.jorel.commandapi.arguments.PlayerArgument;
import dev.jorel.commandapi.arguments.StringArgument;
import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException;
import dev.jorel.commandapi.executors.CommandArguments;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static com.github.alathra.siegeengines.SiegeEngines.*;
public class SiegeEnginesCommand {
public SiegeEnginesCommand() {
new CommandAPICommand("siegeengines")
.withFullDescription("Example command.")
.withShortDescription("Example command.")
.withPermission("siegeengines.command")
.withSubcommands(
commandGetAll(),
commandReload()
)
.executesPlayer(this::onExecute)
.register();
}
private void onExecute(CommandSender sender, CommandArguments args) {
sender.sendMessage(ColorParser.of("<yellow>Incorrect usage, /SiegeEngines get, /SiegeEngines reload").build());
}
private CommandAPICommand commandGetAll() {
return new CommandAPICommand("getAll")
.withPermission("siegeengines.command.getAll")
.executesPlayer(this::onGetAll);
}
private CommandAPICommand commandReload() {
return new CommandAPICommand("reload")
.withPermission("siegeengines.command.reload")
.executesPlayer(this::onReload);
}
private void onGetAll(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException {
if (!(sender instanceof Player player))
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Only players can use this command.").build());
for (GunnerEquipment i : DefinedEquipment.values()) {
giveEquipment(player, i);
}
}
private void onReload(CommandSender sender, CommandArguments args) throws WrapperCommandSyntaxException {
if (!(sender instanceof Player))
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Only players can use this command.").build());
equipment.clear();
TrackedStands.clear();
DefinedEquipment.clear();
AddDefaults();
HashMap<ItemStack, GunnersProjectile> projObj = new HashMap<>();
GunnerEquipment equip = CreateNewGun(null, null, null, null, null, null, projObj);
for (GunnerEquipment i : DefinedEquipment.values()) {
sender.sendMessage(ColorParser.of("<yellow>Enabled Weapon : %s".formatted(i.EquipmentName)).build());
sender.sendMessage(ColorParser.of("<yellow>Weapon Propellant/\"Fuel\" ItemStacks : %s".formatted(i.FuelMaterial)).build());
for (ItemStack proj : i.Projectiles.keySet()) {
sender.sendMessage(ColorParser.of("<yellow>Weapon Projectile ItemStacks : %s".formatted(proj)).build());
}
}
sender.sendMessage("<yellow>Gunners Core configs reloaded");
}
private void giveEquipment(Player player, GunnerEquipment gunnerEquipment) {
ItemStack item = new ItemStack(Material.CARVED_PUMPKIN);
ItemMeta meta = item.getItemMeta();
meta.setCustomModelData(gunnerEquipment.ReadyModelNumber);
meta.displayName(ColorParser.of("<yellow>%s Item".formatted(gunnerEquipment.EquipmentName)).build());
List<Component> lore = new ArrayList<>();
lore.add(ColorParser.of("<yellow>Place as a block to spawn a '%s'".formatted(gunnerEquipment.EquipmentName)).build());
lore.add(ColorParser.of("<yellow>or put on an Armor Stand.").build());
lore.add(ColorParser.of("<yellow>Right click to toggle visibility of stand.").build());
meta.lore(lore);
item.setItemMeta(meta);
player.getInventory().addItem(item);
}
}