diff --git a/src/de/craftinc/gates/Gate.java b/src/de/craftinc/gates/Gate.java index 09c383a..4468dd9 100644 --- a/src/de/craftinc/gates/Gate.java +++ b/src/de/craftinc/gates/Gate.java @@ -10,7 +10,7 @@ import java.util.Map; import org.bukkit.Location; import org.bukkit.configuration.serialization.ConfigurationSerializable; -import de.craftinc.gates.util.LocationSerializer; +import de.craftinc.gates.util.LocationUtil; @@ -81,8 +81,8 @@ public class Gate extends BaseGate implements ConfigurationSerializable isHidden = (Boolean)map.get(isHiddenKey); isOpen = (Boolean)map.get(isOpenKey); - location = LocationSerializer.deserializeLocation((Map) map.get(locationKey)); - exit = LocationSerializer.deserializeLocation((Map) map.get(exitKey)); + location = LocationUtil.deserializeLocation((Map) map.get(locationKey)); + exit = LocationUtil.deserializeLocation((Map) map.get(exitKey)); if (map.containsKey(exitPitchKey)) { exit.setPitch(((Double)map.get(exitPitchKey)).floatValue()); @@ -98,7 +98,7 @@ public class Gate extends BaseGate implements ConfigurationSerializable List> serializedGateBlocks = (List>)map.get(gateBlocksKey); for (Map sgb : serializedGateBlocks) { - gateBlockLocations.add(LocationSerializer.deserializeLocation(sgb)); + gateBlockLocations.add(LocationUtil.deserializeLocation(sgb)); } } catch (Exception e) { @@ -126,8 +126,8 @@ public class Gate extends BaseGate implements ConfigurationSerializable Map retVal = new HashMap(); retVal.put(idKey, id); - retVal.put(locationKey, LocationSerializer.serializeLocation(location)); - retVal.put(exitKey, LocationSerializer.serializeLocation(exit)); + retVal.put(locationKey, LocationUtil.serializeLocation(location)); + retVal.put(exitKey, LocationUtil.serializeLocation(exit)); retVal.put(isHiddenKey, isHidden); retVal.put(isOpenKey, isOpen); @@ -142,7 +142,7 @@ public class Gate extends BaseGate implements ConfigurationSerializable List> serializedGateBlocks = new ArrayList>(); for (Location l : gateBlockLocations) { - serializedGateBlocks.add(LocationSerializer.serializeLocation(l)); + serializedGateBlocks.add(LocationUtil.serializeLocation(l)); } retVal.put(gateBlocksKey, serializedGateBlocks); diff --git a/src/de/craftinc/gates/listeners/BaseLocationListener.java b/src/de/craftinc/gates/listeners/BaseLocationListener.java deleted file mode 100644 index f31ed2e..0000000 --- a/src/de/craftinc/gates/listeners/BaseLocationListener.java +++ /dev/null @@ -1,96 +0,0 @@ -package de.craftinc.gates.listeners; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.event.player.PlayerMoveEvent; - -import de.craftinc.gates.Gate; - - -public abstract class BaseLocationListener -{ - protected Gate getValidGateAtPlayerLocation(PlayerMoveEvent e) { - Gate gate = null; - - Location playerLocation = e.getPlayer().getLocation(); - World playerWorld = playerLocation.getWorld(); - - Block blockTo = e.getFrom().getBlock(); - Block blockToUp = blockTo.getRelative(BlockFace.UP); - - - for (Gate g : Gate.getAll()) { - // Check if the gate is open and useable - World gateWorld = g.getLocation().getWorld(); - - if (!g.isOpen() || !gateWorld.equals(playerWorld)) { - continue; - } - - - // Check if the location matches - for (Location l: g.getGateBlockLocations()) { - - if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation())) { - // Check if the gate is still valid - try { - g.validate(); - - gate = g; - break; - } - catch (Exception e2) { - break; // do nothing - gate got closed - } - } - } - } - - return gate; - } - - - protected Gate getGateAtPlayerLocation(PlayerMoveEvent e) { - Gate gate = null; - - Block blockTo = e.getFrom().getBlock(); - Block blockToUp = blockTo.getRelative(BlockFace.UP); - - - for (Gate g : Gate.getAll()) { - // Check if the location matches - for (Location l: g.getGateBlockLocations()) { - - if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation())) { - gate = g; - } - } - } - - return gate; - } - - - /** - * Does the same as the equal method of Location but ignores pitch and yaw. - */ - protected boolean locationsAreAtSamePositions(final Location l1, final Location l2) - { - if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) { - return false; - } - if (Double.doubleToLongBits(l1.getX()) != Double.doubleToLongBits(l2.getX())) { - return false; - } - if (Double.doubleToLongBits(l1.getY()) != Double.doubleToLongBits(l2.getY())) { - return false; - } - if (Double.doubleToLongBits(l1.getZ()) != Double.doubleToLongBits(l2.getZ())) { - return false; - } - - return true; - } -} diff --git a/src/de/craftinc/gates/listeners/PluginPlayerListener.java b/src/de/craftinc/gates/listeners/PluginPlayerListener.java index 430918c..7bb5374 100644 --- a/src/de/craftinc/gates/listeners/PluginPlayerListener.java +++ b/src/de/craftinc/gates/listeners/PluginPlayerListener.java @@ -14,9 +14,10 @@ import org.bukkit.event.player.PlayerMoveEvent; import de.craftinc.gates.Gate; import de.craftinc.gates.Plugin; +import de.craftinc.gates.util.GateUtil; -public class PluginPlayerListener extends BaseLocationListener implements Listener +public class PluginPlayerListener implements Listener { @EventHandler(priority = EventPriority.NORMAL) public void onPlayerMove(PlayerMoveEvent event) @@ -27,7 +28,7 @@ public class PluginPlayerListener extends BaseLocationListener implements Listen // Find the gate at the current location. - Gate gateAtLocation = getValidGateAtPlayerLocation(event); + Gate gateAtLocation = GateUtil.getGateAtPlayerLocation(event.getTo()); if (gateAtLocation == null) { diff --git a/src/de/craftinc/gates/util/GateUtil.java b/src/de/craftinc/gates/util/GateUtil.java new file mode 100644 index 0000000..66309b8 --- /dev/null +++ b/src/de/craftinc/gates/util/GateUtil.java @@ -0,0 +1,88 @@ +package de.craftinc.gates.util; + +import java.util.Iterator; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.BlockFace; + +import de.craftinc.gates.Gate; + +public class GateUtil +{ + public static Gate closestGate(Location location) + { + Gate gate = null; + double minmalDist = Double.MAX_VALUE; + + for (Gate g : Gate.getAll()) { + + if (!g.getLocation().getWorld().equals(location.getWorld())) + { + continue; + } + + double tempDist = g.getLocation().distance(location); + + if (tempDist < minmalDist) + { + gate = g; + } + + } + + return gate; + } + + + + public static Gate getGateAtPlayerLocation(Location location) + { + Gate gate = null; + World playerWorld = location.getWorld(); + + // players are sometime stuck into the ground + Location locationUp = location.getBlock().getRelative(BlockFace.UP).getLocation(); + + System.out.println("player location: " + location); + + for (Gate g : Gate.getAll()) + { + if (gate != null) + { + break; + } + + // Check if the gate is open and useable + World gateWorld = g.getLocation().getWorld(); + + if (!g.isOpen() || !gateWorld.equals(playerWorld)) + { + continue; + } + + + // Check if the location matches + for (Location l: g.getGateBlockLocations()) { + + if (LocationUtil.locationsAreAtSamePositions(l, location) || LocationUtil.locationsAreAtSamePositions(l, locationUp)) + { + System.out.println("gate location: " + l); + + // Check if the gate is still valid + try { + g.validate(); + + gate = g; + break; + } + catch (Exception e2) { + break; // do nothing - gate got closed + } + } + } + } + + return gate; + } +} diff --git a/src/de/craftinc/gates/util/GeometryUtil.java b/src/de/craftinc/gates/util/GeometryUtil.java index ab94c8e..86ee675 100644 --- a/src/de/craftinc/gates/util/GeometryUtil.java +++ b/src/de/craftinc/gates/util/GeometryUtil.java @@ -2,8 +2,8 @@ package de.craftinc.gates.util; import org.bukkit.Location; -public class GeometryUtil { - +public class GeometryUtil +{ // How long between two locations? public static double distanceBetweenLocations(Location location1, Location location2) { diff --git a/src/de/craftinc/gates/util/LocationSerializer.java b/src/de/craftinc/gates/util/LocationUtil.java similarity index 75% rename from src/de/craftinc/gates/util/LocationSerializer.java rename to src/de/craftinc/gates/util/LocationUtil.java index e295484..f93940c 100644 --- a/src/de/craftinc/gates/util/LocationSerializer.java +++ b/src/de/craftinc/gates/util/LocationUtil.java @@ -12,7 +12,7 @@ import de.craftinc.gates.Plugin; /** * NOTE: We do not care about yaw and pitch for gate locations. So we won't serialize them. */ -public class LocationSerializer +public class LocationUtil { protected static String worldKey = "world"; protected static String xKey = "x"; @@ -83,4 +83,23 @@ public class LocationSerializer return new Location(w, x, y, z); } + + + public static boolean locationsAreAtSamePositions(final Location l1, final Location l2) + { + if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) { + return false; + } + if (new Double(l1.getX()).longValue() != new Double(l2.getX()).longValue()) { + return false; + } + if (new Double(l1.getY()).longValue() != new Double(l2.getY()).longValue()) { + return false; + } + if (new Double(l1.getZ()).longValue() != new Double(l2.getZ()).longValue()) { + return false; + } + + return true; + } }