diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Main.java b/src/main/java/me/ShermansWorld/AlathraWar/Main.java index 99b9de3..03fb0b1 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Main.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Main.java @@ -1,6 +1,6 @@ package me.ShermansWorld.AlathraWar; -import me.ShermansWorld.AlathraWar.commands.*; +import me.ShermansWorld.AlathraWar.commands.CommandManager; import me.ShermansWorld.AlathraWar.data.WarData; import me.ShermansWorld.AlathraWar.hooks.TABHook; import me.ShermansWorld.AlathraWar.items.WarItemRegistry; @@ -9,20 +9,28 @@ import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; public class Main extends JavaPlugin { - public static Main instance; public static Economy econ; public static AlathraWarLogger warLogger; + private static Main instance; static { instance = null; econ = null; } + private final CommandManager commandManager = new CommandManager(Main.getInstance()); + + @NotNull + public static Main getInstance() { + return instance; + } + /** * Dangerous if done at the wrong time! */ @@ -35,10 +43,6 @@ public class Main extends JavaPlugin { WarData.setWars(WarData.createWars()); } - public static Main getInstance() { - return instance; - } - public static void initLogs() { File logsFolder = new File("plugins" + File.separator + "AlathraWar" + File.separator + "logs"); if (!logsFolder.exists()) { @@ -74,21 +78,21 @@ public class Main extends JavaPlugin { return econ != null; } + @Override + public void onLoad() { + Main.instance = this; + commandManager.onLoad(); + } + + @Override public void onEnable() { - instance = this; this.saveDefaultConfig(); initLogs(); new WarData(this); // new TimeoutData(this); - new WarCommands(this); - new SiegeCommands(this); - new RaidCommands(this); - new AdminCommands(this); - getCommand("war").setTabCompleter(new WarTabCompletion()); - getCommand("siege").setTabCompleter(new SiegeTabCompletion()); - getCommand("raid").setTabCompleter(new RaidTabCompletion()); - getCommand("alathrawaradmin").setTabCompleter(new AdminTabCompletion()); + + getServer().getPluginManager().registerEvents(new KillsListener(), this); getServer().getPluginManager().registerEvents(new CommandsListener(), this); getServer().getPluginManager().registerEvents(new JoinListener(), this); @@ -100,13 +104,17 @@ public class Main extends JavaPlugin { // //run second // new WarRecipeRegistry(); + commandManager.onEnable(); initData(); initAPIs(); setupEconomy(); initLogs(); } + @Override public void onDisable() { + commandManager.onDisable(); + for (War war : WarData.getWars()) { war.save(); } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Raid.java b/src/main/java/me/ShermansWorld/AlathraWar/Raid.java index dc7cf6f..25aa542 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Raid.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Raid.java @@ -68,18 +68,18 @@ public class Raid { //Ticks between activity, this is lower than siege because more activity is occuring in a raid private static final int incremental = 40; + final int[] bukkitId; private final String name; public final NamespacedKey bossBarKey = new NamespacedKey(Main.getInstance(), "raidBar." + Raid.this.getName()); + private final String defenders; public ArrayList activeRaiders = new ArrayList<>(); public ArrayList raiderPlayers = new ArrayList<>(); public ArrayList defenderPlayers = new ArrayList<>(); public Map lootedChunks; - final int[] bukkitId; private War war; private Town raidedTown; private Town gatherTown; private String raiders; - private final String defenders; private boolean side1AreRaiders; private int raiderScore; private int defenderScore; diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Siege.java b/src/main/java/me/ShermansWorld/AlathraWar/Siege.java index b4e318c..78b496a 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Siege.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Siege.java @@ -337,7 +337,7 @@ public class Siege { } int homeBlockY = 253; final Location beaconLoc = new Location(this.town.getWorld(), homeBlockX, - homeBlockY - 2, homeBlockZ); + homeBlockY - 2, homeBlockZ); while (beaconLoc.getBlock().getType().equals(Material.AIR)) { --homeBlockY; beaconLoc.setY(homeBlockY); @@ -357,13 +357,13 @@ public class Siege { this.beaconLocs.add( new Location(world, homeBlockX, homeBlockY - 1, homeBlockZ - 1)); this.beaconLocs.add(new Location(world, homeBlockX + 1, homeBlockY - 1, - homeBlockZ + 1)); + homeBlockZ + 1)); this.beaconLocs.add(new Location(world, homeBlockX + 1, homeBlockY - 1, - homeBlockZ - 1)); + homeBlockZ - 1)); this.beaconLocs.add(new Location(world, homeBlockX - 1, homeBlockY - 1, - homeBlockZ + 1)); + homeBlockZ + 1)); this.beaconLocs.add(new Location(world, homeBlockX - 1, homeBlockY - 1, - homeBlockZ - 1)); + homeBlockZ - 1)); for (int i = 2; i < this.beaconLocs.size(); ++i) { if (this.beaconLocs.get(i).getBlock().getType() != Material.AIR) { i = 1; diff --git a/src/main/java/me/ShermansWorld/AlathraWar/War.java b/src/main/java/me/ShermansWorld/AlathraWar/War.java index c421dbb..f224398 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/War.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/War.java @@ -10,10 +10,16 @@ import me.ShermansWorld.AlathraWar.data.WarData; import java.io.File; import java.util.ArrayList; +import java.util.List; +import java.util.UUID; public class War { + // References + private final ArrayList sieges = new ArrayList<>(); + private final ArrayList raids = new ArrayList<>(); // Object Fields + private final UUID uuid; // TODO Actually convert to using UUID for identifying this war private String name; private String side1; private String side2; @@ -22,10 +28,6 @@ public class War { private ArrayList surrenderedTowns = new ArrayList<>(); private int side1Points = 0; private int side2Points = 0; - - // References - private final ArrayList sieges = new ArrayList<>(); - private final ArrayList raids = new ArrayList<>(); private int lastRaidTimeSide1 = 0; private int lastRaidTimeSide2 = 0; @@ -37,6 +39,7 @@ public class War { * @param side2 - Side 2 name */ public War(final String name, final String side1, final String side2) { + this.uuid = generateUUID(); this.name = name; this.side1 = side1; this.side2 = side2; @@ -58,6 +61,16 @@ public class War { return returnList; } + private UUID generateUUID() { + UUID uuid = UUID.randomUUID(); + + while (WarData.getWar(uuid) != null) { + uuid = UUID.randomUUID(); + } + + return UUID.randomUUID(); + } + /** * Add a nation to a side of the war * @@ -147,6 +160,10 @@ public class War { surrenderedTowns.remove(town); } + public UUID getUUID() { + return this.uuid; + } + /** * Gets side of town * -1 - Surrendered @@ -186,6 +203,10 @@ public class War { this.save(); } + public List getSides() { + return List.of(getSide1(), getSide2()); + } + public String getSide1() { return this.side1; } @@ -302,4 +323,12 @@ public class War { public void addSide2Points(int points) { side2Points += points; } + + public boolean equals(War war) { + return getUUID().equals(war.getUUID()); + } + + public boolean equals(UUID uuid) { + return getUUID().equals(uuid); + } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/CommandManager.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/CommandManager.java new file mode 100644 index 0000000..e59247d --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/CommandManager.java @@ -0,0 +1,32 @@ +package me.ShermansWorld.AlathraWar.commands; + +import dev.jorel.commandapi.CommandAPI; +import dev.jorel.commandapi.CommandAPIBukkitConfig; +import me.ShermansWorld.AlathraWar.Main; + +public class CommandManager { + private final Main instance; + + public CommandManager(Main instance) { + this.instance = instance; + } + + public void onLoad() { + CommandAPI.onLoad(new CommandAPIBukkitConfig(instance).shouldHookPaperReload(true).silentLogs(true)); + } + + public void onEnable() { + CommandAPI.onEnable(); + + // Register commands + new WarCommand(); + new SiegeCommand(); + new RaidCommand(); + new AdminCommands(instance); +// getCommand("alathrawaradmin").setTabCompleter(new AdminTabCompletion()); + } + + public void onDisable() { + CommandAPI.onDisable(); + } +} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommand.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommand.java new file mode 100644 index 0000000..3ab7f57 --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommand.java @@ -0,0 +1,767 @@ +package me.ShermansWorld.AlathraWar.commands; + +import com.github.milkdrinkers.colorparser.ColorParser; +import com.palmergames.bukkit.towny.TownyAPI; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.exceptions.TownyException; +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.TownyWorld; +import com.palmergames.bukkit.towny.object.WorldCoord; +import dev.jorel.commandapi.CommandAPIBukkit; +import dev.jorel.commandapi.CommandAPICommand; +import dev.jorel.commandapi.arguments.ArgumentSuggestions; +import dev.jorel.commandapi.arguments.BooleanArgument; +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 me.ShermansWorld.AlathraWar.*; +import me.ShermansWorld.AlathraWar.data.RaidData; +import me.ShermansWorld.AlathraWar.data.RaidPhase; +import me.ShermansWorld.AlathraWar.data.SiegeData; +import me.ShermansWorld.AlathraWar.data.WarData; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collections; + +public class RaidCommand { + + public RaidCommand() { + new CommandAPICommand("raid") + .withSubcommands( + commandRaid(), + commandStop(), + commandJoin(), + commandLeave(), + commandAbandon(), + commandList(), + commandHelp() + ) + .executesPlayer((sender, args) -> { + if (args.count() == 0) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "Invalid Arguments. /siege help").build()); + }) + .register(); + } + + public static CommandAPICommand commandRaid() { + return new CommandAPICommand("raid") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.strings( + WarData.getWarsNames() + ) + ), + new StringArgument("target") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + } + ) + ), + new StringArgument("gathertown") + .setOptional(true) + .withPermission("AlathraWar.admin") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { // TODO Make getHostileTowns method where we reverse from list + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + } + ) + ), + new PlayerArgument("leader") + .setOptional(true) + .withPermission("AlathraWar.admin"), + new BooleanArgument("minutemen") + .setOptional(true) + .withPermission("AlathraWar.admin") + + ) + .executesPlayer((Player p, CommandArguments args) -> raidStart(p, args, false)); + } + + public static CommandAPICommand commandStop() { + return new CommandAPICommand("stop") + .withPermission("AlathraWar.admin") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> + WarData.getWarsNames() + ) + ), + new StringArgument("town") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + }) + ) + ) + .executesPlayer(RaidCommand::raidStop); + } + + public static CommandAPICommand commandJoin() { + return new CommandAPICommand("join") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> + WarData.getWarsNames() + ) + ), + new StringArgument("town") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + }) + ), + new PlayerArgument("player") + .setOptional(true) + .withPermission("AlathraWar.admin"), + new StringArgument("side") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + String warName = (String) info.previousArgs().get("warname"); + + War war = WarData.getWar(warName); + if (war != null) { + return war.getSides(); + } + + return Collections.emptyList(); + }) + ), + new BooleanArgument("minutemen") + .setOptional(true) + .withPermission("AlathraWar.admin") + + ) + .executesPlayer((Player p, CommandArguments args) -> raidJoin(p, args, false)); + } + + public static CommandAPICommand commandLeave() { + return new CommandAPICommand("leave") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> + WarData.getWarsNames() + ) + ), + new StringArgument("town") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + }) + ), + new PlayerArgument("player") + .setOptional(true) + .withPermission("AlathraWar.admin") + ) + .executesPlayer((Player p, CommandArguments args) -> raidLeave(p, args, false)); + } + + public static CommandAPICommand commandAbandon() { + return new CommandAPICommand("abandon") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> + WarData.getWarsNames() + ) + ), + new StringArgument("town") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + }) + ) + ) + .executesPlayer(RaidCommand::raidAbandon); + } + + public static CommandAPICommand commandList() { + return new CommandAPICommand("list") + .executesPlayer(RaidCommand::raidsList); + } + + public static CommandAPICommand commandHelp() { + return new CommandAPICommand("help") + .executesPlayer(RaidCommand::raidHelp); + } + + /** + * Starts a raid + * + * @param p + * @param args + */ + protected static void raidStart(Player p, CommandArguments args, boolean admin) throws WrapperCommandSyntaxException { + boolean warFound = false; + boolean townExists = false; + + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name.").build()); + + for (final War war : WarData.getWars()) { + if (war.getName().equalsIgnoreCase(argWarName)) { + warFound = true; + + final Player raidOwner = (Player) args.getOptional("leader").orElse(p); + + if (raidOwner == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser("Raid Owner is Null").build()); + } + + TownyWorld townyWorld; + townyWorld = WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownyWorld(); + + String side; + if (war.getSide1Players().contains(raidOwner.getName())) { + side = war.getSide1(); + } else if (war.getSide2Players().contains(raidOwner.getName())) { + side = war.getSide2(); + } else { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]").build()); + } + + //Minuteman countermeasures + int minuteman = CommandHelper.isPlayerMinuteman(raidOwner.getName()); + boolean minuteManOverride = (boolean) args.getOptional("minutemen").orElse(false); + + if (minuteManOverride) { + p.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); + } + + //minuteManOverride? + if (!minuteManOverride) { + if (minuteman != 0) { + if (minuteman == 1) { + //player has joined to recently + if (admin) + p.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + raidOwner.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + return; + } else if (minuteman == 2) { + //player has played too little + if (admin) + p.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + raidOwner.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + return; + } + } + } + + //check if in a town + //takes the command runners town as the gather point + Town gatherTown; + try { + @Nullable String argGatherTown = (String) args.getOptional("gathertown").orElse(null); + + if (admin && argGatherTown != null) { + if (argGatherTown.equalsIgnoreCase("defaultCode")) { + //use default behavior + gatherTown = TownyAPI.getInstance().getTownOrNull(WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownBlock()); + } else if (TownyAPI.getInstance().getTown(argGatherTown) != null) { + gatherTown = TownyAPI.getInstance().getTown(argGatherTown); + } else { + if (admin) + p.sendMessage(Helper.chatLabel() + "No valid town set for gather town, defaulting to current location"); + raidOwner.sendMessage(Helper.chatLabel() + "No valid town set for gather town, defaulting to current location"); + gatherTown = TownyAPI.getInstance().getTownOrNull(WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownBlock()); + } + } else { + gatherTown = TownyAPI.getInstance().getTownOrNull(WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownBlock()); + } + } catch (NotRegisteredException e2) { + if (admin) + p.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); + raidOwner.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); + return; + } + if (gatherTown == null) { + if (admin) + p.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); + raidOwner.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); + return; + } + + //parse the argunment for town + for (final String entry : townyWorld.getTowns().keySet()) { + final Town raidedTown = townyWorld.getTowns().get(entry); + + //if this is run as admin, shift our check forward a slot + if (raidedTown.getName().equalsIgnoreCase((String) args.get("target")) && gatherTown != null) { + + Main.warLogger.log("Raided town: " + raidedTown.getName()); + Main.warLogger.log("Gather town: " + gatherTown.getName()); + Main.warLogger.log("Raid Owner: " + raidOwner.getName()); + p.sendMessage("Raided town: " + raidedTown.getName()); + p.sendMessage("Gather town: " + gatherTown.getName()); + p.sendMessage("Raid Owner: " + raidOwner.getName()); + //DEBUG PRINT + for (String t : war.getSide1Towns()) { + Main.warLogger.log("Side1 town: " + t); + p.sendMessage("Side1 town: " + t); + } + for (String t : war.getSide2Towns()) { + Main.warLogger.log("Side2 town: " + t); + p.sendMessage("Side2 town: " + t); + } + + //check if the town were attempting to raid is in the war + if (!war.getSide1Towns().contains(raidedTown.getName().toLowerCase()) && !war.getSide2Towns().contains(raidedTown.getName().toLowerCase())) { + if (war.getSurrenderedTowns().contains(raidedTown.getName().toLowerCase())) { + if (admin) + p.sendMessage(Helper.chatLabel() + "The town you are trying to raid has already surrendered!"); + raidOwner.sendMessage(Helper.chatLabel() + "The town you are trying to raid has already surrendered!"); + return; + } + if (admin) + p.sendMessage(Helper.chatLabel() + "The town you are trying to raid is not part of the war!"); + raidOwner.sendMessage(Helper.chatLabel() + "The town you are trying to raid is not part of the war!"); + return; + } + + // Is being sieged check + for (Siege s : SiegeData.getSieges()) { + if (s.getTown().getName().equalsIgnoreCase(raidedTown.getName())) { + raidOwner.sendMessage(Helper.chatLabel() + "That town is already currently being sieged! Cannot raid at this time!"); + if (admin) + p.sendMessage(Helper.chatLabel() + "That town is already currently being sieged! Cannot raid at this time!"); + } + } + + townExists = true; + + Raid raid2; + if (war.getSide1Players().contains(raidOwner.getName())) { + //Time and raid activity validity check + int c = RaidData.isValidRaid(war, side, raidedTown); + if (c == 2) { + //if were admin, see if an owner arg exists, and if so then use it + raid2 = new Raid(war, raidedTown, gatherTown, true, raidOwner); + } else if (c == 1) { + if (admin) + p.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); + raidOwner.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); + return; + } else if (c == 0) { + if (admin) + p.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); + raidOwner.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); + return; + } else if (c == -1) { + if (admin) + p.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); + raidOwner.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); + return; + } else if (c == -2) { + if (admin) + p.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); + raidOwner.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); + return; + } else { + throw new IllegalArgumentException(); + } + } else { + //Something broke if this runs + if (!war.getSide2Players().contains(raidOwner.getName())) { + Bukkit.getLogger().info("Unable to find player declaring raid in the war"); + return; + } + //Time and raid activity validity check + int c = RaidData.isValidRaid(war, side, raidedTown); + if (c == 2) { + raid2 = new Raid(war, raidedTown, gatherTown, false, raidOwner); + } else if (c == 1) { + if (admin) + p.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); + raidOwner.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); + return; + } else if (c == 0) { + if (admin) + p.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); + raidOwner.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); + return; + } else if (c == -1) { + if (admin) + p.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); + raidOwner.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); + return; + } else if (c == -2) { + if (admin) + p.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); + raidOwner.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); + return; + } else { + throw new IllegalArgumentException(); + } + } + + // Player participance check + Town leaderTown = TownyAPI.getInstance().getResident(raidOwner).getTownOrNull(); + int sideR = war.getSide(leaderTown.getName().toLowerCase()); + if (sideR == 0) { + if (admin) p.sendMessage(Helper.chatLabel() + "You are not in this war."); + raidOwner.sendMessage(Helper.chatLabel() + "You are not in this war."); + return; + } else if (sideR == -1) { + if (admin) p.sendMessage(Helper.chatLabel() + "You have surrendered."); + raidOwner.sendMessage(Helper.chatLabel() + "You have surrendered."); + return; + } + + // Attacking own side + if (war.getSide(raidedTown) == sideR) { + if (admin) p.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); + raidOwner.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); + return; + } + + //check player balance + final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(raidOwner.getUniqueId()); + if (Main.econ.getBalance(offlinePlayer) <= 1000.0) { + if (admin) + p.sendMessage(Helper.chatLabel() + "You must have at least $1000 to put up to start a raid."); + raidOwner.sendMessage(Helper.chatLabel() + "You must have at least $1000 to put up to start a raid."); + return; + } + + //publish raid + Main.econ.withdrawPlayer(offlinePlayer, 1000); + raid2.addActiveRaider(raidOwner.getName()); + RaidData.addRaid(raid2); + raid2.start(); //funny funny method haha + WarData.saveWar(raid2.getWar()); + + //broadcast + Bukkit.broadcastMessage(Helper.chatLabel() + "As part of " + war.getName() + ", forces from " + raid2.getRaiderSide() + " are gathering to raid the town of " + raidedTown.getName() + "!"); + Bukkit.broadcastMessage(Helper.chatLabel() + "All players from the defending side have been drafted for the town's defense."); + Bukkit.broadcastMessage(Helper.chatLabel() + "The raid of " + + raidedTown.getName() + " will begin in " + (RaidPhase.TRAVEL.startTick / 20 / 60) + " minutes!"); + Bukkit.broadcastMessage(Helper.chatLabel() + + "The Raiders are gathering at " + gatherTown.getName() + " before making the journey over!"); + + Main.warLogger.log(raidOwner.getName() + " started a raid."); + if (admin) Main.warLogger.log(raidOwner.getName() + " started a raid from admin interference."); + Main.warLogger.log("As part of " + war.getName() + ", forces from " + raid2.getRaiderSide() + " are raiding the town of " + raidedTown.getName() + "!"); + Main.warLogger.log("The Raiders are gathering at " + gatherTown.getName() + " before making the journey over!"); + } + } + } + } + if (!warFound) { + p.sendMessage(Helper.chatLabel() + "That war does not exist! /raid start [war] [town]"); + return; + } + if (!townExists) { + p.sendMessage(Helper.chatLabel() + "That town does not exist! /raid start [war] [town]"); + } + } + + /** + * Stops the current raid by admins + * + * @param p + * @param args + */ + private static void raidStop(Player p, CommandArguments args) throws WrapperCommandSyntaxException { + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name!").build()); + + if (!(args.get("town") instanceof final String argTown)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a town name!").build()); + + + //are there even any raids + if (RaidData.getRaids().isEmpty()) { + fail(p, args, "noRaids"); + } + + //find our desired raid and stop it + Raid raid = RaidData.getRaidOrNull(argWarName + "-" + argTown); + + if (raid != null) { + p.sendMessage(Helper.chatLabel() + "raid cancelled"); + Bukkit.broadcastMessage(Helper.chatLabel() + "The raid of " + raid.getRaidedTown().getName() + " has been cancelled by an admin"); + raid.stop(); //this purges itself from raids + } else { + fail(p, args, "badName"); + } + } + + protected static void raidJoin(Player p, CommandArguments args, boolean admin) throws WrapperCommandSyntaxException { + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name!").build()); + + if (!(args.get("town") instanceof final String argTown)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a town name!").build()); + + //Grab raid! + Raid raid = RaidData.getRaidOrNull(argWarName + "-" + argTown); + + if (raid == null) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "No raid is gathering in this town or this town does not exist!").build()); + + if (raid.getWar().getName().equalsIgnoreCase(argWarName)) { + final Player joiner = (Player) args.getOptional("player").orElse(p); + + if (joiner == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser("Joiner is Null").build()); + } + + if (!(args.get("side") instanceof final String argSide)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a side!").build()); + + if (admin) { + if (argSide != null) { + if (argSide.equalsIgnoreCase(raid.getDefenderSide())) { + if (!raid.getDefenderPlayers().contains(joiner.getName())) + raid.getDefenderPlayers().add(joiner.getName()); + + p.sendMessage(Helper.chatLabel() + "Player " + joiner.getName() + " added to defender side."); + Main.warLogger.log("Player " + joiner.getName() + " added to defender side."); + joiner.sendMessage(Helper.chatLabel() + "You were forcefully added to the defender side against the raid party on " + raid.getRaidedTown() + " by an admin."); + return; + } else { + if (!raid.getRaiderPlayers().contains(joiner.getName())) + raid.getRaiderPlayers().add(joiner.getName()); + + p.sendMessage(Helper.chatLabel() + "Player " + joiner.getName() + " added to raider side."); + Main.warLogger.log("Player " + joiner.getName() + " added to raider side."); + joiner.sendMessage(Helper.chatLabel() + "You were forcefully added to the raid party on " + raid.getRaidedTown() + " by an admin. Leaving the gather town will remove you from the raid party."); + } + } + + } + + if (!raid.getWar().getSide1Players().contains(joiner.getName()) && !raid.getWar().getSide2Players().contains(joiner.getName())) { + joiner.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); + } + + //Minutemen countermeasures, 86400 * 4 time. 86400 seconds in a day, 4 days min playtime + //also 12 hours min platim by default + int minuteman = CommandHelper.isPlayerMinuteman(joiner.getName()); + boolean minuteManOverride = (boolean) args.getOptional("minutemen").orElse(false); + + if (minuteManOverride) { + p.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); + } + + if (!minuteManOverride) { + if (minuteman != 0) { + if (minuteman == 1) { + //player has joined to recently + if (admin) + p.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + joiner.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + return; + } else if (minuteman == 2) { + //player has played too little + if (admin) + p.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + joiner.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + return; + } + } + } + + //check if gather phase + if (!admin) { + if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START) { + try { + //check if the player is in the gather town + ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); + if (cluster.contains(WorldCoord.parseWorldCoord(joiner))) { + raid.addActiveRaider(joiner.getName()); + joiner.sendMessage(Helper.chatLabel() + "You have joined the raid on " + raid.getRaidedTown().getName() + "!"); + joiner.sendMessage(Helper.chatLabel() + "Leaving " + raid.getGatherTown().getName() + " will cause you to leave the raid party."); + } else { + joiner.sendMessage(Helper.chatLabel() + "You must be in the gathering town to join the raid on " + raid.getRaidedTown().getName() + "."); + } + } catch (TownyException e) { + throw new RuntimeException(e); + } + } else { + joiner.sendMessage(Helper.chatLabel() + "You cannot join the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); + } + } else { + try { + //check if the player is in the gather town + ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); + if (cluster.contains(WorldCoord.parseWorldCoord(joiner))) { + raid.addActiveRaider(joiner.getName()); + joiner.sendMessage(Helper.chatLabel() + "You have joined the raid on " + raid.getRaidedTown().getName() + "!"); + joiner.sendMessage(Helper.chatLabel() + "Leaving " + raid.getGatherTown().getName() + " will cause you to leave the raid party."); + } else { + joiner.sendMessage(Helper.chatLabel() + "You must be in the gathering town to join the raid on " + raid.getRaidedTown().getName() + "."); + } + } catch (TownyException e) { + throw new RuntimeException(e); + } + } + + raid.save(); + } + } + + protected static void raidLeave(Player player, CommandArguments args, boolean admin) throws WrapperCommandSyntaxException { + //Get raid! + final Player p = (Player) args.getOptional("leader").orElse(player); + + if (p == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser("Raided is Null").build()); + } + + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name!").build()); + + if (!(args.get("town") instanceof final String argTown)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a town name!").build()); + + + Raid raid = RaidData.getRaidOrNull(argWarName + "-" + argTown); + if (raid == null) { + p.sendMessage(Helper.chatLabel() + "No raid is gathering in this town or this town does not exist!"); + if (admin) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "No raid is gathering in this town or this town does not exist!").build()); + } + + if (raid.getWar().getName().equalsIgnoreCase(argWarName)) { + if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { + p.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); + if (admin) + player.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); + } + + //check if gather phase + if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START) { + try { + //check if player is in gather town + ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); + if (cluster.contains(WorldCoord.parseWorldCoord(p))) { + raid.removeActiveRaider(p.getName()); + p.sendMessage(Helper.chatLabel() + "You have left the raid on " + raid.getRaidedTown().getName() + "!"); + if (admin) + player.sendMessage(Helper.chatLabel() + "You have left the raid on " + raid.getRaidedTown().getName() + "!"); + + } else { + p.sendMessage(Helper.chatLabel() + "You cannot leave the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); + if (admin) + player.sendMessage(Helper.chatLabel() + "You cannot leave the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); + } + } catch (TownyException e) { + throw new RuntimeException(e); + } + } + } + + raid.save(); + } + + /** + * Abandons the raid + * + * @param p + * @param args + */ + private static void raidAbandon(Player p, CommandArguments args) throws WrapperCommandSyntaxException { + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name!").build()); + + if (!(args.get("town") instanceof final String argTown)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a town name!").build()); + + + //Get raid + Raid raid = RaidData.getRaidOrNull(argWarName + "-" + argTown); + + if (raid == null) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cThis raid does not exist!").build()); + + + if (raid.getWar().getName().equalsIgnoreCase(argWarName)) { + if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou are not in this war! Type /war join [war] [side]").build()); + } + + if (raid.getOwner().getName().equalsIgnoreCase(p.getName())) { + //check if gather phase + if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START || raid.getPhase() == RaidPhase.TRAVEL || raid.getPhase() == RaidPhase.COMBAT) { + //force defender victory + Bukkit.broadcast(new ColorParser(Helper.chatLabel() + "The raid on " + raid.getRaidedTown().getName() + " has been abandoned by " + raid.getRaiderSide()).build()); + Main.warLogger.log(p.getName() + " abandoned the raid on " + raid.getRaidedTown().getName() + " they started at " + raid.getGatherTown().getName()); + raid.defendersWin(raid.getRaiderScore(), raid.getDefenderScore()); + } else { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou cannot abandon the raid on " + raid.getRaidedTown().getName() + "! It has a problem!").build()); + } + } else { + fail(p, args, "owner"); + } + } + } + + private static void raidsList(Player p, CommandArguments args) { + if (RaidData.getRaids().isEmpty()) { + p.sendMessage(new ColorParser(Helper.chatLabel() + "There are currently no Raids in progress").build()); + return; + } + + p.sendMessage(new ColorParser(Helper.chatLabel() + "Raids currently in progress:").build()); + for (final Raid raid : RaidData.getRaids()) { + p.sendMessage(new ColorParser("Name: " + raid.getName()).build()); + p.sendMessage(new ColorParser("War: " + raid.getWar().getName()).build()); + p.sendMessage(new ColorParser("Owner: " + raid.getOwner().getName()).build()); + p.sendMessage(new ColorParser("Raiding: " + raid.getRaidedTown().getName().toLowerCase()).build()); + p.sendMessage(new ColorParser("Gathering In: " + raid.getGatherTown().getName().toLowerCase()).build()); + p.sendMessage(new ColorParser("Raiders: " + raid.getRaiderSide()).build()); + p.sendMessage(new ColorParser("Defenders: " + raid.getDefenderSide()).build()); + p.sendMessage(new ColorParser("Raider Score: " + raid.getRaiderScore()).build()); + p.sendMessage(new ColorParser("Defender Score: " + raid.getDefenderScore()).build()); + p.sendMessage(new ColorParser("Raid Phase: " + raid.getPhase().name()).build()); + p.sendMessage(new ColorParser("Time Left: " + (RaidPhase.END.startTick - raid.getRaidTicks()) / 1200 + " minutes").build()); + p.sendMessage(new ColorParser("-=-=-=-=-=-=-=-=-=-=-=-").build()); + } + } + + private static void raidHelp(Player p, CommandArguments args) { + if (p.hasPermission("AlathraWar.admin")) { + p.sendMessage(new ColorParser(Helper.chatLabel() + "/raid stop [name]").build()); + } + p.sendMessage(new ColorParser(Helper.chatLabel() + "/raid start [war] [town]").build()); + p.sendMessage(new ColorParser(Helper.chatLabel() + "/raid join [war] [town]").build()); + p.sendMessage(new ColorParser(Helper.chatLabel() + "/raid leave [war] [town]").build()); + p.sendMessage(new ColorParser(Helper.chatLabel() + "/raid abandon [war] [town]").build()); + p.sendMessage(new ColorParser(Helper.chatLabel() + "/raid list").build()); + } + + private static void fail(CommandSender p, CommandArguments args, String type) throws WrapperCommandSyntaxException { + switch (type) { + case "permissions" -> + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou do not have permission to do this.").build()); + case "syntax" -> + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cInvalid Syntax. /raid help").build()); + case "noRaids" -> + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cThere are currently no Raids in progress.").build()); + case "badName" -> + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cA raid could not be found with this Name! Type /raid list to view current raids.").build()); + case "owner" -> + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cOnly the player who started the raid can abandon it.").build()); + default -> + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cInvalid Arguments. /raid help").build()); + } + } +} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java deleted file mode 100644 index 2784141..0000000 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java +++ /dev/null @@ -1,629 +0,0 @@ -package me.ShermansWorld.AlathraWar.commands; - -import com.palmergames.bukkit.towny.TownyAPI; -import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; -import com.palmergames.bukkit.towny.exceptions.TownyException; -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyWorld; -import com.palmergames.bukkit.towny.object.WorldCoord; -import me.ShermansWorld.AlathraWar.*; -import me.ShermansWorld.AlathraWar.data.RaidData; -import me.ShermansWorld.AlathraWar.data.RaidPhase; -import me.ShermansWorld.AlathraWar.data.SiegeData; -import me.ShermansWorld.AlathraWar.data.WarData; -import net.md_5.bungee.api.ChatColor; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.ArrayList; - -public class RaidCommands implements CommandExecutor { - - public RaidCommands(final Main plugin) { - plugin.getCommand("raid").setExecutor(this); - } - - /** - * Starts a raid - * - * @param p - * @param args - */ - protected static void startRaid(CommandSender p, String[] args, boolean admin) { - boolean warFound = false; - boolean townExists = false; - for (final War war : WarData.getWars()) { - //if this is run as admin, shift our check forward a slot - if (war.getName().equalsIgnoreCase(args[1 + (admin ? 1 : 0)])) { - warFound = true; - - Player raidOwner = null; - if ((p instanceof Player)) { - raidOwner = (Player) p; - } - if (admin && args.length >= 6) { - raidOwner = Bukkit.getPlayer(args[5]); - if (raidOwner == null) { - p.sendMessage("Player Does not exist"); - return; - } - } - - if (raidOwner == null) { - p.sendMessage("Raid Owner is Null"); - return; - } - - TownyWorld townyWorld; - townyWorld = WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownyWorld(); - - String side; - if (war.getSide1Players().contains(raidOwner.getName())) { - side = war.getSide1(); - } else if (war.getSide2Players().contains(raidOwner.getName())) { - side = war.getSide2(); - } else { - p.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); - return; - } - - //Minuteman countermeasures - int minuteman = CommandHelper.isPlayerMinuteman(raidOwner.getName()); - boolean override = false; - if (admin) { - if (args.length >= 7) { - if (Boolean.parseBoolean(args[6])) { - //player has joined to recently - p.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); - override = true; - } - } - } - //override? - if (!override) { - if (minuteman != 0) { - if (minuteman == 1) { - //player has joined to recently - if (admin) - p.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - raidOwner.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - return; - } else if (minuteman == 2) { - //player has played too little - if (admin) - p.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - raidOwner.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - return; - } - } - } - - //check if in a town - //takes the command runners town as the gather point - Town gatherTown; - try { - //if admin, and we have a gather town argument, use it - if (admin && args.length >= 5) { - if (args[4].equalsIgnoreCase("defaultCode")) { - //use default behavior - gatherTown = TownyAPI.getInstance().getTownOrNull(WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownBlock()); - } else if (TownyAPI.getInstance().getTown(args[4]) != null) { - gatherTown = TownyAPI.getInstance().getTown(args[4]); - } else { - if (admin) - p.sendMessage(Helper.chatLabel() + "No valid town set for gather town, defaulting to current location"); - raidOwner.sendMessage(Helper.chatLabel() + "No valid town set for gather town, defaulting to current location"); - gatherTown = TownyAPI.getInstance().getTownOrNull(WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownBlock()); - } - } else { - gatherTown = TownyAPI.getInstance().getTownOrNull(WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownBlock()); - } - } catch (NotRegisteredException e2) { - if (admin) - p.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); - raidOwner.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); - return; - } - if (gatherTown == null) { - if (admin) - p.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); - raidOwner.sendMessage(Helper.chatLabel() + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); - return; - } - - //parse the argunment for town - for (final String entry : townyWorld.getTowns().keySet()) { - final Town raidedTown = townyWorld.getTowns().get(entry); - - //if this is run as admin, shift our check forward a slot - if (raidedTown.getName().equalsIgnoreCase(args[2 + (admin ? 1 : 0)]) && gatherTown != null) { - - Main.warLogger.log("Raided town: " + raidedTown.getName()); - Main.warLogger.log("Gather town: " + gatherTown.getName()); - Main.warLogger.log("Raid Owner: " + raidOwner.getName()); - p.sendMessage("Raided town: " + raidedTown.getName()); - p.sendMessage("Gather town: " + gatherTown.getName()); - p.sendMessage("Raid Owner: " + raidOwner.getName()); - //DEBUG PRINT - for (String t : war.getSide1Towns()) { - Main.warLogger.log("Side1 town: " + t); - p.sendMessage("Side1 town: " + t); - } - for (String t : war.getSide2Towns()) { - Main.warLogger.log("Side2 town: " + t); - p.sendMessage("Side2 town: " + t); - } - - //check if the town were attempting to raid is in the war - if (!war.getSide1Towns().contains(raidedTown.getName().toLowerCase()) && !war.getSide2Towns().contains(raidedTown.getName().toLowerCase())) { - if (war.getSurrenderedTowns().contains(raidedTown.getName().toLowerCase())) { - if (admin) - p.sendMessage(Helper.chatLabel() + "The town you are trying to raid has already surrendered!"); - raidOwner.sendMessage(Helper.chatLabel() + "The town you are trying to raid has already surrendered!"); - return; - } - if (admin) - p.sendMessage(Helper.chatLabel() + "The town you are trying to raid is not part of the war!"); - raidOwner.sendMessage(Helper.chatLabel() + "The town you are trying to raid is not part of the war!"); - return; - } - - // Is being sieged check - for (Siege s : SiegeData.getSieges()) { - if (s.getTown().getName().equalsIgnoreCase(raidedTown.getName())) { - raidOwner.sendMessage(Helper.chatLabel() + "That town is already currently being sieged! Cannot raid at this time!"); - if (admin) - p.sendMessage(Helper.chatLabel() + "That town is already currently being sieged! Cannot raid at this time!"); - } - } - - townExists = true; - - Raid raid2; - if (war.getSide1Players().contains(raidOwner.getName())) { - //Time and raid activity validity check - int c = RaidData.isValidRaid(war, side, raidedTown); - if (c == 2) { - //if were admin, see if an owner arg exists, and if so then use it - raid2 = new Raid(war, raidedTown, gatherTown, true, raidOwner); - } else if (c == 1) { - if (admin) - p.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); - raidOwner.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); - return; - } else if (c == 0) { - if (admin) - p.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); - raidOwner.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); - return; - } else if (c == -1) { - if (admin) - p.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); - raidOwner.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); - return; - } else if (c == -2) { - if (admin) - p.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); - raidOwner.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); - return; - } else { - throw new IllegalArgumentException(); - } - } else { - //Something broke if this runs - if (!war.getSide2Players().contains(raidOwner.getName())) { - Bukkit.getLogger().info("Unable to find player declaring raid in the war"); - return; - } - //Time and raid activity validity check - int c = RaidData.isValidRaid(war, side, raidedTown); - if (c == 2) { - raid2 = new Raid(war, raidedTown, gatherTown, false, raidOwner); - } else if (c == 1) { - if (admin) - p.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); - raidOwner.sendMessage(Helper.chatLabel() + "This town is already being raided at this time!"); - return; - } else if (c == 0) { - if (admin) - p.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); - raidOwner.sendMessage(Helper.chatLabel() + "Your side has raided too recently!"); - return; - } else if (c == -1) { - if (admin) - p.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); - raidOwner.sendMessage(Helper.chatLabel() + "This town was raided too recently!"); - return; - } else if (c == -2) { - if (admin) - p.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); - raidOwner.sendMessage(Helper.chatLabel() + "At least on member of the raided town must be online to defend it!"); - return; - } else { - throw new IllegalArgumentException(); - } - } - - // Player participance check - Town leaderTown = TownyAPI.getInstance().getResident(raidOwner).getTownOrNull(); - int sideR = war.getSide(leaderTown.getName().toLowerCase()); - if (sideR == 0) { - if (admin) p.sendMessage(Helper.chatLabel() + "You are not in this war."); - raidOwner.sendMessage(Helper.chatLabel() + "You are not in this war."); - return; - } else if (sideR == -1) { - if (admin) p.sendMessage(Helper.chatLabel() + "You have surrendered."); - raidOwner.sendMessage(Helper.chatLabel() + "You have surrendered."); - return; - } - - // Attacking own side - if (war.getSide(raidedTown) == sideR) { - if (admin) p.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); - raidOwner.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); - return; - } - - //check player balance - final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(raidOwner.getUniqueId()); - if (Main.econ.getBalance(offlinePlayer) <= 1000.0) { - if (admin) - p.sendMessage(Helper.chatLabel() + "You must have at least $1000 to put up to start a raid."); - raidOwner.sendMessage(Helper.chatLabel() + "You must have at least $1000 to put up to start a raid."); - return; - } - - //publish raid - Main.econ.withdrawPlayer(offlinePlayer, 1000); - raid2.addActiveRaider(raidOwner.getName()); - RaidData.addRaid(raid2); - raid2.start(); //funny funny method haha - WarData.saveWar(raid2.getWar()); - - //broadcast - Bukkit.broadcastMessage(Helper.chatLabel() + "As part of " + war.getName() + ", forces from " + raid2.getRaiderSide() + " are gathering to raid the town of " + raidedTown.getName() + "!"); - Bukkit.broadcastMessage(Helper.chatLabel() + "All players from the defending side have been drafted for the town's defense."); - Bukkit.broadcastMessage(Helper.chatLabel() + "The raid of " - + raidedTown.getName() + " will begin in " + (RaidPhase.TRAVEL.startTick / 20 / 60) + " minutes!"); - Bukkit.broadcastMessage(Helper.chatLabel() + - "The Raiders are gathering at " + gatherTown.getName() + " before making the journey over!"); - - Main.warLogger.log(raidOwner.getName() + " started a raid."); - if (admin) Main.warLogger.log(raidOwner.getName() + " started a raid from admin interference."); - Main.warLogger.log("As part of " + war.getName() + ", forces from " + raid2.getRaiderSide() + " are raiding the town of " + raidedTown.getName() + "!"); - Main.warLogger.log("The Raiders are gathering at " + gatherTown.getName() + " before making the journey over!"); - } - } - } - } - if (!warFound) { - p.sendMessage(Helper.chatLabel() + "That war does not exist! /raid start [war] [town]"); - return; - } - if (!townExists) { - p.sendMessage(Helper.chatLabel() + "That town does not exist! /raid start [war] [town]"); - } - } - - /** - * Stops the current raid by admins - * - * @param p - * @param args - */ - private static void stopRaid(CommandSender p, String[] args) { - //admin check - if (!p.hasPermission("AlathraWar.admin")) { - fail(p, args, "permission"); - return; - } - - //are there even any raids - if (RaidData.getRaids().isEmpty()) { - fail(p, args, "noRaids"); - return; - } - - //find our desired raid and stop it - Raid raid = RaidData.getRaidOrNull(args[1] + "-" + args[2]); - - if (raid != null) { - p.sendMessage(Helper.chatLabel() + "raid cancelled"); - Bukkit.broadcastMessage(Helper.chatLabel() + "The raid of " + raid.getRaidedTown().getName() + " has been cancelled by an admin"); - raid.stop(); //this purges itself from raids - } else { - fail(p, args, "badName"); - } - } - - protected static void joinRaid(CommandSender p, String[] args, boolean admin) { - //Grab raid! - Raid raid = RaidData.getRaidOrNull(args[1] + "-" + args[2]); - if (raid != null) { - if (raid.getWar().getName().equalsIgnoreCase(args[1])) { - Player joiner = null; - if (p instanceof Player) { - joiner = (Player) p; - } - - if (admin) { - joiner = Bukkit.getPlayer(args[3]); - - if (joiner != null) { - if (args[4] != null) { - if (args[4].equalsIgnoreCase(raid.getDefenderSide())) { - if (!raid.getDefenderPlayers().contains(joiner.getName())) - raid.getDefenderPlayers().add(joiner.getName()); - p.sendMessage(Helper.chatLabel() + "Player " + joiner.getName() + " added to defender side."); - Main.warLogger.log("Player " + joiner.getName() + " added to defender side."); - joiner.sendMessage(Helper.chatLabel() + "You were forcefully added to the defender side against the raid party on " + raid.getRaidedTown() + " by an admin."); - return; - } else { - if (!raid.getRaiderPlayers().contains(joiner.getName())) - raid.getRaiderPlayers().add(joiner.getName()); - p.sendMessage(Helper.chatLabel() + "Player " + joiner.getName() + " added to raider side."); - Main.warLogger.log("Player " + joiner.getName() + " added to raider side."); - joiner.sendMessage(Helper.chatLabel() + "You were forcefully added to the raid party on " + raid.getRaidedTown() + " by an admin. Leaving the gather town will remove you from the raid party."); - } - } - } else { - p.sendMessage(Helper.chatLabel() + "Player " + args[3] + " not found!"); - return; - } - } - - if (joiner == null) { - p.sendMessage(Helper.chatLabel() + "Player not found! Is something broken?"); - return; - } - - if (!raid.getWar().getSide1Players().contains(joiner.getName()) && !raid.getWar().getSide2Players().contains(joiner.getName())) { - joiner.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); - } - - //Minutemen countermeasures, 86400 * 4 time. 86400 seconds in a day, 4 days min playtime - //also 12 hours min platim by default - int minuteman = CommandHelper.isPlayerMinuteman(joiner.getName()); - boolean override = false; - if (admin) { - if (args.length >= 6) { - if (Boolean.parseBoolean(args[5])) { - //player has joined to recently - p.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); - override = true; - } - } - } - if (!override) { - if (minuteman != 0) { - if (minuteman == 1) { - //player has joined to recently - if (admin) - p.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - joiner.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - return; - } else if (minuteman == 2) { - //player has played too little - if (admin) - p.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - joiner.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - return; - } - } - } - - //check if gather phase - if (!admin) { - if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START) { - try { - //check if the player is in the gather town - ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); - if (cluster.contains(WorldCoord.parseWorldCoord(joiner))) { - raid.addActiveRaider(joiner.getName()); - joiner.sendMessage(Helper.chatLabel() + "You have joined the raid on " + raid.getRaidedTown().getName() + "!"); - joiner.sendMessage(Helper.chatLabel() + "Leaving " + raid.getGatherTown().getName() + " will cause you to leave the raid party."); - } else { - joiner.sendMessage(Helper.chatLabel() + "You must be in the gathering town to join the raid on " + raid.getRaidedTown().getName() + "."); - } - } catch (TownyException e) { - throw new RuntimeException(e); - } - } else { - joiner.sendMessage(Helper.chatLabel() + "You cannot join the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); - } - } else { - try { - //check if the player is in the gather town - ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); - if (cluster.contains(WorldCoord.parseWorldCoord(joiner))) { - raid.addActiveRaider(joiner.getName()); - joiner.sendMessage(Helper.chatLabel() + "You have joined the raid on " + raid.getRaidedTown().getName() + "!"); - joiner.sendMessage(Helper.chatLabel() + "Leaving " + raid.getGatherTown().getName() + " will cause you to leave the raid party."); - } else { - joiner.sendMessage(Helper.chatLabel() + "You must be in the gathering town to join the raid on " + raid.getRaidedTown().getName() + "."); - } - } catch (TownyException e) { - throw new RuntimeException(e); - } - } - - raid.save(); - } - } else { - p.sendMessage(Helper.chatLabel() + "No raid is gathering in this town or this town does not exist!"); - } - } - - protected static void leaveRaid(CommandSender player, String[] args, boolean admin) { - //Get raid! - Player p = null; - if (admin) { - p = Bukkit.getPlayer(args[3]); - if (p == null) { - player.sendMessage(Helper.chatLabel() + "Error: player not found!"); - return; - } - } - if (p == null) { - p = (Player) player; - } - - Raid raid = RaidData.getRaidOrNull(args[1] + "-" + args[2]); - if (raid != null) { - if (raid.getWar().getName().equalsIgnoreCase(args[1])) { - if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { - p.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); - if (admin) - player.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); - } - - //check if gather phase - if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START) { - try { - //check if player is in gather town - ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); - if (cluster.contains(WorldCoord.parseWorldCoord(p))) { - raid.removeActiveRaider(p.getName()); - p.sendMessage(Helper.chatLabel() + "You have left the raid on " + raid.getRaidedTown().getName() + "!"); - if (admin) - player.sendMessage(Helper.chatLabel() + "You have left the raid on " + raid.getRaidedTown().getName() + "!"); - - } else { - p.sendMessage(Helper.chatLabel() + "You cannot leave the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); - if (admin) - player.sendMessage(Helper.chatLabel() + "You cannot leave the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); - } - } catch (TownyException e) { - throw new RuntimeException(e); - } - } - } - - raid.save(); - } else { - p.sendMessage(Helper.chatLabel() + "No raid is gathering in this town or this town does not exist!"); - if (admin) - player.sendMessage(Helper.chatLabel() + "No raid is gathering in this town or this town does not exist!"); - } - } - - /** - * Abandons the raid - * - * @param p - * @param args - */ - private static void abandonRaid(CommandSender p, String[] args) { - //Get raid - Raid raid = RaidData.getRaidOrNull(args[1] + "-" + args[2]); - if (raid != null) { - if (raid.getWar().getName().equalsIgnoreCase(args[1])) { - if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { - p.sendMessage(Helper.chatLabel() + "You are not in this war! Type /war join [war] [side]"); - return; - } - - if (raid.getOwner().getName().equalsIgnoreCase(p.getName())) { - //check if gather phase - if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START || raid.getPhase() == RaidPhase.TRAVEL || raid.getPhase() == RaidPhase.COMBAT) { - //force defender victory - Bukkit.broadcastMessage(Helper.chatLabel() + "The raid on " + raid.getRaidedTown().getName() + " has been abandoned by " + raid.getRaiderSide()); - Main.warLogger.log(p.getName() + " abandoned the raid on " + raid.getRaidedTown().getName() + " they started at " + raid.getGatherTown().getName()); - raid.defendersWin(raid.getRaiderScore(), raid.getDefenderScore()); - } else { - p.sendMessage(Helper.chatLabel() + "You cannot abandon the raid on " + raid.getRaidedTown().getName() + "! It has a problem!"); - } - } else { - fail(p, args, "owner"); - } - - } - } - p.sendMessage(Helper.chatLabel() + "This raid does not exist!"); - } - - private static void listRaids(CommandSender p, String[] args) { - if (RaidData.getRaids().isEmpty()) { - p.sendMessage(Helper.chatLabel() + "There are currently no Raids in progress"); - return; - } - p.sendMessage(Helper.chatLabel() + "Raids currently in progress:"); - for (final Raid raid : RaidData.getRaids()) { - p.sendMessage("Name: " + raid.getName()); - p.sendMessage("War: " + raid.getWar().getName()); - p.sendMessage("Owner: " + raid.getOwner().getName()); - p.sendMessage("Raiding: " + raid.getRaidedTown().getName().toLowerCase()); - p.sendMessage("Gathering In: " + raid.getGatherTown().getName().toLowerCase()); - p.sendMessage("Raiders: " + raid.getRaiderSide()); - p.sendMessage("Defenders: " + raid.getDefenderSide()); - p.sendMessage("Raider Score: " + raid.getRaiderScore()); - p.sendMessage("Defender Score: " + raid.getDefenderScore()); - p.sendMessage("Raid Phase: " + raid.getPhase().name()); - p.sendMessage("Time Left: " + (RaidPhase.END.startTick - raid.getRaidTicks()) / 1200 + " minutes"); - p.sendMessage("-=-=-=-=-=-=-=-=-=-=-=-"); - } - } - - private static void raidHelp(CommandSender p, String[] args) { - if (p.hasPermission("!AlathraWar.admin")) { - p.sendMessage(Helper.chatLabel() + "/raid stop [name]"); - } - p.sendMessage(Helper.chatLabel() + "/raid start [war] [town]"); - p.sendMessage(Helper.chatLabel() + "/raid join [war] [town]"); - p.sendMessage(Helper.chatLabel() + "/raid leave [war] [town]"); - p.sendMessage(Helper.chatLabel() + "/raid abandon [war] [town]"); - p.sendMessage(Helper.chatLabel() + "/raid list"); - } - - private static void fail(CommandSender p, String[] args, String type) { - switch (type) { - case "permissions" -> - p.sendMessage(Helper.chatLabel() + "&cYou do not have permission to do this"); - case "syntax" -> p.sendMessage(Helper.chatLabel() + "Invalid Syntax. /raid help"); - case "noRaids" -> - p.sendMessage(Helper.chatLabel() + "There are currently no Raids in progress"); - case "badName" -> - p.sendMessage(Helper.chatLabel() + "A raid could not be found with this Name! Type /raid list to view current raids"); - case "owner" -> - p.sendMessage(Helper.chatLabel() + "Only the player who started the raid can abandon it."); - default -> p.sendMessage(Helper.chatLabel() + "Invalid Arguments. /raid help"); - } - } - - public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { -// final Player p = (Player) sender; - CommandHelper.logCommand(sender, label, args); - if (args.length == 0) { - fail(sender, args, "syntax"); - } else if (args.length == 1) { - if (args[0].equalsIgnoreCase("list")) { - listRaids(sender, args); - } else if (args[0].equalsIgnoreCase("help")) { - raidHelp(sender, args); - } else { - fail(sender, args, "syntax"); - } - } else if (args.length == 3) { - if (args[0].equalsIgnoreCase("start")) { - startRaid(sender, args, false); - } else if (args[0].equalsIgnoreCase("stop")) { - stopRaid(sender, args); - } else if (args[0].equalsIgnoreCase("join")) { - joinRaid(sender, args, false); - } else if (args[0].equalsIgnoreCase("leave")) { - leaveRaid(sender, args, false); - } else if (args[0].equalsIgnoreCase("abandon")) { - abandonRaid(sender, args); - } else { - fail(sender, args, "syntax"); - } - } else { - fail(sender, args, "syntax"); - } - return true; - } -} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidTabCompletion.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidTabCompletion.java deleted file mode 100644 index fec739d..0000000 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidTabCompletion.java +++ /dev/null @@ -1,61 +0,0 @@ -package me.ShermansWorld.AlathraWar.commands; - -import com.palmergames.bukkit.towny.utils.NameUtil; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabCompleter; - -import java.util.Collections; -import java.util.List; - -public class RaidTabCompletion implements TabCompleter { - - private static final List base = List.of(new String[]{ - "abandon", - "help", - "join", - "leave", - "list", - "start" - }); - - private static final List baseAdmin = List.of(new String[]{ - "abandon", - "help", - "join", - "leave", - "list", - "start", - "stop" - }); - - @Override - public List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { - boolean flag = sender.hasPermission("AlathraWar.admin"); - - if (args.length > 0) { - if (args.length > 1) { - switch (args[0]) { - case "abandon", "join", "leave", "start", "stop" -> { - if (args.length > 2) { - if (args.length > 3) { - return Collections.emptyList(); - } - return NameUtil.filterByStart(CommandHelper.getTownyWarTowns(args[1]), args[2]); - } else { - return NameUtil.filterByStart(CommandHelper.getWarNames(), args[1]); - } - } - //help, list - default -> { - return Collections.emptyList(); - } - } - } else { - return NameUtil.filterByStart(flag ? baseAdmin : base, args[0]); - } - } else { - return flag ? baseAdmin : base; - } - } -} \ No newline at end of file diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommand.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommand.java new file mode 100644 index 0000000..a897e07 --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommand.java @@ -0,0 +1,339 @@ +package me.ShermansWorld.AlathraWar.commands; + +import com.github.milkdrinkers.colorparser.ColorParser; +import com.palmergames.bukkit.towny.TownyAPI; +import com.palmergames.bukkit.towny.object.Town; +import dev.jorel.commandapi.CommandAPIBukkit; +import dev.jorel.commandapi.CommandAPICommand; +import dev.jorel.commandapi.arguments.ArgumentSuggestions; +import dev.jorel.commandapi.arguments.BooleanArgument; +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 me.ShermansWorld.AlathraWar.*; +import me.ShermansWorld.AlathraWar.data.RaidData; +import me.ShermansWorld.AlathraWar.data.SiegeData; +import me.ShermansWorld.AlathraWar.data.WarData; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import java.util.HashSet; + +public class SiegeCommand { + public SiegeCommand() { + new CommandAPICommand("siege") + .withSubcommands( + commandStart(), + commandStop(), + commandAbandon(), + commandList(), + commandHelp() + ) + .executesPlayer((sender, args) -> { + if (args.count() == 0) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "Invalid Arguments. /siege help").build()); + }) + .register(); + } + + public static CommandAPICommand commandStart() { + return new CommandAPICommand("start") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.strings( + WarData.getWarsNames() + ) + ), + new StringArgument("target") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + }) + ), + new PlayerArgument("leader") + .setOptional(true) + .withPermission("AlathraWar.admin"), + new BooleanArgument("minutemen") + .setOptional(true) + .withPermission("AlathraWar.admin") + ) + .executesPlayer((Player p, CommandArguments args) -> siegeStart(p, args, false)); + } + + public static CommandAPICommand commandStop() { + return new CommandAPICommand("stop") + .withPermission("AlathraWar.admin") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> + WarData.getWarsNames() + ) + ), + new StringArgument("town") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + }) + ) + ) + .executesPlayer(SiegeCommand::siegeStop); + } + + public static CommandAPICommand commandAbandon() { + return new CommandAPICommand("abandon") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> + WarData.getWarsNames() + ) + ), + new StringArgument("town") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final String warname = (String) info.previousArgs().get("warname"); + return CommandHelper.getTownyWarTowns(warname); + }) + ) + ) + .executesPlayer(SiegeCommand::siegeAbandon); + } + + public static CommandAPICommand commandList() { + return new CommandAPICommand("list") + .executesPlayer(SiegeCommand::siegeList); + } + + public static CommandAPICommand commandHelp() { + return new CommandAPICommand("help") + .executesPlayer(SiegeCommand::siegeHelp); + } + + + protected static void siegeStart(Player sender, CommandArguments args, boolean admin) throws WrapperCommandSyntaxException { +// if (!(sender instanceof Player)) return; + + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name.").build()); + + //if this is admin mode use the forth arg instad of sender. + //if player is null after this then force end + final Player siegeOwner = (Player) args.getOptional("leader").orElse(sender); + + if (siegeOwner == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser("Owner is Null").build()); + } + + /*if (args.length < 3) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "/war siege [war] [town]").build()); + }*/ + + // War check + War war = WarData.getWar(argWarName); + if (war == null) { + siegeOwner.sendMessage(Helper.chatLabel() + "That war does not exist! /siege start [war] [town]"); + if (admin) + sender.sendMessage(Helper.chatLabel() + "That war does not exist! /siege start [war] [town]"); + return; + } + + + // Player participance check + Town leaderTown = TownyAPI.getInstance().getResident(siegeOwner).getTownOrNull(); + int side = war.getSide(leaderTown.getName().toLowerCase()); + + //DEBUG PRINT + Main.warLogger.log("Leader Town: " + leaderTown.getName()); + Main.warLogger.log("Siege Owner: " + siegeOwner.getName()); + sender.sendMessage("Leader Town: " + leaderTown.getName()); + sender.sendMessage("Siege Owner: " + siegeOwner.getName()); + //DEBUG PRINT + for (String t : war.getSide1Towns()) { + Main.warLogger.log("Side1 town: " + t); + sender.sendMessage("Side1 town: " + t); + } + for (String t : war.getSide2Towns()) { + Main.warLogger.log("Side2 town: " + t); + sender.sendMessage("Side2 town: " + t); + } + + if (side == 0) { + siegeOwner.sendMessage(Helper.chatLabel() + "You are not in this war."); + if (admin) sender.sendMessage(Helper.chatLabel() + "You are not in this war."); + return; + } else if (side == -1) { + siegeOwner.sendMessage(Helper.chatLabel() + "You have surrendered."); + if (admin) sender.sendMessage(Helper.chatLabel() + "You have surrendered."); + return; + } + + //Minutemen countermeasures, 86400 * 4 time. 86400 seconds in a day, 4 days min playtime + //Minuteman countermeasures + int minuteman = CommandHelper.isPlayerMinuteman(siegeOwner.getName()); + boolean minuteManOverride = (boolean) args.getOptional("minutemen").orElse(false); + + if (minuteManOverride) { + sender.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); + } + + //override? + if (!minuteManOverride) { + if (minuteman != 0) { + if (minuteman == 1) { + //player has joined to recently + if (admin) + sender.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + siegeOwner.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + return; + } else if (minuteman == 2) { + //player has played too little + if (admin) + sender.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + siegeOwner.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + return; + } + } + } + + if (!(args.get("target") instanceof final String argTownName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "Specify a town! /siege start [war] [town]").build()); + + // Town check + Town town = TownyAPI.getInstance().getTown(argTownName); + if (town == null) { + siegeOwner.sendMessage(Helper.chatLabel() + "That town does not exist! /siege start [war] [town]"); + if (admin) + sender.sendMessage(Helper.chatLabel() + "That town does not exist! /siege start [war] [town]"); + return; + } + Main.warLogger.log("Attacked Town: " + town.getName().toLowerCase()); + sender.sendMessage("Attacked Town: " + town.getName().toLowerCase()); + + // Is being raided check + for (Raid r : RaidData.getRaids()) { + if (r.getRaidedTown().getName().equalsIgnoreCase(town.getName())) { + siegeOwner.sendMessage(Helper.chatLabel() + "That town is already currently being raided! Cannot siege at this time!"); + if (admin) + sender.sendMessage(Helper.chatLabel() + "That town is already currently being raided! Cannot siege at this time!"); + return; + } + } + + // Attacking own side + if (war.getSide(town) == side) { + siegeOwner.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); + if (admin) sender.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); + return; + } + + Siege siege = new Siege(war, town, siegeOwner); + SiegeData.addSiege(siege); + war.addSiege(siege); + + Bukkit.broadcastMessage(Helper.chatLabel() + "A siege has been laid on " + siege.getTown() + " by " + siege.getAttackerSide() + "!"); + + war.save(); + siege.start(); + } + + private static void siegeStop(Player sender, CommandArguments args) throws WrapperCommandSyntaxException { + final HashSet sieges = SiegeData.getSieges(); + if (sieges.isEmpty()) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "There are currently no sieges in progress").build()); + } + + if (args.count() < 2) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "/siege stop [town]").build()); + } + + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "No war name supplied. /siege stop [war] [town]").build()); + + // War check + War war = WarData.getWar(argWarName); + if (war == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "That war does not exist! /siege stop [war] [town]").build()); + } + + if (!(args.get("town") instanceof final String argTownName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "No war name supplied. /siege stop [war] [town]").build()); + + for (Siege siege : sieges) { + if (siege.getTown().getName().equalsIgnoreCase(argTownName) && siege.getWar().equals(war)) { + sender.sendMessage(new ColorParser(Helper.chatLabel() + "siege cancelled").build()); + Bukkit.broadcast(new ColorParser(Helper.chatLabel() + "The siege at " + siege.getTown().getName() + " has been cancelled by an admin.").build()); + //siege.clearBeacon(); + siege.stop(); + return; + } + } + + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "A siege could not be found with this town name! Type /siege list to view current sieges").build()); + } + + private static void siegeAbandon(Player p, CommandArguments args) throws WrapperCommandSyntaxException { + if (args.count() < 2) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "/siege abandon [town]").build()); + } + + // War check + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "No war name supplied. /siege stop [war] [town]").build()); + + // War check + War war = WarData.getWar(argWarName); + if (war == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "That war does not exist! /siege abandon [war] [town]").build()); + } + + if (!(args.get("town") instanceof final String argTown)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "No town name supplied. /siege stop [war] [town]").build()); + + Siege siege = SiegeData.getSiege(argTown); + if (siege == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "Invalid siege").build()); + } + + OfflinePlayer oPlayer = siege.getSiegeLeader(); + if (oPlayer != p) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "You are not the leader of this siege.").build()); + } + + Bukkit.broadcast(new ColorParser(Helper.chatLabel() + "The siege at " + siege.getTown().getName() + " has been abandoned.").build()); + Main.warLogger.log(p.getName() + " abandoned the siege they started at " + siege.getTown().getName()); + siege.defendersWin(); + } + + private static void siegeList(Player sender, CommandArguments args) throws WrapperCommandSyntaxException { + HashSet sieges = SiegeData.getSieges(); + if (sieges.isEmpty()) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "There are currently no sieges in progress").build()); + + sender.sendMessage(new ColorParser(Helper.chatLabel() + "Sieges currently in progress:").build()); + for (Siege siege : sieges) { + War war = siege.getWar(); + String color = (siege.getSide1AreAttackers() && (war.getSide(siege.getTown().getName().toLowerCase()) == 1)) ? "&c" : "&9"; + sender.sendMessage(new ColorParser(war.getName() + " - " + color + siege.getTown().getName().toLowerCase()).build()); + sender.sendMessage(new ColorParser(war.getSide1() + " - " + (siege.getSide1AreAttackers() ? siege.getAttackerPoints() : siege.getDefenderPoints())).build()); + sender.sendMessage(new ColorParser(war.getSide2() + " - " + (siege.getSide1AreAttackers() ? siege.getDefenderPoints() : siege.getAttackerPoints())).build()); + sender.sendMessage(new ColorParser("Time Left: " + (Siege.maxSiegeTicks - siege.getSiegeTicks()) / 1200 + " minutes").build()); + sender.sendMessage(new ColorParser("-=-=-=-=-=-=-=-=-=-=-=-").build()); + } + } + + private static void siegeHelp(Player sender, CommandArguments args) { + if (sender.hasPermission("AlathraWar.admin")) { + sender.sendMessage(new ColorParser(Helper.chatLabel() + "/siege stop [war] [town]").build()); + } + sender.sendMessage(new ColorParser(Helper.chatLabel() + "/siege start [war] [town]").build()); + sender.sendMessage(new ColorParser(Helper.chatLabel() + "/siege abandon [war] [town]").build()); + sender.sendMessage(new ColorParser(Helper.chatLabel() + "/siege list").build()); + } +} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommands.java deleted file mode 100644 index 82e8707..0000000 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommands.java +++ /dev/null @@ -1,293 +0,0 @@ -package me.ShermansWorld.AlathraWar.commands; - -import com.palmergames.bukkit.towny.TownyAPI; -import com.palmergames.bukkit.towny.object.Town; -import me.ShermansWorld.AlathraWar.*; -import me.ShermansWorld.AlathraWar.data.RaidData; -import me.ShermansWorld.AlathraWar.data.SiegeData; -import me.ShermansWorld.AlathraWar.data.WarData; -import net.md_5.bungee.api.ChatColor; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.HashSet; - -public class SiegeCommands implements CommandExecutor { - public SiegeCommands(final Main plugin) { - plugin.getCommand("siege").setExecutor(this); - } - - protected static void siegeStart(CommandSender sender, String[] args, boolean admin) { -// if (!(sender instanceof Player)) return; - //if this is admin mode use the forth arg instad of sender. - //if player is null after this then force end - Player siegeOwner = null; - if ((sender instanceof Player)) { - siegeOwner = (Player) sender; - } - if (admin && args.length >= 5) { - siegeOwner = Bukkit.getPlayer(args[3]); - if (siegeOwner == null) { - sender.sendMessage("Player Does not exist"); - return; - } - } - - if (siegeOwner == null) { - sender.sendMessage("Owner is Null"); - return; - } - - if (args.length < 3) { - sender.sendMessage(Helper.chatLabel() + "/war siege [war] [town]"); - return; - } - - // War check - War war = WarData.getWar(args[1]); - if (war == null) { - siegeOwner.sendMessage(Helper.chatLabel() + "That war does not exist! /siege start [war] [town]"); - if (admin) - sender.sendMessage(Helper.chatLabel() + "That war does not exist! /siege start [war] [town]"); - return; - } - - - // Player participance check - Town leaderTown = TownyAPI.getInstance().getResident(siegeOwner).getTownOrNull(); - int side = war.getSide(leaderTown.getName().toLowerCase()); - - //DEBUG PRINT - Main.warLogger.log("Leader Town: " + leaderTown.getName()); - Main.warLogger.log("Siege Owner: " + siegeOwner.getName()); - sender.sendMessage("Leader Town: " + leaderTown.getName()); - sender.sendMessage("Siege Owner: " + siegeOwner.getName()); - //DEBUG PRINT - for (String t : war.getSide1Towns()) { - Main.warLogger.log("Side1 town: " + t); - sender.sendMessage("Side1 town: " + t); - } - for (String t : war.getSide2Towns()) { - Main.warLogger.log("Side2 town: " + t); - sender.sendMessage("Side2 town: " + t); - } - - if (side == 0) { - siegeOwner.sendMessage(Helper.chatLabel() + "You are not in this war."); - if (admin) sender.sendMessage(Helper.chatLabel() + "You are not in this war."); - return; - } else if (side == -1) { - siegeOwner.sendMessage(Helper.chatLabel() + "You have surrendered."); - if (admin) sender.sendMessage(Helper.chatLabel() + "You have surrendered."); - return; - } - - //Minutemen countermeasures, 86400 * 4 time. 86400 seconds in a day, 4 days min playtime - //Minuteman countermeasures - int minuteman = CommandHelper.isPlayerMinuteman(siegeOwner.getName()); - boolean override = false; - if (admin) { - if (args.length >= 6) { - if (Boolean.parseBoolean(args[5])) { - //player has joined to recently - sender.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); - override = true; - } - } - } - //override? - if (!override) { - if (minuteman != 0) { - if (minuteman == 1) { - //player has joined to recently - if (admin) - sender.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - siegeOwner.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - return; - } else if (minuteman == 2) { - //player has played too little - if (admin) - sender.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - siegeOwner.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - return; - } - } - } - - // Town check - Town town = TownyAPI.getInstance().getTown(args[2]); - if (town == null) { - siegeOwner.sendMessage(Helper.chatLabel() + "That town does not exist! /siege start [war] [town]"); - if (admin) - sender.sendMessage(Helper.chatLabel() + "That town does not exist! /siege start [war] [town]"); - return; - } - Main.warLogger.log("Attacked Town: " + town.getName().toLowerCase()); - sender.sendMessage("Attacked Town: " + town.getName().toLowerCase()); - - // Is being raided check - for (Raid r : RaidData.getRaids()) { - if (r.getRaidedTown().getName().equalsIgnoreCase(town.getName())) { - siegeOwner.sendMessage(Helper.chatLabel() + "That town is already currently being raided! Cannot siege at this time!"); - if (admin) - sender.sendMessage(Helper.chatLabel() + "That town is already currently being raided! Cannot siege at this time!"); - return; - } - } - - // Attacking own side - if (war.getSide(town) == side) { - siegeOwner.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); - if (admin) sender.sendMessage(Helper.chatLabel() + "You cannot attack your own towns."); - return; - } - - Siege siege = new Siege(war, town, siegeOwner); - SiegeData.addSiege(siege); - war.addSiege(siege); - - Bukkit.broadcastMessage(Helper.chatLabel() + "A siege has been laid on " + siege.getTown() + " by " + siege.getAttackerSide() + "!"); - - war.save(); - siege.start(); - } - - private static void siegeStop(CommandSender sender, String[] args) { - if (!sender.hasPermission("AlathraWar.admin")) { - sender.sendMessage(Helper.chatLabel() + Helper.color("&cYou do not have permission to do this")); - return; - } - HashSet sieges = SiegeData.getSieges(); - if (sieges.isEmpty()) { - sender.sendMessage(Helper.chatLabel() + "There are currently no sieges in progress"); - return; - } - - if (args.length < 3) { - sender.sendMessage(Helper.chatLabel() + "/siege stop [town]"); - return; - } - - // War check - War war = WarData.getWar(args[1]); - if (war == null) { - sender.sendMessage(Helper.chatLabel() + "That war does not exist! /siege stop [war] [town]"); - return; - } - - for (Siege siege : sieges) { - if (siege.getTown().getName().equalsIgnoreCase(args[2])) { - sender.sendMessage(Helper.chatLabel() + "siege cancelled"); - Bukkit.broadcastMessage(Helper.chatLabel() + "The siege at " + siege.getTown().getName() + " has been cancelled by an admin"); - //siege.clearBeacon(); - siege.stop(); - return; - } - } - sender.sendMessage(Helper.chatLabel() + "A siege could not be found with this town name! Type /siege list to view current sieges"); - } - - private static void siegeAbandon(CommandSender sender, String[] args) { - if (!(sender instanceof Player p)) return; - - if (args.length < 3) { - sender.sendMessage(Helper.chatLabel() + "/siege abandon [town]"); - return; - } - - // War check - War war = WarData.getWar(args[1]); - if (war == null) { - p.sendMessage(Helper.chatLabel() + "That war does not exist! /siege abandon [war] [town]"); - return; - } - - Siege siege = SiegeData.getSiege(args[2]); - if (siege == null) { - p.sendMessage(Helper.chatLabel() + "Invalid siege"); - return; - } - - OfflinePlayer oPlayer = siege.getSiegeLeader(); - if (oPlayer != p) { - p.sendMessage(Helper.chatLabel() + "You are not the leader of this siege."); - return; - } - - Bukkit.broadcastMessage(Helper.chatLabel() + "The siege at " + siege.getTown().getName() + " has been abandoned."); - Main.warLogger.log(p.getName() + " abandoned the siege they started at " + siege.getTown().getName()); - siege.defendersWin(); - - } - - private static void siegeList(CommandSender sender, String[] args) { - HashSet sieges = SiegeData.getSieges(); - if (sieges.isEmpty()) { - sender.sendMessage(Helper.chatLabel() + "There are currently no sieges in progress"); - return; - } - - sender.sendMessage(Helper.chatLabel() + "Sieges currently in progress:"); - for (Siege siege : sieges) { - War war = siege.getWar(); - String color = (siege.getSide1AreAttackers() && (war.getSide(siege.getTown().getName().toLowerCase()) == 1)) ? "§c" : "§9"; - sender.sendMessage(war.getName() + " - " + color + siege.getTown().getName().toLowerCase()); - sender.sendMessage(war.getSide1() + " - " + (siege.getSide1AreAttackers() ? siege.getAttackerPoints() : siege.getDefenderPoints())); - sender.sendMessage(war.getSide2() + " - " + (siege.getSide1AreAttackers() ? siege.getDefenderPoints() : siege.getAttackerPoints())); - sender.sendMessage("Time Left: " + (Siege.maxSiegeTicks - siege.getSiegeTicks()) / 1200 + " minutes"); - sender.sendMessage("-=-=-=-=-=-=-=-=-=-=-=-"); - } - - } - - private static void siegeHelp(CommandSender sender, String[] args) { - if (sender.hasPermission("AlathraWar.admin")) { - sender.sendMessage(Helper.chatLabel() + "/siege stop [war] [town]"); - } - sender.sendMessage(Helper.chatLabel() + "/siege start [war] [town]"); - sender.sendMessage(Helper.chatLabel() + "/siege abandon [war] [town]"); - sender.sendMessage(Helper.chatLabel() + "/siege list"); - } - - public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { - CommandHelper.logCommand(sender, label, args); - if (args.length == 0) { - sender.sendMessage(Helper.chatLabel() + "Invalid Arguments. /siege help"); - return true; - } - - // Start - // Stop - // Abandon - // List - // Help - - switch (args[0].toLowerCase()) { - case "start" -> { - siegeStart(sender, args, false); - return true; - } - case "stop" -> { - siegeStop(sender, args); - return true; - } - case "abandon" -> { - siegeAbandon(sender, args); - return true; - } - case "list" -> { - siegeList(sender, args); - return true; - } - case "help" -> { - siegeHelp(sender, args); - return true; - } - } - return false; - } -} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeTabCompletion.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeTabCompletion.java deleted file mode 100644 index 867649d..0000000 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeTabCompletion.java +++ /dev/null @@ -1,60 +0,0 @@ -package me.ShermansWorld.AlathraWar.commands; - -import com.palmergames.bukkit.towny.utils.NameUtil; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabCompleter; - -import java.util.Collections; -import java.util.List; - -public class SiegeTabCompletion implements TabCompleter { - - - private static final List baseAdmin = List.of(new String[]{ - "abandon", - "help", - "list", - "start", - "stop" - }); - - private static final List base = List.of(new String[]{ - "abandon", - "help", - "list", - "start" - }); - - @Override - public List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { - boolean flag = sender.hasPermission("AlathraWar.admin"); - - if (args.length > 0) { - if (args.length > 1) { - switch (args[0]) { - case "abandon", "start", "stop" -> { - if (args.length > 2) { - if (args.length > 3) { - return Collections.emptyList(); - } else { - return NameUtil.filterByStart(CommandHelper.getTownyWarTowns(args[1]), args[2]); - } - } else { - return NameUtil.filterByStart(CommandHelper.getWarNames(), args[1]); - } - } - //help, list, debug - default -> { - return Collections.emptyList(); - } - } - } else { - return NameUtil.filterByStart(flag ? baseAdmin : base, args[0]); - } - } else { - return flag ? baseAdmin : base; - } - } -} - diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommand.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommand.java new file mode 100644 index 0000000..9c39cc5 --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommand.java @@ -0,0 +1,442 @@ +package me.ShermansWorld.AlathraWar.commands; + +import com.github.milkdrinkers.colorparser.ColorParser; +import com.palmergames.bukkit.towny.TownyAPI; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.Town; +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 me.ShermansWorld.AlathraWar.*; +import me.ShermansWorld.AlathraWar.data.WarData; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class WarCommand { + public WarCommand() { + new CommandAPICommand("war") + .withSubcommands( + commandCreate(), + commandDelete(), + commandJoin(), + commandSurrender(), + commandList(), + commandInfo(), + commandHelp() + ) + .executesPlayer((sender, args) -> { + if (args.count() == 0) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "Invalid Arguments. /war help").build()); + }) + .register(); + } + + public static CommandAPICommand commandCreate() { + return new CommandAPICommand("create") + .withPermission("AlathraWar.admin") + .withArguments( + new StringArgument("warname"), + new StringArgument("side1") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final List nations = CommandHelper.getTownyNations(); + final List towns = CommandHelper.getTownyTowns(); + + Collections.sort(nations); + Collections.sort(towns); + + nations.addAll(towns); + + return nations; + }) + ), + new StringArgument("side2") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + final List nations = CommandHelper.getTownyNations(); + final List towns = CommandHelper.getTownyTowns(); + + Collections.sort(nations); + Collections.sort(towns); + + nations.addAll(towns); + + return nations; + }) + ) + ) + .executesPlayer(WarCommand::warCreate); + } + + public static CommandAPICommand commandDelete() { + return new CommandAPICommand("delete") + .withPermission("AlathraWar.admin") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> WarData.getWarsNames()) + ) + ) + .executesPlayer(WarCommand::warDelete); + } + + public static CommandAPICommand commandJoin() { + return new CommandAPICommand("join") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> CommandHelper.getWarNames()) + ), + new StringArgument("side") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> { + String warName = (String) info.previousArgs().get("warname"); + + War war = WarData.getWar(warName); + if (war != null) { + return war.getSides(); + } + + return Collections.emptyList(); + }) + ), + new PlayerArgument("player").setOptional(true).withPermission("AlathraWar.admin") + ) + .executesPlayer((Player p, CommandArguments args) -> warJoin(p, args, false)); + } + + public static CommandAPICommand commandSurrender() { + return new CommandAPICommand("surrender") + .withArguments( + new StringArgument("warname") + .replaceSuggestions( + ArgumentSuggestions.stringCollection(info -> CommandHelper.getWarNames()) + ), + new PlayerArgument("player") + .setOptional(true) + .withPermission("AlathraWar.admin") + ) + .executesPlayer((Player p, CommandArguments args) -> warSurrender(p, args, false)); + } + + public static CommandAPICommand commandList() { + return new CommandAPICommand("list") + .executesPlayer(WarCommand::warList); + } + + public static CommandAPICommand commandInfo() { + return new CommandAPICommand("info") + .withArguments( + new PlayerArgument("player") + ) + .executesPlayer(WarCommand::warInfo); + } + + public static CommandAPICommand commandHelp() { + return new CommandAPICommand("help") + .executesPlayer(WarCommand::warHelp); + } + + protected static void warCreate(CommandSender p, CommandArguments args) throws WrapperCommandSyntaxException { + if (args.count() >= 3) { + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name.").build()); + + if (WarData.getWar(argWarName) != null) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cA war already exists with that name.").build()); + + if (!(args.get("side1") instanceof final String argSide1)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a side.").build()); + + if (!(args.get("side2") instanceof final String argSide2)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a side.").build()); + + if (argSide1.contains(argSide2)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cCannot declare war on oneself.").build()); + + // TODO Ensure side1 valid + // TODO Ensure side2 valid + // TODO Deep Check if war exists where the same sides are fighting??? + + // TODO Add checks here for wether the towns/nations names are valid + War war = new War(argWarName, argSide1, argSide2); + WarData.addWar(war); + war.save(); + + Bukkit.broadcastMessage(Helper.chatLabel() + "War created with the name " + argWarName + ", " + + argSide1 + " vs. " + argSide2); + Main.warLogger.log(p.getName() + " created a new war with the name " + argWarName + ", " + argSide1 + + " vs. " + argSide2); + } else { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + + "Invalid Arguments. /war create [name] [side1] [side2]").build()); + } + + } + + private static void warDelete(Player p, CommandArguments args) throws WrapperCommandSyntaxException { + if (args.count() >= 1) { + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name.").build()); + + War war = WarData.getWar(argWarName); + + if (war == null) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cWar not found. Type /war list to view current wars.").build()); + + for (Siege s : war.getSieges()) { + s.stop(); + } + for (Raid r : war.getRaids()) { + r.stop(); + } + + WarData.removeWar(war); + + Bukkit.broadcast(new ColorParser(Helper.chatLabel() + "War " + argWarName + " deleted.").build()); + Main.warLogger.log(p.getName() + " deleted " + argWarName); + } else { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + + "Invalid Arguments. /war delete [name]").build()); + } + } + + /** + * @param p - executor + * @param args + * @param admin - admin behavior + */ + protected static void warJoin(Player p, CommandArguments args, boolean admin) throws WrapperCommandSyntaxException { + // Sufficient args check + if (args.count() < 2) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + + "/war join [name] [side], type /war list to view current wars").build()); + + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name.").build()); + + War war = WarData.getWar(argWarName); + + if (war == null) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cWar not found. /war join [name] [side], type /war list to view current wars.").build()); + + if (!(args.get("side") instanceof final String argSide)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a side.").build()); + + if (!(args.getOptional("player").orElse(null) instanceof @Nullable Player argPlayer)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cPlayer not found.").build()); + + if (argPlayer == null) { + argPlayer = p; + } + + // Towny Resident Object, if admin then dont use command runner, use declared player + Resident res = TownyAPI.getInstance().getResident(argPlayer); + + // Town check + Town town = res.getTownOrNull(); + if (town == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(ChatColor.RED + "You are not in a town.").build()); + } + + //Minuteman countermeasures + final int minuteman = CommandHelper.isPlayerMinuteman(argPlayer.getName()); + final boolean minuteManOverride = (boolean) args.getOptional("minutemen").orElse(false); + + if (minuteManOverride) { + p.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); + } + + //override? + if (!minuteManOverride) { + if (minuteman != 0) { + if (minuteman == 1) { + //player has joined to recently + if (admin) + p.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + argPlayer.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); + return; + } else if (minuteman == 2) { + //player has played too little + if (admin) + p.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + argPlayer.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); + return; + } + } + } + + // Side checks + final int side = war.getSide(town.getName().toLowerCase()); + if (side == -1) { + argPlayer.sendMessage(Helper.chatLabel() + "You've already surrendered!"); + return; + } else if (side > 0) { + argPlayer.sendMessage(Helper.chatLabel() + "You're already in this war!"); + return; + } + + if (res.hasNation()) { + if (res.getPlayer().hasPermission("AlathraWar.nationjoin") || res.isKing()) { + // Has nation declaration permission + war.addNation(res.getNationOrNull(), argSide); + res.getPlayer().sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getNationOrNull().getName()); + p.sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getNationOrNull().getName()); + Bukkit.broadcastMessage(Helper.chatLabel() + "The nation of " + res.getNationOrNull().getName() + " has joined the war on the side of " + argSide + "!"); + war.save(); + } else { + // Cannot declare nation involvement + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "You cannot declare war for your nation.").build()); + } + } else if (res.hasTown()) { + if (res.getPlayer().hasPermission("AlathraWar.townjoin") || res.isMayor()) { + // Is in indepdenent town & has declaration perms + war.addTown(res.getTownOrNull(), argSide); + res.getPlayer().sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getTownOrNull().getName()); + p.sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getTownOrNull().getName()); + Bukkit.broadcastMessage(Helper.chatLabel() + "The town of " + res.getTownOrNull().getName() + " has joined the war on the side of " + argSide + "!"); + war.save(); + } else { + // No perms + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "You cannot declare war for your town.").build()); + } + } + } + + protected static void warSurrender(Player p, CommandArguments args, boolean admin) throws WrapperCommandSyntaxException { + // Sufficient args check + if (args.count() < 1) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + + "/war surrender [name], type /war list to view current wars").build()); + } + + if (!(args.get("warname") instanceof final String argWarName)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a war name.").build()); + + // War check + War war = WarData.getWar(argWarName); + if (war == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + + "War not found. Type /war list to view current wars").build()); + } + + if (!(args.get("reason") instanceof final String argReason)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cYou need to specify a reason.").build()); + + if (!(args.getOptional("player").orElse(null) instanceof @Nullable Player argPlayer)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "&cPlayer not found.").build()); + + if (argPlayer == null) { + argPlayer = p; + } + + // Towny Resident Object + Resident res = TownyAPI.getInstance().getResident(argPlayer); + + // Town check + Town town = res.getTownOrNull(); + if (town == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(ChatColor.RED + "You are not in a town.").build()); + } + + if (res.hasNation()) { + if (p.hasPermission("AlathraWar.nationsurrender") || res.isKing() || admin) { + // Has nation surrender permission + war.surrenderNation(res.getNationOrNull()); + argPlayer.sendMessage(Helper.chatLabel() + "You have surrendered the war for " + res.getNationOrNull().getName()); + Bukkit.broadcastMessage(Helper.chatLabel() + "The nation of " + res.getNationOrNull().getName() + " has surrendered! They were fighting for " + argReason + "."); + war.save(); + } else { + // Cannot surrender nation involvement + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "You cannot surrender war for your nation.").build()); + } + } else if (res.hasTown()) { + if (p.hasPermission("AlathraWar.townsurrender") || res.isMayor() || admin) { + // Is in indepdenent town & has surrender perms + war.surrenderTown(res.getTownOrNull().getName()); + argPlayer.sendMessage(Helper.chatLabel() + "You have surrendered the war for " + res.getTownOrNull().getName()); + Bukkit.broadcastMessage(Helper.chatLabel() + "The town of " + res.getTownOrNull().getName() + " has surrendered! They were fighting for " + argReason + "."); + war.save(); + } else { + // No perms + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "You cannot surrender war for your town.").build()); + } + } + + } + + private static void warList(Player p, CommandArguments args) throws WrapperCommandSyntaxException { + ArrayList wars = WarData.getWars(); + + if (wars.isEmpty()) { + p.sendMessage("There are no current wars."); + } else { + p.sendMessage(Helper.chatLabel() + "Wars:"); + + for (War war : wars) { + p.sendMessage(war.getName() + " - " + war.getSide1() + " (" + war.getSide1Points() + ") vs. " + war.getSide2() + " (" + war.getSide2Points() + ")"); + } + } + } + + private static void warInfo(Player p, CommandArguments args) throws WrapperCommandSyntaxException { + if (args.count() < 1) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "/war info [Player]").build()); + } + + if (!(args.get("player") instanceof Player argsPlayer)) + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser("Specify a player.").build()); + + Resident res = TownyAPI.getInstance().getResident(argsPlayer); + if (res == null || res.getTownOrNull() == null) { + throw CommandAPIBukkit.failWithAdventureComponent(new ColorParser(Helper.chatLabel() + "Invalid town resident.").build()); + } + + p.sendMessage(new ColorParser(Helper.chatLabel() + p.getName() + "'s wars:").build()); + for (War war : WarData.getWars()) { + int side = war.getSide(res.getTownOrNull()); + if (side != 0) { + if (side == -1) { + p.sendMessage(war.getName() + " - Surrendered"); + } else if (side == 1) { + p.sendMessage(war.getName() + " - " + war.getSide1()); + } else if (side == 2) { + p.sendMessage(war.getName() + " - " + war.getSide2()); + } + } + } + } + + private static void warHelp(Player p, CommandArguments args) throws WrapperCommandSyntaxException { + // TODO + } + + /** + * Method for console access + * + * @param sender + * @param args + */ +// private static void consoleCommand(CommandSender sender, CommandArguments args) { +// if (args.length < 1) return; +// if (args[0].equalsIgnoreCase("list")) { +// ArrayList warList = WarData.getWars(); +// sender.sendMessage("Wars: " + warList.size()); +// for (War war : WarData.getWars()) { +// if (war == null) continue; +// sender.sendMessage(war.toString()); +// } +// } +// } +} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommands.java deleted file mode 100644 index 63cabe0..0000000 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommands.java +++ /dev/null @@ -1,326 +0,0 @@ -package me.ShermansWorld.AlathraWar.commands; - -import com.palmergames.bukkit.towny.TownyAPI; -import com.palmergames.bukkit.towny.object.Resident; -import com.palmergames.bukkit.towny.object.Town; -import me.ShermansWorld.AlathraWar.*; -import me.ShermansWorld.AlathraWar.data.WarData; -import net.md_5.bungee.api.ChatColor; -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.ArrayList; - -public class WarCommands implements CommandExecutor { - - public WarCommands(final Main plugin) { - plugin.getCommand("war").setExecutor(this); - } - - protected static void warCreate(CommandSender p, String[] args) { - if (!p.hasPermission("AlathraWar.admin")) { - p.sendMessage("You do not have permission to run this command."); - return; - } - if (args.length >= 4) { - War war = new War(args[1], args[2], args[3]); - WarData.addWar(war); - war.save(); - - Bukkit.broadcastMessage(Helper.chatLabel() + "War created with the name " + args[1] + ", " - + args[2] + " vs. " + args[3]); - Main.warLogger.log(p.getName() + " created a new war with the name " + args[1] + ", " + args[2] - + " vs. " + args[3]); - } else { - p.sendMessage(Helper.chatLabel() - + "Invalid Arguments. /war create [name] [side1] [side2]"); - } - - } - - private static void warDelete(Player p, String[] args) { - if (!p.hasPermission("AlathraWar.admin")) { - p.sendMessage("You do not have permission to run this command."); - return; - } - - if (args.length >= 2) { - War war = WarData.getWar(args[1]); - - // War Check - if (war == null) { - p.sendMessage(Helper.chatLabel() - + "War not found. Type /war list to view current wars."); - return; - } - - for (Siege s : war.getSieges()) { - s.stop(); - } - for (Raid r : war.getRaids()) { - r.stop(); - } - - WarData.removeWar(war); - - Bukkit.broadcastMessage(Helper.chatLabel() + "War " + args[1] + " deleted."); - Main.warLogger.log(p.getName() + " deleted " + args[1]); - } else { - p.sendMessage(Helper.chatLabel() - + "Invalid Arguments. /war delete [name]"); - } - } - - /** - * @param p - executor - * @param args - * @param admin - admin behavior - */ - protected static void warJoin(CommandSender p, String[] args, boolean admin) { - // Sufficient args check - if (args.length < 3) { - p.sendMessage(Helper.chatLabel() - + "/war join [name] [side], type /war list to view current wars"); - return; - } - - // War check - War war = WarData.getWar(args[1]); - if (war == null) { - p.sendMessage(Helper.chatLabel() - + "War not found. /war join [name] [side], type /war list to view current wars"); - return; - } - - Player player = null; - if (admin) { - player = Bukkit.getPlayer(args[3]); - if (player == null) { - p.sendMessage(Helper.chatLabel() + "Player " + args[3] + " not found!"); - return; - } - } - if (player == null) { - player = (Player) p; - } - - - // Towny Resident Object, if admin then dont use command runner, use declared player - Resident res = TownyAPI.getInstance().getResident(player); - - // Town check - Town town = res.getTownOrNull(); - if (town == null) { - p.sendMessage(ChatColor.RED + "You are not in a town."); - return; - } - - //Minuteman countermeasures - int minuteman = CommandHelper.isPlayerMinuteman(player.getName()); - boolean override = false; - if (admin) { - if (args.length >= 6) { - if (Boolean.parseBoolean(args[5])) { - //player has joined to recently - p.sendMessage(ChatColor.RED + "Warning! Ignoring Minuteman Countermeasure!"); - override = true; - } - } - } - //override? - if (!override) { - if (minuteman != 0) { - if (minuteman == 1) { - //player has joined to recently - if (admin) - p.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - player.sendMessage(ChatColor.RED + "You have joined the server too recently! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayerAge") + " days from joining."); - return; - } else if (minuteman == 2) { - //player has played too little - if (admin) - p.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - player.sendMessage(ChatColor.RED + "You have not played enough! You can only join a raid after " + Main.getInstance().getConfig().getInt("minimumPlayTime") + " hours of play."); - return; - } - } - } - - // Side checks - int side = war.getSide(town.getName().toLowerCase()); - if (side == -1) { - p.sendMessage(Helper.chatLabel() + "You've already surrendered!"); - return; - } else if (side > 0) { - p.sendMessage(Helper.chatLabel() + "You're already in this war!"); - return; - } - - if (res.hasNation()) { - if (res.getPlayer().hasPermission("AlathraWar.nationjoin") || res.isKing()) { - // Has nation declaration permission - war.addNation(res.getNationOrNull(), args[2]); - res.getPlayer().sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getNationOrNull().getName()); - p.sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getNationOrNull().getName()); - Bukkit.broadcastMessage(Helper.chatLabel() + "The nation of " + res.getNationOrNull().getName() + " has joined the war on the side of " + args[2] + "!"); - war.save(); - } else { - // Cannot declare nation involvement - p.sendMessage(Helper.chatLabel() + "You cannot declare war for your nation."); - } - } else if (res.hasTown()) { - if (res.getPlayer().hasPermission("AlathraWar.townjoin") || res.isMayor()) { - // Is in indepdenent town & has declaration perms - war.addTown(res.getTownOrNull(), args[2]); - res.getPlayer().sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getTownOrNull().getName()); - p.sendMessage(Helper.chatLabel() + "You have joined the war for " + res.getTownOrNull().getName()); - Bukkit.broadcastMessage(Helper.chatLabel() + "The town of " + res.getTownOrNull().getName() + " has joined the war on the side of " + args[2] + "!"); - war.save(); - } else { - // No perms - p.sendMessage(Helper.chatLabel() + "You cannot declare war for your town."); - } - } - } - - protected static void warSurrender(Player p, String[] args, boolean admin) { - // Sufficient args check - if (args.length < 2) { - p.sendMessage(Helper.chatLabel() - + "/war surrender [name], type /war list to view current wars"); - return; - } - - // War check - War war = WarData.getWar(args[1]); - if (war == null) { - p.sendMessage(Helper.chatLabel() - + "War not found. Type /war list to view current wars"); - return; - } - - // Towny Resident Object - Resident res = TownyAPI.getInstance().getResident(p); - - // Town check - Town town = res.getTownOrNull(); - if (town == null) { - p.sendMessage(ChatColor.RED + "You are not in a town."); - return; - } - - if (res.hasNation()) { - if (p.hasPermission("AlathraWar.nationsurrender") || res.isKing()) { - // Has nation surrender permission - war.surrenderNation(res.getNationOrNull()); - p.sendMessage(Helper.chatLabel() + "You have surrendered the war for " + res.getNationOrNull().getName()); - Bukkit.broadcastMessage(Helper.chatLabel() + "The nation of " + res.getNationOrNull().getName() + " has surrendered! They were fighting for " + args[2] + "."); - war.save(); - } else { - // Cannot surrender nation involvement - p.sendMessage(Helper.chatLabel() + "You cannot surrender war for your nation."); - } - } else if (res.hasTown()) { - if (p.hasPermission("AlathraWar.townsurrender") || res.isMayor()) { - // Is in indepdenent town & has surrender perms - war.surrenderTown(res.getTownOrNull().getName()); - p.sendMessage(Helper.chatLabel() + "You have surrendered the war for " + res.getTownOrNull().getName()); - Bukkit.broadcastMessage(Helper.chatLabel() + "The town of " + res.getTownOrNull().getName() + " has surrendered! They were fighting for " + args[2] + "."); - war.save(); - } else { - // No perms - p.sendMessage(Helper.chatLabel() + "You cannot surrender war for your town."); - } - } - - } - - private static void warList(Player p, String[] args) { - p.sendMessage(Helper.chatLabel() + "Wars:"); - ArrayList wars = WarData.getWars(); - if (wars.isEmpty()) { - p.sendMessage("There are no current wars."); - } else { - for (War war : wars) { - p.sendMessage(war.getName() + " - " + war.getSide1() + " (" + war.getSide1Points() + ") vs. " + war.getSide2() + " (" + war.getSide2Points() + ")"); - } - } - } - - private static void warInfo(Player p, String[] args) { - if (args.length < 2) { - p.sendMessage(Helper.chatLabel() + "/war info [Player]"); - } - - Resident res = TownyAPI.getInstance().getResident(args[1]); - if (res == null || res.getTownOrNull() == null) { - p.sendMessage(Helper.chatLabel() + "Invalid town resident."); - } - - p.sendMessage(Helper.chatLabel() + p.getName() + "'s wars:"); - for (War war : WarData.getWars()) { - int side = war.getSide(args[1]); - if (side != 0) { - if (side == -1) { - p.sendMessage(war.getName() + " - Surrendered"); - } else if (side == 1) { - p.sendMessage(war.getName() + " - " + war.getSide1()); - } else if (side == 2) { - p.sendMessage(war.getName() + " - " + war.getSide2()); - } - } - } - } - - private static void warHelp(Player p, String[] args) { - // TODO - } - - /** - * Method for console access - * - * @param sender - * @param args - */ - private static void consoleCommand(CommandSender sender, String[] args) { - if (args.length < 1) return; - if (args[0].equalsIgnoreCase("list")) { - ArrayList warList = WarData.getWars(); - sender.sendMessage("Wars: " + warList.size()); - for (War war : WarData.getWars()) { - if (war == null) continue; - sender.sendMessage(war.toString()); - } - } - } - - public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { - CommandHelper.logCommand(sender, label, args); - if (!(sender instanceof Player p)) { - consoleCommand(sender, args); - return true; - } - - if (args.length == 0) { - p.sendMessage(Helper.chatLabel() + "Invalid Arguments. /war help"); - } else { - - switch (args[0].toLowerCase()) { - case "create" -> warCreate(p, args); - case "delete" -> warDelete(p, args); - case "join" -> warJoin(p, args, false); - case "surrender" -> warSurrender(p, args, false); - case "list" -> warList(p, args); - case "info" -> warInfo(p, args); - case "help" -> warHelp(p, args); - default -> p.sendMessage(Helper.chatLabel() + "Invalid Arguments. /war help"); - } - - } - return true; - } - -} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/WarTabCompletion.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/WarTabCompletion.java deleted file mode 100644 index 959bd30..0000000 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/WarTabCompletion.java +++ /dev/null @@ -1,72 +0,0 @@ -package me.ShermansWorld.AlathraWar.commands; - -import com.palmergames.bukkit.towny.utils.NameUtil; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabCompleter; - -import java.util.Collections; -import java.util.List; - -public class WarTabCompletion implements TabCompleter { - - private static final List baseAdmin = List.of(new String[]{ - "create", - "delete", - "help", - "info", - "join", - "list", - "surrender" - }); - - private static final List base = List.of(new String[]{ - "help", - "info", - "join", - "list", - "surrender" - }); - - @Override - public List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { - boolean flag = sender.hasPermission("AlathraWar.admin"); - - if (args.length > 0) { - if (args.length > 1) { - switch (args[0]) { - case "join" -> { - if (args.length > 2) { - if (args.length > 3) { - return Collections.emptyList(); - } - return NameUtil.filterByStart(CommandHelper.getWarSides(args[1]), args[2]); - } else { - return NameUtil.filterByStart(CommandHelper.getWarNames(), args[1]); - } - } - case "info" -> { - if (args.length > 2) { - return Collections.emptyList(); - } - return NameUtil.filterByStart(CommandHelper.getPlayers(), args[1]); - } - case "delete", "surrender" -> { - if (args.length > 2) { - return Collections.emptyList(); - } - return NameUtil.filterByStart(CommandHelper.getWarNames(), args[1]); - } - //create, list, help - default -> { - return Collections.emptyList(); - } - } - } else { - return NameUtil.filterByStart(flag ? baseAdmin : base, args[0]); - } - } else { - return flag ? baseAdmin : base; - } - } -} \ No newline at end of file diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/DataManager.java b/src/main/java/me/ShermansWorld/AlathraWar/data/DataManager.java index 388cfa9..9b53fc5 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/DataManager.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/DataManager.java @@ -14,8 +14,8 @@ import java.util.logging.Level; * Manages file IO & Deletion for other data classes. */ public class DataManager { - private static DumperOptions options; private static final String dataFolder = "plugins" + File.separator + "AlathraWar" + File.separator + "data"; + private static DumperOptions options; public DataManager() { diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java index 0e1e65a..42c9e95 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java @@ -12,6 +12,7 @@ import me.ShermansWorld.AlathraWar.War; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.annotation.Nonnull; import java.util.*; @@ -37,7 +38,7 @@ public class RaidData { * @param name - Name to check * @return Raid or Null */ - public static Raid getRaidOrNull(String name) { + public static @Nullable Raid getRaidOrNull(String name) { for (Raid raid : raids) { if (raid.getName().equalsIgnoreCase(name)) return raid; } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java index b605a86..f352547 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java @@ -7,17 +7,15 @@ import me.ShermansWorld.AlathraWar.War; import java.io.File; import java.io.FilenameFilter; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; +import java.util.*; public class WarData { private final static String dataFolderPath = "plugins" + File.separator + "AlathraWar" + File.separator + "data"; - // Static War list for all active wars - private static ArrayList wars = new ArrayList<>(); // Filter for only accessing yml files private static final FilenameFilter ymlFilter = (dir, name) -> name.endsWith(".yml"); + // Static War list for all active wars + private static ArrayList wars = new ArrayList<>(); // Constructor used to initialise folder. public WarData(final Main plugin) { @@ -35,6 +33,18 @@ public class WarData { WarData.wars = wars; } + public static Collection getWarsNames() { + List warNames = new ArrayList<>(); + + for (War war : getWars()) { + warNames.add(war.getName()); + } + + Collections.sort(warNames); + + return warNames; + } + /** * Gets a war with a specific name * @@ -48,6 +58,13 @@ public class WarData { return null; } + public static War getWar(UUID uuid) { + for (War war : wars) { + if (war.getUUID().equals(uuid)) return war; + } + return null; + } + public static void addWar(War war) { if (war == null) { Main.warLogger.log("Attempted to add NULL to war list."); diff --git a/src/main/java/me/ShermansWorld/AlathraWar/hooks/TABHook.java b/src/main/java/me/ShermansWorld/AlathraWar/hooks/TABHook.java index 86c9374..d642aa4 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/hooks/TABHook.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/hooks/TABHook.java @@ -27,7 +27,7 @@ public class TABHook { if (tabAPI.getTabListFormatManager() == null) return; tabAPI.getTabListFormatManager().setSuffix(tabPlayer, - Helper.color(" &c[") + war.getSide1() + "]&r"); + Helper.color(" &c[") + war.getSide1() + "]&r"); } public static void assignSide2WarSuffix(Player p, War war) { diff --git a/src/main/java/me/ShermansWorld/AlathraWar/items/WarItemRegistry.java b/src/main/java/me/ShermansWorld/AlathraWar/items/WarItemRegistry.java index 9829d32..2d111a2 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/items/WarItemRegistry.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/items/WarItemRegistry.java @@ -4,6 +4,7 @@ import me.ShermansWorld.AlathraWar.Helper; import me.ShermansWorld.AlathraWar.Main; import me.ShermansWorld.AlathraWar.items.item.RamItem; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; @@ -59,7 +60,7 @@ public class WarItemRegistry { * @param registryName * @return */ - public ItemStack getOrNull(String registryName) { + public @Nullable ItemStack getOrNull(String registryName) { return itemRegistry.get(registryName(registryName)).getItemStack().clone(); } @@ -70,7 +71,7 @@ public class WarItemRegistry { * @param registryName * @return */ - public ItemStack getOrNullNamespace(String registryName) { + public @Nullable ItemStack getOrNullNamespace(String registryName) { return itemRegistry.get(registryName).getItemStack().clone(); }