From 22762bf5218070d5a68d5fab8d747dd97d8076fa Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 1 Jul 2013 19:29:38 +0200 Subject: [PATCH] Fix setup issue and remove region expansion This commit consists of two components: - Restarts are no longer required after setting up the first arena - Regions no longer auto-expand The restart requirement is fixed by preventing the ArenaMaster from actually creating an Arena object and assigning it to the selected arena upon generating the default arena node in the config-file. The problem was that the selected arena was set to this initial Arena object, but another Arena object was created afterwards, which due to the equals() method of ArenaImpl overwrote the previously created Arena object in the arenas-list, but didn't change the reference in the selected arena. In other words; two identical Arena objects were created, but used in different places (arena list vs. selected arena). The auto-expansion is removed by simply never auto-expanding regions upon setting the arena warp or spawnpoints. Instead, setting these warps throws an error, if p1 and p2 have not yet been set, or if the location is outside the region. --- .../garbagemule/MobArena/ArenaMasterImpl.java | 8 +- .../commands/setup/AddSpawnpointCommand.java | 14 ++- .../setup/RemoveContainerCommand.java | 2 +- .../commands/setup/SetWarpCommand.java | 15 ++- .../MobArena/region/ArenaRegion.java | 111 ++++++------------ 5 files changed, 68 insertions(+), 82 deletions(-) diff --git a/src/com/garbagemule/MobArena/ArenaMasterImpl.java b/src/com/garbagemule/MobArena/ArenaMasterImpl.java index 9e88b68..159b36a 100644 --- a/src/com/garbagemule/MobArena/ArenaMasterImpl.java +++ b/src/com/garbagemule/MobArena/ArenaMasterImpl.java @@ -488,7 +488,7 @@ public class ArenaMasterImpl implements ArenaMaster // If no arenas were found, create a default node. if (arenanames == null || arenanames.isEmpty()) { - createArenaNode("default", plugin.getServer().getWorlds().get(0)); + createArenaNode("default", plugin.getServer().getWorlds().get(0), false); } arenas = new LinkedList(); @@ -571,6 +571,10 @@ public class ArenaMasterImpl implements ArenaMaster } public Arena createArenaNode(String arenaName, World world) { + return createArenaNode(arenaName, world, true); + } + + private Arena createArenaNode(String arenaName, World world, boolean load) { String path = "arenas." + arenaName; if (config.getConfigSection(path) != null) throw new IllegalArgumentException("Arena already exists!"); @@ -589,7 +593,7 @@ public class ArenaMasterImpl implements ArenaMaster config.save(); // Load the arena - return loadArena(arenaName); + return (load ? loadArena(arenaName) : null); } public void removeArenaNode(Arena arena) { diff --git a/src/com/garbagemule/MobArena/commands/setup/AddSpawnpointCommand.java b/src/com/garbagemule/MobArena/commands/setup/AddSpawnpointCommand.java index d771207..8cb4a87 100644 --- a/src/com/garbagemule/MobArena/commands/setup/AddSpawnpointCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/AddSpawnpointCommand.java @@ -34,8 +34,18 @@ public class AddSpawnpointCommand implements Command return true; } - am.getSelectedArena().getRegion().addSpawn(arg1, p.getLocation()); - Messenger.tellPlayer(sender, "Spawnpoint " + arg1 + " added for arena \"" + am.getSelectedArena().configName() + "\""); + // Make sure we're inside the region + if (am.getSelectedArena().getRegion().contains(p.getLocation())) { + am.getSelectedArena().getRegion().addSpawn(arg1, p.getLocation()); + Messenger.tellPlayer(sender, "Spawnpoint " + arg1 + " added for arena \"" + am.getSelectedArena().configName() + "\""); + } else { + // If not, make sure the region is defined + if (am.getSelectedArena().getRegion().isDefined()) { + Messenger.tellPlayer(sender, "You must be inside the arena region!"); + } else { + Messenger.tellPlayer(sender, "You must first set the region points p1 and p2"); + } + } return true; } } diff --git a/src/com/garbagemule/MobArena/commands/setup/RemoveContainerCommand.java b/src/com/garbagemule/MobArena/commands/setup/RemoveContainerCommand.java index f5508b2..c67341f 100644 --- a/src/com/garbagemule/MobArena/commands/setup/RemoveContainerCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/RemoveContainerCommand.java @@ -26,7 +26,7 @@ public class RemoveContainerCommand implements Command return false; } - if (am.getSelectedArena().getRegion().removeSpawn(arg1)) + if (am.getSelectedArena().getRegion().removeChest(arg1)) Messenger.tellPlayer(sender, "Container " + arg1 + " removed for arena '" + am.getSelectedArena().configName() + "'"); else Messenger.tellPlayer(sender, "Could not find the container " + arg1 + "for the arena '" + am.getSelectedArena().configName() + "'"); diff --git a/src/com/garbagemule/MobArena/commands/setup/SetWarpCommand.java b/src/com/garbagemule/MobArena/commands/setup/SetWarpCommand.java index 49933bd..ece24f3 100644 --- a/src/com/garbagemule/MobArena/commands/setup/SetWarpCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/SetWarpCommand.java @@ -34,9 +34,18 @@ public class SetWarpCommand implements Command return true; } - am.getSelectedArena().getRegion().set(arg1, p.getLocation()); - Messenger.tellPlayer(sender, "Warp point " + arg1 + " was set for arena '" + am.getSelectedArena().configName() + "'"); - Messenger.tellPlayer(sender, "Type /ma checkdata to see if you're missing anything..."); + // Make sure the arena warp is inside the region + if (arg1.equals("arena") && !am.getSelectedArena().getRegion().contains(p.getLocation())) { + if (am.getSelectedArena().getRegion().isDefined()) { + Messenger.tellPlayer(sender, "You must be inside the arena region!"); + } else { + Messenger.tellPlayer(sender, "You must first set the region points p1 and p2"); + } + } else { + am.getSelectedArena().getRegion().set(arg1, p.getLocation()); + Messenger.tellPlayer(sender, "Warp point " + arg1 + " was set for arena '" + am.getSelectedArena().configName() + "'"); + Messenger.tellPlayer(sender, "Type /ma checkdata to see if you're missing anything..."); + } return true; } } diff --git a/src/com/garbagemule/MobArena/region/ArenaRegion.java b/src/com/garbagemule/MobArena/region/ArenaRegion.java index e719aa8..4881372 100644 --- a/src/com/garbagemule/MobArena/region/ArenaRegion.java +++ b/src/com/garbagemule/MobArena/region/ArenaRegion.java @@ -44,7 +44,6 @@ public class ArenaRegion this.chests = coords.getConfigSection("containers"); reloadAll(); - adjustRegion(); } public void reloadAll() { @@ -60,9 +59,11 @@ public class ArenaRegion public void reloadRegion() { p1 = coords.getLocation("p1", world); p2 = coords.getLocation("p2", world); + fixRegion(); l1 = coords.getLocation("l1", world); l2 = coords.getLocation("l2", world); + fixLobbyRegion(); } public void reloadWarps() { @@ -149,7 +150,7 @@ public class ArenaRegion } public boolean contains(Location l) { - if (!l.getWorld().getName().equals(world.getName()) || !setup) { + if (!l.getWorld().getName().equals(world.getName()) || !isDefined()) { return false; } @@ -172,7 +173,7 @@ public class ArenaRegion } public boolean contains(Location l, int radius) { - if (!l.getWorld().getName().equals(world.getName()) || !setup) { + if (!l.getWorld().getName().equals(world.getName()) || !isDefined()) { return false; } @@ -264,88 +265,42 @@ public class ArenaRegion return; } + boolean modified = false; + if (loc1.getX() > loc2.getX()) { double tmp = loc1.getX(); loc1.setX(loc2.getX()); loc2.setX(tmp); + modified = true; } if (loc1.getZ() > loc2.getZ()) { double tmp = loc1.getZ(); loc1.setZ(loc2.getZ()); loc2.setZ(tmp); + modified = true; } if (loc1.getY() > loc2.getY()) { double tmp = loc1.getY(); loc1.setY(loc2.getY()); loc2.setY(tmp); + modified = true; } - if (!arena.getWorld().getName().equals(world.getName())) + if (!arena.getWorld().getName().equals(world.getName())) { arena.setWorld(world); + modified = true; + } + if (!modified) { + return; + } + coords.set(location1, loc1); coords.set(location2, loc2); } - private void adjustRegion() { - if (!setup) { - return; - } - - // Make sure the arena warp is inside the region. - readjustRegion(arenaWarp); - - // Re-adjust for all spawnpoints and containers. - for (Location spawnpoint : spawnpoints.values()) { - readjustRegion(spawnpoint); - } - - for (Location chest : containers.values()) { - readjustRegion(chest); - } - } - - private void readjustRegion(Location l) { - if (p1 == null || p2 == null) { - return; - } - - int x = l.getBlockX(); - int y = l.getBlockY(); - int z = l.getBlockZ(); - - int p1x = p1.getBlockX(); - int p1y = p1.getBlockY(); - int p1z = p1.getBlockZ(); - - int p2x = p2.getBlockX(); - int p2y = p2.getBlockY(); - int p2z = p2.getBlockZ(); - - if (x <= p1x) { - expandP1(p1x - x + 2, 0); - } - else if (x >= p2x) { - expandP2(x - p2x + 2, 0); - } - - if (y <= p1y) { - expandDown((int) (p1y - y + 2)); - } - else if (y >= p2y) { - expandUp((int) (y - p2y + 2)); - } - - if (z <= p1z) { - expandP1(0, p1z - z + 2); - } - else if (z >= p2z) { - expandP2(0, z - p2z + 2); - } - } - public List getChunks() { List result = new ArrayList(); @@ -398,56 +353,64 @@ public class ArenaRegion } public void set(String point, Location loc) { + // Set the point and save coords.set(point, loc); + save(); - // Adjust the region to accomodate any bounding box breaking. - if (point.equals("arena") || point.equals("lobby") || point.equals("spectator")) { - readjustRegion(loc); - } - - fixRegion(); - fixLobbyRegion(); + // Reload region, warps and leaderboards reloadRegion(); reloadWarps(); reloadLeaderboards(); verifyData(); - save(); } public void addSpawn(String name, Location loc) { + // Add the spawn and save spawns.set(name, loc); - readjustRegion(loc); + save(); + + // Reload spawnpoints and verify data reloadSpawnpoints(); verifyData(); - save(); } public boolean removeSpawn(String name) { + // Check if the spawnpoint exists if (spawns.getString(name) == null) { return false; } + // Null the spawnpoint and save spawns.set(name, null); + save(); + + // Reload spawnpoints and verify data reloadSpawnpoints(); verifyData(); - save(); return true; } public void addChest(String name, Location loc) { + // Add the chest location and save chests.set(name, loc); - reloadChests(); save(); + + // Reload the chests + reloadChests(); } public boolean removeChest(String name) { + // Check if the chest exists if (chests.getString(name) == null) { return false; } + // Null the chest and save chests.set(name, null); - reloadChests(); save(); + + // Reload the chests + reloadChests(); return true; }