diff --git a/.gitignore b/.gitignore index 65c4cf4..17c1943 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .classpath .project /.idea/ -/.vscode/ \ No newline at end of file +/.vscode/ +AlathraWar.iml \ No newline at end of file diff --git a/resources/raids.yml b/resources/raids.yml new file mode 100644 index 0000000..a5d1c24 --- /dev/null +++ b/resources/raids.yml @@ -0,0 +1,3 @@ +MaxID: 0 + +Raids: \ No newline at end of file diff --git a/resources/warstemplate.yml b/resources/warstemplate.yml new file mode 100644 index 0000000..8446cba --- /dev/null +++ b/resources/warstemplate.yml @@ -0,0 +1 @@ +id: diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Helper.java b/src/main/java/me/ShermansWorld/AlathraWar/Helper.java index 75c8674..0678dba 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Helper.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Helper.java @@ -1,7 +1,16 @@ package me.ShermansWorld.AlathraWar; +import com.palmergames.bukkit.towny.object.WorldCoord; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; public class Helper { @@ -16,4 +25,188 @@ public class Helper public static void testMsg() { Bukkit.broadcastMessage("test message"); } + + /** + * Damages all gear in a players inventory + * + * @param p + * @author ShermansWorld + */ + public static void damageAllGear(Player p) { + final Inventory inv = p.getInventory(); + for (int i = 0; i < inv.getSize(); ++i) { + final ItemStack is = inv.getItem(i); + if (is != null && (is.getType() == Material.NETHERITE_AXE || is.getType() == Material.NETHERITE_SWORD || is.getType() == Material.DIAMOND_AXE || is.getType() == Material.DIAMOND_SWORD || is.getType() == Material.IRON_AXE || is.getType() == Material.IRON_SWORD)) { + ItemMeta meta = is.getItemMeta(); + final Damageable damageable = (Damageable)meta; + if (is.getType() == Material.NETHERITE_SWORD || is.getType() == Material.NETHERITE_AXE) { + damageable.setDamage(damageable.getDamage() + 507); + if (damageable.getDamage() > 2000) { + damageable.setDamage(2030); + } + } + else if (is.getType() == Material.DIAMOND_SWORD || is.getType() == Material.DIAMOND_AXE) { + damageable.setDamage(damageable.getDamage() + 390); + if (damageable.getDamage() > 1500) { + damageable.setDamage(1560); + } + } + else if (is.getType() == Material.IRON_SWORD || is.getType() == Material.IRON_AXE) { + damageable.setDamage(damageable.getDamage() + 62); + if (damageable.getDamage() > 200) { + damageable.setDamage(249); + } + } + meta = (ItemMeta)damageable; + is.setItemMeta(meta); + } + } + final ItemStack[] armor = p.getInventory().getArmorContents(); + for (int j = 0; j < armor.length; ++j) { + if (armor[j] != null) { + final ItemStack is2 = armor[j]; + if (is2.getType() == Material.NETHERITE_HELMET || is2.getType() == Material.NETHERITE_CHESTPLATE || is2.getType() == Material.NETHERITE_LEGGINGS || is2.getType() == Material.NETHERITE_BOOTS || is2.getType() == Material.DIAMOND_HELMET || is2.getType() == Material.DIAMOND_CHESTPLATE || is2.getType() == Material.DIAMOND_LEGGINGS || is2.getType() == Material.NETHERITE_BOOTS || is2.getType() == Material.IRON_HELMET || is2.getType() == Material.IRON_CHESTPLATE || is2.getType() == Material.IRON_LEGGINGS || is2.getType() == Material.IRON_BOOTS) { + ItemMeta meta2 = is2.getItemMeta(); + final Damageable damageable2 = (Damageable)meta2; + if (is2.getType() == Material.NETHERITE_HELMET) { + damageable2.setDamage(damageable2.getDamage() + 101); + if (damageable2.getDamage() > 400) { + damageable2.setDamage(406); + } + } + if (is2.getType() == Material.NETHERITE_CHESTPLATE) { + damageable2.setDamage(damageable2.getDamage() + 148); + if (damageable2.getDamage() > 450) { + damageable2.setDamage(591); + } + } + if (is2.getType() == Material.NETHERITE_LEGGINGS) { + damageable2.setDamage(damageable2.getDamage() + 138); + if (damageable2.getDamage() > 420) { + damageable2.setDamage(554); + } + } + if (is2.getType() == Material.NETHERITE_BOOTS) { + damageable2.setDamage(damageable2.getDamage() + 120); + if (damageable2.getDamage() > 370) { + damageable2.setDamage(480); + } + } + if (is2.getType() == Material.DIAMOND_HELMET) { + damageable2.setDamage(damageable2.getDamage() + 90); + if (damageable2.getDamage() > 280) { + damageable2.setDamage(362); + } + } + if (is2.getType() == Material.DIAMOND_CHESTPLATE) { + damageable2.setDamage(damageable2.getDamage() + 132); + if (damageable2.getDamage() > 410) { + damageable2.setDamage(527); + } + } + if (is2.getType() == Material.DIAMOND_LEGGINGS) { + damageable2.setDamage(damageable2.getDamage() + 123); + if (damageable2.getDamage() > 380) { + damageable2.setDamage(494); + } + } + if (is2.getType() == Material.DIAMOND_BOOTS) { + damageable2.setDamage(damageable2.getDamage() + 107); + if (damageable2.getDamage() > 330) { + damageable2.setDamage(428); + } + } + if (is2.getType() == Material.IRON_HELMET) { + damageable2.setDamage(damageable2.getDamage() + 41); + if (damageable2.getDamage() > 125) { + damageable2.setDamage(164); + } + } + if (is2.getType() == Material.IRON_CHESTPLATE) { + damageable2.setDamage(damageable2.getDamage() + 60); + if (damageable2.getDamage() > 190) { + damageable2.setDamage(239); + } + } + if (is2.getType() == Material.IRON_LEGGINGS) { + damageable2.setDamage(damageable2.getDamage() + 56); + if (damageable2.getDamage() > 180) { + damageable2.setDamage(224); + } + } + if (is2.getType() == Material.IRON_BOOTS) { + damageable2.setDamage(damageable2.getDamage() + 48); + if (damageable2.getDamage() > 150) { + damageable2.setDamage(194); + } + } + meta2 = (ItemMeta)damageable2; + is2.setItemMeta(meta2); + } + } + } + } + + /** + * + * @param worldCoord worldCoord + * @return List of connected chunks + * @author NinjaMandalorian + */ + private static ArrayList getAdjCells(WorldCoord worldCoord) { + ArrayList ReturnList = new ArrayList<>(); + + int[][] XZarray = new int[][]{ + {-1, 0}, + {1, 0}, + {0, -1}, + {0, 1} + }; // Array that contains relative orthogonal shifts from origin + for ( int[] pair: XZarray) { + // Constructs new WorldCoord for comparison + WorldCoord tCoord = new WorldCoord(worldCoord.getWorldName(), worldCoord.getX() + pair[0], worldCoord.getZ() + pair[1]); + if (tCoord.getTownOrNull() != null && tCoord.getTownOrNull() == worldCoord.getTownOrNull()) { + // If in town, and in same town, adds to return list + ReturnList.add(tCoord); + } + } + return ReturnList; + } + + /** Gets all adjacently connected townblocks + * @param chunkCoord - WorldCoord to check at + * @return List of WorldCoords + * @author NinjaMandalorian + */ + public static ArrayList getCluster(WorldCoord chunkCoord){ + // WCoordList is the returning array, SearchList is the to-search list. + ArrayList WCoordList = new ArrayList<>(); + ArrayList SearchList = new ArrayList<>(); + SearchList.add(chunkCoord); // Adds 1st chunk to list + + // Iterates through SearchList, to create a full list of every adjacent cell. + while (SearchList.size() > 0) { + // Gets WorldCoord + WorldCoord toSearch = SearchList.get(0); + // Gets adjacent cells + ArrayList adjCells = getAdjCells(toSearch); + for (WorldCoord cell : adjCells) { + // If in final list, ignore. + if (WCoordList.contains(cell)) { + continue; + // If in to-search list, ignore + } else if (SearchList.contains(cell)) { + continue; + // Otherwise, add to search-list. + } else { + SearchList.add(cell); + } + } + // Removes from search list and adds to finished list. After checking all adjacent chunks. + SearchList.remove(toSearch); + WCoordList.add(toSearch); + } + + return WCoordList; // Returns list + } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Main.java b/src/main/java/me/ShermansWorld/AlathraWar/Main.java index e5bf486..adc7f10 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Main.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Main.java @@ -2,6 +2,7 @@ package me.ShermansWorld.AlathraWar; import me.ShermansWorld.AlathraWar.commands.*; import me.ShermansWorld.AlathraWar.data.RaidData; +import me.ShermansWorld.AlathraWar.listeners.CommandsListener; import org.bukkit.event.Listener; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.Plugin; @@ -27,7 +28,6 @@ import net.milkbowl.vault.economy.Economy; import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin { - public static Main instance; public static Economy econ; public static AlathraWarLogger warLogger; @@ -98,6 +98,7 @@ public class Main extends JavaPlugin { getCommand("siege").setTabCompleter(new SiegeTabCompletion()); //getCommand("raid").setTabCompleter(new RaidTabCompletion()); getServer().getPluginManager().registerEvents((Listener) new KillsListener(), (Plugin) this); + getServer().getPluginManager().registerEvents((Listener) new CommandsListener(), (Plugin) this); getServer().getPluginManager().registerEvents((Listener) new JoinListener(), (Plugin) this); getServer().getPluginManager().registerEvents((Listener) new BlockBreakListener(), (Plugin) this); initData(); diff --git a/src/main/java/me/ShermansWorld/AlathraWar/Raid.java b/src/main/java/me/ShermansWorld/AlathraWar/Raid.java index b3b5d11..72ae9da 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/Raid.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/Raid.java @@ -1,15 +1,28 @@ package me.ShermansWorld.AlathraWar; +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.Resident; import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.TownBlock; -import me.ShermansWorld.AlathraWar.commands.RaidCommands; +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 org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.plugin.Plugin; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; /* RAID EXPLANATION @@ -27,155 +40,799 @@ and raided players may run out of the town, forfeiting the cost of the raid. The raid is split into two phases: - STAGING PHASE - - Raiders collect at raiding town, joining raid through command - - Defenders are given time to get to the town. + v/ Raiders collect at raiding town, joining raid through command + v/ Defenders are given time to get to the town. (travel is announced way in ahead) - COMBAT PHASE - - Raiders go to town + v/ Raiders go to town - Combat occurs - Decides outcome of raid TODO LIST: -- No teleporting by raiders -- Points management - - Both sides gain points for kills - - Raiders gain points for -- Raiders on death are teleported to their town spawn. +v/ No teleporting by raiders +v/ Points management + v/ Raider death is negative score + v/ Defender death is positive score + v/ Raiders can loot townblocks for extra score +v/ Raiders on death are teleported to their town spawn. + */ /** - * This is based on the existing Siege Class - * + * Implementation of Raids for alathrawar + * * @author AubriTheHuman * @author NinjaMandalorian * @author ShermansWorld */ public class Raid { + //Ticks between activity, this is lower than siege because more activity is occuring in a raid + private static final int incremental = 40; private War war; - private Town town; + private final String name; + private Town raidedTown; + private Town gatherTown; private String raiders; private String defenders; private boolean side1AreRaiders; - private boolean side2AreRaiders; - private int id; - private int raiderPoints; - private int defenderPoints; - private int MAXRAIDTICKS; + private int raidScore; private Player owner; private int raidTicks; - private TownBlock homeBlock; - private Location townSpawn; + private TownBlock homeBlockRaided; + private TownBlock homeBlockGather; + private RaidPhase phase; + private Location townSpawnRaided; + private Location townSpawnGather; int[] bukkitId; + public ArrayList activeRaiders; public ArrayList raiderPlayers; public ArrayList defenderPlayers; + public Map lootedChunks; + + /** Constructs raid for staging phase + * + * @param war + * @param raidedTown + * @param gatherTown + * @param side1AreRaiders + * @param raidTicks + */ + public Raid(final War war, final Town raidedTown, final Town gatherTown, + final boolean side1AreRaiders, final int raidTicks) { + + this.bukkitId = new int[1]; + this.activeRaiders = activeRaiders == null ? new ArrayList<>() : activeRaiders; + this.war = war; + this.raidTicks = raidTicks; + this.raidedTown = raidedTown; + this.gatherTown = gatherTown; + + //AttackSide-Town + this.name = (side1AreRaiders ? war.getSide1() : war.getSide2()).toLowerCase() + "-" + raidedTown.getName().toLowerCase(); + this.lootedChunks = new HashMap<>(); - // Constructs raid for staging phase - public Raid(final int id, final War war, final Town town, final String raiders, final String defenders, - final boolean side1AreRaiders, final boolean side2AreRaiders) { - /* - * this.raidTicks = 0; - * this.bukkitId = new int[1]; - * this.raiderPlayers = new ArrayList(); - * this.defenderPlayers = new ArrayList(); - * this.war = war; - * this.town = town; - * this.raiders = raiders; - * this.id = id; - */ } + /** + * IDK why this constructor exists but it does + * @param war + * @param raidedTown + * @param gatherTown + * @param side1AreRaiders + * @param raidTicks + * @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); + this.activeRaiders = activeRaiders; + this.setRaidPhase(phase); + } + + + /** + * Initial Constructor + * @param war + * @param raidedTown + * @param gatherTown + * @param side1AreRaiders + */ + public Raid(final War war, final Town raidedTown, final Town gatherTown, + final boolean side1AreRaiders) { + this.bukkitId = new int[1]; + this.activeRaiders = activeRaiders == null ? new ArrayList<>() : activeRaiders; + this.war = war; + this.raidTicks = 0; + this.raidedTown = raidedTown; + + //AttackSide-Town + this.name = (side1AreRaiders ? war.getSide1() : war.getSide2()).toLowerCase() + "-" + raidedTown.getName().toLowerCase(); + this.lootedChunks = new HashMap<>(); + } + + /** + * Run when raid starts + */ public void start() { + //start + this.phase = RaidPhase.START; + + if (this.side1AreRaiders) { + this.raiderPlayers = this.war.getSide1Players(); + this.defenderPlayers = this.war.getSide2Players(); + } else { + this.raiderPlayers = this.war.getSide2Players(); + this.defenderPlayers = this.war.getSide1Players(); + } + // Sets homeBlock and spawn for town. (So no mid-raid changes.) try { - homeBlock = town.getHomeBlock(); - townSpawn = town.getSpawn(); + homeBlockRaided = raidedTown.getHomeBlock(); + townSpawnRaided = raidedTown.getSpawn(); + homeBlockGather = gatherTown.getHomeBlock(); + townSpawnGather = gatherTown.getSpawn(); } catch (TownyException e) { e.printStackTrace(); } + //set the last raided time to now + raidedTown.addMetaData(new LongDataField("lastRaided", (System.currentTimeMillis() / 1000))); + war.setLastRaidTime((System.currentTimeMillis() / 1000)); + // Creates 10 second looping function for Raid this.bukkitId[0] = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) Main.getInstance(), (Runnable) new Runnable() { @Override public void run() { - if (homeBlock != null) { - town.setHomeBlock(homeBlock); - town.setSpawn(townSpawn); + if (homeBlockRaided != null) { + raidedTown.setHomeBlock(homeBlockRaided); + raidedTown.setSpawn(townSpawnRaided); } + if (homeBlockGather != null) { + gatherTown.setHomeBlock(homeBlockGather); + gatherTown.setSpawn(townSpawnGather); + } + if (Raid.this.side1AreRaiders) { + Raid.this.raiderPlayers = Raid.this.war.getSide1Players(); + Raid.this.defenderPlayers = Raid.this.war.getSide2Players(); + } else { + Raid.this.raiderPlayers = Raid.this.war.getSide2Players(); + Raid.this.defenderPlayers = Raid.this.war.getSide1Players(); + } + if (Raid.this.raidTicks >= RaidPhase.END.startTick) { - // If time runs out, stops scheduled task. - if (Raid.this.raidTicks >= Raid.this.MAXRAIDTICKS) { Bukkit.getServer().getScheduler().cancelTask(Raid.this.bukkitId[0]); - if (Raid.this.raiderPoints > Raid.this.defenderPoints) { - Raid.this.raidersWin(Raid.this.owner); + 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 > 700) { + Raid.this.raidersWin(Raid.this.owner, Raid.this.raidScore); } else { - Raid.this.defendersWin(); + Raid.this.defendersWin(Raid.this.raidScore); } } else { - final Raid this$0 = Raid.this; - Raid.access$7(this$0, this$0.raidTicks + 200); + raidTicks += incremental; - // Raid update - if (Raid.this.raidTicks % 6000 == 0) { - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "Report on the raid of " - + Raid.this.town.getName() + ":"); - Bukkit.broadcastMessage( - "Raider Points - " + String.valueOf(Raid.this.raiderPoints)); - Bukkit.broadcastMessage( - "Defender Points - " + String.valueOf(Raid.this.defenderPoints)); + //check and start travel phase + if (Raid.this.raidTicks >= RaidPhase.TRAVEL.startTick && Raid.this.phase != RaidPhase.TRAVEL) { + startTravel(); + } + + //check and start combat phase + if (Raid.this.raidTicks >= RaidPhase.COMBAT.startTick && Raid.this.phase != RaidPhase.COMBAT) { + startCombat(); + } + + //Raid start 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); + 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]"); + } + } catch (NullPointerException e) { + e.printStackTrace(); + } catch (NotRegisteredException e) { + e.printStackTrace(); + } catch (TownyException e) { + e.printStackTrace(); + } + } + + } + + //Raid Travel phase behavior + else if (Raid.this.phase == RaidPhase.TRAVEL) { + + 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) and if so start combat + for (String player : activeRaiders) { + WorldCoord playercoord = WorldCoord.parseWorldCoord(Bukkit.getPlayer(player)); + if (Raid.this.getRaidedTown().hasTownBlock(playercoord)) { + startCombat(); + } + } + + } + //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)); + } + + //Looting chunks + for (String name : Raid.this.getActiveRaiders()) { + Player p = Bukkit.getPlayer(name); + WorldCoord wc = WorldCoord.parseWorldCoord(p); + if (p.isSneaking()) { + //check if our chunk is looted, if so skip this player + //Idk if thisll work but if it do then ye + if (Raid.this.lootedChunks.containsKey(wc)) { + if (Raid.this.lootedChunks.get(wc).finished) { + p.sendMessage(Helper.Chatlabel() + "This chunk is already looted, try another."); + } else { + doLootAt(p, wc); + } + } else { + if (Raid.this.getRaidedTown().hasTownBlock(wc)) { + doLootAt(p, wc); + } else { + 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; +// } +// } + + } + } } } } - }, 0L, 200L); - + }, 0L, incremental); } - // End of raid + + /** + * Resumes a Raid (after a server restart e.t.c.) + */ + public void resume() { + if (this.side1AreRaiders) { + this.raiderPlayers = this.war.getSide1Players(); + this.defenderPlayers = this.war.getSide2Players(); + } else { + this.raiderPlayers = this.war.getSide2Players(); + this.defenderPlayers = this.war.getSide1Players(); + } + + // Sets homeBlock and spawn for town. (So no mid-raid changes.) + try { + homeBlockRaided = raidedTown.getHomeBlock(); + townSpawnRaided = raidedTown.getSpawn(); + homeBlockGather = gatherTown.getHomeBlock(); + townSpawnGather = gatherTown.getSpawn(); + } catch (TownyException e) { + e.printStackTrace(); + } + + // Creates 10 second looping function for Raid, restarting this! + this.bukkitId[0] = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) Main.getInstance(), + (Runnable) new Runnable() { + + @Override + public void run() { + if (homeBlockRaided != null) { + raidedTown.setHomeBlock(homeBlockRaided); + raidedTown.setSpawn(townSpawnRaided); + } + if (homeBlockGather != null) { + gatherTown.setHomeBlock(homeBlockGather); + gatherTown.setSpawn(townSpawnGather); + } + if (Raid.this.side1AreRaiders) { + Raid.this.raiderPlayers = Raid.this.war.getSide1Players(); + Raid.this.defenderPlayers = Raid.this.war.getSide2Players(); + } else { + 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]); + 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 > 700) { + Raid.this.raidersWin(Raid.this.owner, Raid.this.raidScore); + } 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.TRAVEL) { + startTravel(); + } + + //check and start combat phase + if (Raid.this.raidTicks >= RaidPhase.COMBAT.startTick && Raid.this.phase != RaidPhase.COMBAT) { + startCombat(); + } + + //Raid start 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); + 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]"); + } + } catch (NullPointerException e) { + e.printStackTrace(); + } catch (NotRegisteredException e) { + e.printStackTrace(); + } catch (TownyException e) { + e.printStackTrace(); + } + } + + } + + //Raid Travel phase behavior + else if (Raid.this.phase == RaidPhase.TRAVEL) { + + 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) and if so start combat + for (String player : activeRaiders) { + WorldCoord playercoord = WorldCoord.parseWorldCoord(Bukkit.getPlayer(player)); + if (Raid.this.getRaidedTown().hasTownBlock(playercoord)) { + startCombat(); + } + } + + } + //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)); + } + + //Looting chunks + for (String name : Raid.this.getActiveRaiders()) { + Player p = Bukkit.getPlayer(name); + WorldCoord wc = WorldCoord.parseWorldCoord(p); + if (p.isSneaking()) { + //check if our chunk is looted, if so skip this player + //Idk if thisll work but if it do then ye + if (Raid.this.lootedChunks.containsKey(wc)) { + if (Raid.this.lootedChunks.get(wc).finished) { + p.sendMessage(Helper.Chatlabel() + "This chunk is already looted, try another."); + } else { + doLootAt(p, wc); + } + } else { + if (Raid.this.getRaidedTown().hasTownBlock(wc)) { + doLootAt(p, wc); + } else { + 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; +// } +// } + + } + } + } + } + } + }, 0L, incremental); + } + + /** + * End of raid + */ public void stop() { + this.phase = RaidPhase.END; Bukkit.getScheduler().cancelTask(this.bukkitId[0]); - RaidCommands.raids.remove(this); // im not making this rn -Aubri + RaidData.removeRaid(this); } + /** - * Need to add call to KillsListener (defined seperate from Siege) + * Do loot logic for a chunk, synchronized in case multiple players are looting at once and something breaks + * + * @param p + * @param wc */ - public void raiderKilled() { + private 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 + if (this.lootedChunks.containsKey(wc)) { + LootBlock lb = this.lootedChunks.get(wc); + lb.ticks += incremental; + //60 seconds to loot + if (lb.ticks >= 1200 && !lb.finished) { + lb.finished = true; + Random r = new Random(); + //The base value can be abjusted + lb.value = r.nextDouble() * 100; + + //score for looting + this.addPointsToRaidScore(10); + + Main.warLogger.log(String.format("A townblock has been looted for $%.2d", 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)); + } + 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)); + } + } + } 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!"); + } + for (String s : defenderPlayers) { + Bukkit.getPlayer(s).sendMessage(Helper.Chatlabel() + "Raiders have started looting a town block!"); + } + } } /** - * Need to add call to KillsListener (defined seperate from Siege) + * Begin the Gather phase of the raid */ - public void defenderKilled() { - + private void startGather() { + Raid.this.phase = RaidPhase.GATHER; } - public void raidersWin(final Player owner) { - + /** + * Begin the Travel phase of the raid + */ + private void startTravel() { + Raid.this.phase = RaidPhase.TRAVEL; + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The Raiders of " + + (side1AreRaiders ? Raid.this.war.getSide1() : Raid.this.war.getSide2()) + + " are coming to raid " + + Raid.this.getRaidedTown().getName() + "!"); + //in the case were not at that stage already, if this happens we have an issue + Raid.this.raidTicks = RaidPhase.TRAVEL.startTick; } - public void defendersWin() { - + /** + * Begin the combat phase of the raid + */ + private void startCombat() { + Raid.this.phase = RaidPhase.COMBAT; + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The Raiders of " + + (side1AreRaiders ? Raid.this.war.getSide1() : Raid.this.war.getSide2()) + + " have arrived at " + + Raid.this.getRaidedTown().getName() + " and the fighting has begun!"); + //in the case were not at that stage already + Raid.this.raidTicks = RaidPhase.COMBAT.startTick; } - public void addPointsToRaiders(final int points) { - this.raiderPoints += points; + /** + * Raider killed in combat + */ + public void raiderKilledInCombat(PlayerDeathEvent event) { + + //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) { + } + } + 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); + } + } } - public void addPointsToDefenders(final int points) { - this.defenderPoints += points; + /** + * Defender killed in combat + */ + 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) { + } + } + for (final String playerName : this.getDefenderPlayers()) { + try { + Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! +20 Raid Score"); + } catch (NullPointerException ex6) { + } + } + + //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."); + } } - public int getID() { - return this.id; + /** + * Raider killed outside of combat + */ + public void raiderKilledOutofCombat(PlayerDeathEvent event) { + + //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) { + } + } + 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) { + } + } + + //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); + } + } } - public void setID(final int id) { - this.id = id; + /** + * Defender killed out of combat + */ + 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) { + } + } + 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) { + } + } + + //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 before being raided and have been teleported back to your town's spawn."); + } } + /** + * Raiders win, give payout and finalize raid + * @param owner + * @param raidScore + */ + public void raidersWin(final Player owner, int raidScore) { + //TODO finalize payout + + //Calc win factor + //difference of score minus 700, then divided by 100 + //raid needs 10+ kills over defenders to win + //factor is 0.5 -> 3 + double factor = ((raidScore - 700.0) / 100) + 0.5; + if (factor > 3.0) factor = 3.0; + + final Resident resident = TownyAPI.getInstance().getResident(owner); + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raiders from " + this.raiders + + " have successfully raided " + this.raidedTown.getName() + "!"); + + /* + if town has more than 10k, 1/10th of its valuables times the factor is taken + if it has less than 10k, 1000 is taken + + the raid chest is also kept + */ + final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(owner.getUniqueId()); + + //raid chest + Main.econ.depositPlayer(offlinePlayer, 1000); + double amt = 0.0; + if (this.raidedTown.getAccount().getHoldingBalance() > 10000) { + amt = Math.floor(this.raidedTown.getAccount().getHoldingBalance()) / 10.0; + amt *= factor; + if (amt > 10000.0) amt = 10000.0; + this.raidedTown.getAccount().withdraw(amt, "war loot"); + } else if (this.raidedTown.getAccount().getHoldingBalance() < 1000) { + amt = 1000; + this.raidedTown.getAccount().withdraw(this.raidedTown.getAccount().getHoldingBalance(), "war loot"); + } else { + amt = 1000; + this.raidedTown.getAccount().withdraw(amt, "war loot"); + } + + String statement = "raided"; + if (factor <= 1.0) { + statement = "looted"; + } else if (factor <= 2.0) { + statement = "ransacked"; + } else if (factor <= 3.0) { + statement = "emptied"; + } + + double looted = 0.0; + //calc looted chunks + for (WorldCoord wc : this.getLootedChunks().keySet()) { + if (this.getLootedChunks().get(wc).finished) looted += this.getLootedChunks().get(wc).value; + } + + amt += looted; + + Bukkit.broadcastMessage("The town of " + this.raidedTown.getName() + " has been " + statement + " by " + this.getRaiders() + + " in a raid for $" + String.valueOf(amt)); + Main.warLogger.log("The town of " + this.raidedTown.getName() + " has been " + statement + " by " + this.getRaiders() + + " in a raid for $" + String.valueOf(amt)); + Main.econ.depositPlayer(offlinePlayer, amt); + + + stop(); + } + + /** + * Defenders win, payout, and finalize raid + * @param raidScore + */ + public void defendersWin(int raidScore) { + //TODO finalize payout + /* + If defenders win, they get the 1000 deposit. + */ + + //at a raid score of 699.9999, this results in 0, 700 is considered raider victory + //at 600 this results in 0.5 + //at 500 this results in 1.0 + //at 0 this results in 3.5, which is capped to 3 + + double factor = (3 - ((raidScore) / 200.0) - 0.5); + if (factor > 3.0) factor = 3.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"); + Main.warLogger + .log("The defenders from " + this.defenders + " have won the raid of " + this.raidedTown.getName() + "!"); + this.raidedTown.getAccount().deposit(500, "Raid chest"); + } else if (factor <= 1.5) { + 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"); + Main.warLogger + .log("The defenders from " + this.defenders + " have won the raid of " + this.raidedTown.getName() + "!"); + this.raidedTown.getAccount().deposit(1000, "Raid chest"); + } else if (factor <= 3.0) { + 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."); + Main.warLogger + .log("The defenders from " + this.defenders + " have won the raid of " + this.raidedTown.getName() + "!"); + this.raidedTown.getAccount().deposit(1000, "Raid chest"); + } + + stop(); + } + + /** + * Positively impact the raid score + * @param points + */ + public void addPointsToRaidScore(final int points) { + this.raidScore += points; + //cap + if (raidScore > 1000) this.raidScore = 1000; + } + + /** + * Negatively impact the raid score + * @param points + */ + public void subtractPointsFromRaidScore(final int points) { + this.raidScore -= points; + //cap + if (raidScore < 0) this.raidScore = 0; + } + + public String getName() { + return this.name; + } + + public War getWar() { return this.war; } @@ -184,12 +841,28 @@ public class Raid { this.war = war; } - public Town getTown() { - return this.town; + public Town getRaidedTown() { + return this.raidedTown; } - public void setTown(final Town town) { - this.town = town; + public void setRaidedTown(final Town town) { + this.raidedTown = town; + } + + public Town getGatherTown() { + return this.gatherTown; + } + + public void setGatherTown(final Town town) { + this.gatherTown = town; + } + + public Player getOwner() { + return this.owner; + } + + public void setOwner(Player owner) { + this.owner = owner; } public String getRaiders() { @@ -208,20 +881,12 @@ public class Raid { this.raiders = defenders; } - public int getRaiderPoints() { - return this.raiderPoints; + public int getRaidScore() { + return this.raidScore; } - public void raiderPoints(final int raiderPoints) { - this.raiderPoints = raiderPoints; - } - - public int getDefenderPoints() { - return this.defenderPoints; - } - - public void defenderPoints(final int defenderPoints) { - this.defenderPoints = this.raiderPoints; + public void setRaidScore(final int raidScore) { + this.raidScore = raidScore; } public boolean getSide1AreRaiders() { @@ -232,14 +897,6 @@ public class Raid { this.side1AreRaiders = side1AreRaiders; } - public boolean getSide2AreRaiders() { - return this.side2AreRaiders; - } - - public void setSide2AreRaiders(final boolean side2AreRaiders) { - this.side2AreRaiders = side2AreRaiders; - } - public ArrayList getRaiderPlayers() { return this.raiderPlayers; } @@ -256,7 +913,78 @@ public class Raid { this.defenderPlayers = defenderPlayers; } + public int getRaidTicks() { + return this.raidTicks; + } + + public void setRaidTicks(int ticks) { + this.raidTicks = ticks; + } + static /* synthetic */ void access$7(final Raid raid, final int raidTicks) { raid.raidTicks = raidTicks; } + + public RaidPhase getPhase() { + return this.phase; + } + + public void setRaidPhase(final RaidPhase phase) { + this.phase = phase; + } + + public ArrayList getActiveRaiders() { + return this.activeRaiders; + } + + public void setActiveRaiders(ArrayList activeRaiders) { + this.activeRaiders = activeRaiders; + } + + public void addActiveRaider(String player) { + this.activeRaiders.add(player); + } + + public void removeActiveRaider(String name) { + this.activeRaiders.remove(name); + } + + public Map getLootedChunks() { + if (lootedChunks == null) { + lootedChunks = new HashMap<>(); + } + return lootedChunks; + } + + public void setLootedChunks(Map lootedChunks) { + this.lootedChunks = lootedChunks; + } + + public void addLootedChunk(WorldCoord c) { + this.lootedChunks.put(c, new LootBlock(c, 0, 0.0)); + } + + public static class LootBlock { + + public WorldCoord worldCoord; + public int ticks; + public double value; + public boolean finished; + + public LootBlock(WorldCoord c, int ticks, double value) { + this.worldCoord = c; + this.ticks = ticks; + this.value = value; + this.finished = false; + } + + public LootBlock(WorldCoord c, int ticks, double value, boolean finished) { + this.worldCoord = c; + this.ticks = ticks; + this.value = value; + this.finished = finished; + } + + + } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/War.java b/src/main/java/me/ShermansWorld/AlathraWar/War.java index 4131dd9..55ed18e 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/War.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/War.java @@ -22,6 +22,7 @@ public class War { // References private ArrayList sieges = new ArrayList(); private ArrayList raids = new ArrayList(); + private Long lastRaidTime; /** * War Constructor @@ -204,4 +205,11 @@ public class War { return returnList; } + public void setLastRaidTime(Long lastRaidTime) { + this.lastRaidTime = lastRaidTime; + } + + public Long getLastRaidTime() { + return this.lastRaidTime; + } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java index 2c6da5f..9fd696b 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidCommands.java @@ -1,11 +1,14 @@ package me.ShermansWorld.AlathraWar.commands; +import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; -import com.palmergames.bukkit.towny.object.Resident; -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyWorld; -import com.palmergames.bukkit.towny.object.WorldCoord; +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; +import me.ShermansWorld.AlathraWar.data.RaidData; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -15,8 +18,7 @@ import org.bukkit.entity.Player; import java.util.ArrayList; -public class RaidCommands implements CommandExecutor -{ +public class RaidCommands implements CommandExecutor { public static ArrayList towns; public static ArrayList raids; public static int maxID; @@ -31,230 +33,367 @@ public class RaidCommands implements CommandExecutor } public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { - /*final Player p = (Player)sender; + final Player p = (Player) sender; if (args.length == 0) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); - } - else if (args.length == 1) { + fail(p, args, "syntax"); + } else if (args.length == 1) { if (args[0].equalsIgnoreCase("list")) { - if (RaidCommands.raids.isEmpty()) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "There are currently no Raids in progress"); - return false; - } - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raids currently in progress:"); - for (final Raid raid : RaidCommands.raids) { - p.sendMessage("ID: " + String.valueOf(raid.getID())); - p.sendMessage("War: " + raid.getWar().getName()); - p.sendMessage("Town: " + raid.getTown().getName()); - p.sendMessage("Raiders: " + raid.getRaiders()); - p.sendMessage("Defenders: " + raid.getDefenders()); - p.sendMessage("Raider Points: " + String.valueOf(raid.getRaiderPoints())); - p.sendMessage("Defender Points: " + String.valueOf(raid.getDefenderPoints())); - p.sendMessage("Time Left: " + String.valueOf((108000 - Main.raidData.getConfig().getInt("Raids." + String.valueOf(String.valueOf(raid.getID()) + ".raidticks"))) / 1200) + " minutes"); - p.sendMessage("-=-=-=-=-=-=-=-=-=-=-=-"); - } + listRaids(p, args); } else if (args[0].equalsIgnoreCase("help")) { - if (p.hasPermission("!AlathraWar.admin")) { - p.sendMessage(Helper.Chatlabel() + "/raid stop [id]"); - } - p.sendMessage(Helper.Chatlabel() + "/raid start [war] [town]"); - p.sendMessage(Helper.Chatlabel() + "/raid abandon [id]"); - p.sendMessage(Helper.Chatlabel() + "/raid list"); + raidHelp(p, args); } else { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); + fail(p, args, "syntax"); } - - } - else if (args.length == 2) { + } else if (args.length == 3) { if (args[0].equalsIgnoreCase("stop")) { - if (!p.hasPermission("AlathraWar.admin")) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("&cYou do not have permission to do this")); - return false; - } - if (RaidCommands.raids.isEmpty()) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "There are currently no Raids in progress"); - return false; - } - boolean found = false; - for (int i = 0; i < RaidCommands.raids.size(); ++i) { - if (String.valueOf(RaidCommands.raids.get(i).getID()).equalsIgnoreCase(args[1])) { - found = true; - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "raid cancelled"); - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid at " + RaidCommands.raids.get(i).getTown().getName() + " has been cancelled by an admin"); - RaidCommands.raids.get(i).stop(); - break; - } - } - if (!found) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "A raid could not be found with this ID! Type /raid list to view current raids"); - return false; - } - } - else if (args[0].equalsIgnoreCase("abandon")) { - boolean found2 = false; - if (Main.raidData.getConfig().getString("Raids." + args[1] + ".owner") == null) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raid not found! Usage: /raid abandon [raid id]"); - return false; - } - final String configOwner = Main.raidData.getConfig().getString("Raids." + args[1] + ".owner"); - if (configOwner.equalsIgnoreCase(p.getName())) { - for (int j = 0; j < RaidCommands.raids.size(); ++j) { - if (String.valueOf(RaidCommands.raids.get(j).getID()).equalsIgnoreCase(args[1])) { - found2 = true; - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid at " + RaidCommands.raids.get(j).getTown().getName() + " has been abandoned by " + RaidCommands.raids.get(j).getRaiders()); - Main.warLogger.log(p.getName() + " abandoned the raid they started at " + RaidCommands.raids.get(j).getTown().getName()); - RaidCommands.raids.get(j).defendersWin(); - break; - } - } - } - if (!found2) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Only the player who started the raid can abandon it"); - } + stopRaid(p, args); } else { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); + fail(p, args, "syntax"); } - } - else if (args.length == 3) { + } else if (args.length == 3) { if (args[0].equalsIgnoreCase("start")) { - boolean warFound = false; - boolean townExists = false; - for (final War war : WarCommands.wars) { - if (war.getName().equalsIgnoreCase(args[1])) { - 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]"); - } - TownyWorld townyWorld; - townyWorld = WorldCoord.parseWorldCoord(p.getLocation()).getTownyWorld(); - if (!RaidCommands.towns.isEmpty()) { - RaidCommands.towns.clear(); - } + startRaid(p, args); + } else if (args[0].equalsIgnoreCase("join")) { + joinRaid(p, args); + } else if (args[0].equalsIgnoreCase("leave")) { + leaveRaid(p, args); + } else if (args[0].equalsIgnoreCase("abandon")) { + abandonRaid(p, args); + } else { + fail(p, args, "syntax"); + } + } else { + fail(p, args, "syntax"); + } + return false; - //TODO: This needs to change - //needs to have be in raiders town to delcare raid added - //so that surprise raids arent a thing, ill leave that to someone else - for (final String entry : townyWorld.getTowns().keySet()) { - final Town town = townyWorld.getTowns().get(entry); - RaidCommands.towns.add(town); - if (town.getName().equalsIgnoreCase(args[2])) { - townExists = true; - boolean attackingOwnSide = false; - ++RaidCommands.maxID; - Main.raidData.getConfig().set("MaxID", (Object)RaidCommands.maxID); - Main.raidData.saveConfig(); - Raid raid2; - if (war.getSide1Players().contains(p.getName())) { - raid2 = new Raid(RaidCommands.maxID, war, town, war.getSide1(), war.getSide2(), true, false); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".war", (Object)war.getName()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".town", (Object)town.getName()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".raiders", (Object)war.getSide1()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".defenders", (Object)war.getSide2()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".side1areraiders", (Object)true); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".side2areraiders", (Object)false); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".raidticks", (Object)0); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".raiderpoints", (Object)0); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".defenderpoints", (Object)0); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".owner", (Object)p.getName()); - Main.raidData.saveConfig(); - } - else { - if (!war.getSide2Players().contains(p.getName())) { - Bukkit.getLogger().info("Unable to find player declaring raid in the war"); - return false; - } - raid2 = new Raid(RaidCommands.maxID, war, town, war.getSide2(), war.getSide1(), false, true); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".war", (Object)war.getName()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".town", (Object)town.getName()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".raiders", (Object)war.getSide2()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".defenders", (Object)war.getSide1()); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".side1areraiders", (Object)false); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".side2areraiders", (Object)true); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".raidticks", (Object)0); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".raiderpoints", (Object)0); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".defenderpoints", (Object)0); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID) + ".owner", (Object)p.getName()); - Main.raidData.saveConfig(); - } - final ArrayList residentNames = new ArrayList(); - for (final Resident resident : town.getResidents()) { - residentNames.add(resident.getName()); - } - 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!"); - return false; - } - Town currentTown; - try { - currentTown = WorldCoord.parseWorldCoord(p.getLocation()).getTownBlock().getTown(); - } - catch (NotRegisteredException e2) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must be within the town's claim to begin a raid"); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID), (Object)null); - Main.raidData.saveConfig(); - return false; - } - if (!currentTown.equals(town)) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must be within the town's claim to begin a raid"); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID), (Object)null); - Main.raidData.saveConfig(); - return false; - } - for (final Raid raidEntry : RaidCommands.raids) { - if (raidEntry.getTown().getName().equalsIgnoreCase(town.getName())) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already under raid by your side!"); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID), (Object)null); - Main.raidData.saveConfig(); - return false; - } - } - final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(p.getUniqueId()); - if (Main.econ.getBalance(offlinePlayer) <= 2500.0) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You must have at least $20,000 to put up to start a raid"); - Main.raidData.getConfig().set("Raids." + String.valueOf(RaidCommands.maxID), (Object)null); - Main.raidData.saveConfig(); - return false; - } - Main.econ.withdrawPlayer(offlinePlayer, 2500.0); - RaidCommands.raids.add(raid2); - raid2.start(); - Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "As part of " + war.getName() + ", forces from " + raid2.getRaiders() + " are raiding the town of " + town.getName() + "!"); - Main.warLogger.log(p.getName() + " started a raid."); - Main.warLogger.log("As part of " + war.getName() + ", forces from " + raid2.getRaiders() + " are raiding the town of " + town.getName() + "!"); + } + + /** + * Starts a raid + * + * @param p + * @param args + */ + private static void startRaid(Player p, String[] args) { + boolean warFound = false; + boolean townExists = false; + for (final War war : WarData.getWars()) { + if (war.getName().equalsIgnoreCase(args[1])) { + 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]"); + } + TownyWorld townyWorld; + townyWorld = WorldCoord.parseWorldCoord(p.getLocation()).getTownyWorld(); + + //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()); + } 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."); + 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."); + return; + } + + //parse the argunment for town + for (final String entry : townyWorld.getTowns().keySet()) { + final Town raidedTown = townyWorld.getTowns().get(entry); + + if (raidedTown.getName().equalsIgnoreCase(args[2]) && gatherTown != null) { + 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); + if (c == 1) { + raid2 = new Raid(war, raidedTown, gatherTown, true); + } else if (c == 0) { + p.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!"); + return; + } else if (c == 2) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); + return; + } else { + throw new IllegalArgumentException(); + } + } else { + //Something broke if this runs + if (!war.getSide2Players().contains(p.getName())) { + Bukkit.getLogger().info("Unable to find player declaring raid in the war"); + return; + } + //Time and raid activity validity check + int c = RaidData.isValidRaid(war, raidedTown); + if (c == 1) { + raid2 = new Raid(war, raidedTown, gatherTown, false); + } else if (c == 0) { + p.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!"); + return; + } else if (c == 2) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This town is already being raided at this time!"); + 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()); + } + + 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!"); + 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."); + return; + } + + //publish raid + Main.econ.withdrawPlayer(offlinePlayer, 1000); + RaidData.addRaid(raid2); + raid2.start(); //funny funny method haha + WarData.saveWar(raid2.getWar()); + + //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() + "!"); + Main.warLogger.log(p.getName() + " started a raid."); + Main.warLogger.log("As part of " + war.getName() + ", forces from " + raid2.getRaiders() + " are raiding the town of " + raidedTown.getName() + "!"); } } - if (!warFound) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "That war does not exist! /raid start [war] [town]"); - return false; - } - if (!townExists) { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "That town does not exist! /raid start [war] [town]"); - } - } else { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); } } - else { - p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Invalid Arguments. /raid help"); - }*/ - return false; - + if (!warFound) { + p.sendMessage(String.valueOf(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]"); + } + } + + /** + * Stops the current raid by admins + * + * @param p + * @param args + */ + private static void stopRaid(Player p, String[] args) { + //admin check + if (!p.hasPermission("AlathraWar.admin")) { + fail(p, args, "permission"); + return; + } + + //are there even any raids + if (RaidData.getRaids().isEmpty()) { + fail(p, args, "noRaids"); + return; + } + boolean found = false; + + //find our desired raid and stop it + Raid raid = RaidData.getRaidOrNull(args[1]); + if (raid != null) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "raid cancelled"); + Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The raid of " + raid.getRaidedTown().getName() + " has been cancelled by an admin"); + raid.stop(); //this purges itself from raids + } else { + fail(p, args, "badName"); + } + } + + private static void joinRaid(Player p, String[] args) { + //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]"); + } + + //check if gather phase + if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START) { + try { + //check if the player is in the gather town + ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); + if (cluster.contains(WorldCoord.parseWorldCoord(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."); + } else { + p.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!"); + } + } + } 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) { + //Get raid! + Raid raid = RaidData.getRaidOrNull(args[1] + "-" + args[2]); + if (raid != null) { + if (raid.getWar().getName().equalsIgnoreCase(args[1])) { + if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); + } + + //check if gather phase + if (raid.getPhase() == RaidPhase.GATHER || raid.getPhase() == RaidPhase.START) { + try { + //check if player is in gather town + ArrayList cluster = Helper.getCluster(raid.getGatherTown().getHomeBlock().getWorldCoord()); + if (cluster.contains(WorldCoord.parseWorldCoord(p))) { + raid.removeActiveRaider(p.getName()); + p.sendMessage(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!"); + } + } catch (TownyException e) { + throw new RuntimeException(e); + } + } + } + } else { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "No raid is gathering in this town or this town does not exist!"); + } + } + + /** + * Abandons the raid + * + * @param p + * @param args + */ + private static void abandonRaid(Player p, String[] args) { + //Get raid + Raid raid = RaidData.getRaidOrNull(args[1] + "-" + args[2]); + if (raid != null) { + if (raid.getWar().getName().equalsIgnoreCase(args[1])) { + if (!raid.getWar().getSide1Players().contains(p.getName()) && !raid.getWar().getSide2Players().contains(p.getName())) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are not in this war! Type /war join [war] [side]"); + } + + 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()); + } + } else { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You cannot abandon the raid on " + raid.getRaidedTown().getName() + "! It has a problem!"); + } + } else { + fail(p, args, "owner"); + } + + } + } else { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "This raid does not exist!"); + } + } + + private static void listRaids(Player p, String[] args) { + if (RaidData.getRaids().isEmpty()) { + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "There are currently no Raids in progress"); + return; + } + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "Raids currently in progress:"); + for (final Raid raid : RaidData.getRaids()) { + p.sendMessage("Name: " + raid.getName()); + p.sendMessage("War: " + raid.getWar().getName()); + p.sendMessage("Owner: " + raid.getOwner().getName()); + p.sendMessage("Raiding: " + raid.getRaidedTown().getName()); + p.sendMessage("Gathering In: " + raid.getGatherTown().getName()); + p.sendMessage("Raiders: " + raid.getRaiders()); + p.sendMessage("Defenders: " + raid.getDefenders()); + p.sendMessage("Raid Score: " + String.valueOf(raid.getRaidScore())); + p.sendMessage("Raid Phase: " + raid.getPhase().name()); + p.sendMessage("Time Left: " + String.valueOf((RaidPhase.END.startTick - raid.getRaidTicks()) / 1200) + " minutes"); + p.sendMessage("-=-=-=-=-=-=-=-=-=-=-=-"); + } + } + + private static void raidHelp(Player p, String[] args) { + if (p.hasPermission("!AlathraWar.admin")) { + p.sendMessage(Helper.Chatlabel() + "/raid stop [name]"); + } + p.sendMessage(Helper.Chatlabel() + "/raid start [war] [town]"); + p.sendMessage(Helper.Chatlabel() + "/raid join [war] [town]"); + p.sendMessage(Helper.Chatlabel() + "/raid leave [war] [town]"); + p.sendMessage(Helper.Chatlabel() + "/raid abandon [war] [town]"); + p.sendMessage(Helper.Chatlabel() + "/raid list"); + } + + private static void fail(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"); + + } + } } } diff --git a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidTabCompletion.java b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidTabCompletion.java index 1262a95..d1a143b 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidTabCompletion.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/commands/RaidTabCompletion.java @@ -4,6 +4,8 @@ import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.object.Town; import me.ShermansWorld.AlathraWar.Raid; import me.ShermansWorld.AlathraWar.War; +import me.ShermansWorld.AlathraWar.data.RaidData; +import me.ShermansWorld.AlathraWar.data.WarData; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -28,19 +30,42 @@ public class RaidTabCompletion implements TabCompleter { completions.add("abandon"); completions.add("list"); completions.add("help"); + completions.add("join"); + completions.add("leave"); return completions; } else if (args.length == 2) { if (args[0].equalsIgnoreCase("start")) { - if (!WarCommands.wars.isEmpty()) { - for (War war : WarCommands.wars) { + if (!WarData.getWars().isEmpty()) { + for (War war : WarData.getWars()) { completions.add(war.getName()); } return completions; } - } else if (args[0].equalsIgnoreCase("stop") || args[0].equalsIgnoreCase("abandon")) { - if (!RaidCommands.raids.isEmpty()) { - for (Raid raid : RaidCommands.raids) { - completions.add(String.valueOf(raid.getID())); + } else if (args[0].equalsIgnoreCase("stop")) { + if (!RaidData.getRaids().isEmpty()) { + for (Raid raid : RaidData.getRaids()) { + completions.add(String.valueOf(raid.getName())); + } + return completions; + } + } else if (args[0].equalsIgnoreCase("join")) { + if (!WarData.getWars().isEmpty()) { + for (War war : WarData.getWars()) { + completions.add(war.getName()); + } + return completions; + } + } else if (args[0].equalsIgnoreCase("abandon")) { + if (!WarData.getWars().isEmpty()) { + for (War war : WarData.getWars()) { + completions.add(war.getName()); + } + return completions; + } + } else if (args[0].equalsIgnoreCase("leave")) { + if (!WarData.getWars().isEmpty()) { + for (War war : WarData.getWars()) { + completions.add(war.getName()); } return completions; } @@ -53,6 +78,27 @@ public class RaidTabCompletion implements TabCompleter { } return completions; } + } else if (args[0].equalsIgnoreCase("join")) { + if (!TownyAPI.getInstance().getTowns().isEmpty()) { + for (Town town : TownyAPI.getInstance().getTowns()) { + completions.add(town.getName()); + } + return completions; + } + } else if (args[0].equalsIgnoreCase("abandon")) { + if (!TownyAPI.getInstance().getTowns().isEmpty()) { + for (Town town : TownyAPI.getInstance().getTowns()) { + completions.add(town.getName()); + } + return completions; + } + } else if (args[0].equalsIgnoreCase("leave")) { + if (!TownyAPI.getInstance().getTowns().isEmpty()) { + for (Town town : TownyAPI.getInstance().getTowns()) { + completions.add(town.getName()); + } + return completions; + } } } */ diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java index 4866a3a..180fc42 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidData.java @@ -1,64 +1,244 @@ package me.ShermansWorld.AlathraWar.data; +import com.palmergames.bukkit.towny.TownyAPI; +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.configuration.Configuration; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.*; +import java.io.File; +import java.io.FilenameFilter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; public class RaidData { - private Main plugin; - private FileConfiguration dataConfig; - private File configFile; + // Static Raid list for all active raids + private static ArrayList raids = new ArrayList(); + private final static String dataFolderPath = "plugins" + File.separator + "AlathraWar" + File.separator + "data"; - public RaidData(final Main plugin) { - this.dataConfig = null; - this.configFile = null; - this.plugin = plugin; - this.saveDefaultConfig(); + // Filter for only accessing yml files + private static FilenameFilter ymlFilter = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith(".yml"); + } + }; + + public static ArrayList getRaids() { + return raids; } - public void reloadConfig() { - if (this.configFile == null) { - this.configFile = new File(this.plugin.getDataFolder(), "raids.yml"); + /** + * Gets a raid with a specific name + * + * @param name - Name to check + * @return Raid or Null + */ + public static Raid getRaidOrNull(String name) { + for (Raid raid : raids) { + if (raid.getName().equals(name)) return raid; } - this.dataConfig = (FileConfiguration) YamlConfiguration.loadConfiguration(this.configFile); - final InputStream defaultStream = this.plugin.getResource("raids.yml"); - if (defaultStream != null) { - final YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration((Reader)new InputStreamReader(defaultStream)); - this.dataConfig.setDefaults((Configuration)defaultConfig); + return null; + } + + public static void setRaids(ArrayList raids) { + for (Raid raid : raids) { + addRaid(raid); } } - public FileConfiguration getConfig() { - if (this.dataConfig == null) { - this.reloadConfig(); - } - return this.dataConfig; - } - - public void saveConfig() { - if (this.dataConfig == null || this.configFile == null) { + public static void addRaid(Raid raid) { + if (raid == null) { + Main.warLogger.log("Attempted to add NULL to raid list."); return; } - try { - this.getConfig().save(this.configFile); - } - catch (IOException e) { - e.printStackTrace(); - } + raids.add(raid); } - public void saveDefaultConfig() { - if (this.configFile == null) { - this.configFile = new File(this.plugin.getDataFolder(), "raids.yml"); + public static void removeRaid(Raid raid) { + raid.getWar().getRaids().remove(raid); + raids.remove(raid); + + //deleteRaid(raid); + } + + /** + * Creates a raid object from a provided HashMap + * + * @param fileData + * @return Raid object + */ + @SuppressWarnings("unchecked") + public static Raid fromMap(War war, HashMap fileData) { + + if (fileData.get("raidedTown") == null || fileData.get("gatherTown") == null || fileData.get("side1AreRaiders") == null) { + return null; } - if (!this.configFile.exists()) { - this.plugin.saveResource("raids.yml", false); + + 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")); + + Raid raid = new Raid(war, raidedTown, gatherTown, attackBoolean); + + //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.setActiveRaiders((ArrayList) fileData.get("activeRaiders")); + + //looted chunks + HashMap o = (HashMap) fileData.get("lootedChunks"); + for (String s : o.keySet()) { + //grab the chunk + HashMap chunk = (HashMap) o.get(s); + + //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"))); + int ticks = (int) chunk.get("ticks"); + double value = (double) chunk.get("value"); + boolean finished = (boolean) chunk.get("finished"); + + //put + Raid.LootBlock lb = new Raid.LootBlock(worldCoordc, ticks, value, finished); + raid.getLootedChunks().put(worldCoordc, lb); + } } + + return raid; + } + + + /** + * Saves the war into files. + * + * @param raid - War to be saved. + */ + public static void saveRaid(Raid raid) { + War war = raid.getWar(); + war.save(); + } + + /** + * deletes raid, unused + */ +// private static void deleteRaid(War war) { +// File[] files = new File(dataFolderPath + File.separator + "wars").listFiles(ymlFilter); +// +// for (File file : files) { +// if (file.getName().startsWith(war.getName())) { +// file.delete(); +// } +// } +// } + + /** + * Turns a raid into a map + * + * @param raid - Raid + * @return Map + */ + public static HashMap raidToMap(Raid raid) { + 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("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()); + + //create a list from the looted chunks + List chunkList = new ArrayList(); + for (Raid.LootBlock b : raid.getLootedChunks().values()) { + HashMap blockMap = new HashMap(); + //worldcoord + HashMap wcMap = new HashMap(); + wcMap.put("world", b.worldCoord.getWorldName()); + wcMap.put("x", b.worldCoord.getCoord().getX()); + wcMap.put("z", b.worldCoord.getCoord().getZ()); + blockMap.put("worldCoord", wcMap); + //properties + blockMap.put("ticks", b.ticks); + blockMap.put("value", b.value); + blockMap.put("finished", b.finished); + chunkList.add(blockMap); + } + + returnMap.put("lootedChunks", chunkList); + + return returnMap; + } + + /** + * Creates a map of raid maps + * + * @param war - War to map + * @return Map of Maps + */ + public static HashMap getRaidMap(War war) { + HashMap returnMap = new HashMap(); + for (Raid raid : war.getRaids()) { + returnMap.put(raid.getRaidedTown().getName(), raidToMap(raid)); + } + return returnMap; + } + + /** + * @return + * @Isaac this is for getting when the last raid was on a town + */ + public static long whenTownLastRaided(Town town) { + if (town.hasMeta("lastRaided")) { + CustomDataField field = town.getMetadata("lastRaided"); + if (field != null) { + if (field instanceof IntegerDataField) { + return ((IntegerDataField) field).getValue(); + } + } + return -1L; + } else { + town.addMetaData(new LongDataField("lastRaided", 0L)); + } + return -1L; + } + + /** + * @return + * @Isaac this is for getting if a town can be raided, return (-1, 0, 1, 2) based on status + * (24 hours town cooldown, 6 hour war cooldown, valid time to raid, town being raided already) + */ + public static int isValidRaid(War war, Town town) { + long townTime = whenTownLastRaided(town); + + //24 hours town cooldown + if ((System.currentTimeMillis() / 1000) - townTime <= 86400) { + return -1; + } + + //6 hour raid cooldown for war + if ((System.currentTimeMillis() / 1000) - war.getLastRaidTime() <= 21600) { + return 0; + } + + //check if being raided + for (Raid raid : war.getRaids()) { + if (raid.getRaidedTown().getName().equals(town.getName())) { + return 2; + } + } + + return 1; } public static Object getRaidMap(War war) { diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/RaidPhase.java b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidPhase.java new file mode 100644 index 0000000..391b8fe --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/RaidPhase.java @@ -0,0 +1,47 @@ +package me.ShermansWorld.AlathraWar.data; + +public enum RaidPhase { + START(0, 0), + GATHER(1, 1), + TRAVEL(2, 18000), + COMBAT(3, 24000), + END(4, 72000); + + //Start phase starts when the command to start a raid is ran, it allows the raiders to join, and for people to get geared up + //after 15 minutes travel phase begins, where the raiders start travelling and moving to the new town. + //no killing can occur during these two phases. + //Once at least one player has stepped into the defending town, or 5 min have passed during the travel phase, + //the combat begins and lasts 40 minutes. + + public final int id; + public final int startTick; + + private RaidPhase(int id, int startTick) { + this.id = id; + this.startTick = startTick; + } + + /** + * Grabs the phase by name, ignores case + * @param name + * @return + */ + public static RaidPhase getByName(String name) { + for(RaidPhase p : RaidPhase.values()) { + if(p.name().equalsIgnoreCase(name)) return p; + } + return null; + } + + /** + * Grabs phase by id number for easy storage reading + * @param id + * @return + */ + public static RaidPhase getByID(int id) { + for(RaidPhase p : RaidPhase.values()) { + if(p.id == id) return p; + } + 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 ba44d5d..977e109 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/SiegeData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/SiegeData.java @@ -85,7 +85,8 @@ public class SiegeData /** * Saves the war into files. - * @param war - War to be saved. + + * @param siege - Siege to be saved. */ public static void saveSiege(Siege siege) { War war = siege.getWar(); diff --git a/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java b/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java index f083f68..6788bb1 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/data/WarData.java @@ -98,6 +98,7 @@ public class WarData war.setSide1Towns((ArrayList) fileData.get("side1Towns")); war.setSide2Towns((ArrayList) fileData.get("side2Towns")); + war.setLastRaidTime((Long) fileData.get("lastRaidTime")); return war; } @@ -119,6 +120,7 @@ public class WarData sHashMap.put("sieges", SiegeData.getSiegeMap(war)); sHashMap.put("raids", RaidData.getRaidMap(war)); + sHashMap.put("lastRaidTime", war.getLastRaidTime()); DataManager.saveData("wars" + File.separator + war.getName() + ".yml", sHashMap); diff --git a/src/main/java/me/ShermansWorld/AlathraWar/listeners/CommandsListener.java b/src/main/java/me/ShermansWorld/AlathraWar/listeners/CommandsListener.java new file mode 100644 index 0000000..eac3f88 --- /dev/null +++ b/src/main/java/me/ShermansWorld/AlathraWar/listeners/CommandsListener.java @@ -0,0 +1,155 @@ +package me.ShermansWorld.AlathraWar.listeners; + +import me.ShermansWorld.AlathraWar.Helper; +import me.ShermansWorld.AlathraWar.Raid; +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.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +public class CommandsListener implements Listener { + + final static String[] prefixes = new String[]{ + "", "towny", "essentials", "wild", "minecraft" + }; + final static String[] blacklistedLong = new String[]{ + "n spawn", "nat spawn", "nation spawn", + "t spawn", "town spawn" + }; + final static String[] blacklistedShort = new String[]{ + "home", "homes", "warp", "warps", + "wild", "rtp", "spawn", "wilderness", "wildtp", + "tpa", "tpahere", "tpaccept", "tpacancel", + "etpa", "etpahere", "etpaccept", "etpacancel", + "ehome", "ehomes", "ewarp", "ewarps" + + }; + + + @EventHandler(priority = EventPriority.HIGH) + public void onCommandSend(final PlayerCommandPreprocessEvent event) { + + Player p = event.getPlayer(); + + //if player is admin, ignore this behavior + if (p.hasPermission("!AlathraWar.admin")) return; + + String[] args = event.getMessage().split(" "); + /* + Prevent any teleport command events from occuring + If using PlayerTeleportEvent, it breaks the death logic + + allow n spawn and t spawn for defenders trapped in spawn + */ + for (War war : WarData.getWars()) { + for (Raid raid : war.getRaids()) { + //Only do this during regular phases, start and end ignored in case something breaks + 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())) { + //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) { + for (String cmd : blacklistedLong) { + //check for each prefix + if (parse.equals(prefix + ":" + 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."); + return; + } + p.sendMessage(String.valueOf(Helper.Chatlabel()) + "You are stuck in spawn and are allowed to teleport to your town or nation."); + event.setCancelled(true); + return; + } + + //else + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("c") + "You cannot teleport whilst in a raid!"); + event.setCancelled(true); + return; + } + } + } + } else 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]; + for (String prefix : prefixes) { + for (String cmd : blacklistedLong) { + //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!"); + event.setCancelled(true); + return; + } + } + } + } + } + } + } + + /* + Prevent players from teleporting during a siege + */ + for (Siege siege : war.getSieges()) { + //Only do this during regular phases, start and end ignored in case something breaks + if (siege.getSiegeTicks() > 0) { + //only do for active raiders and any defenders + if (siege.getAttackerPlayers().contains(p.getName()) || siege.getDefenders().contains(p.getName())) { + //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) { + for (String cmd : blacklistedLong) { + //check for each prefix + if (parse.equals(prefix + ":" + 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!"); + return; + } + } + + //else + p.sendMessage(String.valueOf(Helper.Chatlabel()) + Helper.color("c") + "You cannot teleport whilst in a raid!"); + event.setCancelled(true); + return; + } + } + } + } else 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]; + for (String prefix : prefixes) { + for (String cmd : blacklistedLong) { + //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!"); + 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 337e336..86589ff 100644 --- a/src/main/java/me/ShermansWorld/AlathraWar/listeners/KillsListener.java +++ b/src/main/java/me/ShermansWorld/AlathraWar/listeners/KillsListener.java @@ -1,5 +1,9 @@ package me.ShermansWorld.AlathraWar.listeners; +import me.ShermansWorld.AlathraWar.Raid; +import me.ShermansWorld.AlathraWar.commands.RaidCommands; +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; @@ -37,17 +41,18 @@ public final class KillsListener implements Listener town = WorldCoord.parseWorldCoord((Entity)killed).getTownBlock().getTown(); } catch (NotRegisteredException ex2) {} - boolean playerCloseToHomeBlock = false; + boolean playerCloseToHomeBlockSiege = false; + boolean playerCloseToHomeBlockRaid = false; try { for (final Siege siege : SiegeData.getSieges()) { final int homeBlockXCoord = siege.getTown().getHomeBlock().getCoord().getX() * 16; final int homeBlockZCoord = siege.getTown().getHomeBlock().getCoord().getZ() * 16; if (Math.abs(killed.getLocation().getBlockX() - homeBlockXCoord) <= 300 && Math.abs(killed.getLocation().getBlockZ() - homeBlockZCoord) <= 300) { - playerCloseToHomeBlock = true; + playerCloseToHomeBlockSiege = true; } - if (siege.getAttackerPlayers().contains(killer.getName()) && ((town != null && town.equals(siege.getTown())) || playerCloseToHomeBlock)) { - if (siege.getDefenderPlayers().contains(killed.getName())) { - siege.addPointsToAttackers(20); + if (siege.getAttackerPlayers().contains(killer.getName()) && ((town != null && town.equals(siege.getTown())) || playerCloseToHomeBlockSiege)) { + if (siege.getDefenderPlayers().contains(killed.getName())) { + siege.addPointsToAttackers(20); for (final String playerName : siege.getAttackerPlayers()) { try { Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Defender killed! + 20 Attacker Points"); @@ -60,13 +65,13 @@ public final class KillsListener implements Listener } catch (NullPointerException ex6) {} } - } + } this.siegeKill(killed, event); return; } - if (siege.getDefenderPlayers().contains(killer.getName()) && ((town != null && town.equals(siege.getTown())) || playerCloseToHomeBlock)) { - if (siege.getAttackerPlayers().contains(killed.getName())) { - siege.addPointsToDefenders(20); + if (siege.getDefenderPlayers().contains(killer.getName()) && ((town != null && town.equals(siege.getTown())) || playerCloseToHomeBlockSiege)) { + if (siege.getAttackerPlayers().contains(killed.getName())) { + siege.addPointsToDefenders(20); for (final String playerName : siege.getAttackerPlayers()) { try { Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "Attacker killed! + 20 Defender Points"); @@ -79,12 +84,52 @@ public final class KillsListener implements Listener } catch (NullPointerException ex4) {} } - } - this.siegeKill(killed, event); + } + this.siegeKill(killed, event); return; } - if ( (town != null && town.equals(siege.getTown()) || playerCloseToHomeBlock) ) { - this.siegeKill(killed, event); + if ( (town != null && town.equals(siege.getTown()) || playerCloseToHomeBlockSiege) ) { + this.siegeKill(killed, event); + } + } + + + //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; + 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)) { + + if(raid.getPhase() == RaidPhase.COMBAT) { + if (raid.getDefenderPlayers().contains(killed.getName())) { + raid.defenderKilledInCombat(event); + } + this.siegeKill(killed, event); + return; + } else { + raid.defenderKilledOutofCombat(event); + } + } + if (raid.getDefenderPlayers().contains(killer.getName()) && ((town != null && town.equals(raid.getRaidedTown())) || playerCloseToHomeBlockRaid)) { + + if(raid.getPhase() == RaidPhase.COMBAT) { + if (raid.getActiveRaiders().contains(killed.getName())) { + raid.raiderKilledInCombat(event); + } + this.siegeKill(killed, event); + return; + } else { + raid.raiderKilledOutofCombat(event); + } + + } + if ( (town != null && town.equals(raid.getRaidedTown()) || playerCloseToHomeBlockRaid) ) { + this.siegeKill(killed, event); } } } @@ -92,120 +137,18 @@ public final class KillsListener implements Listener } } - + + /** + * Damage all gear held by the player (and then send them to spawn?) + * They don't lose items from death. + * @param killed + * @param event + */ private void siegeKill(final Player killed, final PlayerDeathEvent event) { - final Inventory inv = (Inventory)killed.getInventory(); - for (int i = 0; i < inv.getSize(); ++i) { - final ItemStack is = inv.getItem(i); - if (is != null && (is.getType() == Material.NETHERITE_AXE || is.getType() == Material.NETHERITE_SWORD || is.getType() == Material.DIAMOND_AXE || is.getType() == Material.DIAMOND_SWORD || is.getType() == Material.IRON_AXE || is.getType() == Material.IRON_SWORD)) { - ItemMeta meta = is.getItemMeta(); - final Damageable damageable = (Damageable)meta; - if (is.getType() == Material.NETHERITE_SWORD || is.getType() == Material.NETHERITE_AXE) { - damageable.setDamage(damageable.getDamage() + 507); - if (damageable.getDamage() > 2000) { - damageable.setDamage(2030); - } - } - else if (is.getType() == Material.DIAMOND_SWORD || is.getType() == Material.DIAMOND_AXE) { - damageable.setDamage(damageable.getDamage() + 390); - if (damageable.getDamage() > 1500) { - damageable.setDamage(1560); - } - } - else if (is.getType() == Material.IRON_SWORD || is.getType() == Material.IRON_AXE) { - damageable.setDamage(damageable.getDamage() + 62); - if (damageable.getDamage() > 200) { - damageable.setDamage(249); - } - } - meta = (ItemMeta)damageable; - is.setItemMeta(meta); - } - } - final ItemStack[] armor = killed.getInventory().getArmorContents(); - for (int j = 0; j < armor.length; ++j) { - if (armor[j] != null) { - final ItemStack is2 = armor[j]; - if (is2.getType() == Material.NETHERITE_HELMET || is2.getType() == Material.NETHERITE_CHESTPLATE || is2.getType() == Material.NETHERITE_LEGGINGS || is2.getType() == Material.NETHERITE_BOOTS || is2.getType() == Material.DIAMOND_HELMET || is2.getType() == Material.DIAMOND_CHESTPLATE || is2.getType() == Material.DIAMOND_LEGGINGS || is2.getType() == Material.NETHERITE_BOOTS || is2.getType() == Material.IRON_HELMET || is2.getType() == Material.IRON_CHESTPLATE || is2.getType() == Material.IRON_LEGGINGS || is2.getType() == Material.IRON_BOOTS) { - ItemMeta meta2 = is2.getItemMeta(); - final Damageable damageable2 = (Damageable)meta2; - if (is2.getType() == Material.NETHERITE_HELMET) { - damageable2.setDamage(damageable2.getDamage() + 101); - if (damageable2.getDamage() > 400) { - damageable2.setDamage(406); - } - } - if (is2.getType() == Material.NETHERITE_CHESTPLATE) { - damageable2.setDamage(damageable2.getDamage() + 148); - if (damageable2.getDamage() > 450) { - damageable2.setDamage(591); - } - } - if (is2.getType() == Material.NETHERITE_LEGGINGS) { - damageable2.setDamage(damageable2.getDamage() + 138); - if (damageable2.getDamage() > 420) { - damageable2.setDamage(554); - } - } - if (is2.getType() == Material.NETHERITE_BOOTS) { - damageable2.setDamage(damageable2.getDamage() + 120); - if (damageable2.getDamage() > 370) { - damageable2.setDamage(480); - } - } - if (is2.getType() == Material.DIAMOND_HELMET) { - damageable2.setDamage(damageable2.getDamage() + 90); - if (damageable2.getDamage() > 280) { - damageable2.setDamage(362); - } - } - if (is2.getType() == Material.DIAMOND_CHESTPLATE) { - damageable2.setDamage(damageable2.getDamage() + 132); - if (damageable2.getDamage() > 410) { - damageable2.setDamage(527); - } - } - if (is2.getType() == Material.DIAMOND_LEGGINGS) { - damageable2.setDamage(damageable2.getDamage() + 123); - if (damageable2.getDamage() > 380) { - damageable2.setDamage(494); - } - } - if (is2.getType() == Material.DIAMOND_BOOTS) { - damageable2.setDamage(damageable2.getDamage() + 107); - if (damageable2.getDamage() > 330) { - damageable2.setDamage(428); - } - } - if (is2.getType() == Material.IRON_HELMET) { - damageable2.setDamage(damageable2.getDamage() + 41); - if (damageable2.getDamage() > 125) { - damageable2.setDamage(164); - } - } - if (is2.getType() == Material.IRON_CHESTPLATE) { - damageable2.setDamage(damageable2.getDamage() + 60); - if (damageable2.getDamage() > 190) { - damageable2.setDamage(239); - } - } - if (is2.getType() == Material.IRON_LEGGINGS) { - damageable2.setDamage(damageable2.getDamage() + 56); - if (damageable2.getDamage() > 180) { - damageable2.setDamage(224); - } - } - if (is2.getType() == Material.IRON_BOOTS) { - damageable2.setDamage(damageable2.getDamage() + 48); - if (damageable2.getDamage() > 150) { - damageable2.setDamage(194); - } - } - meta2 = (ItemMeta)damageable2; - is2.setItemMeta(meta2); - } - } - } + //Helper + Helper.damageAllGear(killed); + + //Siege specific Bukkit.dispatchCommand((CommandSender)Bukkit.getConsoleSender(), "spawn " + killed.getName()); event.setKeepInventory(true); event.getDrops().clear();