diff --git a/resources/plugin.yml b/resources/plugin.yml index ee1f347..6d9766b 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,25 +1,25 @@ -name: AlathraWar -version: 1.3.2 -api-version: 1.19 -author: ShermansWorld -main: me.ShermansWorld.AlathraWar.Main -depend: [Vault, Towny, TAB] - -commands: - war: - description: used to create, delete, join and leave wars - softdepend: [Vault, Towny, TAB] - usage: /war - siege: - description: used to start, end, and view sieges - softdepend: [Vault, Towny] - usage: /siege - raid: - description: used to start, end, and view raids - softdepend: [Vault, Towny] - usage: /raid - alathrawaradmin: - description: used to administrate wars and modify existing events - softdepend: [Vault, Towny, TAB] - usage: /alathrawaradmin - aliases: [awa] \ No newline at end of file +name: AlathraWar +version: 1.3.2 +api-version: 1.19 +author: ShermansWorld +main: me.ShermansWorld.AlathraWar.Main +depend: [Vault, Towny, TAB] + +commands: + war: + description: used to create, delete, join and leave wars + softdepend: [Vault, Towny, TAB] + usage: /war + siege: + description: used to start, end, and view sieges + softdepend: [Vault, Towny] + usage: /siege + raid: + description: used to start, end, and view raids + softdepend: [Vault, Towny] + usage: /raid + alathrawaradmin: + description: used to administrate wars and modify existing events + softdepend: [Vault, Towny, TAB] + usage: /alathrawaradmin + aliases: [awa] \ No newline at end of file diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Main.java b/src/main/java/me/ShermansWorld/AlathraWar/Main.java index 4a8e16b..16f5816 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Main.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Main.java @@ -1,6 +1,7 @@ package me.ShermansWorld.AlathraWar; import me.ShermansWorld.AlathraWar.commands.*; +import me.ShermansWorld.AlathraWar.data.TimeoutData; import me.ShermansWorld.AlathraWar.listeners.CommandsListener; import org.bukkit.event.Listener; import org.bukkit.plugin.RegisteredServiceProvider; @@ -81,12 +82,15 @@ public class Main extends JavaPlugin { 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((Listener) new KillsListener(), (Plugin) this); getServer().getPluginManager().registerEvents((Listener) new CommandsListener(), (Plugin) this); getServer().getPluginManager().registerEvents((Listener) new JoinListener(), (Plugin) this); diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Raid.java b/src/main/java/me/ShermansWorld/AlathraWar/Raid.java index 1bf0a12..ffbdd16 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Raid.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Raid.java @@ -10,6 +10,7 @@ import com.palmergames.bukkit.towny.object.WorldCoord; import com.palmergames.bukkit.towny.object.metadata.LongDataField; import me.ShermansWorld.AlathraWar.data.RaidData; import me.ShermansWorld.AlathraWar.data.RaidPhase; +import me.ShermansWorld.AlathraWar.data.SiegeData; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.OfflinePlayer; @@ -76,7 +77,7 @@ public class Raid { private String defenders; private boolean side1AreRaiders; private int raidScore; - private Player owner; + private OfflinePlayer owner; private int raidTicks; private TownBlock homeBlockRaided; private TownBlock homeBlockGather; @@ -84,9 +85,9 @@ public class Raid { private Location townSpawnRaided; private Location townSpawnGather; int[] bukkitId; - public ArrayList activeRaiders; - public ArrayList raiderPlayers; - public ArrayList defenderPlayers; + public ArrayList activeRaiders = new ArrayList<>(); + public ArrayList raiderPlayers = new ArrayList<>(); + public ArrayList defenderPlayers = new ArrayList<>(); public Map lootedChunks; /** @@ -99,17 +100,20 @@ public class Raid { * @param raidTicks */ public Raid(final War war, final Town raidedTown, final Town gatherTown, - final boolean side1AreRaiders, final int raidTicks) { + final boolean side1AreRaiders, final int raidTicks, OfflinePlayer owner) { this.bukkitId = new int[1]; -// this.activeRaiders = activeRaiders == null ? new ArrayList<>() : activeRaiders; this.war = war; this.raidTicks = raidTicks; this.raidedTown = raidedTown; this.gatherTown = gatherTown; + this.owner = owner; + this.raiders = side1AreRaiders ? war.getSide1() : war.getSide2(); + this.defenders = !side1AreRaiders ? war.getSide1() : war.getSide2(); + this.raidScore = 500; //AttackSide-Town - this.name = (side1AreRaiders ? war.getSide1() : war.getSide2()).toLowerCase() + "-" + raidedTown.getName().toLowerCase(); + this.name = war.getName() + "-" + raidedTown.getName().toLowerCase(); this.lootedChunks = new HashMap<>(); } @@ -125,8 +129,8 @@ public class Raid { * @param activeRaiders * @param phase */ - public Raid(final War war, final Town raidedTown, final Town gatherTown, final boolean side1AreRaiders, final int raidTicks, ArrayList activeRaiders, RaidPhase phase) { - this(war, raidedTown, gatherTown, side1AreRaiders, raidTicks); + public Raid(final War war, final Town raidedTown, final Town gatherTown, final boolean side1AreRaiders, final int raidTicks, ArrayList activeRaiders, RaidPhase phase, OfflinePlayer owner) { + this(war, raidedTown, gatherTown, side1AreRaiders, raidTicks, owner); this.activeRaiders = activeRaiders; this.setRaidPhase(phase); } @@ -141,16 +145,20 @@ public class Raid { * @param side1AreRaiders */ public Raid(final War war, final Town raidedTown, final Town gatherTown, - final boolean side1AreRaiders) { + final boolean side1AreRaiders, OfflinePlayer owner) { this.bukkitId = new int[1]; // this.activeRaiders = activeRaiders == null ? new ArrayList<>() : activeRaiders; this.war = war; this.raidTicks = 0; this.raidedTown = raidedTown; this.gatherTown = gatherTown; + this.owner = owner; + this.raiders = side1AreRaiders ? war.getSide1() : war.getSide2(); + this.defenders = !side1AreRaiders ? war.getSide1() : war.getSide2(); + this.raidScore = 500; //AttackSide-Town - this.name = (side1AreRaiders ? war.getSide1() : war.getSide2()).toLowerCase() + "-" + raidedTown.getName().toLowerCase(); + this.name = war.getName() + "-" + raidedTown.getName().toLowerCase(); this.lootedChunks = new HashMap<>(); } @@ -182,13 +190,22 @@ public class Raid { //set the last raided time to now raidedTown.addMetaData(new LongDataField("lastRaided", (System.currentTimeMillis() / 1000))); - war.setLastRaidTime((int) (System.currentTimeMillis() / 1000)); + if(war.getSide1Players().contains(owner.getName())) { + war.setLastRaidTimeSide1((int) (System.currentTimeMillis() / 1000)); + } else if(war.getSide2Players().contains(owner.getName())) { + war.setLastRaidTimeSide2((int) (System.currentTimeMillis() / 1000)); + } else { + + } // Creates 10 second looping function for Raid this.bukkitId[0] = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) Main.getInstance(), getTickLoop(), 0L, incremental); + this.getWar().addRaid(this); + this.save(); + } @@ -221,12 +238,10 @@ public class Raid { } - /** * End of raid */ public void stop() { - this.phase = RaidPhase.END; Bukkit.getScheduler().cancelTask(this.bukkitId[0]); RaidData.removeRaid(this); } @@ -238,7 +253,7 @@ public class Raid { * @param p * @param wc */ - private synchronized void doLootAt(Player p, WorldCoord wc) { + public synchronized void doLootAt(Player p, WorldCoord wc) { p.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 60, 0, true, false, true)); //check if this loot is in progress @@ -255,23 +270,29 @@ public class Raid { //score for looting this.addPointsToRaidScore(10); - Main.warLogger.log(String.format("A townblock has been looted for $%.2d", lb.value)); + Main.warLogger.log(String.format("A townblock has been looted for $%.2f", lb.value)); //Broadcast to the whole raid for (String s : activeRaiders) { - Bukkit.getPlayer(s).sendMessage(Helper.Chatlabel() + String.format("A townblock has been looted for $%.2d and +10 points. (Added to pool)", lb.value)); + Player pl = Bukkit.getPlayer(s); + if(pl != null) pl.sendMessage(Helper.Chatlabel() + String.format("A townblock has been looted for $%.2f and +10 points. (Added to pool)", lb.value)); } for (String s : defenderPlayers) { - Bukkit.getPlayer(s).sendMessage(Helper.Chatlabel() + String.format("A townblock has been looted for $%.2d and +10 points. (Added to pool)", lb.value)); + Player pl = Bukkit.getPlayer(s); + if(pl != null) pl.sendMessage(Helper.Chatlabel() + String.format("A townblock has been looted for $%.2f and +10 points. (Added to pool)", lb.value)); } + + this.save(); } } else { //if not make a new property for it this.addLootedChunk(wc); for (String s : activeRaiders) { - Bukkit.getPlayer(s).sendMessage(Helper.Chatlabel() + "Raiders have started looting a town block!"); + Player pl = Bukkit.getPlayer(s); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raiders have started looting a town block!"); } for (String s : defenderPlayers) { - Bukkit.getPlayer(s).sendMessage(Helper.Chatlabel() + "Raiders have started looting a town block!"); + Player pl = Bukkit.getPlayer(s); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raiders have started looting a town block!"); } } @@ -318,27 +339,12 @@ public class Raid { //award negative score this.subtractPointsFromRaidScore(40); for (final String playerName : this.getActiveRaiders()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed! -40 Raid Score"); - } catch (NullPointerException ex3) { - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed! -40 Raid Score"); } for (final String playerName : this.getDefenderPlayers()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed! -40 Raid Score"); - } catch (NullPointerException ex4) { - } - } - - //tp to gather town spawn - final Player killed = event.getEntity(); - if (this.getActiveRaiders().contains(killed.getName())) { - try { - killed.teleport(this.getGatherTown().getSpawn()); - killed.sendMessage(String.valueOf(Helper.Chatlabel()) + "You died raiding and have been teleported back to the gather point."); - } catch (TownyException e) { - throw new RuntimeException(e); - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed! -40 Raid Score"); } } @@ -348,24 +354,20 @@ public class Raid { public void defenderKilledInCombat(PlayerDeathEvent event) { this.addPointsToRaidScore(20); for (final String playerName : this.getActiveRaiders()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! +20 Raid Score"); - } catch (NullPointerException ex5) { - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! +20 Raid Score"); } for (final String playerName : this.getDefenderPlayers()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! +20 Raid Score"); - } catch (NullPointerException ex6) { - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! +20 Raid Score"); } - //tp to raid town spawn - final Player killed = event.getEntity(); - if (this.getActiveRaiders().contains(killed.getName())) { - // killed.teleport(this.getGatherTown().getSpawn()); -// killed.sendMessage(String.valueOf(Helper.Chatlabel()) + "You died raiding and have been teleported back to your town's spawn."); - } +// //tp to raid town spawn +// final Player killed = event.getEntity(); +// if (this.getActiveRaiders().contains(killed.getName())) { +// // killed.teleport(this.getGatherTown().getSpawn()); +//// killed.sendMessage(String.valueOf(Helper.Chatlabel()) + "You died raiding and have been teleported back to your town's spawn."); +// } } /** @@ -375,36 +377,32 @@ public class Raid { //award negative score for (final String playerName : this.getActiveRaiders()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed before combat, no points awarded and death treated normally."); - } catch (NullPointerException ex3) { - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed before combat, no points awarded and death treated normally."); } for (final String playerName : this.getDefenderPlayers()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed before combat, no points awarded and death treated normally."); - } catch (NullPointerException ex4) { - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raider killed before combat, no points awarded and death treated normally."); } - - //tp to gather town spawn +// +// //tp to gather town spawn final Player killed = event.getEntity(); - if (this.getActiveRaiders().contains(killed.getName())) { - try { - killed.teleport(this.getGatherTown().getSpawn()); - killed.sendMessage(String.valueOf(Helper.Chatlabel()) + "You died before raiding and have been teleported back to the gather point."); - } catch (TownyException e) { - throw new RuntimeException(e); - } - } +// if (this.getActiveRaiders().contains(killed.getName())) { +// try { +// } catch (TownyException e) { +// throw new RuntimeException(e); +// } +// } //teleport the killer back to the raided towns spawn - if (this.getDefenders().contains(killed.getKiller().getName())) { - try { - killed.getKiller().teleport(this.getRaidedTown().getSpawn()); - killed.getKiller().sendMessage(String.valueOf(Helper.Chatlabel()) + "You killed a raider before the raid began and have been teleported back to the defending town."); - } catch (TownyException e) { - throw new RuntimeException(e); + if(killed.getKiller() != null) { + if (this.getDefenders().contains(killed.getKiller().getName())) { + try { + killed.getKiller().teleport(this.getRaidedTown().getSpawn()); + killed.getKiller().sendMessage(String.valueOf(Helper.Chatlabel()) + "You killed a raider before the raid began and have been teleported back to the defending town."); + } catch (TownyException e) { + throw new RuntimeException(e); + } } } } @@ -414,27 +412,23 @@ public class Raid { */ public void defenderKilledOutofCombat(PlayerDeathEvent event) { for (final String playerName : this.getActiveRaiders()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed before combat, no points awarded and death treated normally."); - } catch (NullPointerException ex5) { - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed before combat, no points awarded and death treated normally."); } for (final String playerName : this.getDefenderPlayers()) { - try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed before combat, no points awarded and death treated normally."); - } catch (NullPointerException ex6) { - } + Player pl = Bukkit.getPlayer(playerName); + if(pl != null) pl.sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed before combat, no points awarded and death treated normally."); } //tp to raid town spawn final Player killed = event.getEntity(); - if (this.getDefenders().contains(killed.getName())) { -// killed.teleport(this.getGatherTown().getSpawn()); -// killed.sendMessage(String.valueOf(Helper.Chatlabel()) + "You died before being raided and have been teleported back to the defending town's spawn."); - } +// if (this.getDefenders().contains(killed.getName())) { +//// killed.teleport(this.getGatherTown().getSpawn()); +//// killed.sendMessage(String.valueOf(Helper.Chatlabel()) + "You died before being raided and have been teleported back to the defending town's spawn."); +// } - //teleport the killer back to the raided towns spawn - if (killed.getKiller() instanceof Player) { + //teleport the killer back to the gather towns spawn + if (killed.getKiller() != null) { if (this.getActiveRaiders().contains(killed.getKiller().getName())) { try { killed.getKiller().teleport(this.getGatherTown().getSpawn()); @@ -452,7 +446,7 @@ public class Raid { * @param owner * @param raidScore */ - public void raidersWin(final Player owner, int raidScore) { + public void raidersWin(final OfflinePlayer owner, int raidScore) { //TODO finalize payout //Calc win factor @@ -461,8 +455,9 @@ public class Raid { //factor is 0.5 -> 3 double factor = ((raidScore - 800.0) / 100) + 0.5; if (factor > 3.0) factor = 3.0; + if (factor < 0) factor = 0.0; - final Resident resident = TownyAPI.getInstance().getResident(owner); + final Resident resident = TownyAPI.getInstance().getResident(owner.getUniqueId()); Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raiders from " + this.raiders + " have successfully raided " + this.raidedTown.getName() + "!"); @@ -535,13 +530,14 @@ public class Raid { double factor = (3 - ((raidScore) / 200.0) - 0.5); if (factor > 3.0) factor = 3.0; + if (factor < 0) factor = 0.0; String statement = "raided"; if (factor <= 0.5) { Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The defenders from " + this.defenders + " have barely pushed back the raiders of " + this.raiders + ". More has been lost than gained."); Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + this.raidedTown.getName() - + " has recovered part of the attackers' raid chest, valued at $625"); + + " has recovered part of the attackers' raid chest, valued at $500"); Main.warLogger .log("The defenders from " + this.defenders + " have won the raid of " + this.raidedTown.getName() + "!"); this.raidedTown.getAccount().deposit(500, "Raid chest"); @@ -549,7 +545,7 @@ public class Raid { Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The defenders from " + this.defenders + " have fended off the raiders of " + this.raiders + "!"); Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + this.raidedTown.getName() - + " has recovered the attackers' raid chest, valued at $1,250"); + + " has recovered the attackers' raid chest, valued at $1000"); Main.warLogger .log("The defenders from " + this.defenders + " have won the raid of " + this.raidedTown.getName() + "!"); this.raidedTown.getAccount().deposit(1000, "Raid chest"); @@ -557,7 +553,7 @@ public class Raid { Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The defenders from " + this.defenders + " have wholly defeated the raiders of " + this.raiders + "! They barely broke the walls."); Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + this.raidedTown.getName() - + " has recovered the attackers' raid chest, valued at $1,250."); + + " has recovered the attackers' raid chest, valued at $1000."); Main.warLogger .log("The defenders from " + this.defenders + " have won the raid of " + this.raidedTown.getName() + "!"); this.raidedTown.getAccount().deposit(1000, "Raid chest"); @@ -566,6 +562,20 @@ public class Raid { stop(); } + /** + * No winner declared + */ + public void noWinner() { + //TODO finalize payout + + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The defenders from " + this.defenders + + " didn't push back the raiders of " + this.raiders + ". Yet no loot was taken, raid considered a draw."); + Main.warLogger + .log("No one won the raid of " + this.raidedTown.getName() + "!"); + + stop(); + } + /** * Positively impact the raid score * @@ -575,6 +585,7 @@ public class Raid { this.raidScore += points; //cap if (raidScore > 1200) this.raidScore = 1200; + if (raidScore < 0) this.raidScore = 0; } /** @@ -585,6 +596,7 @@ public class Raid { public void subtractPointsFromRaidScore(final int points) { this.raidScore -= points; //cap + if (raidScore > 1200) this.raidScore = 1200; if (raidScore < 0) this.raidScore = 0; } @@ -617,11 +629,11 @@ public class Raid { this.gatherTown = town; } - public Player getOwner() { + public OfflinePlayer getOwner() { return this.owner; } - public void setOwner(Player owner) { + public void setOwner(OfflinePlayer owner) { this.owner = owner; } @@ -744,9 +756,29 @@ public class Raid { Raid.this.raiderPlayers = Raid.this.war.getSide2Players(); Raid.this.defenderPlayers = Raid.this.war.getSide1Players(); } - if (Raid.this.raidTicks >= RaidPhase.END.startTick) { - Bukkit.getServer().getScheduler().cancelTask(Raid.this.bukkitId[0]); + raidTicks += incremental; + + //check for end + if (Raid.this.raidTicks >= RaidPhase.END.startTick && Raid.this.phase == RaidPhase.COMBAT) { Raid.this.phase = RaidPhase.END; + } + + //check and start travel phase + if (Raid.this.raidTicks >= RaidPhase.TRAVEL.startTick && Raid.this.phase == RaidPhase.GATHER) { + startTravel(); + } + + //check and start combat phase + if (Raid.this.raidTicks >= RaidPhase.COMBAT.startTick && Raid.this.phase == RaidPhase.TRAVEL) { + startCombat(); + } + + //check and start gather phase + if (Raid.this.raidTicks >= RaidPhase.GATHER.startTick && Raid.this.phase == RaidPhase.START) { + startGather(); + } + + if (Raid.this.phase == RaidPhase.END) { //TODO: fix raid scoring //for raiders to win, they need a significant up from the defenders if (Raid.this.raidScore > 800) { @@ -754,68 +786,62 @@ public class Raid { } else { Raid.this.defendersWin(Raid.this.raidScore); } - } else { - raidTicks += incremental; + } - //check and start travel phase - if (Raid.this.raidTicks >= RaidPhase.TRAVEL.startTick && Raid.this.phase == RaidPhase.GATHER) { - startTravel(); + //Raid gather phase behavior + if (Raid.this.phase == RaidPhase.GATHER) { + //Broadcast + if (raidTicks % 6000 == 0) { + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid of " + + Raid.this.getRaidedTown().getName() + " will begin in " + (int) ((RaidPhase.TRAVEL.startTick - raidTicks) / 20 / 60) + " minutes!"); + Bukkit.broadcastMessage( + "The Raiders are gathering at " + getGatherTown().getName() + " before making the journey over!"); } - //check and start combat phase - if (Raid.this.raidTicks >= RaidPhase.COMBAT.startTick && Raid.this.phase == RaidPhase.TRAVEL) { - startCombat(); - } - //Raid gather phase behavior - if (Raid.this.phase == RaidPhase.GATHER) { - //Broadcast - if (raidTicks % 6000 == 0) { - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid of " - + Raid.this.getRaidedTown().getName() + " will begin in " + (int) (RaidPhase.TRAVEL.startTick / 20 / 60) + " minutes!"); - Bukkit.broadcastMessage( - "The Raiders are gathering at " + getGatherTown().getName() + " before making the journey over!"); - } - - - //Prevent players from leaving gather town prematurely - for (String playerName : getActiveRaiders()) { - try { - Player p = Bukkit.getPlayer(playerName); + //Prevent players from leaving gather town prematurely, if player is raid owner, dont do that + for (String playerName : getActiveRaiders()) { + try { + Player p = Bukkit.getPlayer(playerName); + if(p != null) { ArrayList cluster = Helper.getCluster(Raid.this.getGatherTown().getHomeBlock().getWorldCoord()); if (!cluster.contains(WorldCoord.parseWorldCoord(p))) { - Raid.this.removeActiveRaider(p.getName()); - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "By leaving the gathering town you have left the raid on " + Raid.this.getRaidedTown().getName() + "!"); - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "To rejoin, do /raid join [war] [town]"); + if (p.equals(owner)) { + p.teleport(gatherTown.getSpawn()); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You cannot leave the gather town. As raid party leader, you have remained in the party. To stop the raid type /raid abandon"); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You have been teleported to the gather town's spawn."); + } else { + Raid.this.removeActiveRaider(p.getName()); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "By leaving the gathering town you have left the raid on " + Raid.this.getRaidedTown().getName() + "!"); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "To rejoin, do /raid join [war] [town]"); + } } - } catch (NullPointerException e) { - e.printStackTrace(); - } catch (NotRegisteredException e) { - e.printStackTrace(); - } catch (TownyException e) { - e.printStackTrace(); } + } catch (NullPointerException | TownyException e) { + e.printStackTrace(); } } + } - //Raid Travel phase behavior - else if (Raid.this.phase == RaidPhase.TRAVEL) { + //Raid Travel phase behavior + else if (Raid.this.phase == RaidPhase.TRAVEL) { - //Report - if (raidTicks % 2400 == 0) { - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The Raiders of " - + (side1AreRaiders ? Raid.this.war.getSide1() : Raid.this.war.getSide2()) - + " are on their way to raid " - + Raid.this.getRaidedTown().getName() + "!"); - } + //Report + if (raidTicks % 2400 == 0) { + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The Raiders of " + + (side1AreRaiders ? Raid.this.war.getSide1() : Raid.this.war.getSide2()) + + " are on their way to raid " + + Raid.this.getRaidedTown().getName() + "!"); + } - //Check if a player has arrived at the town (in it, or within 200 blocks) and if so start combat - for (String player : activeRaiders) { - try { - boolean playerCloseToHomeBlockRaid = false; - final int homeBlockXCoordRaided = Raid.this.getRaidedTown().getHomeBlock().getCoord().getX() * 16; - final int homeBlockZCoordRaided = Raid.this.getRaidedTown().getHomeBlock().getCoord().getZ() * 16; - Player p = Bukkit.getPlayer(player); + //Check if a player has arrived at the town (in it, or within 200 blocks) and if so start combat + for (String player : activeRaiders) { + try { + boolean playerCloseToHomeBlockRaid = false; + final int homeBlockXCoordRaided = Raid.this.getRaidedTown().getHomeBlock().getCoord().getX() * 16; + final int homeBlockZCoordRaided = Raid.this.getRaidedTown().getHomeBlock().getCoord().getZ() * 16; + Player p = Bukkit.getPlayer(player); + if(p != null) { //Carryover from sieges if (Math.abs(p.getLocation().getBlockX() - homeBlockXCoordRaided) <= 200 && Math.abs(p.getLocation().getBlockZ() - homeBlockZCoordRaided) <= 200) { playerCloseToHomeBlockRaid = true; @@ -825,26 +851,30 @@ public class Raid { if (Raid.this.getRaidedTown().hasTownBlock(playercoord) || playerCloseToHomeBlockRaid) { startCombat(); } - } catch (TownyException e) { - e.printStackTrace(); } + } catch (TownyException e) { + e.printStackTrace(); } } + } - //Raid combat phase behavior - else if (Raid.this.phase == RaidPhase.COMBAT) { + //Raid combat phase behavior + else if (Raid.this.phase == RaidPhase.COMBAT) { - //Report - if (Raid.this.raidTicks % 6000 == 0) { - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "Report on the raid of " - + Raid.this.getRaidedTown().getName() + ":"); - Bukkit.broadcastMessage( - "Raid Score - " + String.valueOf(Raid.this.raidScore)); - } + //Report + if (Raid.this.raidTicks % 6000 == 0) { + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "Report on the raid of " + + Raid.this.getRaidedTown().getName() + ":"); + Bukkit.broadcastMessage( + "Raid Score - " + String.valueOf(Raid.this.raidScore)); + Bukkit.broadcastMessage( + "Chunks Looted - " + String.valueOf(Raid.this.lootedChunks.size()) + "/" + raidedTown.getTownBlocks().size()); + } - //Looting chunks - for (String name : Raid.this.getActiveRaiders()) { - Player p = Bukkit.getPlayer(name); + //Looting chunks + for (String name : Raid.this.getActiveRaiders()) { + Player p = Bukkit.getPlayer(name); + if(p != null) { WorldCoord wc = WorldCoord.parseWorldCoord(p); if (p.isSneaking()) { //check if our chunk is looted, if so skip this player @@ -862,14 +892,6 @@ public class Raid { p.sendMessage(Helper.Chatlabel() + "This space is not part of the raided town, you cannot loot this area."); } } -// for (LootBlock c : Raid.this.lootedChunks.values()) { -// if (c.worldCoord.equals(wc)) { -// looted = true; -// p.sendMessage(Helper.Chatlabel() + "This chunk is already looted, try another."); -// continue; -// } -// } - } } } @@ -878,6 +900,53 @@ public class Raid { }; } + public void save() { + RaidData.saveRaid(this); + } + + public TownBlock getHomeBlockRaided() { + return homeBlockRaided; + } + + public void setHomeBlockRaided(TownBlock homeBlockRaided) { + this.homeBlockRaided = homeBlockRaided; + } + + public TownBlock getHomeBlockGather() { + return homeBlockGather; + } + + public void setHomeBlockGather(TownBlock homeBlockGather) { + this.homeBlockGather = homeBlockGather; + } + + public void setPhase(RaidPhase phase) { + this.phase = phase; + if(phase == RaidPhase.GATHER) { + startGather(); + } else if(phase == RaidPhase.TRAVEL) { + startTravel(); + } else if(phase == RaidPhase.COMBAT) { + startCombat(); + } + } + + public Location getTownSpawnRaided() { + return townSpawnRaided; + } + + public void setTownSpawnRaided(Location townSpawnRaided) { + this.townSpawnRaided = townSpawnRaided; + } + + public Location getTownSpawnGather() { + return townSpawnGather; + } + + public void setTownSpawnGather(Location townSpawnGather) { + this.townSpawnGather = townSpawnGather; + } + public static class LootBlock { public WorldCoord worldCoord; diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Siege.java b/src/main/java/me/ShermansWorld/AlathraWar/Siege.java index 6518fdc..6052f75 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Siege.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Siege.java @@ -9,6 +9,7 @@ import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.TownyUniverse; import com.palmergames.bukkit.towny.TownyAPI; +import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.WorldCoord; @@ -94,13 +95,13 @@ public class Siege { if (WorldCoord.parseWorldCoord(Main.getInstance().getServer().getPlayer(playerName)) .getTownBlock().isHomeBlock() && WorldCoord - .parseWorldCoord( - Main.getInstance().getServer().getPlayer(playerName)) - .getTownBlock().getTown().equals(town) + .parseWorldCoord( + Main.getInstance().getServer().getPlayer(playerName)) + .getTownBlock().getTown().equals(town) && !Bukkit.getPlayer(playerName).isDead() && (Math.abs( - Bukkit.getServer().getPlayer(playerName).getLocation().getBlockY() - - townSpawn.getBlockY())) < 10) { + Bukkit.getServer().getPlayer(playerName).getLocation().getBlockY() + - townSpawn.getBlockY())) < 10) { attackersAreOnHomeBlock = true; } } catch (NotRegisteredException ex) { @@ -112,13 +113,13 @@ public class Siege { if (WorldCoord.parseWorldCoord(Main.getInstance().getServer().getPlayer(playerName)) .getTownBlock().isHomeBlock() && WorldCoord - .parseWorldCoord( - Main.getInstance().getServer().getPlayer(playerName)) - .getTownBlock().getTown().equals(town) + .parseWorldCoord( + Main.getInstance().getServer().getPlayer(playerName)) + .getTownBlock().getTown().equals(town) && !Bukkit.getPlayer(playerName).isDead() && (Math.abs( - Bukkit.getServer().getPlayer(playerName).getLocation().getBlockY() - - townSpawn.getBlockY())) < 10) { + Bukkit.getServer().getPlayer(playerName).getLocation().getBlockY() + - townSpawn.getBlockY())) < 10) { defendersAreOnHomeBlock = true; } } catch (NotRegisteredException ex3) { @@ -279,6 +280,18 @@ public class Siege { clearBeacon(); } + /** + * No winnder declared + */ + public void noWinner() { + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The siege of " + this.town.getName() + " was a draw!"); + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "No money has been recovered."); + Main.warLogger + .log(war.getName() + ": No one won the siege of " + this.town.getName() + "!"); + stop(); + clearBeacon(); + } + public void createBeacon() { try { World world = town.getWorld(); @@ -396,6 +409,14 @@ public class Siege { this.town = town; } + public OfflinePlayer getSiegeOwner() { + return siegeLeader; + } + + public void setSiegeOwner(OfflinePlayer siegeOwner) { + this.siegeLeader = siegeOwner; + } + /** Gets attacker name string */ public String getAttackers() { if (side1AreAttackers) { @@ -422,6 +443,38 @@ public class Siege { return this.defenderPoints; } + public int getMaxSiegeTicks() { + return maxSiegeTicks; + } + + public void setSiegeTicks(int siegeTicks) { + this.siegeTicks = siegeTicks; + } + + public TownBlock getHomeBlock() { + return homeBlock; + } + + public void setHomeBlock(TownBlock homeBlock) { + this.homeBlock = homeBlock; + } + + public Location getTownSpawn() { + return townSpawn; + } + + public void setTownSpawn(Location townSpawn) { + this.townSpawn = townSpawn; + } + + public void setAttackerPoints(int points) { + this.attackerPoints = points; + } + + public void setDefenderPoints(int points) { + this.defenderPoints = points; + } + public boolean getSide1AreAttackers() { return this.side1AreAttackers; } @@ -457,4 +510,8 @@ public class Siege { public void save() { SiegeData.saveSiege(this); } + + public String getName() { + return this.getWar().getName() + "-" + this.getTown(); + } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/War.java b/src/main/java/me/ShermansWorld/AlathraWar/War.java index 6065737..e5f38ca 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/War.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/War.java @@ -1,5 +1,6 @@ package me.ShermansWorld.AlathraWar; +import java.io.File; import java.util.ArrayList; import com.palmergames.bukkit.towny.TownyAPI; @@ -7,6 +8,7 @@ import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; +import me.ShermansWorld.AlathraWar.data.DataManager; import me.ShermansWorld.AlathraWar.data.WarData; public class War { @@ -22,7 +24,8 @@ public class War { // References private ArrayList sieges = new ArrayList(); private ArrayList raids = new ArrayList(); - private int lastRaidTime = 0; + private int lastRaidTimeSide1 = 0; + private int lastRaidTimeSide2 = 0; /** * War Constructor @@ -83,8 +86,10 @@ public class War { * @param nation - Nation to surrender */ public void surrenderNation(Nation nation) { - for (Town town : nation.getTowns()) { - surrenderTown(town.getName()); + if(nation != null) { + for (Town town : nation.getTowns()) { + surrenderTown(town.getName()); + } } } @@ -98,6 +103,24 @@ public class War { surrenderedTowns.add(town); } + /** + * unsurrender town, perfidy time + * @param nation - Nation to unsurrender + */ + public void unSurrenderNation(Nation nation) { + for (Town town : nation.getTowns()) { + unSurrenderTown(town.getName()); + } + } + + /** + * unsurrender town, perfidy time + * @param town - Town to unsurrender + */ + public void unSurrenderTown(String town) { + surrenderedTowns.remove(town); + } + /** * Gets side of town * -1 - Surrendered @@ -125,7 +148,7 @@ public class War { public int getSide(Town town) { return getSide(town.getName()); } - + public String getName() { return this.name; } @@ -158,6 +181,9 @@ public class War { return surrenderedTowns; } + public void setSurrenderedTowns(ArrayList towns) { + surrenderedTowns = towns; + } public ArrayList getSieges() { return sieges; @@ -175,6 +201,20 @@ public class War { raids.add(raid); } + public void setName(String name) { + DataManager.deleteFile("wars" + File.separator + this.getName() + ".yml"); + this.name = name; + this.save(); + } + + public void setSide1(String side1) { + this.side1 = side1; + } + + public void setSide2(String side2) { + this.side2 = side2; + } + /** * Saves the war into .yml file folders. */ @@ -209,11 +249,19 @@ public class War { return returnList; } - public void setLastRaidTime(int lastRaidTime) { - this.lastRaidTime = lastRaidTime; + public void setLastRaidTimeSide1(int lastRaidTime) { + this.lastRaidTimeSide1 = lastRaidTime; } - public int getLastRaidTime() { - return this.lastRaidTime; + public int getLastRaidTimeSide1() { + return this.lastRaidTimeSide1; + } + + public void setLastRaidTimeSide2(int lastRaidTime) { + this.lastRaidTimeSide2 = lastRaidTime; + } + + public int getLastRaidTimeSide2() { + return this.lastRaidTimeSide2; } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/AdminCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/AdminCommands.java new file mode 100644 index 0000000..e670a7e --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/AdminCommands.java @@ -0,0 +1,1662 @@ +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.Nation; +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; +import com.palmergames.bukkit.towny.object.metadata.LongDataField; +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 org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.sql.Timestamp; +import java.util.Random; + +public class AdminCommands implements CommandExecutor { + + public AdminCommands(final Main plugin) { + plugin.getCommand("alathrawaradmin").setExecutor(this); + plugin.getCommand("awa").setExecutor(this); + } + + /** + * //done + * //edit war/event in real time, side can be "both" to effect both + * -modify raid score [war] [town] [value] + * -modify raid townspawn [war] [town] (x) (Z) + * -modify raid gather [war] [town] [town] + * -modify raid phase [war] [town] [phase] //"next" to move to next phase + * -modify raid loot [war] [town] (x) (z) [value,looted,ticks,reset] [amt] //no coords just does current chunk, reset deletes it from the list + * -modify raid time [war] [town] [add/set] [value] + * -modify raid owner [war] [town] [add/set] [value] + * -modify raid move [war] [town] [newWar] //low priority, moves raid to other war + * -modify raid clearActive [war] [town] //low priority + *

+ * -modify siege score [war] [town] [side] [amt] + * -modify siege homeblock [war] [town] (x) (Z) + * -modify siege time [war] [town] [add/set/max] [value] //max modified the max length + * -modify siege owner [war] [town] [add/set] [value] + * -modify siege move [war] [town] [newWar] //low priority, moves siege to other war + *

+ * -modify war score [war] [side] [amt] + * -modify war side [war] [side] [name] + * -modify war name [war] [name] + * -modify war add town [war] [town] + * -modify war add nation [war] [nation] + * -modify war surrender town [war] [town] //adds town to surrender list + * -modify war surrender nation [war] [town] //adds all towns to surrender list + * -modify war raidTime [add,set,reset] [war] [town] [amt] //set when last raid was + *

+ * // low priority idea + * -info war score [war] + * -info war surrenderedTowns [war] + * -info war raids [war] + * -info war sieges [war] + * -info war towns [war] + * -info war lastRaidTime [war] + *

+ * -info raid homeblock [war] [town] + * -info raid lootedTownBlocks [war] [town] + * -info raid timeSinceLastRaid [town] + *

+ * -info siege homeblock [war] [town] + *

+ * //done + * //force make event or war, if owner isnt defined, idk havent decided + * -create siege [war] [town] (owner) + * -create raid [war] [raidTown] (gatherTown) (owner) + * -create war [name] [side1] [side2] + *

+ * //force end a war/event, can declare winner side, or no winner + * -force end war [war] (side/victor) + * -force end siege [war] [town] (side/victor) + * -force end raid [war] [town] (side/victor) + *

+ * //done + * //force player into or out of a war/event + * -force join war [player] [war] [side] + * -force join siege [player] [war] [town] (side) + * -force join raid [player] [war] [town] (side) + *

+ * //done + * -force leave raid [war] [player] (timeout) //kicks from raid party + *

+ * //ultra low priority + * //exclude players from join a raid or war for a time + * //raid subcommand prevents joining or being raided + * -exclude player war [war] (side) (timeout) + * -exclude town war [war] (side) (timeout) + * -exclude nation war [war] (side) (timeout) + *

+ * //ultra low priority + * -exclude player raid [join/target] [war] (timeout) + * -exclude town raid [join/target] [war] (timeout) + * -exclude nation raid [join/target] [war] (timeout) + *

+ * // Ultra low priority idea + * -rule raidersRespawnAtGatherTown [true/false] + * -rule siegersRespawnAtTown [true/false] + * -rule anyoneCanJoinRaid [true/false] + * + * @param sender Source of the command + * @param command Command which was executed + * @param label Alias of the command which was used + * @param args Passed command arguments + * @return Valid command + */ + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + final Player p = (Player) sender; + if (!p.hasPermission("AlathraWar.admin")) { + return fail(p, args, "permissions"); + } + + if (args.length == 0) { + return fail(p, args, "syntax"); + } else if (args.length >= 1) { + if (args[0].equalsIgnoreCase("create")) { + return create(p, args); + } else if (args[0].equalsIgnoreCase("force")) { + return force(p, args); + } else if (args[0].equalsIgnoreCase("help")) { + return help(p, args); + } else if (args[0].equalsIgnoreCase("info")) { + return info(p, args); + } else if (args[0].equalsIgnoreCase("modify")) { + return modify(p, args); + } else if (args[0].equalsIgnoreCase("awa")) { + return awa(p, args); + } + } + return true; + } + + + /** + * //force make event or war, if owner isn't defined, idk haven't decided + * -create siege [war] [town] (owner) + * -create raid [war] [raidTown] [gatherTown] (owner) + * -create war [name] [side1] [side2] + */ + private static boolean create(Player p, String[] args) { + if (args.length >= 2) { + if (args[1].equalsIgnoreCase("raid")) { + if (args.length >= 4) { + //specific behavior exists if an admin ran this + //using same method so that this doesnt get fucked up if we go back and change original implementation + RaidCommands.startRaid(p, args, true); + } else { + //defaultCode will bypass the custom gather town to force set owner + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin create raid [war] [raidTown] (gatherTown/\"defaultCode\") (owner)")); + } + return true; + } else if (args[1].equalsIgnoreCase("siege")) { + //this exists to force a seige with a new owner + if (args.length >= 4) { + String[] adjusted = new String[]{ + "start", + args[2], + args[3], + args[4] + }; + SiegeCommands.siegeStart(p, adjusted, true); + p.sendMessage(Helper.Chatlabel() + "Try again later!"); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin create siege [war] [town] (owner)")); + return true; + } + } else if (args[1].equalsIgnoreCase("war")) { + //this command basically is a copy or /war create + if (args.length == 5) { + //should purge the first argument; + String[] adjusted = new String[]{ + args[1], + args[2], + args[3], + args[4] + }; + WarCommands.warCreate(p, adjusted); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin create war [name] [side1] [side2]")); + return true; + } + } + } else { + return fail(p, args, "syntax"); + } + return fail(p, args, "syntax"); + } + + private static boolean exclude(Player p, String[] args) { + return true; + } + + /** + * //force end a war/event, can declare winner side, or no winner + * -force end war [name] (side/victor) + * -force end siege [war] [town] (side/victor) + * -force end raid [war] [town] (side/victor) + *

+ * //force player into or out of a war/event + * -force join siege [player] [war] [town] (side) + * -force join raid [player] [war] [town] (side) + * -force leave raid [war] [player] (timeout) //kicks from raid party + * -force surrender war [name] [town] + * + * @param p player + * @param args args + * @return result + */ + private static boolean force(Player p, String[] args) { + if (args.length >= 2) { + if (args[1].equalsIgnoreCase("end")) { + if (args.length >= 3) { + if (args[2].equalsIgnoreCase("raid")) { + for (Raid r : RaidData.getRaids()) { + if (args.length >= 6) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + if (r.getSide1AreRaiders()) { + if (args[5].equals(r.getWar().getSide1())) { + r.raidersWin(r.getOwner(), r.getRaidScore()); + p.sendMessage(Helper.Chatlabel() + "Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (raiders) declared as victor."); + Main.warLogger.log("Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (raiders) declared as victor."); + return true; + } else if (args[5].equals(r.getWar().getSide2())) { + r.defendersWin(r.getRaidScore()); + p.sendMessage(Helper.Chatlabel() + "Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + Main.warLogger.log("Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + return true; + } else { + p.sendMessage(Helper.color("&cSide not found!")); + return true; + } + } else { + if (args[5].equals(r.getWar().getSide1())) { + r.defendersWin(r.getRaidScore()); + p.sendMessage(Helper.Chatlabel() + "Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + Main.warLogger.log("Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + return true; + } else if (args[5].equals(r.getWar().getSide2())) { + r.raidersWin(r.getOwner(), r.getRaidScore()); + p.sendMessage(Helper.Chatlabel() + "Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (raiders) declared as victor."); + Main.warLogger.log("Raid forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (raiders) declared as victor."); + return true; + } else { + p.sendMessage(Helper.color("&cSide not found!")); + return true; + } + } + } + } else if (args.length == 5) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + r.noWinner(); + p.sendMessage(Helper.Chatlabel() + "Raid forcefully ended on " + args[4] + " in war " + args[3] + " with no victor."); + Main.warLogger.log("Raid forcefully ended on " + args[4] + " in war " + args[3] + " with no victor."); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force end raid [war] [town] (side)")); + return true; + } + } + p.sendMessage(Helper.color("&cRaid not found!")); + return true; + + } else if (args[2].equalsIgnoreCase("siege")) { + for (Siege s : SiegeData.getSieges()) { + if (args.length >= 6) { + if (s.getWar().getName().equals(args[3]) && s.getTown().getName().equals(args[4])) { + if (s.getSide1AreAttackers()) { + if (args[5].equals(s.getWar().getSide1())) { + s.attackersWin(s.getSiegeOwner()); + p.sendMessage(Helper.Chatlabel() + "Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (attackers) declared as victor."); + Main.warLogger.log("Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (attackers) declared as victor."); + return true; + } else if (args[5].equals(s.getWar().getSide2())) { + s.defendersWin(); + p.sendMessage(Helper.Chatlabel() + "Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + Main.warLogger.log("Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + return true; + } else { + p.sendMessage(Helper.color("&cSide not found!")); + return true; + } + } else { + if (args[5].equals(s.getWar().getSide1())) { + s.defendersWin(); + p.sendMessage(Helper.Chatlabel() + "Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + Main.warLogger.log("Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (defenders) declared as victor."); + return true; + } else if (args[5].equals(s.getWar().getSide2())) { + s.attackersWin(s.getSiegeOwner()); + p.sendMessage(Helper.Chatlabel() + "Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (attackers) declared as victor."); + Main.warLogger.log("Siege forcefully ended on " + args[4] + " in war " + args[3] + " with " + args[6] + " (attackers) declared as victor."); + return true; + } else { + p.sendMessage(Helper.color("&cSide not found!")); + return true; + } + } + } + } else if (args.length == 5) { + if (s.getWar().getName().equals(args[3]) && s.getTown().getName().equals(args[4])) { + s.noWinner(); + p.sendMessage(Helper.Chatlabel() + "Siege forcefully ended on " + args[4] + " in war " + args[3] + " with no victor."); + Main.warLogger.log("Siege forcefully ended on " + args[4] + " in war " + args[3] + " with no victor."); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force end siege [war] [town] (side)")); + return true; + } + } + p.sendMessage(Helper.color("&cSiege not found!")); + return true; + } else if (args[2].equalsIgnoreCase("war")) { + //TODO determine if needed + p.sendMessage(Helper.color("&cUnused! use /war delete")); + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force end [raid/siege]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force end [raid/siege]")); + return true; + } + } else if (args[1].equalsIgnoreCase("join")) { + if (args.length >= 3) { + if (args[2].equalsIgnoreCase("raid")) { + if (args.length >= 6) { + //fix args + String[] adjusted = new String[]{ + args[1], //join + args[4], //war + args[5], //town + args[3], //player + args.length >= 7 ? args[6] : null //side + }; + RaidCommands.joinRaid(p, adjusted, true); + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force join raid [player] [war] [town] (side)")); + } + return true; + } else if (args[2].equalsIgnoreCase("siege")) { + if (args.length >= 6) { + p.sendMessage(Helper.color("&cError! Unimplemented!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force join siege [player] [war] [town] (side)")); + } + return true; + } else if (args[2].equalsIgnoreCase("war")) { + if (args.length >= 6) { + //fix args to match + String[] adjusted = new String[]{ + args[1], + args[4], + args[5], + args[3] + }; + //find the player + Player target = p; + if (Bukkit.getPlayer(args[3]) != null) { + target = Bukkit.getPlayer(args[3]); + } else { + p.sendMessage(Helper.color("c") + args[3] + " does not exist!"); + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force join war [player] [war] [side]")); + return true; + } + WarCommands.warJoin(p, adjusted, true); + p.sendMessage(Helper.color("&cForced " + args[3] + " to join the war " + args[4] + " on side " + args[5])); + Main.warLogger.log("Forced " + args[3] + " to join the war " + args[4] + " on side " + args[5]); + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force join war [player] [war] [side]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force join [raid/siege/war]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force join [raid/siege/war]")); + return true; + } + } else if (args[1].equalsIgnoreCase("leave")) { + if (args.length >= 3) { + if (args[2].equalsIgnoreCase("raid")) { + if (args.length >= 6) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + if (Bukkit.getPlayer(args[5]) != null) { + String[] fixed = new String[]{ + args[1], + args[3], + args[4], + args[5] + }; + RaidCommands.leaveRaid(p, fixed, true); + p.sendMessage(Helper.Chatlabel() + "Forced player " + args[5] + " to leave raid on " + args[4] + " in war " + args[3]); + Main.warLogger.log("Forced player " + args[5] + " to leave raid on " + args[4] + " in war " + args[3]); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cPlayer not found!")); + return true; + } + } + } + p.sendMessage(Helper.color("&cRaid not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force leave raid [war] [town] [player] (timeout)")); + } + return true; + } else if (args[2].equalsIgnoreCase("siege")) { + //TODO determine if needed + p.sendMessage(Helper.color("&cUnused! use /siege abandon or /war surrender")); + return true; + } else if (args[2].equalsIgnoreCase("war")) { + //TODO determine if needed + p.sendMessage(Helper.color("&cUnused! use /war surrender")); + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force leave [raid/siege/war]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin force leave [raid/siege/war]")); + return true; + } + } else { + return fail(p, args, "syntax"); + } + } + return fail(p, args, "syntax"); + } + + private static boolean help(Player p, String[] args) { + p.sendMessage(Helper.Chatlabel() + "/alathrawaradmin create"); + p.sendMessage(Helper.Chatlabel() + "/alathrawaradmin force"); + p.sendMessage(Helper.Chatlabel() + "/alathrawaradmin help"); + p.sendMessage(Helper.Chatlabel() + "/alathrawaradmin info"); + p.sendMessage(Helper.Chatlabel() + "/alathrawaradmin modify"); + return true; + } + + /** + * // low priority idea + * -info war [war] + *

+ * -info raid [war] [town] + *

+ * -info siege [war] [town] + * + * @param p player + * @param args args + * @return result + */ + private static boolean info(Player p, String[] args) { + if (args.length >= 2) { + if (args[1].equalsIgnoreCase("war")) { + if (args.length >= 3) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[2])) { + p.sendMessage(Helper.Chatlabel() + "Info dump for war: " + w.getName()); + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + p.sendMessage(Helper.Chatlabel() + "Name: " + w.getName()); + p.sendMessage(Helper.Chatlabel() + "Side 1: " + w.getSide1()); + p.sendMessage(Helper.Chatlabel() + "Side 2: " + w.getSide2()); + p.sendMessage(Helper.Chatlabel() + "Last Raid for Side 1: " + new Timestamp(((long) w.getLastRaidTimeSide1()) * 1000L)); + p.sendMessage(Helper.Chatlabel() + "Last Raid for Side 2: " + new Timestamp(((long) w.getLastRaidTimeSide2()) * 1000L)); + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + StringBuilder side1Towns = new StringBuilder(); + StringBuilder side1Players = new StringBuilder(); + for (String t : w.getSide1Towns()) { + side1Towns.append(t); + side1Towns.append(", "); + } + for (String pl : w.getSide1Players()) { + side1Players.append(pl); + side1Players.append(", "); + } + //cut off last two characters + if(side1Towns.length() > 2) side1Towns = new StringBuilder(side1Towns.substring(0, side1Towns.length() - 2)); + if(side1Players.length() > 2) side1Players = new StringBuilder(side1Players.substring(0, side1Players.length() - 2)); + p.sendMessage(Helper.Chatlabel() + w.getSide1() + " Towns: " + side1Towns); + p.sendMessage(Helper.Chatlabel() + w.getSide1() + " Players: " + side1Players); + + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + StringBuilder side2Towns = new StringBuilder(); + StringBuilder side2Players = new StringBuilder(); + for (String t : w.getSide2Towns()) { + side2Towns.append(t); + side2Towns.append(", "); + } + for (String pl : w.getSide2Players()) { + side2Players.append(pl); + side2Players.append(", "); + } + //cut off last two characters + if(side2Towns.length() > 2) side2Towns = new StringBuilder(side2Towns.substring(0, side2Towns.length() - 2)); + if(side2Players.length() > 2) side2Players = new StringBuilder(side2Players.substring(0, side2Players.length() - 2)); + p.sendMessage(Helper.Chatlabel() + w.getSide2() + " Towns: " + side2Towns); + p.sendMessage(Helper.Chatlabel() + w.getSide2() + " Players: " + side2Players); + + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + StringBuilder surrenderedTowns = new StringBuilder(); + for (String t : w.getSurrenderedTowns()) { + surrenderedTowns.append(t); + surrenderedTowns.append(", "); + } + //cut off last two characters + if(surrenderedTowns.length() > 2) surrenderedTowns = new StringBuilder(surrenderedTowns.substring(0, surrenderedTowns.length() - 2)); + p.sendMessage(Helper.Chatlabel() + "Surrendered Towns: " + surrenderedTowns); + return true; + } + } + p.sendMessage(Helper.color("&cError Raid not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin info [raid/siege/war]")); + } + return true; + } else if (args[1].equalsIgnoreCase("raid")) { + if (args.length >= 4) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[2]) && r.getRaidedTown().getName().equals(args[3])) { + p.sendMessage(Helper.Chatlabel() + "Info dump for raid: " + r.getName()); + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + p.sendMessage(Helper.Chatlabel() + "Name: " + r.getName()); + p.sendMessage(Helper.Chatlabel() + "Raiders: " + r.getRaiders()); + p.sendMessage(Helper.Chatlabel() + "Defenders: " + r.getDefenders()); + p.sendMessage(Helper.Chatlabel() + "Raid Score: " + r.getRaidScore()); + p.sendMessage(Helper.Chatlabel() + "War: " + r.getWar().getName()); + p.sendMessage(Helper.Chatlabel() + "Raided Town: " + r.getRaidedTown().getName()); + p.sendMessage(Helper.Chatlabel() + "Gather Town: " + r.getGatherTown().getName()); + p.sendMessage(Helper.Chatlabel() + "Current Phase: " + r.getPhase().name()); + p.sendMessage(Helper.Chatlabel() + "Tick progress: " + r.getRaidTicks()); + p.sendMessage(Helper.Chatlabel() + "Owner: " + r.getOwner().getName()); + p.sendMessage(Helper.Chatlabel() + "Gather Homeblock: " + r.getHomeBlockGather().toString()); + p.sendMessage(Helper.Chatlabel() + "Raided Homeblock: " + r.getHomeBlockRaided().toString()); + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + StringBuilder activeRaiders = new StringBuilder(); + for (String pl : r.getActiveRaiders()) { + activeRaiders.append(pl); + activeRaiders.append(", "); + } + //cut off last two characters + if(activeRaiders.length() > 2) activeRaiders = new StringBuilder(activeRaiders.substring(0, activeRaiders.length() - 2)); + p.sendMessage(Helper.Chatlabel() + "Raiding Players: " + activeRaiders); + return true; + } + } + p.sendMessage(Helper.color("&cError Raid not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin info [raid/siege/war]")); + } + return true; + } else if (args[1].equalsIgnoreCase("siege")) { + if (args.length >= 4) { + for (Siege s : SiegeData.getSieges()) { + if (s.getWar().getName().equals(args[2]) && s.getTown().getName().equals(args[3])) { + p.sendMessage(Helper.Chatlabel() + "Info dump for siege: " + s.getName()); + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + p.sendMessage(Helper.Chatlabel() + "Name: " + s.getName()); + p.sendMessage(Helper.Chatlabel() + "Attackers: " + s.getAttackers()); + p.sendMessage(Helper.Chatlabel() + "Defenders: " + s.getDefenders()); + p.sendMessage(Helper.Chatlabel() + "Attacker points: " + s.getAttackerPoints()); + p.sendMessage(Helper.Chatlabel() + "Defender points: " + s.getDefenderPoints()); + p.sendMessage(Helper.Chatlabel() + "War: " + s.getWar().getName()); + p.sendMessage(Helper.Chatlabel() + "Attacked Town: " + s.getTown().getName()); + p.sendMessage(Helper.Chatlabel() + "Max Ticks: " + s.getMaxSiegeTicks()); + p.sendMessage(Helper.Chatlabel() + "Tick progress: " + s.getSiegeTicks()); + p.sendMessage(Helper.Chatlabel() + "Owner: " + s.getSiegeOwner()); + p.sendMessage(Helper.Chatlabel() + "Homeblock: " + s.getHomeBlock().toString()); + p.sendMessage(Helper.Chatlabel() + "oOo------------===------------oOo"); + StringBuilder attackers = new StringBuilder(); + for (String pl : s.getAttackerPlayers()) { + attackers.append(pl); + attackers.append(", "); + } + //cut off last two characters + if(attackers.length() > 2) attackers = new StringBuilder(attackers.substring(0, attackers.length() - 2)); + p.sendMessage(Helper.Chatlabel() + "Attacking Players: " + attackers); + StringBuilder defenders = new StringBuilder(); + for (String pl : s.getAttackerPlayers()) { + defenders.append(pl); + defenders.append(", "); + } + //cut off last two characters + if(defenders.length() > 2) defenders = new StringBuilder(defenders.substring(0, defenders.length() - 2)); + p.sendMessage(Helper.Chatlabel() + "Defending Players: " + defenders); + return true; + } + } + p.sendMessage(Helper.color("&cError Siege not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin info [raid/siege/war]")); + } + return true; + } else { + return fail(p, args, "syntax"); + } + } + return fail(p, args, "syntax"); + } + + /** + * //edit war/event in real time, side can be "both" to effect both + * -modify raid score [add/set] [war] [town] [value] + * -modify raid townspawn [war] [town] (x) (y) (Z) + * -modify raid gather [war] [town] [town] + * -modify raid phase [war] [town] [phase] //"next" to move to next phase + * -modify raid loot [war] [town] [value,looted,ticks,reset] [amt] (x) (z) //no coords just does current chunk, reset deletes it from the list + * -modify raid time [war] [town] [add/set] [value] + * -modify raid owner [war] [town] [player] + * -modify raid move [war] [town] [newWar] //low priority, moves raid to other war/ town + * -modify raid clearActive [war] [town] //low priority + *

+ * -modify siege score [war] [town] [side] [amt] + * -modify siege townspawn [war] [town] (x) (Z) + * -modify siege time [war] [town] [add/set/max] [value] //max modified the max length + * -modify siege owner [war] [town] [add/set] [value] + * -modify siege move [war] [town] [newWar] //low priority, moves siege to other war/town + *

+ * -modify war score [war] [side] [amt] + * -modify war side [war] [side] [name] + * -modify war name [war] [name] + * -modify war add town [war] [town] + * -modify war add nation [war] [nation] + * -modify war surrender town [war] [town] //adds town to surrender list + * -modify war surrender nation [war] [town] //adds all towns to surrender list + * -modify war raidTime [add,set,reset] [war] [town] [amt] //set when last raid was + * + * @param p player + * @param args args + * @return result + */ + private static boolean modify(Player p, String[] args) { + if (args.length >= 2) { + if (args[1].equalsIgnoreCase("raid")) { + if (args.length >= 3) { + if (args[2].equalsIgnoreCase("score")) { + if (args.length >= 7) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + if (args[5].equalsIgnoreCase("add")) { + r.addPointsToRaidScore(Integer.parseInt(args[6])); + p.sendMessage(Helper.Chatlabel() + "Added " + args[6] + " points to the raid score in the war " + args[3] + " on town " + args[4]); + Main.warLogger.log("Added " + args[6] + " points to the raid score in the war " + args[3] + " on town " + args[4]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("subtract")) { + r.subtractPointsFromRaidScore(Integer.parseInt(args[6])); + p.sendMessage(Helper.Chatlabel() + "Subtracted " + args[6] + " points to the raid score in the war " + args[3] + " on town " + args[4]); + Main.warLogger.log("Subtracted " + args[6] + " points to the raid score in the war " + args[3] + " on town " + args[4]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("set")) { + r.setRaidScore(Integer.parseInt(args[6])); + p.sendMessage(Helper.Chatlabel() + "Set " + args[6] + " points as the raid score in the war " + args[3] + " on town " + args[4]); + Main.warLogger.log("Set " + args[6] + " points as the raid score in the war " + args[3] + " on town " + args[4]); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid score [add/subtract/set] [war] [town] [value]")); + return true; + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cRaid cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid score [add/subtract/set] [war] [town] [value]")); + } + return true; + } else if (args[2].equalsIgnoreCase("townspawn")) { + if (args.length >= 5) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + if (args.length == 6 || args.length == 7) { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid townspawn [war] [town] (x) (Z)")); + return true; + } + Town t = r.getRaidedTown(); + if (args.length >= 8) { + if (p.getWorld() == t.getWorld()) { + try { + WorldCoord tb = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) Double.parseDouble(args[5]), (int) Double.parseDouble(args[7])); + if (t.hasTownBlock(tb)) { + t.setHomeBlock(tb.getTownBlock()); + t.setSpawn(new Location(p.getWorld(), Double.parseDouble(args[5]), Double.parseDouble(args[6]), Double.parseDouble(args[7]))); + r.setHomeBlockRaided(tb.getTownBlock()); + r.setTownSpawnRaided(t.getSpawn()); + p.sendMessage(Helper.Chatlabel() + "Set town spawn for raided town " + args[4] + " in war " + args[3] + " to " + p.getLocation().toString()); + Main.warLogger.log("Set town spawn for raided town " + args[4] + " in war " + args[3] + " to [" + args[5] + "," + args[6] + "," + args[7] + "]"); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cTown does not contain town block at [" + args[5] + "," + args[7] + "]")); + return true; + } + } catch (NotRegisteredException e) { + p.sendMessage(Helper.color("&cError! Townblock does not exist!")); + return true; + } catch (TownyException e) { + p.sendMessage(Helper.color("&cError!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cError! Wrong world!")); + return true; + } + } else { + if (p.getWorld() == t.getWorld()) { + try { + WorldCoord tb = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) p.getLocation().getX(), (int) p.getLocation().getZ()); + if (t.hasTownBlock(tb)) { + t.setHomeBlock(tb.getTownBlock()); + t.setSpawn(p.getLocation()); + r.setHomeBlockRaided(tb.getTownBlock()); + r.setTownSpawnRaided(t.getSpawn()); + p.sendMessage(Helper.Chatlabel() + "Set town spawn for raided town " + args[4] + " in war " + args[3] + " to " + p.getLocation().toString()); + Main.warLogger.log("Set town spawn for raided town " + args[4] + " in war " + args[3] + " to " + p.getLocation().toString()); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cTown does not contain town block at your location [" + (int) p.getLocation().getX() + "," + (int) p.getLocation().getZ() + "]")); + return true; + } + } catch (NotRegisteredException e) { + p.sendMessage(Helper.color("&cError! Townblock does not exist!")); + return true; + } catch (TownyException e) { + p.sendMessage(Helper.color("&cError!")); + return true; + } + } else { + return true; + } + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cRaid cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid homeblock [war] [town] (x) (Z)")); + } + return true; + } else if (args[2].equalsIgnoreCase("gather")) { + if (args.length >= 6) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + Town t = TownyAPI.getInstance().getTown(args[5]); + if (t != null) { + r.setRaidedTown(t); + try { + r.setTownSpawnGather(t.getSpawn()); + r.setHomeBlockGather(t.getHomeBlock()); + } catch (TownyException e) { + p.sendMessage(Helper.Chatlabel() + "Error!"); + throw new RuntimeException(e); + } + p.sendMessage(Helper.Chatlabel() + "Set town new gather town " + t.getName() + " for raid against " + args[4] + " in war " + args[3]); + Main.warLogger.log("Set town new gather town " + t.getName() + " for raid against " + args[4] + " in war " + args[3]); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cTown does not exists!")); + return true; + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cRaid cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid gather [war] [town] [town]")); + } + return true; + } else if (args[2].equalsIgnoreCase("phase")) { + if (args.length >= 6) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + //parse phase + RaidPhase ph; + if (args[5].equalsIgnoreCase("next")) { + ph = RaidPhase.getNext(r.getPhase()); + } else { + ph = RaidPhase.getByName(args[5]); + } + + //if we found it + if (ph != null) { + r.setPhase(ph); + r.setRaidTicks(ph.startTick); + + p.sendMessage(Helper.Chatlabel() + "Set phase for raid against " + args[4] + " in war " + args[3] + " to " + ph.name()); + Main.warLogger.log("Set phase for raid against " + args[4] + " in war " + args[3] + " to " + ph.name()); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cPhase does not exist!")); + return true; + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cRaid cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid phase [war] [town] [phase]")); + } + return true; + } else if (args[2].equalsIgnoreCase("loot")) { + if (args.length >= 5) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + //parse phase + if (!p.getWorld().equals(r.getRaidedTown().getWorld())) { + p.sendMessage(Helper.Chatlabel() + "Error wrong world"); + return true; + } + if (args.length >= 9) { + if (args[5].equalsIgnoreCase("value")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) Double.parseDouble(args[7]), (int) Double.parseDouble(args[8])); + Raid.LootBlock lb = r.getLootedChunks().get(wc); + if(lb == null) { + r.addLootedChunk(wc); + lb = r.getLootedChunks().get(wc); + p.sendMessage(Helper.Chatlabel() + "Created empty loot chunk data to modify"); + } + lb.value = Double.parseDouble(args[6]); + p.sendMessage(Helper.Chatlabel() + "Set value for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set value for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("looted")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) Double.parseDouble(args[7]), (int) Double.parseDouble(args[8])); + Raid.LootBlock lb = r.getLootedChunks().get(wc); + //The base value can be abjusted + if(!lb.finished && Boolean.parseBoolean(args[6])) { + lb.value = new Random().nextDouble() * 100; + //score for looting + r.addPointsToRaidScore(10); + p.sendMessage(Helper.Chatlabel() + "Gave value and score because flag was false before."); + Main.warLogger.log("Gave value and score because flag was false before."); + } + + lb.finished = Boolean.parseBoolean(args[6]); + p.sendMessage(Helper.Chatlabel() + "Set finished flag for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set finished flag for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("ticks")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) Double.parseDouble(args[7]), (int) Double.parseDouble(args[8])); + Raid.LootBlock lb = r.getLootedChunks().get(wc); + lb.ticks = Integer.parseInt(args[6]); + p.sendMessage(Helper.Chatlabel() + "Set ticks for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set ticks for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("reset")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) Double.parseDouble(args[6]), (int) Double.parseDouble(args[7])); + r.getLootedChunks().remove(wc); + p.sendMessage(Helper.Chatlabel() + "Reset loot for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3]); + Main.warLogger.log("Reset loot for a chunk [" + args[7] + "," + args[8] + "] in raid against " + args[4] + " in war " + args[3]); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid loot [war] [town] [value,looted,ticks,reset] [amt] (x) (z)")); + return true; + } + } else if (args.length == 8) { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid loot [war] [town] [value,looted,ticks,reset] [amt] (x) (z)")); + return true; + } else { + if (args[5].equalsIgnoreCase("value")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getLocation()); + Raid.LootBlock lb = r.getLootedChunks().get(wc); + lb.value = Integer.parseInt(args[6]); + p.sendMessage(Helper.Chatlabel() + "Set value for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set value for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("looted")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getLocation()); + Raid.LootBlock lb = r.getLootedChunks().get(wc); + lb.finished = Boolean.parseBoolean(args[6]); + p.sendMessage(Helper.Chatlabel() + "Set looted status for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set looted status for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("ticks")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getLocation()); + Raid.LootBlock lb = r.getLootedChunks().get(wc); + lb.ticks = Integer.parseInt(args[6]); + p.sendMessage(Helper.Chatlabel() + "Set ticks for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set ticks for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("reset")) { + WorldCoord wc = WorldCoord.parseWorldCoord(p.getLocation()); + r.getLootedChunks().remove(wc); + p.sendMessage(Helper.Chatlabel() + "Reset loot for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3]); + Main.warLogger.log("Reset Loot for a chunk [" + p.getLocation().getX() + "," + p.getLocation().getZ() + "] in raid against " + args[4] + " in war " + args[3]); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid loot [war] [town] [value,looted,ticks,reset] [amt] (x) (z)")); + return true; + } + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cRaid cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid loot [war] [town] [value,looted,ticks,reset] [amt] (x) (z)")); + } + return true; + } else if (args[2].equalsIgnoreCase("time")) { + if (args.length >= 7) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + //parse phase + if (args[5].equalsIgnoreCase("add")) { + r.setRaidTicks(r.getRaidTicks() + Integer.parseInt(args[6])); + p.sendMessage(Helper.Chatlabel() + "Set time for raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set time for raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else if (args[5].equalsIgnoreCase("set")) { + int t = Integer.parseInt(args[6]); + if (t >= r.getPhase().startTick) { + r.setRaidTicks(t); + p.sendMessage(Helper.Chatlabel() + "Set time for raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set time for raid against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.Chatlabel() + Helper.color("&cTime set before current phase, use \"/alathrawaradmin modify raid phase\" instead")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid time [war] [town] [add/set] [value]")); + return true; + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cRaid cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid time [war] [town] [add/set] [value]")); + } + return true; + } else if (args[2].equalsIgnoreCase("owner")) { + if (args.length >= 6) { + for (Raid r : RaidData.getRaids()) { + if (r.getWar().getName().equals(args[3]) && r.getRaidedTown().getName().equals(args[4])) { + Player own = Bukkit.getPlayer(args[5]); + if (own != null) { + if(r.getActiveRaiders().contains(own.getName())) { + r.setOwner(own); + p.sendMessage(Helper.Chatlabel() + "Set owner of raid against " + args[4] + " in war " + args[3] + " to " + own.getName()); + Main.warLogger.log("Set owner of raid against " + args[4] + " in war " + args[3] + " to " + own.getName()); + return finalizeRaid(r); + } else { + p.sendMessage(Helper.Chatlabel() + "Player is not an active raider!"); + return true; + } + } else { + p.sendMessage(Helper.color("&cPlayer not found!")); + return true; + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cRaid cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid owner [war] [town] [player]")); + } + return true; + } else if (args[2].equalsIgnoreCase("move")) { + //TODO later + p.sendMessage(Helper.color("&cError! Not implemented!")); + return true; + } else if (args[2].equalsIgnoreCase("clearActive")) { + //TODO later + p.sendMessage(Helper.color("&cError! Not implemented!")); + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid [propery]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify [raid/siege/war] [propery]")); + return true; + } + } else if (args[1].equalsIgnoreCase("siege")) { + if (args.length >= 3) { + if (args[2].equalsIgnoreCase("score")) { + if (args.length >= 8) { + for (Siege s : SiegeData.getSieges()) { + if (s.getWar().getName().equals(args[3]) && s.getTown().getName().equals(args[4])) { + if (args[5].equalsIgnoreCase("add")) { + if (s.getWar().getSide1().equals(args[6])) { + if (s.getSide1AreAttackers()) { + s.addPointsToAttackers(Integer.parseInt(args[7])); + } else { + s.addPointsToDefenders(Integer.parseInt(args[7])); + } + p.sendMessage(Helper.Chatlabel() + "Added " + args[7] + " points to side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + Main.warLogger.log("Added " + args[7] + " points to side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + return finalizeSiege(s); + } else if (s.getWar().getSide2().equals(args[6])) { + if (s.getSide1AreAttackers()) { + s.addPointsToDefenders(Integer.parseInt(args[7])); + } else { + s.addPointsToAttackers(Integer.parseInt(args[7])); + } + p.sendMessage(Helper.Chatlabel() + "Added " + args[7] + " points to side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + Main.warLogger.log("Added " + args[7] + " points to side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + return finalizeSiege(s); + } else { + p.sendMessage(Helper.color("&cSide not found")); + return true; + } + } else if (args[5].equalsIgnoreCase("set")) { + if (s.getWar().getSide1().equals(args[6])) { + if (s.getSide1AreAttackers()) { + s.setAttackerPoints(Integer.parseInt(args[7])); + } else { + s.setDefenderPoints(Integer.parseInt(args[7])); + } + p.sendMessage(Helper.Chatlabel() + "Set " + args[7] + " points for side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + Main.warLogger.log("Set " + args[7] + " points for side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + return finalizeSiege(s); + } else if (s.getWar().getSide2().equals(args[6])) { + if (s.getSide1AreAttackers()) { + s.setDefenderPoints(Integer.parseInt(args[7])); + } else { + s.setAttackerPoints(Integer.parseInt(args[7])); + } + p.sendMessage(Helper.Chatlabel() + "Set " + args[7] + " points for side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + Main.warLogger.log("Set " + args[7] + " points for side " + args[6] + " in sige on " + args[4] + " in war " + args[3]); + return finalizeSiege(s); + } else { + p.sendMessage(Helper.color("&cSide not found")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege score [war] [town] [add/set] [side] [amt]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege score [war] [town] [add/set] [side] [amt]")); + return true; + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cSiege cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege score [war] [town] [add/set] [side] [amt]")); + } + return true; + } else if (args[2].equalsIgnoreCase("townspawn")) { + if (args.length >= 5) { + for (Siege s : SiegeData.getSieges()) { + if (s.getWar().getName().equals(args[3]) && s.getTown().getName().equals(args[4])) { + if (args.length == 6 || args.length == 7) { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege townspawn [war] [town] (x) (y) (Z)")); + return true; + } + Town t = s.getTown(); + if (args.length >= 8) { + if (p.getWorld() == t.getWorld()) { + try { + WorldCoord tb = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) Double.parseDouble(args[5]), (int) Double.parseDouble(args[7])); + if (t.hasTownBlock(tb)) { + t.setHomeBlock(tb.getTownBlock()); + t.setSpawn(new Location(p.getWorld(), (int) Double.parseDouble(args[5]), (int) Double.parseDouble(args[6]), (int) Double.parseDouble(args[7]))); + s.setHomeBlock(tb.getTownBlock()); + s.setTownSpawn(t.getSpawn()); + p.sendMessage(Helper.Chatlabel() + "Set town spawn for sieged town " + args[4] + " in war " + args[3] + " to " + p.getLocation().toString()); + Main.warLogger.log("Set town spawn for sieged town " + args[4] + " in war " + args[3] + " to [" + args[5] + "," + args[6] + "," + args[7] + "]"); + return finalizeSiege(s); + } else { + p.sendMessage(Helper.color("&cTown does not contain town block at [" + args[5] + "," + args[7] + "]")); + return true; + } + } catch (NotRegisteredException e) { + p.sendMessage(Helper.color("&cError! Townblock does not exist!")); + return true; + } catch (TownyException e) { + p.sendMessage(Helper.color("&cError!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cError! Wrong world!")); + return true; + } + } else { + if (p.getWorld() == t.getWorld()) { + try { + WorldCoord tb = WorldCoord.parseWorldCoord(p.getWorld().getName(), (int) p.getLocation().getX(), (int) p.getLocation().getZ()); + if (t.hasTownBlock(tb)) { + t.setHomeBlock(tb.getTownBlock()); + t.setSpawn(p.getLocation()); + s.setHomeBlock(tb.getTownBlock()); + s.setTownSpawn(t.getSpawn()); + p.sendMessage(Helper.Chatlabel() + "Set town spawn for sieged town " + args[4] + " in war " + args[3] + " to " + p.getLocation().toString()); + Main.warLogger.log("Set town spawn for sieged town " + args[4] + " in war " + args[3] + " to " + p.getLocation().toString()); + return finalizeSiege(s); + } else { + p.sendMessage(Helper.color("&cTown does not contain town block at your location [" + (int) p.getLocation().getX() + "," + (int) p.getLocation().getZ() + "]")); + return true; + } + } catch (NotRegisteredException e) { + p.sendMessage(Helper.color("&cError! Townblock does not exist!")); + return true; + } catch (TownyException e) { + p.sendMessage(Helper.color("&cError!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege townspawn [war] [town] (x) (y) (Z)")); + return true; + } + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cSiege cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege townspawn [war] [town] (x) (y) (Z)")); + } + return true; + } else if (args[2].equalsIgnoreCase("time")) { + if (args.length >= 7) { + for (Siege s : SiegeData.getSieges()) { + if (s.getWar().getName().equals(args[3]) && s.getTown().getName().equals(args[4])) { + //parse phase + if (args[5].equalsIgnoreCase("add")) { + s.setSiegeTicks(s.getSiegeTicks() + Integer.parseInt(args[6])); + p.sendMessage(Helper.Chatlabel() + "Set time for siege against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set time for siege against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeSiege(s); + } else if (args[5].equalsIgnoreCase("set")) { + s.setSiegeTicks(Integer.parseInt(args[6])); + p.sendMessage(Helper.Chatlabel() + "Set time for siege against " + args[4] + " in war " + args[3] + " to " + args[6]); + Main.warLogger.log("Set time for siege against " + args[4] + " in war " + args[3] + " to " + args[6]); + return finalizeSiege(s); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify raid time [war] [town] [add/set] [value]")); + return true; + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cSiege cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege time [war] [town] [add/set/max] [value]")); + } + return true; + } else if (args[2].equalsIgnoreCase("owner")) { + if (args.length >= 7) { + for (Siege s : SiegeData.getSieges()) { + if (s.getWar().getName().equals(args[3]) && s.getTown().getName().equals(args[4])) { + Player own = Bukkit.getPlayer(args[5]); + if (own != null) { + if (s.attackerPlayers.contains(own.getName())) { + s.setSiegeOwner(own); + p.sendMessage(Helper.Chatlabel() + "Set owner of siege against " + args[4] + " in war " + args[3] + " to " + own.getName()); + Main.warLogger.log("Set owner of siege against " + args[4] + " in war " + args[3] + " to " + own.getName()); + return finalizeSiege(s); + } else { + p.sendMessage(Helper.color("&cPlayer not on attacking side!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cPlayer not found!")); + return true; + } + } + } + p.sendMessage(Helper.Chatlabel() + Helper.color("&cSiege cannot be found.")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify siege owner [war] [town] [newOwner]")); + } + return true; + } else if (args[2].equalsIgnoreCase("move")) { + //TODO later + p.sendMessage(Helper.color("&cError!")); + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify [raid/siege/war] [propery]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify [raid/siege/war] [propery]")); + return true; + } + } else if (args[1].equalsIgnoreCase("war")) { + if (args.length >= 3) { + if (args[2].equalsIgnoreCase("score")) { + if (args.length >= 6) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[3])) { + //TODO war score + p.sendMessage(Helper.color("&cError! Feature unimplemented!")); + return true; + } + } + p.sendMessage(Helper.color("&cError: War not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war score [war] [side] [add/set] [amt]")); + } + return true; + } else if (args[2].equalsIgnoreCase("side")) { + if (args.length >= 6) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[3])) { + if (w.getSide1().equals(args[4])) { + w.setSide1(args[5]); + w.save(); + p.sendMessage(Helper.Chatlabel() + "Set side " + args[4] + " to " + args[5] + " in war " + args[3]); + Main.warLogger.log(Helper.Chatlabel() + "Set side " + args[4] + " to " + args[5] + " in war " + args[3]); + return finalizeWar(w); + } else if (w.getSide2().equals(args[4])) { + w.setSide2(args[5]); + w.save(); + p.sendMessage(Helper.Chatlabel() + "Set side " + args[4] + " to " + args[5] + " in war " + args[3]); + Main.warLogger.log(Helper.Chatlabel() + "Set side " + args[4] + " to " + args[5] + " in war " + args[3]); + return finalizeWar(w); + } else { + p.sendMessage(Helper.color("&cError: Side not found!")); + return true; + } + } + } + p.sendMessage(Helper.color("&cError: War not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war side [war] [side] [name]")); + } + return true; + } else if (args[2].equalsIgnoreCase("name")) { + if (args.length >= 5) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[3])) { + w.setName(args[4]); + p.sendMessage(Helper.Chatlabel() + "Set name of war " + args[3] + " to " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Set name of war " + args[3] + " to " + args[4]); + return finalizeWar(w); + } + } + p.sendMessage(Helper.color("&cError: War not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war name [war] [name]")); + } + return true; + } + //TODO If a town is surrendered, use last arg as flag to force unsurrender + else if (args[2].equalsIgnoreCase("add")) { + if (args.length >= 7) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[3])) { + if (!w.getSide1().equals(args[4]) && !w.getSide2().equals(args[4])) { + p.sendMessage(Helper.color("&cError: Side not found!")); + return true; + } + if (args[5].equalsIgnoreCase("town")) { + Town t = TownyAPI.getInstance().getTown(args[6]); + if (t != null) { + if(args.length >= 8) { + //forceful add, ignore surrender and override it if the town is + if(Boolean.parseBoolean(args[7])) { + //if we already have surrendered + if(w.getSurrenderedTowns().contains(args[6])) { + w.unSurrenderTown(args[6]); + p.sendMessage(Helper.Chatlabel() + "Un-surrendered town " + args[6]); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered town " + args[6]); + } + if(w.getSide1().equals(args[4])) { + if(w.getSide2Towns().remove(args[6])) { + p.sendMessage(Helper.Chatlabel() + "Removed town " + args[6] + " from side " + w.getSide2()); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered town " + args[6]); + } + } else if(w.getSide2().equals(args[4])) { + if(w.getSide1Towns().remove(args[6])) { + p.sendMessage(Helper.Chatlabel() + "Removed town " + args[6] + " from side " + w.getSide2()); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered town " + args[6]); + } + } + } + } + // Side checks + int side = w.getSide(t.getName().toLowerCase()); + if (side == -1) { + p.sendMessage(Helper.Chatlabel() + "Town already surrendered!"); + return true; + } else if (side > 0) { + p.sendMessage(Helper.Chatlabel() + "Town already in this war!"); + return true; + } + + w.addTown(t, args[4]); + p.sendMessage(Helper.Chatlabel() + "Forcefully added town " + args[6] + " war " + args[3] + " on side " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Forcefully added town " + args[6] + " war " + args[3] + " on side " + args[4]); + return true; + } else { + p.sendMessage(Helper.color("&cError: Town not found!")); + } + return finalizeWar(w); + } else if (args[5].equalsIgnoreCase("nation")) { + Nation n = TownyAPI.getInstance().getNation(args[6]); + if (n != null) { + if(args.length >= 8) { + //forceful add, ignore surrender and override it if the town is + if(Boolean.parseBoolean(args[7])) { + //if we already have surrendered + for(Town t : n.getTowns()) { + if(w.getSurrenderedTowns().contains(t.getName())) { + w.unSurrenderTown(t.getName()); + p.sendMessage(Helper.Chatlabel() + "Unsurrendered town " + t.getName()); + Main.warLogger.log(Helper.Chatlabel() + "Unsurrendered town " + t.getName()); + } + if(w.getSide1().equals(args[4])) { + if(w.getSide2Towns().remove(t.getName())) { + p.sendMessage(Helper.Chatlabel() + "Removed town " + t.getName() + " from side " + w.getSide2()); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered town " + t.getName()); + } + } else if(w.getSide2().equals(args[4])) { + if(w.getSide1Towns().remove(t.getName())) { + p.sendMessage(Helper.Chatlabel() + "Removed town " + t.getName() + " from side " + w.getSide2()); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered town " + t.getName()); + } + } + } + } + } + + for(Town t : n.getTowns()) { + // Side checks + int side = w.getSide(t.getName().toLowerCase()); + if (side == -1) { + p.sendMessage(Helper.Chatlabel() + "Town " + t.getName() + " already surrendered!"); + continue; + } else if (side > 0) { + p.sendMessage(Helper.Chatlabel() + "Town " + t.getName() + " already in this war!"); + continue; + } + + w.addTown(t, args[4]); + } + p.sendMessage(Helper.Chatlabel() + "Forcefully added nation " + args[6] + " war " + args[3] + " on side " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Forcefully added nation " + args[6] + " war " + args[3] + " on side " + args[4]); + return finalizeWar(w); + } else { + p.sendMessage(Helper.color("&cError: Nation not found!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war add [war] [side] town/nation [town/nation] [force]")); + return true; + } + } + } + p.sendMessage(Helper.color("&cError: War not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war add [war] [side] town/nation [town/nation] [force] ")); + } + return true; + } + //TODO do + else if (args[2].equalsIgnoreCase("unsurrender")) { + if (args.length >= 6) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[3])) { + if (args[4].equalsIgnoreCase("town")) { + Town t = TownyAPI.getInstance().getTown(args[5]); + if (t != null) { + if(w.getSurrenderedTowns().contains(args[5])) { + w.unSurrenderTown(args[5]); + p.sendMessage(Helper.Chatlabel() + "Un-surrendered town " + args[5]); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered town " + args[5]); + p.sendMessage(Helper.color("&cTo re-add, use: /alathrawaradmin modify war add, or have the town loader join.")); + return true; + } + p.sendMessage(Helper.Chatlabel() + "Town " + args[5] + " not already surrendered, ignoring."); + Main.warLogger.log(Helper.Chatlabel() + "Town " + args[5] + " not already surrendered, ignoring."); + return true; + } else { + p.sendMessage(Helper.color("&cError: Town not found!")); + } + return finalizeWar(w); + } else if (args[4].equalsIgnoreCase("nation")) { + Nation n = TownyAPI.getInstance().getNation(args[5]); + if (n != null) { + for(Town t : n.getTowns()) { + if (w.getSurrenderedTowns().contains(t.getName())) { + w.unSurrenderTown(t.getName()); + p.sendMessage(Helper.Chatlabel() + "Un-surrendered town " + t.getName()); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered town " + t.getName()); + } + } + + p.sendMessage(Helper.Chatlabel() + "Un-surrendered nation " + args[5] + " in war " + args[3]); + Main.warLogger.log(Helper.Chatlabel() + "Un-surrendered nation " + args[5] + " in war " + args[3]); + p.sendMessage(Helper.color("&cTo re-add, use: /alathrawaradmin modify war add, or have the nation loader join.")); + return finalizeWar(w); + } else { + p.sendMessage(Helper.color("&cError: Nation not found!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war unsurrender [war] town/nation [town/nation]")); + p.sendMessage(Helper.color("&cTo re-add, use: /alathrawaradmin modify war add, or have the town/nation loader join.")); + return true; + } + } + } + p.sendMessage(Helper.color("&cError: War not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war unsurrender [war] town/nation [town/nation]")); + } + return true; + } + //TODO IDK IF THIS WORKS + else if (args[2].equalsIgnoreCase("surrender")) { + if (args.length >= 7) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[3])) { + if (!w.getSide1().equals(args[4]) && !w.getSide2().equals(args[4])) { + p.sendMessage(Helper.color("&cError: Side not found!")); + return true; + } + if (args[5].equalsIgnoreCase("town")) { + Town t = TownyAPI.getInstance().getTown(args[6]); + if (t != null) { + if(w.getSide1Towns().contains(t.getName()) && w.getSide1().equals(args[4])) { + w.surrenderTown(t.getName()); + p.sendMessage(Helper.Chatlabel() + "Forcefully surrendered town " + args[6] + " war " + args[3] + " on side " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Forcefully surrendered town " + args[6] + " war " + args[3] + " on side " + args[4]); + return finalizeWar(w); + } else if(w.getSide2Towns().contains(t.getName()) && w.getSide2().equals(args[4])) { + w.surrenderTown(t.getName()); + p.sendMessage(Helper.Chatlabel() + "Forcefully surrendered town " + args[6] + " war " + args[3] + " on side " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Forcefully surrendered town " + args[6] + " war " + args[3] + " on side " + args[4]); + return finalizeWar(w); + } else { + if (w.getSide1().equals(args[4]) || w.getSide2().equals(args[4])) { + p.sendMessage(Helper.Chatlabel() + "Town " + args[5] + " is not on either side of the war!"); + Main.warLogger.log(Helper.Chatlabel() + "Town " + args[5] + " is not on either side of the war!"); + return true; + } + p.sendMessage(Helper.Chatlabel() + "Town " + args[5] + " is on a side but side not correct! ERROR!"); + Main.warLogger.log(Helper.Chatlabel() + "Town " + args[5] + " is on a side but side not correct! ERROR!"); + return true; + } + } else { + p.sendMessage(Helper.color("&cError: Town not found!")); + return true; + } + } else if (args[5].equalsIgnoreCase("nation")) { + Nation n = TownyAPI.getInstance().getNation(args[6]); + if (n != null) { + for (Town t : n.getTowns()) { + if(w.getSide1Towns().contains(t.getName()) && w.getSide1().equals(args[4])) { + w.surrenderTown(t.getName()); + p.sendMessage(Helper.Chatlabel() + "Forcefully surrendered town " + t.getName() + " war " + args[3] + " on side " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Forcefully surrendered town " + t.getName() + " war " + args[3] + " on side " + args[4]); + + } else if(w.getSide2Towns().contains(t.getName()) && w.getSide2().equals(args[4])) { + w.surrenderTown(t.getName()); + p.sendMessage(Helper.Chatlabel() + "Forcefully surrendered town " + t.getName() + " war " + args[3] + " on side " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Forcefully surrendered town " + t.getName() + " war " + args[3] + " on side " + args[4]); + + } else { + if (w.getSide1().equals(args[4]) || w.getSide2().equals(args[4])) { + p.sendMessage(Helper.Chatlabel() + "Town " + t.getName() + " is not on either side of the war!"); + Main.warLogger.log(Helper.Chatlabel() + "Town " + t.getName() + " is not on either side of the war!"); + return true; + } + p.sendMessage(Helper.Chatlabel() + "Town " + t.getName() + " is on a side but side not correct! ERROR!"); + Main.warLogger.log(Helper.Chatlabel() + "Town " + t.getName() + " is on a side but side not correct! ERROR!"); + return true; + } + } + p.sendMessage(Helper.Chatlabel() + "Surrendered nation " + args[6] + " war " + args[3] + " on side " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Surrendered nation " + args[6] + " war " + args[3] + " on side " + args[4]); + return finalizeWar(w); + } else { + p.sendMessage(Helper.color("&cError: Nation not found!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war surrender [war] [side] town/nation [town/nation]")); + return true; + } + } + } + p.sendMessage(Helper.color("&cError: War not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war surrender town [war] [town]")); + } + return true; + } else if (args[2].equalsIgnoreCase("raidTimeWar")) { + if (args.length >= 5) { + for (War w : WarData.getWars()) { + if (w.getName().equals(args[4])) { + if (args[3].equalsIgnoreCase("add")) { + if (args.length >= 7) { + if (args[6].equals(w.getSide1())) { + w.setLastRaidTimeSide1(w.getLastRaidTimeSide1() + Integer.parseInt(args[5])); + p.sendMessage(Helper.Chatlabel() + "Added " + args[6] + " to last raid time in war " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Added " + args[6] + " to last raid time in war " + args[4]); + return finalizeWar(w); + } else if (args[6].equals(w.getSide2())) { + w.setLastRaidTimeSide2(w.getLastRaidTimeSide2() + Integer.parseInt(args[5])); + p.sendMessage(Helper.Chatlabel() + "Added " + args[6] + " to last raid time in war " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Added " + args[6] + " to last raid time in war " + args[4]); + return finalizeWar(w); + } else { + p.sendMessage(Helper.color("&cSide not found!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeWar [add,set,reset] [town/war] [amt] [side]")); + return true; + } + } else if (args[3].equalsIgnoreCase("set")) { + if (args.length >= 7) { + if (args[6].equals(w.getSide1())) { + w.setLastRaidTimeSide1(Integer.parseInt(args[5])); + p.sendMessage(Helper.Chatlabel() + "Set last raid time in war " + args[4] + " to " + args[6]); + Main.warLogger.log(Helper.Chatlabel() + "Set last raid time in war " + args[4] + " to " + args[6]); + return finalizeWar(w); + } else if (args[6].equals(w.getSide2())) { + w.setLastRaidTimeSide2(Integer.parseInt(args[5])); + p.sendMessage(Helper.Chatlabel() + "Set last raid time in war " + args[4] + " to " + args[6]); + Main.warLogger.log(Helper.Chatlabel() + "Set last raid time in war " + args[4] + " to " + args[6]); + return finalizeWar(w); + } else { + p.sendMessage(Helper.color("&cSide not found!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeWar [add,set,reset] [town/war] [amt] [side]")); + return true; + } + } else if (args[3].equalsIgnoreCase("reset")) { + + if (args.length >= 6) { + if (args[5].equals(w.getSide1())) { + w.setLastRaidTimeSide1(0); + p.sendMessage(Helper.Chatlabel() + "Reset last raid time in war " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Reset last raid time in war " + args[4]); + return finalizeWar(w); + } else if (args[5].equals(w.getSide2())) { + w.setLastRaidTimeSide2(0); + p.sendMessage(Helper.Chatlabel() + "Reset last raid time in war " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Reset last raid time in war " + args[4]); + return finalizeWar(w); + } else { + p.sendMessage(Helper.color("&cSide not found!")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeWar [add,set,reset] [town/war] [amt] [side]")); + return true; + } + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeWar [add,set,reset] [town/war] [amt] [side]")); + return true; + } + } + } + p.sendMessage(Helper.color("&cError: War not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeWar [add,set,reset] [war] [amt] [side]")); + } + return true; + } else if (args[2].equalsIgnoreCase("raidTimeTown")) { + if (args.length >= 5) { + for (Town t : TownyAPI.getInstance().getTowns()) { + if (t.getName().equals(args[4])) { + if (args[3].equalsIgnoreCase("add")) { + if (args.length >= 6) { + LongDataField last = (LongDataField) t.getMetadata("lastRaided"); + if (last != null) { + t.addMetaData(new LongDataField("lastRaided", last.getValue() + Long.parseLong(args[6]))); + } else { + t.addMetaData(new LongDataField("lastRaided", Long.parseLong(args[6]))); + } + p.sendMessage(Helper.Chatlabel() + "Added " + args[6] + " to last raid time in war " + args[3]); + Main.warLogger.log(Helper.Chatlabel() + "Added " + args[6] + " to last raid time in war " + args[3]); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeTown [add,set,reset] [town/war] [amt] [side]")); + } + return true; + } else if (args[3].equalsIgnoreCase("set")) { + if (args.length >= 6) { + t.addMetaData(new LongDataField("lastRaided", Long.parseLong(args[6]))); + p.sendMessage(Helper.Chatlabel() + "Set last raid time in war " + args[4] + " to " + args[6]); + Main.warLogger.log(Helper.Chatlabel() + "Set last raid time in war " + args[4] + " to " + args[6]); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeTown [add,set,reset] [town/war] [amt] [side]")); + } + return true; + } else if (args[3].equalsIgnoreCase("reset")) { + t.addMetaData(new LongDataField("lastRaided", 0L)); + p.sendMessage(Helper.Chatlabel() + "Reset last raid time in town " + args[4]); + Main.warLogger.log(Helper.Chatlabel() + "Reset last raid time in war " + args[4]); + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify war raidTimeTown [add,set,reset] [town] [amt]")); + return true; + } + } + } + p.sendMessage(Helper.color("&cError: Town not found!")); + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify [raid/siege/war] [propery]")); + } + return true; + } else { + p.sendMessage(Helper.color("&cUsage: /alathrawaradmin modify [raid/siege/war] [propery]")); + return true; + } + } else { + return fail(p, args, "syntax"); + } + } else { + return fail(p, args, "syntax"); + } + } + return true; + } + + + private static boolean rule(Player p, String[] args) { + return true; + } + + + private static boolean awa(Player p, String[] args) { + p.chat("awa awa! ^.^ UwU"); + return true; + } + + private static boolean fail(Player p, String[] args, String type) { + switch (type) { + case "permissions" -> { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou do not have permission to do this.")); + return true; + } + case "syntax" -> { + p.sendMessage(Helper.Chatlabel() + "Invalid Arguments. /alathrawaradmin help"); + return true; + } + default -> { + p.sendMessage(Helper.Chatlabel() + "Something wrong. /alathrawaradmin help"); + return true; + } + } + } + + private static boolean finalizeRaid(Raid r) { + r.save(); + return true; + } + + private static boolean finalizeSiege(Siege s) { + s.save(); + return true; + } + + private static boolean finalizeWar(War w) { + w.save(); + return true; + } +} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/AdminTabCompletion.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/AdminTabCompletion.java new file mode 100644 index 0000000..4f91747 --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/AdminTabCompletion.java @@ -0,0 +1,724 @@ +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 org.bukkit.entity.Player; + +import java.util.List; + +public class AdminTabCompletion implements TabCompleter { + + List base = List.of(new String[]{ + "create", + "force", + "help", + "info", + "modify" + }); + + List type = List.of(new String[]{ + "raid", + "siege", + "war" + }); + + List raidModify = List.of(new String[]{ + "score", + "townspawn", + "gather", + "phase", + "loot", + "time", + "owner" +// , +// "move" +// , +// "clearActive" + }); + + List siegeModify = List.of(new String[]{ + "score", + "townspawn", + "time", + "owner" +// , +// "move" + }); + + List warModify = List.of(new String[]{ + "score", + "side", + "name", + "add", + "surrender", + "unsurrender", + "raidTimeWar", + "raidTimeTown" + }); + + List force = List.of(new String[]{ + "end", + "join", + "leave" + }); + + List addSet = List.of(new String[]{ + "add", + "set" + }); + + + List boolSet = List.of(new String[]{ + "true", + "false" + }); + + + List addSetReset = List.of(new String[]{ + "add", + "set", + "reset" + }); + + List nationTown = List.of(new String[]{ + "town", + "nation" + }); + + List lootSet = List.of(new String[]{ + "value", + "looted", + "ticks", + "reset" + }); + + List empty = List.of(new String[]{ + "" + }); + + + /** + * //done + * //edit war/event in real time, side can be "both" to effect both + * -modify raid score [war] [town] [add/set] [value] + * -modify raid townspawn [war] [town] (x) (y) (Z) + * -modify raid gather [war] [town] [town] + * -modify raid phase [war] [town] [phase] //"next" to move to next phase + * -modify raid loot [war] [town] [value,looted,ticks,reset] [amt] (x) (z) //no coords just does current chunk, reset deletes it from the list + * -modify raid time [war] [town] [add/set] [value] + * -modify raid owner [war] [town] [player] + * -modify raid move [war] [town] [newWar] //low priority, moves raid to other war/ town + * -modify raid clearActive [war] [town] //low priority + *

+ * -modify siege score [war] [town] [side] [amt] + * -modify siege townspawn [war] [town] (x) (Z) + * -modify siege time [war] [town] [add/set/max] [value] //max modified the max length + * -modify siege owner [war] [town] [add/set] [value] + * -modify siege move [war] [town] [newWar] //low priority, moves siege to other war/town + *

+ * -modify war score [war] [side] [amt] + * -modify war side [war] [side] [name] + * -modify war name [war] [name] + * -modify war add town [war] [town] + * -modify war add nation [war] [nation] + * -modify war surrender town [war] [town] //adds town to surrender list + * -modify war surrender nation [war] [town] //adds all towns to surrender list + * -modify war raidTime [add,set,reset] [war] [town] [amt] //set when last raid was + *

+ * // low priority idea + * -info war score [war] + * -info war surrenderedTowns [war] + * -info war raids [war] + * -info war sieges [war] + * -info war towns [war] + * -info war lastRaidTime [war] + *

+ * -info raid homeblock [war] [town] + * -info raid lootedTownBlocks [war] [town] + * -info raid timeSinceLastRaid [town] + *

+ * -info siege homeblock [war] [town] + *

+ * //done + * //force make event or war, if owner isn't defined, IDK haven't decided + * -create siege [war] [town] (owner) + * -create raid [war] [raidTown] (gatherTown) (owner) + * -create war [name] [side1] [side2] + *

+ * //force end a war/event, can declare winner side, or no winner + * -force end war [war] (side/victor) + * -force end siege [war] [town] (side/victor) + * -force end raid [war] [town] (side/victor) + *

+ * //done + * //force player into or out of a war/event + * -force join war [player] [war] [side] + * -force join siege [player] [war] [town] (side) + * -force join raid [player] [war] [town] (side) + *

+ * //done + * -force leave raid [war] [town] [player] (timeout) //kicks from raid party + * + * @param sender Source of the command. For players tab-completing a + * command inside a command block, this will be the player, not + * the command block. + * @param command Command which was executed + * @param label Alias of the command which was used + * @param args The arguments passed to the command, including final + * partial argument to be completed + * @return list of options + */ + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + final Player p = (Player) sender; + if (!p.hasPermission("AlathraWar.admin")) { + return empty; + } + + if (args.length == 0) { + return base; + } else if (args.length == 1) { + return NameUtil.filterByStart(base, args[0]); + } else { + if (args[0].equalsIgnoreCase("create")) { + if (args.length > 2) { + switch (args[1]) { + case "raid" -> { + if (args.length > 3) { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[3]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[2]); + } + } + case "siege" -> { + if (args.length > 3) { + if (args.length > 4) { + if (args.length > 5) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[3]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[2]); + } + } + default -> { + return empty; + } + } + } else { + return NameUtil.filterByStart(type, args[1]); + } + } else if (args[0].equalsIgnoreCase("force")) { + if (args.length > 2) { + if (args[1].equalsIgnoreCase("end")) { + if (args.length > 3) { + if (args[2].equalsIgnoreCase("raid")) { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[3]), args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } else if (args[2].equalsIgnoreCase("siege")) { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[3]), args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } else if (args[2].equalsIgnoreCase("war")) { + if (args.length > 4) { + if (args.length > 5) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[3]), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } + } else { + return NameUtil.filterByStart(type, args[2]); + } + } else if (args[1].equalsIgnoreCase("join")) { + if (args.length > 3) { + if (args[2].equalsIgnoreCase("raid")) { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + if (args.length > 7) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[4]), args[6]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[3]); + } + } else if (args[2].equalsIgnoreCase("siege")) { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + if (args.length > 7) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[4]), args[6]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[3]); + } + } else if (args[2].equalsIgnoreCase("war")) { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[4]), args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[3]); + } + } + } else { + return NameUtil.filterByStart(type, args[2]); + } + } else if (args[1].equalsIgnoreCase("leave")) { + if (args.length > 3) { + if (args[2].equalsIgnoreCase("raid")) { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } else { + return NameUtil.filterByStart(List.of(new String[]{"raid"}), args[3]); + } + } else { + return NameUtil.filterByStart(List.of(new String[]{"raid"}), args[2]); + } + } else { + return NameUtil.filterByStart(force, args[1]); + } + } else { + return NameUtil.filterByStart(force, args[1]); + } + } else if (args[0].equalsIgnoreCase("help")) { + return empty; + } else if (args[0].equalsIgnoreCase("info")) { + if (args.length > 2) { + if (args[1].equalsIgnoreCase("raid")) { + if (args.length > 3) { + if (args.length > 4) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[3]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[2]); + } + } else if (args[1].equalsIgnoreCase("siege")) { + if (args.length > 3) { + if (args.length > 4) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[3]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[2]); + } + } else if (args[1].equalsIgnoreCase("war")) { + if (args.length > 3) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[2]); + } + } else { + return NameUtil.filterByStart(type, args[1]); + } + } else { + return NameUtil.filterByStart(type, args[1]); + } + } else if (args[0].equalsIgnoreCase("modify")) { + if (args.length > 2) { + if (args[1].equalsIgnoreCase("raid")) { + if (args.length > 3) { + if (args.length > 4) { + if (args.length > 5) { + switch (args[2]) { + /* + * -modify raid score [war] [town] [add/set] [value] + * -modify raid townspawn [war] [town] (x) (y) (Z) + * -modify raid gather [war] [town] [town] + * -modify raid phase [war] [town] [phase] //"next" to move to next phase + * -modify raid loot [war] [town] [value,looted,ticks,reset] [amt] (x) (z) //no coords just does current chunk, reset deletes it from the list + * -modify raid time [war] [town] [add/set] [value] + * -modify raid owner [war] [town] [player] + * -modify raid move [war] [town] [newWar] //low priority, moves raid to other war/ town + * -modify raid clearActive [war] [town] //low priority + */ + case "score", "time" -> { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(addSet, args[5]); + } + } + case "townspawn" -> { + if (args.length > 6) { + if (args.length > 7) { + if (args.length > 8) { + return empty; + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getZ())}); + } + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getY())}); + } + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getX())}); + } + } + case "gather" -> { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[5]); + } + } + case "phase" -> { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getRaidPhases(), args[5]); + } + } + case "loot" -> { + if (args.length > 6) { + if (args.length > 7) { + if (args.length > 8) { + if (args.length > 9) { + return empty; + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getZ())}); + } + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getX())}); + } + } else { + return empty; + } + } else { + return NameUtil.filterByStart(lootSet, args[5]); + } + } + case "owner" -> { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[5]); + } + } + default -> { + return empty; + } + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } else { + return NameUtil.filterByStart(raidModify, args[2]); + } + } else if (args[1].equalsIgnoreCase("siege")) { + if (args.length > 3) { + if (args.length > 4) { + if (args.length > 5) { + switch (args[2]) { + case "score", "time" -> { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(addSet, args[5]); + } + } + case "townspawn" -> { + if (args.length > 6) { + if (args.length > 7) { + if (args.length > 8) { + return empty; + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getZ())}); + } + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getY())}); + } + } else { + return List.of(new String[]{String.valueOf(p.getLocation().getX())}); + } + } + case "owner" -> { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getPlayers(), args[5]); + } + } + default -> { + return empty; + } + } + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } else { + return NameUtil.filterByStart(siegeModify, args[2]); + } + } else if (args[1].equalsIgnoreCase("war")) { + if (args.length > 3) { + switch (args[2]) { + /* + * -modify war score [war] [side] [amt] + * -modify war side [war] [side] [name] + * -modify war name [war] [name] + * -modify war add [war] [side] [town/nation] [town/nation] + * -modify war add nation [war] [nation] + * -modify war surrender town [war] [town] //adds town to surrender list + * -modify war surrender nation [war] [town] //adds all towns to surrender list + * -modify war unsurrender [war] town/nation [town/nation] + * -modify war unsurrender [war] town/nation [town/nation] + * -modify war raidTimeTown [add,set,reset] [war] [town] [amt] //set when last raid was + */ + case "score" -> { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + if (args.length > 7) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[5]); + } + } else { + return NameUtil.filterByStart(addSet, args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } + //alathrawaradmin modify war raidTimeTown [add,set,reset] [town/war] [amt] [side] + case "raidTimeTown" -> { + if (args.length > 4) { + if (args.length > 5) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[4]); + } + } else { + return NameUtil.filterByStart(addSetReset, args[3]); + } + } + //alathrawaradmin modify war raidTimeWar [add,set,reset] [war] [amt] [side] + case "raidTimeWar" -> { + if (args.length > 4) { + if (args.length > 5) { + if(args[3].equalsIgnoreCase("reset")) { + if (args.length > 6) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[4]), args[5]); + } + } else { + if (args.length > 6) { + if (args.length > 7) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[4]), args[6]); + } + } else { + return empty; + } + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[4]); + } + } else { + return NameUtil.filterByStart(addSetReset, args[3]); + + } + } + case "side" -> { + + if (args.length > 4) { + if (args.length > 5) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[3]), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } + case "name" -> { + if (args.length > 4) { + return empty; + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } + case "add" -> { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + if (args.length > 7) { + if (args.length > 8) { + return empty; + } else { + return boolSet; + } + } else { + if (args[5].equalsIgnoreCase("town")) { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[6]); + } else if (args[5].equalsIgnoreCase("nation")) { + return NameUtil.filterByStart(CommandHelper.getTownyNations(), args[6]); + } else { + return empty; + } + } + } else { + return NameUtil.filterByStart(nationTown, args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[3]), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } + case "surrender" -> { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + if (args.length > 7) { + return empty; + } else { + if (args[5].equalsIgnoreCase("town")) { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[6]); + } else if (args[5].equalsIgnoreCase("nation")) { + return NameUtil.filterByStart(CommandHelper.getTownyNations(), args[6]); + } else { + return empty; + } + } + } else { + return NameUtil.filterByStart(nationTown, args[5]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarSides(args[3]), args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } + case "unsurrender" -> { + if (args.length > 4) { + if (args.length > 5) { + if (args.length > 6) { + return empty; + } else { + if (args[4].equalsIgnoreCase("town")) { + return NameUtil.filterByStart(CommandHelper.getTownyTowns(), args[5]); + } else if (args[4].equalsIgnoreCase("nation")) { + return NameUtil.filterByStart(CommandHelper.getTownyNations(), args[5]); + } else { + return empty; + } + } + } else { + return NameUtil.filterByStart(nationTown, args[4]); + } + } else { + return NameUtil.filterByStart(CommandHelper.getWarNames(), args[3]); + } + } + default -> { + return empty; + } + } + + } else { + return NameUtil.filterByStart(warModify, args[2]); + } + } else { + return NameUtil.filterByStart(type, args[1]); + } + } else { + return NameUtil.filterByStart(type, args[1]); + } + } else { + return NameUtil.filterByStart(base, args[0]); + } + } + return NameUtil.filterByStart(base, args[0]); + } +} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/CommandHelper.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/CommandHelper.java new file mode 100644 index 0000000..f62e9ed --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/CommandHelper.java @@ -0,0 +1,103 @@ +package me.ShermansWorld.AlathraWar.commands; + +import com.palmergames.bukkit.towny.TownyAPI; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Town; +import me.ShermansWorld.AlathraWar.War; +import me.ShermansWorld.AlathraWar.data.RaidPhase; +import me.ShermansWorld.AlathraWar.data.WarData; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class CommandHelper { + + /** + * Grabs list of all war names + * @return + */ + public static List getWarNames() { + List out = new ArrayList<>(); + for (War w : WarData.getWars()) { + out.add(w.getName()); + } + return out; + } + + /** + * Grabs first matching war and lists the sides + * @param name + * @return + */ + public static List getWarSides(String name) { + List out = new ArrayList<>(); + for (War w : WarData.getWars()) { + if(w.getName().equals(name)) { + out.add(w.getSide1()); + out.add(w.getSide2()); + break; + } + } + return out; + } + + /** + * get list of all towny towns + * + * @return + */ + public static List getTownyTowns() { + List out = new ArrayList<>(); + for (Town t : TownyAPI.getInstance().getTowns()) { + out.add(t.getName()); + } + return out; + } + + /** + * get list of all towny nations + * + * @return + */ + public static List getTownyNations() { + List out = new ArrayList<>(); + for (Town t : TownyAPI.getInstance().getTowns()) { + Nation n = t.getNationOrNull(); + if(n != null) { + if(!out.contains(n.getName())) { + out.add(n.getName()); + } + } + } + return out; + } + + /** + * get a list of all online players + * @return + */ + public static List getPlayers() { + Collection players = Bukkit.getOnlinePlayers(); + List out = new ArrayList<>(); + for (Player p : players) { + out.add(p.getName()); + } + return out; + } + + /** + * get a list of all raid phases + * @return + */ + public static List getRaidPhases() { + List out = new ArrayList<>(); + for (RaidPhase ph : RaidPhase.values()) { + out.add(ph.name()); + } + return out; + } +} diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java index e50aa4b..f34ec15 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java @@ -4,7 +4,6 @@ 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.*; -import com.palmergames.bukkit.towny.object.metadata.LongDataField; import me.ShermansWorld.AlathraWar.*; import me.ShermansWorld.AlathraWar.data.RaidPhase; import me.ShermansWorld.AlathraWar.data.WarData; @@ -44,11 +43,11 @@ public class RaidCommands implements CommandExecutor { } } else if (args.length == 3) { if (args[0].equalsIgnoreCase("start")) { - startRaid(p, args); + startRaid(p, args, false); } else if (args[0].equalsIgnoreCase("join")) { - joinRaid(p, args); + joinRaid(p, args, false); } else if (args[0].equalsIgnoreCase("leave")) { - leaveRaid(p, args); + leaveRaid(p, args, false); } else if (args[0].equalsIgnoreCase("abandon")) { abandonRaid(p, args); } else { @@ -57,8 +56,7 @@ public class RaidCommands implements CommandExecutor { } else { fail(p, args, "syntax"); } - return false; - + return true; } /** @@ -67,29 +65,61 @@ public class RaidCommands implements CommandExecutor { * @param p * @param args */ - private static void startRaid(Player p, String[] args) { + protected static void startRaid(Player p, String[] args, boolean admin) { boolean warFound = false; boolean townExists = false; for (final War war : WarData.getWars()) { - if (war.getName().equalsIgnoreCase(args[1])) { + //if this is run as admin, shift our check forward a slot + if (war.getName().equalsIgnoreCase(args[1 + (admin ? 1 : 0)])) { warFound = true; - if (!war.getSide1Players().contains(p.getName()) && !war.getSide2Players().contains(p.getName())) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); - } + + Player raidOwner = p; TownyWorld townyWorld; - townyWorld = WorldCoord.parseWorldCoord(p.getLocation()).getTownyWorld(); + townyWorld = WorldCoord.parseWorldCoord(raidOwner.getLocation()).getTownyWorld(); + if(admin && args.length >= 6) { + raidOwner = Bukkit.getPlayer(args[5]); + if(raidOwner == null) { + raidOwner = p; + } + } + + String side = ""; + if (war.getSide1Players().contains(p.getName())) { + side = war.getSide1(); + } else if (war.getSide2Players().contains(p.getName())) { + side = war.getSide2(); + } else { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); + return; + } //check if in a town //takes the command runners town as the gather point Town gatherTown = null; try { - gatherTown = TownyAPI.getInstance().getTownOrNull(WorldCoord.parseWorldCoord(p.getLocation()).getTownBlock()); + //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(String.valueOf(Helper.Chatlabel()) + "No valid town set for gather town, defaulting to current location"); + raidOwner.sendMessage(String.valueOf(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) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); + if (admin) p.sendMessage(String.valueOf(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(String.valueOf(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) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must be within a town's claim to begin a raid. That town you are in will be the gathering town."); + if (admin) p.sendMessage(String.valueOf(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(String.valueOf(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; } @@ -97,26 +127,40 @@ public class RaidCommands implements CommandExecutor { for (final String entry : townyWorld.getTowns().keySet()) { final Town raidedTown = townyWorld.getTowns().get(entry); - if (raidedTown.getName().equalsIgnoreCase(args[2]) && gatherTown != null) { + //if this is run as admin, shift our check forward a slot + if (raidedTown.getName().equalsIgnoreCase(args[2 + (admin ? 1 : 0)]) && gatherTown != null) { + + //check if the town were attempting to raid is in the war + if(!war.getSide1Towns().contains(raidedTown.getName()) && !war.getSide2Towns().contains(raidedTown.getName())) { + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "The town you are trying to raid is not part of the war!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "The town you are trying to raid is not part of the war!"); + return; + } + townExists = true; - boolean attackingOwnSide = false; + Raid raid2; if (war.getSide1Players().contains(p.getName())) { //Time and raid activity validity check - int c = RaidData.isValidRaid(war, raidedTown); + int c = RaidData.isValidRaid(war, side, raidedTown); if (c == 2) { - raid2 = new Raid(war, raidedTown, gatherTown, true); + //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) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); return; } else if (c == 0) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Your side has raided too recently!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Your side has raided too recently!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "Your side has raided too recently!"); return; } else if (c == -1) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town was raided too recently!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town was raided too recently!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town was raided too recently!"); return; } else if (c == -2) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town has no online players to defend it!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "At least on member of the raided town must be online to defend it!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "At least on member of the raided town must be online to defend it!"); return; } else { throw new IllegalArgumentException(); @@ -128,59 +172,61 @@ public class RaidCommands implements CommandExecutor { return; } //Time and raid activity validity check - int c = RaidData.isValidRaid(war, raidedTown); + int c = RaidData.isValidRaid(war, side, raidedTown); if (c == 2) { - raid2 = new Raid(war, raidedTown, gatherTown, false); + raid2 = new Raid(war, raidedTown, gatherTown, false, raidOwner); } else if (c == 1) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); return; } else if (c == 0) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Your side has raided too recently!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Your side has raided too recently!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "Your side has raided too recently!"); return; } else if (c == -1) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town was raided too recently!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town was raided too recently!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town was raided too recently!"); return; } else if (c == -2) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "At least on member of the raided town must be online to defend it!"); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "At least on member of the raided town must be online to defend it!"); + raidOwner.sendMessage(String.valueOf(Helper.Chatlabel()) + "At least on member of the raided town must be online to defend it!"); return; } else { throw new IllegalArgumentException(); } } - //validity check over attacking own towns - final ArrayList residentNames = new ArrayList(); - for (final Resident resident : raidedTown.getResidents()) { - residentNames.add(resident.getName()); + // Player participance check + Town leaderTown = TownyAPI.getInstance().getResident(raidOwner).getTownOrNull(); + int sideR = war.getSide(leaderTown.getName()); + 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; } - if (raid2.getSide1AreRaiders()) { - for (final String playerName : war.getSide1Players()) { - if (residentNames.contains(playerName)) { - attackingOwnSide = true; - } - } - } else { - for (final String playerName : war.getSide2Players()) { - if (residentNames.contains(playerName)) { - attackingOwnSide = true; - } - } - } - if (attackingOwnSide) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You cannot raid a town with members on your side of the war!"); + // 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(p.getUniqueId()); if (Main.econ.getBalance(offlinePlayer) <= 1000.0) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must have at least $1000 to put up to start a raid."); + if (admin) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must have at least $1000 to put up to start a raid."); + raidOwner.sendMessage(String.valueOf(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()); @@ -188,12 +234,13 @@ public class RaidCommands implements CommandExecutor { //broadcast Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "As part of " + war.getName() + ", forces from " + raid2.getRaiders() + " are gathering to raid the town of " + raidedTown.getName() + "!"); Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "All players from the defending side have been drafted for the town's defense."); - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid of " + Bukkit.broadcastMessage(Helper.Chatlabel() + "The raid of " + raidedTown.getName() + " will begin in " + (int) (RaidPhase.TRAVEL.startTick / 20 / 60) + " minutes!"); - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + + Bukkit.broadcastMessage(Helper.Chatlabel() + "The Raiders are gathering at " + gatherTown.getName() + " before making the journey over!"); - Main.warLogger.log(p.getName() + " started a raid."); + 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.getRaiders() + " are raiding the town of " + raidedTown.getName() + "!"); Main.warLogger.log("The Raiders are gathering at " + gatherTown.getName() + " before making the journey over!"); } @@ -201,11 +248,11 @@ public class RaidCommands implements CommandExecutor { } } if (!warFound) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "That war does not exist! /raid start [war] [town]"); + p.sendMessage(Helper.Chatlabel() + "That war does not exist! /raid start [war] [town]"); return; } if (!townExists) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "That town does not exist! /raid start [war] [town]"); + p.sendMessage(Helper.Chatlabel() + "That town does not exist! /raid start [war] [town]"); } } @@ -240,13 +287,39 @@ public class RaidCommands implements CommandExecutor { } } - private static void joinRaid(Player p, String[] args) { + protected static void joinRaid(Player 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])) { - if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); + Player joiner = p; + if (admin) { + joiner = Bukkit.getPlayer(args[3]); + + if(joiner != null) { + if (args[4] != null) { + if (args[4].equals(raid.getDefenders())) { + if (!raid.getDefenderPlayers().contains(joiner.getName())) + raid.getDefenderPlayers().add(joiner.getName()); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Player " + joiner.getName() + " added to defender side."); + Main.warLogger.log("Player " + joiner.getName() + " added to defender side."); + joiner.sendMessage(String.valueOf(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(String.valueOf(Helper.Chatlabel()) + "Player " + joiner.getName() + " added to raider side."); + Main.warLogger.log("Player " + joiner.getName() + " added to raider side."); + joiner.sendMessage(String.valueOf(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(String.valueOf(Helper.Chatlabel()) + "Player " + args[3] + " not found!"); + return; + } + } + if (!raid.getWar().getSide1Players().contains(joiner.getName()) && !raid.getWar().getSide2Players().contains(joiner.getName())) { + joiner.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); } //check if gather phase @@ -254,32 +327,44 @@ public class RaidCommands implements CommandExecutor { try { //check if the player is in the gather town ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); - if (cluster.contains(WorldCoord.parseWorldCoord(p))) { - raid.addActiveRaider(p.getName()); - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You have joined the raid on " + raid.getRaidedTown().getName() + "!"); - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Leaving " + raid.getGatherTown().getName() + " will cause you to leave the raid party."); + if (cluster.contains(WorldCoord.parseWorldCoord(joiner))) { + raid.addActiveRaider(joiner.getName()); + joiner.sendMessage(String.valueOf(Helper.Chatlabel()) + "You have joined the raid on " + raid.getRaidedTown().getName() + "!"); + joiner.sendMessage(String.valueOf(Helper.Chatlabel()) + "Leaving " + raid.getGatherTown().getName() + " will cause you to leave the raid party."); } else { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must be in the gathering town to join the raid on " + raid.getRaidedTown().getName() + "."); + joiner.sendMessage(String.valueOf(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 { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You cannot join the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); + joiner.sendMessage(String.valueOf(Helper.Chatlabel()) + "You cannot join the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); } + + raid.save(); } } else { p.sendMessage(String.valueOf(Helper.Chatlabel()) + "No raid is gathering in this town or this town does not exist!"); } } - private static void leaveRaid(Player p, String[] args) { + protected static void leaveRaid(Player player, String[] args, boolean admin) { //Get raid! + Player p = player; + if (admin) { + p = Bukkit.getPlayer(args[3]); + if (p == null) { + player.sendMessage(String.valueOf(Helper.Chatlabel()) + "Error: player not found!"); + return; + } + } + 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(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); + if (admin) player.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); } //check if gather phase @@ -290,17 +375,22 @@ public class RaidCommands implements CommandExecutor { if (cluster.contains(WorldCoord.parseWorldCoord(p))) { raid.removeActiveRaider(p.getName()); p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You have left the raid on " + raid.getRaidedTown().getName() + "!"); + if (admin) player.sendMessage(String.valueOf(Helper.Chatlabel()) + "You have left the raid on " + raid.getRaidedTown().getName() + "!"); } else { p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You cannot leave the raid on " + raid.getRaidedTown().getName() + "! It has already started!"); + if (admin) player.sendMessage(String.valueOf(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(String.valueOf(Helper.Chatlabel()) + "No raid is gathering in this town or this town does not exist!"); + if (admin) player.sendMessage(String.valueOf(Helper.Chatlabel()) + "No raid is gathering in this town or this town does not exist!"); } } @@ -317,17 +407,16 @@ public class RaidCommands implements CommandExecutor { if (raid.getWar().getName().equalsIgnoreCase(args[1])) { if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); + return; } - if (!raid.getOwner().getName().equals(p.getName())) { + if (raid.getOwner().getName().equals(p.getName())) { //check if gather phase if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START || raid.getPhase() == RaidPhase.TRAVEL || raid.getPhase() == RaidPhase.COMBAT) { - if (raid.getGatherTown().hasTownBlock(WorldCoord.parseWorldCoord(p))) { - //force defender victory - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid on " + raid.getRaidedTown().getName() + " has been abandoned by " + raid.getRaiders()); - Main.warLogger.log(p.getName() + " abandoned the raid on " + raid.getRaidedTown().getName() + " they started at " + raid.getGatherTown().getName()); - raid.defendersWin(raid.getRaidScore()); - } + //force defender victory + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid on " + raid.getRaidedTown().getName() + " has been abandoned by " + raid.getRaiders()); + Main.warLogger.log(p.getName() + " abandoned the raid on " + raid.getRaidedTown().getName() + " they started at " + raid.getGatherTown().getName()); + raid.defendersWin(raid.getRaidScore()); } else { p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You cannot abandon the raid on " + raid.getRaidedTown().getName() + "! It has a problem!"); } @@ -336,9 +425,8 @@ public class RaidCommands implements CommandExecutor { } } - } else { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This raid does not exist!"); } + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This raid does not exist!"); } private static void listRaids(Player p, String[] args) { @@ -375,30 +463,12 @@ public class RaidCommands implements CommandExecutor { private static void fail(Player p, String[] args, String type) { switch (type) { - case "permissions": { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou do not have permission to do this")); - return; - } - case "syntax": { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); - return; - } - case "noRaids": { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "There are currently no Raids in progress"); - return; - } - case "badName": { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "A raid could not be found with this Name! Type /raid list to view current raids"); - return; - } - case "owner": { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Only the player who started the raid can abandon it."); - return; - } - default: { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); - - } + case "permissions" -> p.sendMessage(String.valueOf(Helper.Chatlabel()) + "&cYou do not have permission to do this"); + case "syntax" -> p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Syntax. /raid help"); + case "noRaids" -> p.sendMessage(String.valueOf(Helper.Chatlabel()) + "There are currently no Raids in progress"); + case "badName" -> p.sendMessage(String.valueOf(Helper.Chatlabel()) + "A raid could not be found with this Name! Type /raid list to view current raids"); + case "owner" -> p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Only the player who started the raid can abandon it."); + default -> p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); } } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommands.java index 578fd75..7215ad2 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommands.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/SiegeCommands.java @@ -37,7 +37,7 @@ public class SiegeCommands implements CommandExecutor { switch (args[0].toLowerCase()) { case "start": - siegeStart(sender, args); + siegeStart(sender, args, false); return true; case "stop": siegeStop(sender, args); @@ -58,9 +58,15 @@ public class SiegeCommands implements CommandExecutor { return false; } - private static void siegeStart(CommandSender sender, String[] args) { + protected static void siegeStart(CommandSender sender, String[] args, boolean admin) { if (!(sender instanceof Player)) return; - final Player player = (Player) sender; + //if this is admin mode use the forth arg instad of sender. + //if player is null after this then force end + Player player = !admin ? (Player) sender : args.length >= 4 ? Bukkit.getPlayer(args[3]) : null; + if(player == null) { + sender.sendMessage("Forced owner not found!"); + return; + } if (args.length < 3) { sender.sendMessage(Helper.Chatlabel() + "/war siege [war] [town]"); @@ -71,6 +77,7 @@ public class SiegeCommands implements CommandExecutor { War war = WarData.getWar(args[1]); if (war == null) { player.sendMessage(String.valueOf(Helper.Chatlabel()) + "That war does not exist! /siege start [war] [town]"); + if(admin) sender.sendMessage(String.valueOf(Helper.Chatlabel()) + "That war does not exist! /siege start [war] [town]"); return; } @@ -79,9 +86,11 @@ public class SiegeCommands implements CommandExecutor { int side = war.getSide(leaderTown.getName()); if (side == 0) { player.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) { player.sendMessage(Helper.Chatlabel() + "You have surrendered."); + if(admin) sender.sendMessage(Helper.Chatlabel() + "You have surrendered."); return; } @@ -89,18 +98,23 @@ public class SiegeCommands implements CommandExecutor { Town town = TownyAPI.getInstance().getTown(args[2]); if (town == null) { player.sendMessage(String.valueOf(Helper.Chatlabel()) + "That town does not exist! /siege start [war] [town]"); + if(admin) sender.sendMessage(String.valueOf(Helper.Chatlabel()) + "That town does not exist! /siege start [war] [town]"); return; } // Attacking own side if (war.getSide(town) == side) { player.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, player); SiegeData.addSiege(siege); war.addSiege(siege); + + Bukkit.broadcastMessage(Helper.Chatlabel() + "A siege has been laid on " + siege.getTown() + " by " + siege.getAttackers() + "!"); + war.save(); siege.start(); } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommands.java index f423803..273f563 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommands.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/WarCommands.java @@ -1,5 +1,6 @@ package me.ShermansWorld.AlathraWar.commands; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import com.palmergames.bukkit.towny.TownyAPI; @@ -19,21 +20,21 @@ import org.bukkit.command.CommandExecutor; public class WarCommands implements CommandExecutor { - public WarCommands(final Main plugin) { - plugin.getCommand("war").setExecutor((CommandExecutor) this); - } + public WarCommands(final Main plugin) { + plugin.getCommand("war").setExecutor((CommandExecutor) this); + } - public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { + public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { if (!(sender instanceof Player)) { consoleCommand(sender, args); return true; } - final Player p = (Player) sender; - if (args.length == 0) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /war help"); + final Player p = (Player) sender; + if (args.length == 0) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /war help"); return true; - } else if (args.length >= 1) { + } else if (args.length >= 1) { switch(args[0].toLowerCase()) { case "create": @@ -43,10 +44,10 @@ public class WarCommands implements CommandExecutor { warDelete(p, args); return true; case "join": - warJoin(p, args); + warJoin(p, args, false); return true; case "surrender": - warSurrender(p, args); + warSurrender(p, args, false); return true; case "list": warList(p, args); @@ -62,11 +63,11 @@ public class WarCommands implements CommandExecutor { return true; } - } - return false; - } + } + return false; + } - private static void warCreate(Player p, String[] args) { + protected static void warCreate(Player p, String[] args) { if (!p.hasPermission("AlathraWar.admin")) { p.sendMessage("You do not have permission to run this command."); return; @@ -115,7 +116,13 @@ public class WarCommands implements CommandExecutor { } } - private static void warJoin(Player p, String[] args) { + /** + * + * @param p - executor + * @param args + * @param admin - admin behavior + */ + protected static void warJoin(Player p, String[] args, boolean admin) { // Sufficient args check if (args.length < 3) { p.sendMessage(Helper.Chatlabel() @@ -131,8 +138,8 @@ public class WarCommands implements CommandExecutor { return; } - // Towny Resident Object - Resident res = TownyAPI.getInstance().getResident(p); + // Towny Resident Object, if admin then dont use command runner, use declared player + Resident res = TownyAPI.getInstance().getResident(admin ? Bukkit.getPlayer(args[3]) : p); // Town check Town town = res.getTownOrNull(); @@ -152,9 +159,10 @@ public class WarCommands implements CommandExecutor { } if (res.hasNation()) { - if(p.hasPermission("AlathraWar.nationjoin" )|| res.isKing()) { + 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()); war.save(); return; @@ -164,9 +172,10 @@ public class WarCommands implements CommandExecutor { return; } } else if (res.hasTown()) { - if (p.hasPermission("AlathraWar.townjoin") || res.isMayor()) { + 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()); war.save(); return; @@ -178,7 +187,7 @@ public class WarCommands implements CommandExecutor { } } - private static void warSurrender(Player p, String[] args) { + protected static void warSurrender(Player p, String[] args, boolean admin) { // Sufficient args check if (args.length < 2) { p.sendMessage(Helper.Chatlabel() diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java index 47adec1..50676ac 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java @@ -5,18 +5,17 @@ import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.WorldCoord; import com.palmergames.bukkit.towny.object.metadata.CustomDataField; -import com.palmergames.bukkit.towny.object.metadata.IntegerDataField; import com.palmergames.bukkit.towny.object.metadata.LongDataField; import me.ShermansWorld.AlathraWar.Main; import me.ShermansWorld.AlathraWar.Raid; import me.ShermansWorld.AlathraWar.War; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import javax.annotation.Nonnull; import java.io.File; import java.io.FilenameFilter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.util.*; public class RaidData { // Static Raid list for all active raids @@ -85,26 +84,31 @@ public class RaidData { Town raidedTown = TownyAPI.getInstance().getTown((String) fileData.get("raidedTown")); Town gatherTown = TownyAPI.getInstance().getTown((String) fileData.get("gatherTown")); boolean attackBoolean = Boolean.parseBoolean((String) fileData.get("side1AreRaiders")); + //THIS MAY OR MAY NOT WORK + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString((String) fileData.get("owner"))); - Raid raid = new Raid(war, raidedTown, gatherTown, attackBoolean); + if(raidedTown == null || gatherTown == null) { + return null; + } + Raid raid = new Raid(war, raidedTown, gatherTown, attackBoolean, offlinePlayer); //Extra properties, if any are missing the raid doesnt exist yet if (fileData.get("raidTicks") != null && fileData.get("raidPhase") != null && fileData.get("activeRaiders") != null && fileData.get("lootedChunks") != null) { //active properties raid.setRaidPhase(RaidPhase.getByName((String) fileData.get("raidPhase"))); - raid.setRaidTicks((Integer) fileData.get("raidTicks")); + raid.setRaidTicks((int) fileData.get("raidTicks")); raid.setActiveRaiders((ArrayList) fileData.get("activeRaiders")); //looted chunks - HashMap o = (HashMap) fileData.get("lootedChunks"); - for (String s : o.keySet()) { + ArrayList o = (ArrayList) fileData.get("lootedChunks"); + for (Object hm : o) { //grab the chunk - HashMap chunk = (HashMap) o.get(s); + HashMap chunk = (HashMap) hm; //grab data for this chunk //worldcoord is its own map HashMap wc = (HashMap) chunk.get("worldCoord"); - WorldCoord worldCoordc = WorldCoord.parseWorldCoord((String) wc.get("world"), ((Integer) wc.get("x")), ((Integer) wc.get("z"))); + WorldCoord worldCoordc = WorldCoord.parseWorldCoord((String) wc.get("world"), ((int) wc.get("x")), ((int) wc.get("z"))); int ticks = (int) chunk.get("ticks"); double value = (double) chunk.get("value"); boolean finished = (boolean) chunk.get("finished"); @@ -118,6 +122,20 @@ public class RaidData { return raid; } + public static ArrayList createRaids(War war, Collection> raidMaps) { + ArrayList returnList = new ArrayList(); + for (HashMap map : raidMaps) { + Town town = TownyAPI.getInstance().getTown((String) map.get("raidedTown")); + if (town == null) continue; + + Raid raid = fromMap(war, map); + returnList.add(raid); + RaidData.addRaid(raid); + raid.resume(); + } + return returnList; + } + /** * Saves the war into files. @@ -152,13 +170,15 @@ public class RaidData { HashMap returnMap = new HashMap(); // Shoves everything into a map. returnMap.put("name", raid.getName()); - returnMap.put("raidedTown", raid.getRaidedTown()); - returnMap.put("gatherTown", raid.getRaidedTown()); + returnMap.put("raidedTown", raid.getRaidedTown().getName()); + returnMap.put("gatherTown", raid.getGatherTown().getName()); returnMap.put("raidTicks", raid.getRaidTicks()); returnMap.put("raidPhase", raid.getPhase().name()); returnMap.put("raidScore", raid.getRaidScore()); returnMap.put("side1AreRaiders", Boolean.toString(raid.getSide1AreRaiders())); returnMap.put("activeRaiders", raid.getActiveRaiders()); + //THIS MAY OR MAY NOT WORK + returnMap.put("owner", raid.getOwner().getUniqueId().toString()); //create a list from the looted chunks List chunkList = new ArrayList(); @@ -204,8 +224,8 @@ public class RaidData { if (town.hasMeta("lastRaided")) { CustomDataField field = town.getMetadata("lastRaided"); if (field != null) { - if (field instanceof IntegerDataField) { - return ((IntegerDataField) field).getValue(); + if (field instanceof LongDataField) { + return ((LongDataField) field).getValue(); } } return -1L; @@ -220,7 +240,7 @@ public class RaidData { * @Isaac this is for getting if a town can be raided, return (-2, -1, 0, 1, 2) based on status * (no players online, 24 hours town cooldown, 6 hour war cooldown, town being raided already, valid time to raid) */ - public static int isValidRaid(War war, @Nonnull Town town) { + public static int isValidRaid(War war, String side, @Nonnull Town town) { long townTime = whenTownLastRaided(town); //24 hours town cooldown @@ -230,7 +250,7 @@ public class RaidData { long lastRaidTime = war.getLastRaidTime(); //6 hour raid cooldown for war - if ((System.currentTimeMillis() / 1000) - lastRaidTime <= 21600) { + if ((System.currentTimeMillis() / 1000) - (side.equals(war.getSide1()) ? war.getLastRaidTimeSide1() : war.getLastRaidTimeSide2()) <= 21600) { return 0; } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidPhase.java b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidPhase.java index 391b8fe..60a4461 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidPhase.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidPhase.java @@ -2,7 +2,7 @@ package me.ShermansWorld.AlathraWar.data; public enum RaidPhase { START(0, 0), - GATHER(1, 1), + GATHER(1, 0), TRAVEL(2, 18000), COMBAT(3, 24000), END(4, 72000); @@ -44,4 +44,24 @@ public enum RaidPhase { } return null; } + + public static RaidPhase getNext(RaidPhase phase) { + switch (phase) { + case START: { + return GATHER; + } + case GATHER: { + return TRAVEL; + } + case TRAVEL: { + return COMBAT; + } + case COMBAT: { + return END; + } + default: { + return null; + } + } + } } \ No newline at end of file diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/SiegeData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/SiegeData.java index 045f43c..e2913ce 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/SiegeData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/SiegeData.java @@ -54,7 +54,7 @@ public class SiegeData public static void removeSiege(Siege siege) { sieges.remove(siege); - //deleteSiege(siege); + //deleteSiege(siege);d } /** diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/TimeoutData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/TimeoutData.java new file mode 100644 index 0000000..0a156ca --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/TimeoutData.java @@ -0,0 +1,126 @@ +package me.ShermansWorld.AlathraWar.data; + +import me.ShermansWorld.AlathraWar.Main; +import me.ShermansWorld.AlathraWar.War; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.UUID; + +public class TimeoutData { +// // Static War list for all active wars +// private static HashMap timeouts = new HashMap(); +// private final static String dataFolderPath = "plugins" + File.separator + "AlathraWar" + File.separator + "data"; +// private static File timeoutData = new File(dataFolderPath + File.separator + "timeOutData.yml"); +// +//// // Filter for only accessing yml files +//// private static FilenameFilter ymlFilter = new FilenameFilter() { +//// @Override +//// public boolean accept(File dir, String name) { +//// return name.endsWith(".yml"); +//// } +//// }; +// +// // Constructor used to initialise folder. +// public TimeoutData(final Main plugin) { +// if (!timeoutData.exists()) { +// try { +// timeoutData.createNewFile(); +// } catch (IOException e) { +// throw new RuntimeException(e); +// } +// } +// } +// +// public static HashMap getWars() { +// return timeouts; +// } +// +// /** +// * Gets a timeout for the player name, if failed to find return -1 +// * +// * @param name - name to check +// * @return timeout or -1 +// */ +// public static int getTimeout(String name) { +// for (UUID uuid : timeouts.keySet()) { +// if (Bukkit.getPlayer(name).getUniqueId().compareTo(uuid) == 0) return timeouts.get(uuid); +// } +// return -1; +// } +// +// /** +// * Gets a timeout for the player uuid, if failed to find return -1 +// * +// * @param uuidIN - UUID to check +// * @return timeout or -1 +// */ +// public static int getTimeout(UUID uuidIN) { +// for (UUID uuid : timeouts.keySet()) { +// if (uuidIN.compareTo(uuid) == 0) return timeouts.get(uuid); +// } +// return -1; +// } +// +// public static void setTimeouts(HashMap timeouts) { +// TimeoutData.timeouts = timeouts; +// } +// +// /** +// * add player timeout (in seconds) +// * @param player +// * @param time +// */ +// public static void addTimeout(Player player, int time) { +// if (player == null) { +// Main.warLogger.log("Attempted to add NULL to timeout list."); +// return; +// } +// timeouts.put(player.getUniqueId(), time); +// } +// +// public static void removeTimeout(Player player) { +// timeouts.remove(player.getUniqueId()); +//// deleteWar(war); +// } +// +// /** +// * Gets the wars currently saved in files as an ArrayList of War Objects +// * +// * @return War Object ArrayList +// */ +// public static HashMap createTimoutMap() { +// +// HashMap fileData = DataManager.getData(timeoutData); +// HashMap list = (HashMap) fileData.get("timeouts"); +// +// HashMap out = new HashMap<>(); +// for (String s : list.keySet()) { +// out.put(UUID.fromString(s), (Integer) list.get(s)); +// } +// +// return out; +// } +// +// /** +// * Saves the war into files. +// * +// * @param war - War to be saved. +// */ +// public static void saveWar(War war) { +// HashMap sHashMap = new HashMap(); +// +// HashMap list = new HashMap<>(); +// +// for(UUID uuid : timeouts.keySet()) { +// list.put(uuid.toString(), timeouts.get(uuid)); +// } +// // Shoves everything into a map. +// sHashMap.put("timeouts", list); +// +// DataManager.saveData("timeOutData.yml", sHashMap); +// } +} \ No newline at end of file diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java index f6dd577..11c6718 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import me.ShermansWorld.AlathraWar.Main; +import me.ShermansWorld.AlathraWar.Raid; import me.ShermansWorld.AlathraWar.Siege; import me.ShermansWorld.AlathraWar.War; @@ -81,7 +82,8 @@ public class WarData War fileWar = fromMap(fileData); returnList.add(fileWar); } catch (Exception e) { - Main.warLogger.log("Failed to load " + file.getName() + " " + e.getMessage()); + Main.warLogger.log("Failed to load " + file.getName()); + e.printStackTrace(); } } @@ -103,8 +105,12 @@ public class WarData war.setSide1Towns((ArrayList) fileData.get("side1Towns")); war.setSide2Towns((ArrayList) fileData.get("side2Towns")); + war.setSurrenderedTowns((ArrayList) fileData.get("surrenderedTowns")); - war.setLastRaidTime((int) fileData.get("lastRaidTime")); + if(fileData.get("lastRaidTimeSide1") != null && fileData.get("lastRaidTimeSide2") != null) { + war.setLastRaidTimeSide1((int) fileData.get("lastRaidTimeSide1")); + war.setLastRaidTimeSide2((int) fileData.get("lastRaidTimeSide2")); + } // Siege adding from map. @@ -114,6 +120,13 @@ public class WarData war.addSiege(siege); } + // Raid adding from map. + Collection> raidMaps = ((HashMap>) fileData.get("raids")).values(); + ArrayList raids = RaidData.createRaids(war, raidMaps); + for (Raid raid : raids) { + war.addRaid(raid); + } + return war; } @@ -134,15 +147,14 @@ public class WarData sHashMap.put("sieges", SiegeData.getSiegeMap(war)); sHashMap.put("raids", RaidData.getRaidMap(war)); - sHashMap.put("lastRaidTime", war.getLastRaidTime()); - + sHashMap.put("lastRaidTimeSide1", war.getLastRaidTimeSide1()); + sHashMap.put("lastRaidTimeSide2", war.getLastRaidTimeSide2()); DataManager.saveData("wars" + File.separator + war.getName() + ".yml", sHashMap); } private static void deleteWar(War war) { File[] files = new File(dataFolderPath + File.separator + "wars").listFiles(ymlFilter); - for (File file : files) { if (file.getName().startsWith(war.getName())) { file.delete(); diff --git a/src/main/java/me/ShermansWorld/AlathraWar/listeners/BlockBreakListener.java b/src/main/java/me/ShermansWorld/AlathraWar/listeners/BlockBreakListener.java index 8f1271b..f0d1f06 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/listeners/BlockBreakListener.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/listeners/BlockBreakListener.java @@ -1,5 +1,7 @@ package me.ShermansWorld.AlathraWar.listeners; +import me.ShermansWorld.AlathraWar.Raid; +import me.ShermansWorld.AlathraWar.data.RaidData; import org.bukkit.event.EventHandler; import java.util.HashSet; @@ -48,6 +50,15 @@ public class BlockBreakListener implements Listener { if (siege.getTown() == WorldCoord.parseWorldCoord(block).getTownOrNull()) { p.sendMessage(Helper.color("&cYou can not break blocks during sieges")); event.setCancelled(true); + return; + } + } + + for (final Raid raid : RaidData.getRaids()) { + if (raid.getRaidedTown() == WorldCoord.parseWorldCoord(block).getTownOrNull()) { + p.sendMessage(Helper.color("&cYou can not break blocks during raids")); + event.setCancelled(true); + return; } } } @@ -68,6 +79,15 @@ public class BlockBreakListener implements Listener { if (siege.getTown() == WorldCoord.parseWorldCoord(block).getTownOrNull()) { p.sendMessage(Helper.color("&cYou can not place blocks during sieges")); event.setCancelled(true); + return; + } + } + + for (final Raid raid : RaidData.getRaids()) { + if (raid.getRaidedTown() == WorldCoord.parseWorldCoord(block).getTownOrNull()) { + p.sendMessage(Helper.color("&cYou can not place blocks during raids")); + event.setCancelled(true); + return; } } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/listeners/CommandsListener.java b/src/main/java/me/ShermansWorld/AlathraWar/listeners/CommandsListener.java index eac3f88..857d1fd 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/listeners/CommandsListener.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/listeners/CommandsListener.java @@ -6,6 +6,7 @@ import me.ShermansWorld.AlathraWar.Siege; import me.ShermansWorld.AlathraWar.War; import me.ShermansWorld.AlathraWar.data.RaidPhase; import me.ShermansWorld.AlathraWar.data.WarData; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -17,9 +18,24 @@ public class CommandsListener implements Listener { final static String[] prefixes = new String[]{ "", "towny", "essentials", "wild", "minecraft" }; + final static String[] prefixesTowny = new String[]{ + "", "towny" + }; + final static String[] blacklistedXLong = new String[]{ + "n set spawn", "nat set spawn", "nation set spawn", + "t set spawn", "town set spawn", + "t set homeblock", "town set homeblock", + "t set name", "town set name", + "n set name", "nat set name", "nation set name" + }; final static String[] blacklistedLong = new String[]{ "n spawn", "nat spawn", "nation spawn", - "t spawn", "town spawn" + "t spawn", "town spawn", + "t outpost", "town outpost" + }; + final static String[] payment = new String[]{ + "n withdraw", "nat withdraw", "nation withdraw", + "t withdraw", "town withdraw" }; final static String[] blacklistedShort = new String[]{ "home", "homes", "warp", "warps", @@ -31,17 +47,18 @@ public class CommandsListener implements Listener { }; - @EventHandler(priority = EventPriority.HIGH) + @EventHandler(priority = EventPriority.LOWEST) public void onCommandSend(final PlayerCommandPreprocessEvent event) { Player p = event.getPlayer(); //if player is admin, ignore this behavior - if (p.hasPermission("!AlathraWar.admin")) return; +// if (p.hasPermission("!AlathraWar.admin")) return; String[] args = event.getMessage().split(" "); + /* - Prevent any teleport command events from occuring + Prevent any teleport command events from occurring If using PlayerTeleportEvent, it breaks the death logic allow n spawn and t spawn for defenders trapped in spawn @@ -52,43 +69,78 @@ public class CommandsListener implements Listener { if (raid.getPhase() == RaidPhase.COMBAT || raid.getPhase() == RaidPhase.TRAVEL || raid.getPhase() == RaidPhase.GATHER) { //only do for active raiders and any defenders if (raid.getActiveRaiders().contains(p.getName()) || raid.getDefenders().contains(p.getName())) { + //set spawn and properties + if (args.length >= 3) { + String parse = (args[0].charAt(0) == '/' ? args[0].substring(1) : args[0]) + " " + args[1] + " " + args[2]; + for (String prefix : prefixesTowny) { + //payment check + for (String cmd : blacklistedXLong) { + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot modify this property during a raid!")); + event.setCancelled(true); + return; + } + } + } + } //n spawn and t spawn if (args.length >= 2) { //parse what we have, remove the starting sslash - String parse = (args[0].charAt(0) == '/' ? args[0].substring(1, args[0].length() - 1) : args[0]) + " " + args[1]; - for (String prefix : prefixes) { + String parse = (args[0].charAt(0) == '/' ? args[0].substring(1) : args[0]) + " " + args[1]; + for (String prefix : prefixesTowny) { + //payment check + for (String cmd : payment) { + //check for each prefix + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot withdraw money whilst in a raid!")); + event.setCancelled(true); + return; + } + } + for (String cmd : blacklistedLong) { //check for each prefix - if (parse.equals(prefix + ":" + cmd)) { + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { //check if in spawn if (p.getWorld().getName().equals("world")) { if (parse.equals("n spawn") || parse.equals("nat spawn") || parse.equals("nation spawn") || parse.equals("t spawn") || parse.equals("town spawn") || parse.equals("towny:n spawn") || parse.equals("towny:nat spawn") || parse.equals("towny:nation spawn") || parse.equals("towny:t spawn") || parse.equals("towny:town spawn")) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are stuck in spawn and are allowed to teleport to your town or nation."); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eYou are stuck in spawn and are allowed to teleport to your town or nation.")); return; } - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are stuck in spawn and are allowed to teleport to your town or nation."); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eYou are stuck in spawn and are allowed to teleport to your town or nation.")); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eUse /t spawn, or /n spawn")); event.setCancelled(true); return; } //else - p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("c") + "You cannot teleport whilst in a raid!"); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot teleport whilst in a raid!")); event.setCancelled(true); return; } } } - } else if (args.length >= 1) { + } + //random tp commands + if (args.length >= 1) { //parse what we have, remove the starting slash - String parse = args[0].charAt(0) == '/' ? args[0].substring(1, args[0].length() - 1) : args[0]; + String parse = args[0].charAt(0) == '/' ? args[0].substring(1) : args[0]; for (String prefix : prefixes) { - for (String cmd : blacklistedLong) { + for (String cmd : blacklistedShort) { //check for each prefix - if (parse.equals(prefix + ":" + cmd)) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("c") + "You cannot teleport whilst in a raid!"); + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { + //spawn world check + if (p.getWorld().getName().equals("world")) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eYou are stuck in spawn and are allowed to teleport to your town or nation.")); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eUse /t spawn, or /n spawn")); + event.setCancelled(true); + return; + } + + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot teleport whilst in a raid!")); event.setCancelled(true); return; } @@ -107,40 +159,78 @@ public class CommandsListener implements Listener { if (siege.getSiegeTicks() > 0) { //only do for active raiders and any defenders if (siege.getAttackerPlayers().contains(p.getName()) || siege.getDefenders().contains(p.getName())) { + //set spawn and properties + if (args.length >= 3) { + String parse = (args[0].charAt(0) == '/' ? args[0].substring(1) : args[0]) + " " + args[1] + " " + args[2]; + for (String prefix : prefixesTowny) { + //payment check + for (String cmd : blacklistedXLong) { + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot modify this property during a siege!")); + event.setCancelled(true); + return; + } + } + } + } //n spawn and t spawn if (args.length >= 2) { //parse what we have, remove the starting sslash - String parse = (args[0].charAt(0) == '/' ? args[0].substring(1, args[0].length() - 1) : args[0]) + " " + args[1]; - for (String prefix : prefixes) { + String parse = (args[0].charAt(0) == '/' ? args[0].substring(1) : args[0]) + " " + args[1]; + for (String prefix : prefixesTowny) { + //payment check + for (String cmd : payment) { + //check for each prefix + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot withdraw money whilst in a siege!")); + event.setCancelled(true); + return; + } + } + for (String cmd : blacklistedLong) { //check for each prefix - if (parse.equals(prefix + ":" + cmd)) { + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { //check if in spawn if (p.getWorld().getName().equals("world")) { if (parse.equals("n spawn") || parse.equals("nat spawn") || parse.equals("nation spawn") || parse.equals("t spawn") || parse.equals("town spawn") || parse.equals("towny:n spawn") || parse.equals("towny:nat spawn") || parse.equals("towny:nation spawn") || parse.equals("towny:t spawn") || parse.equals("towny:town spawn")) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are stuck in spawn and allowed to teleport to your town or nation!"); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eYou are stuck in spawn and are allowed to teleport to your town or nation.")); return; } + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eYou are stuck in spawn and are allowed to teleport to your town or nation.")); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eUse /t spawn, or /n spawn")); + event.setCancelled(true); + return; } //else - p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("c") + "You cannot teleport whilst in a raid!"); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot teleport whilst in a siege!")); event.setCancelled(true); return; } } } - } else if (args.length >= 1) { + } + //random tp commands + if (args.length >= 1) { //parse what we have, remove the starting slash - String parse = (args[0].charAt(0) == '/' ? args[0].substring(1, args[0].length() - 1) : args[0]) + " " + args[1]; + String parse = args[0].charAt(0) == '/' ? args[0].substring(1) : args[0]; for (String prefix : prefixes) { - for (String cmd : blacklistedLong) { + for (String cmd : blacklistedShort) { //check for each prefix - if (parse.equals(prefix + ":" + cmd)) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("c") + "You cannot teleport whilst in a raid!"); + if (parse.equals(prefix + (prefix.isEmpty() ? "" : ":") + cmd)) { + //spawn world check + if (p.getWorld().getName().equals("world")) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eYou are stuck in spawn and are allowed to teleport to your town or nation.")); + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&eUse /t spawn, or /n spawn")); + event.setCancelled(true); + return; + } + + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou cannot teleport whilst in a siege!")); event.setCancelled(true); return; } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/listeners/KillsListener.java b/src/main/java/me/ShermansWorld/AlathraWar/listeners/KillsListener.java index 1cb0e64..cbdf83b 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/listeners/KillsListener.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/listeners/KillsListener.java @@ -1,49 +1,96 @@ package me.ShermansWorld.AlathraWar.listeners; +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.WorldCoord; +import me.ShermansWorld.AlathraWar.Helper; import me.ShermansWorld.AlathraWar.Raid; -import me.ShermansWorld.AlathraWar.commands.RaidCommands; +import me.ShermansWorld.AlathraWar.Siege; +import me.ShermansWorld.AlathraWar.UUIDFetcher; import me.ShermansWorld.AlathraWar.data.RaidData; import me.ShermansWorld.AlathraWar.data.RaidPhase; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.Inventory; -import org.bukkit.command.CommandSender; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.exceptions.TownyException; -import org.bukkit.Bukkit; -import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; -import org.bukkit.entity.Entity; -import com.palmergames.bukkit.towny.object.WorldCoord; - -import me.ShermansWorld.AlathraWar.Helper; -import me.ShermansWorld.AlathraWar.Siege; -import me.ShermansWorld.AlathraWar.commands.SiegeCommands; import me.ShermansWorld.AlathraWar.data.SiegeData; - +import org.bukkit.Bukkit; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerRespawnEvent; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; public final class KillsListener implements Listener { + + public static final HashMap respawnqueue = new HashMap<>(); + @EventHandler public void onPlayerKilled(final PlayerDeathEvent event) { - if (event.getEntity().getPlayer() == null || !(event.getEntity().getKiller() instanceof Player)) { - return; - } + final Player killed = event.getEntity(); final Player killer = event.getEntity().getKiller(); Town town = null; try { - town = WorldCoord.parseWorldCoord((Entity)killed).getTownBlock().getTown(); + town = WorldCoord.parseWorldCoord(killed).getTownBlock().getTown(); } catch (NotRegisteredException ex2) {} boolean playerCloseToHomeBlockSiege = false; boolean playerCloseToHomeBlockRaid = false; + try { + //Raid logic + for (final Raid raid : RaidData.getRaids()) { + final int homeBlockXCoordRaided = raid.getRaidedTown().getHomeBlock().getCoord().getX() * 16; + final int homeBlockZCoordRaided = raid.getRaidedTown().getHomeBlock().getCoord().getZ() * 16; +// final int homeBlockXCoordGather = raid.getGatherTown().getHomeBlock().getCoord().getX() * 16; +// final int homeBlockZCoordGather = raid.getGatherTown().getHomeBlock().getCoord().getZ() * 16; + //Carryover from sieges + if (Math.abs(killed.getLocation().getBlockX() - homeBlockXCoordRaided) <= 200 && Math.abs(killed.getLocation().getBlockZ() - homeBlockZCoordRaided) <= 200) { + playerCloseToHomeBlockRaid = true; + } + + if (raid.getDefenders().contains(killed.getName()) && ((town != null && town.equals(raid.getRaidedTown())) || playerCloseToHomeBlockRaid)) { + //There is seperate behavior if combat hasnt started + if (raid.getPhase() == RaidPhase.COMBAT && killer != null) { + if (raid.getActiveRaiders().contains(killer.getName())) { + this.raidKill(killed, event); + raid.defenderKilledInCombat(event); + } + } + //teleport back to raided spawn, without damaged gear + this.oocKill(killed, event); + raid.defenderKilledOutofCombat(event); + return; + } + if (raid.getActiveRaiders().contains(killed.getName()) && ((town != null && town.equals(raid.getRaidedTown())) || playerCloseToHomeBlockRaid)) { + //There is seperate behavior if combat hasnt started + if (raid.getPhase() == RaidPhase.COMBAT && killer != null) { + if (raid.getDefenders().contains(killer.getName())) { + this.raidKill(killed, event); + raid.raiderKilledInCombat(event); + respawnqueue.put(killed.getUniqueId(), raid); + } + } + //teleport back to gather town spawn, without damaged gear + //this is to disincentive people prekilling + this.oocKill(killed, event); + raid.raiderKilledOutofCombat(event); + respawnqueue.put(killed.getUniqueId(), raid); + return; + } + if ( (town != null && town.equals(raid.getRaidedTown()) || playerCloseToHomeBlockRaid) ) { + this.siegeKill(killed, event); + } + } + + + if(killer == null) return; + for (final Siege siege : SiegeData.getSieges()) { final int homeBlockXCoord = siege.getTown().getHomeBlock().getCoord().getX() * 16; final int homeBlockZCoord = siege.getTown().getHomeBlock().getCoord().getZ() * 16; @@ -55,15 +102,17 @@ public final class KillsListener implements Listener siege.addPointsToAttackers(20); for (final String playerName : siege.getAttackerPlayers()) { try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! + 20 Attacker Points"); + Player p = Bukkit.getPlayer(playerName); + if (p != null) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! + 20 Attacker Points"); } - catch (NullPointerException ex5) {} + catch (NullPointerException ignored) {} } for (final String playerName : siege.getDefenderPlayers()) { try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! + 20 Attacker Points"); + Player p = Bukkit.getPlayer(playerName); + if (p != null) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! + 20 Attacker Points"); } - catch (NullPointerException ex6) {} + catch (NullPointerException ignored) {} } } this.siegeKill(killed, event); @@ -74,13 +123,15 @@ public final class KillsListener implements Listener siege.addPointsToDefenders(20); for (final String playerName : siege.getAttackerPlayers()) { try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Attacker killed! + 20 Defender Points"); + Player p = Bukkit.getPlayer(playerName); + if (p != null) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Attacker killed! + 20 Defender Points"); } catch (NullPointerException ex3) {} } for (final String playerName : siege.getDefenderPlayers()) { try { - Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Attacker killed! + 20 Defender Points"); + Player p = Bukkit.getPlayer(playerName); + if (p != null) p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Attacker killed! + 20 Defender Points"); } catch (NullPointerException ex4) {} } @@ -93,71 +144,70 @@ public final class KillsListener implements Listener } } - - //Raid logic - for (final Raid raid : RaidData.getRaids()) { - final int homeBlockXCoordRaided = raid.getRaidedTown().getHomeBlock().getCoord().getX() * 16; - final int homeBlockZCoordRaided = raid.getRaidedTown().getHomeBlock().getCoord().getZ() * 16; -// final int homeBlockXCoordGather = raid.getGatherTown().getHomeBlock().getCoord().getX() * 16; -// final int homeBlockZCoordGather = raid.getGatherTown().getHomeBlock().getCoord().getZ() * 16; - //Carryover from sieges - if (Math.abs(killed.getLocation().getBlockX() - homeBlockXCoordRaided) <= 300 && Math.abs(killed.getLocation().getBlockZ() - homeBlockZCoordRaided) <= 300) { - playerCloseToHomeBlockRaid = true; - } - if (raid.getActiveRaiders().contains(killer.getName()) && ((town != null && town.equals(raid.getRaidedTown())) || playerCloseToHomeBlockRaid)) { - //There is seperate behavior if combat hasnt started - if(raid.getPhase() == RaidPhase.COMBAT) { - this.siegeKill(killed, event); - if (raid.getDefenderPlayers().contains(killed.getName())) { - raid.defenderKilledInCombat(event); - } - return; - } else { - //teleport back to raided spawn, without damaged gear - this.oocKill(killed, event); - raid.defenderKilledOutofCombat(event); - return; - } - } - if (raid.getDefenderPlayers().contains(killer.getName()) && ((town != null && town.equals(raid.getRaidedTown())) || playerCloseToHomeBlockRaid)) { - //There is seperate behavior if combat hasnt started - if(raid.getPhase() == RaidPhase.COMBAT) { - this.siegeKill(killed, event); - if (raid.getActiveRaiders().contains(killed.getName())) { - raid.raiderKilledInCombat(event); - } - return; - } else { - //teleport back to gather town spawn, without damaged gear - //this is to disincentive people prekilling - this.oocKill(killed, event); - raid.raiderKilledOutofCombat(event); - return; - } - - } - if ( (town != null && town.equals(raid.getRaidedTown()) || playerCloseToHomeBlockRaid) ) { - this.siegeKill(killed, event); - } - } } - catch (NullPointerException | TownyException ex7) { + catch (NullPointerException | TownyException ignored) { } } + /** + * Runs last so that it overrides everyone else! + * + * @param event + */ + @EventHandler(priority = EventPriority.HIGHEST) + public void onRespawn(PlayerRespawnEvent event) { + if(respawnqueue.containsKey(event.getPlayer().getUniqueId())) { + Raid raid = respawnqueue.get(event.getPlayer().getUniqueId()); + if (raid == null) return; + if(raid.getActiveRaiders().contains(event.getPlayer().getName())) { + //do raider respawn + try { + event.setRespawnLocation(raid.getGatherTown().getSpawn()); + event.getPlayer().sendMessage(String.valueOf(Helper.Chatlabel()) + "You died " + (raid.getPhase().equals(RaidPhase.COMBAT) ? "raiding" : "before combat") + " and have been teleported back to the gather point."); + } catch (TownyException e) { + throw new RuntimeException(e); + } + } else if(raid.getDefenders().contains(event.getPlayer().getName())) { + //do defender respawn ? + } else { + //invalid code + } + respawnqueue.remove(event.getPlayer().getUniqueId()); + } + } + + /** * Damage all gear held by the player (and then send them to spawn?) * They don't lose items from death. - * @param killed - * @param event + * @param killed killed player + * @param event event */ private void siegeKill(final Player killed, final PlayerDeathEvent event) { //Helper Helper.damageAllGear(killed); //Siege specific - Bukkit.dispatchCommand((CommandSender)Bukkit.getConsoleSender(), "spawn " + killed.getName()); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "spawn " + killed.getName()); + event.setKeepInventory(true); + event.getDrops().clear(); + event.setKeepLevel(true); + } + + /** + * Damage all gear held by the player (and then send them to spawn?) + * They don't lose items from death. + * + * @param killed killed player + * @param event event + */ + private void raidKill(final Player killed, final PlayerDeathEvent event) { + //Helper + Helper.damageAllGear(killed); + + //Siege specific +// Bukkit.dispatchCommand((CommandSender)Bukkit.getConsoleSender(), "spawn " + killed.getName()); event.setKeepInventory(true); event.getDrops().clear(); event.setKeepLevel(true); @@ -166,12 +216,13 @@ public final class KillsListener implements Listener /** * Dont damage items held by the player * They don't lose items from death. - * @param killed - * @param event + * + * @param killed killed player + * @param event event */ private void oocKill(final Player killed, final PlayerDeathEvent event) { //Siege specific - Bukkit.dispatchCommand((CommandSender)Bukkit.getConsoleSender(), "spawn " + killed.getName()); +// Bukkit.dispatchCommand((CommandSender)Bukkit.getConsoleSender(), "spawn " + killed.getName()); event.setKeepInventory(true); event.getDrops().clear(); event.setKeepLevel(true);