From f253dd4ccc3c762dae8708031a778c50c7f5da5e Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Tue, 14 May 2013 20:05:25 +0200 Subject: [PATCH] Refactored the GatesManager. Added SimpleChunk and SimpleLocation classes. Made PluginPlayerListener use the gateByLocation method of the GatesManager. --- src/de/craftinc/gates/GatesManager.java | 119 ++++++++++++------ .../gates/listeners/PluginPlayerListener.java | 10 +- src/de/craftinc/gates/util/SimpleChunk.java | 47 +++++++ .../craftinc/gates/util/SimpleLocation.java | 52 ++++++++ 4 files changed, 185 insertions(+), 43 deletions(-) create mode 100644 src/de/craftinc/gates/util/SimpleChunk.java create mode 100644 src/de/craftinc/gates/util/SimpleLocation.java diff --git a/src/de/craftinc/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java index 83d890d..9aa7cf7 100644 --- a/src/de/craftinc/gates/GatesManager.java +++ b/src/de/craftinc/gates/GatesManager.java @@ -12,6 +12,9 @@ import org.bukkit.Location; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import de.craftinc.gates.util.SimpleChunk; +import de.craftinc.gates.util.SimpleLocation; + public class GatesManager { @@ -20,8 +23,8 @@ public class GatesManager private String gatesPath = "gates"; // path to gates inside the yaml file private Map gatesById; - private Map> gatesByChunk; - private Map gatesByLocation; + private Map> gatesByChunk; + private Map gatesByLocation; private List gates; @@ -32,15 +35,17 @@ public class GatesManager } - public Set getGatesInsideChunk(Chunk c) + public Set getGatesInsideChunk(Chunk chunk) { - return gatesByChunk.get(c); + SimpleChunk simpleChunk = new SimpleChunk(chunk); + return gatesByChunk.get(simpleChunk); } - public Gate getGateAtLocation(Location l) + public Gate getGateAtLocation(Location location) { - return gatesByLocation.get(l); + SimpleLocation simpleLocation = new SimpleLocation(location); + return gatesByLocation.get(simpleLocation); } @@ -96,30 +101,23 @@ public class GatesManager gatesById = new HashMap((int)(gates.size() * 1.25)); for (Gate g : gates) { - gatesById.put(g.getId(), g); + this.addGateWithId(g); } } private void fillGatesByChunk() { - HashSet chunksUsedByGates = new HashSet(gates.size()); + HashSet chunksUsedByGates = new HashSet(); for (Gate g : gates) { chunksUsedByGates.add(g.getLocation().getChunk()); } - gatesByChunk = new HashMap>((int)(chunksUsedByGates.size() * 1.25)); + gatesByChunk = new HashMap>((int)(chunksUsedByGates.size() * 1.25)); for (Gate g : gates) { - Chunk c = g.getLocation().getChunk(); - Set gatesForC = gatesByChunk.get(c); - - if (gatesForC == null) { - gatesForC = new HashSet(); // NOTE: not optimizing size here - } - - gatesForC.add(g); + this.addGateByChunk(g); } } @@ -132,14 +130,66 @@ public class GatesManager numGateBlocks += g.gateBlockLocations.size(); } - gatesByLocation = new HashMap((int)(numGateBlocks*1.25)); + gatesByLocation = new HashMap((int)(numGateBlocks*1.25)); for (Gate g : gates) { - gatesByLocation.put(g.getLocation(), g); + this.addGateByLocations(g); } } + private void removeGateById(String id) + { + gatesById.remove(id); + } + + + private void addGateWithId(Gate g) + { + gatesById.put(g.getId(), g); + } + + + private void removeGateFromLocations(Set gateBlocks) + { + for (Location l : gateBlocks) { + SimpleLocation sl = new SimpleLocation(l); + gatesByLocation.remove(sl); + } + } + + + private void addGateByLocations(Gate g) + { + for (Location c : g.getGateBlockLocations()) { + SimpleLocation sl = new SimpleLocation(c); + gatesByLocation.put(sl, g); + } + } + + + private void removeGateFromChunk(Gate g, Location l) + { + SimpleChunk sc = new SimpleChunk(l.getChunk()); + Set gatesInChunk = gatesByChunk.get(sc); + gatesInChunk.remove(g); + } + + + private void addGateByChunk(Gate g) + { + SimpleChunk c = new SimpleChunk(g.getLocation().getChunk()); + Set gatesForC = gatesByChunk.get(c); + + if (gatesForC == null) { + gatesForC = new HashSet(); // NOTE: not optimizing size here + gatesByChunk.put(c, gatesForC); + } + + gatesForC.add(g); + } + + public void storeInvalidGate(Map map) { File invalidGatesFile = new File(Plugin.getPlugin().getDataFolder(), "invalid_gates.yml"); @@ -197,39 +247,34 @@ public class GatesManager public void handleGateIdChange(Gate g, String oldId) { - gatesById.remove(oldId); - gatesById.put(g.getId(), g); + this.removeGateById(oldId); + this.addGateWithId(g); } - public void handleGateLocationChange(Gate g, Location oldLocation) + public void handleGateLocationChange(Gate g, Location oldLocation, Set oldGateBlockLocations) { - gatesByLocation.remove(oldLocation); - gatesByLocation.put(g.getLocation(), g); + this.removeGateFromChunk(g, oldLocation); + this.addGateByChunk(g); - - gatesByChunk.get(oldLocation.getChunk()).remove(g); - - Set newChunkGates = gatesByChunk.get(g.getLocation().getChunk()); - - if (newChunkGates == null) { - newChunkGates = new HashSet(); // NOTE: not optimizing size here - } - - newChunkGates.add(g); - gatesByChunk.put(g.getLocation().getChunk(), newChunkGates); + this.removeGateFromLocations(oldGateBlockLocations); + this.addGateByLocations(g); } public void handleNewGate(Gate g) { - // TODO: implement! + this.addGateByChunk(g); + this.addGateByLocations(g); + this.addGateWithId(g); } public void handleDeletion(Gate g) { - // TODO: implement! + this.removeGateById(g.getId()); + this.removeGateFromChunk(g, g.getLocation()); + this.removeGateFromLocations(g.getGateBlockLocations()); } diff --git a/src/de/craftinc/gates/listeners/PluginPlayerListener.java b/src/de/craftinc/gates/listeners/PluginPlayerListener.java index e884b34..416dffc 100644 --- a/src/de/craftinc/gates/listeners/PluginPlayerListener.java +++ b/src/de/craftinc/gates/listeners/PluginPlayerListener.java @@ -16,8 +16,8 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; import de.craftinc.gates.Gate; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; -import de.craftinc.gates.util.GateUtil; public class PluginPlayerListener implements Listener @@ -31,11 +31,9 @@ public class PluginPlayerListener implements Listener return; } - - // Find the gate at the current location. - Gate gateAtLocation = GateUtil.getGateAtPlayerLocation(event.getTo()); - - + GatesManager gateManager = Plugin.getPlugin().getGatesManager(); + Gate gateAtLocation = gateManager.getGateAtLocation(event.getTo()); + if (gateAtLocation == null) { return; } diff --git a/src/de/craftinc/gates/util/SimpleChunk.java b/src/de/craftinc/gates/util/SimpleChunk.java new file mode 100644 index 0000000..3c3b032 --- /dev/null +++ b/src/de/craftinc/gates/util/SimpleChunk.java @@ -0,0 +1,47 @@ +package de.craftinc.gates.util; + +import org.bukkit.Chunk; + +public class SimpleChunk +{ + private int x; + private int z; + private String world; + + public SimpleChunk(Chunk c) + { + this.x = c.getX(); + this.z = c.getZ(); + this.world = c.getWorld().getName(); + } + + + @Override + public boolean equals(Object o) + { + if (o instanceof SimpleChunk) { + SimpleChunk otherLocation = (SimpleChunk)o; + + if (otherLocation.x == this.x + && otherLocation.z == this.z + && otherLocation.world.equals(this.world)) { + + return true; + } + } + + return false; + } + + + @Override + public int hashCode() + { + int hash = 11; + hash = 29 * hash + x; + hash = 37 * hash + z; + hash = 29 * hash + world.hashCode(); + + return hash; + } +} diff --git a/src/de/craftinc/gates/util/SimpleLocation.java b/src/de/craftinc/gates/util/SimpleLocation.java new file mode 100644 index 0000000..3a99cf5 --- /dev/null +++ b/src/de/craftinc/gates/util/SimpleLocation.java @@ -0,0 +1,52 @@ +package de.craftinc.gates.util; + +import org.bukkit.Location; + +public class SimpleLocation +{ + private String world; + private int x; + private int y; + private int z; + + + public SimpleLocation(Location l) + { + this.world = l.getWorld().getName(); + this.x = (int)l.getX(); + this.y = (int)l.getY(); + this.z = (int)l.getZ(); + } + + + @Override + public boolean equals(Object o) + { + if (o instanceof SimpleLocation) { + SimpleLocation otherLocation = (SimpleLocation)o; + + if (otherLocation.x == this.x + && otherLocation.y == this.y + && otherLocation.z == this.z + && otherLocation.world.equals(this.world)) { + + return true; + } + } + + return false; + } + + + @Override + public int hashCode() + { + int hash = 13; + hash = 37 * hash + x; + hash = 31 * hash + y; + hash = 37 * hash + z; + hash = 31 * hash + world.hashCode(); + + return hash; + } +}