diff --git a/src/org/mcteam/ancientgates/Plugin.java b/src/org/mcteam/ancientgates/Plugin.java index a5f3101..77fd0d8 100644 --- a/src/org/mcteam/ancientgates/Plugin.java +++ b/src/org/mcteam/ancientgates/Plugin.java @@ -20,6 +20,7 @@ import org.mcteam.ancientgates.commands.*; import org.mcteam.ancientgates.listeners.PluginBlockListener; import org.mcteam.ancientgates.listeners.PluginPlayerListener; +import org.mcteam.ancientgates.listeners.PluginPortalListener; public class Plugin extends JavaPlugin @@ -33,6 +34,7 @@ public class Plugin extends JavaPlugin public PluginPlayerListener playerListener = new PluginPlayerListener(); public PluginBlockListener blockListener = new PluginBlockListener(); + public PluginPortalListener portalListener = new PluginPortalListener(); private String baseCommand; @@ -88,6 +90,7 @@ public class Plugin extends JavaPlugin PluginManager pm = this.getServer().getPluginManager(); pm.registerEvents(this.playerListener, this); pm.registerEvents(this.blockListener, this); + pm.registerEvents(this.portalListener, this); // Load gates loadGates(); diff --git a/src/org/mcteam/ancientgates/listeners/BaseLocationListener.java b/src/org/mcteam/ancientgates/listeners/BaseLocationListener.java new file mode 100644 index 0000000..de70c5c --- /dev/null +++ b/src/org/mcteam/ancientgates/listeners/BaseLocationListener.java @@ -0,0 +1,95 @@ +package org.mcteam.ancientgates.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 org.mcteam.ancientgates.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() == false || !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) { + // do nothing - gate is closed + } + } + } + } + + return gate; + } + + + protected Gate getGateAtPlayerLocation(PlayerMoveEvent e) { + Gate gate = null; + + Block blockTo = e.getFrom().getBlock(); + Block blockToUp = blockTo.getRelative(BlockFace.UP); + + System.out.println(blockTo.getLocation().getWorld().getName()); + + + 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/org/mcteam/ancientgates/listeners/PluginPlayerListener.java b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java index c7fb734..e9975f9 100644 --- a/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java +++ b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java @@ -6,7 +6,6 @@ import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -17,7 +16,7 @@ import org.mcteam.ancientgates.Gate; import org.mcteam.ancientgates.Plugin; -public class PluginPlayerListener implements Listener +public class PluginPlayerListener extends BaseLocationListener implements Listener { @EventHandler(priority = EventPriority.NORMAL) public void onPlayerMove(PlayerMoveEvent event) @@ -31,8 +30,8 @@ public class PluginPlayerListener implements Listener return; } - // Find the nearest gate! - Gate gateAtLocation = getGateAtPlayerLocation(event); + // Find the gate at the current location. + Gate gateAtLocation = getValidGateAtPlayerLocation(event); if (gateAtLocation == null) { @@ -72,28 +71,6 @@ public class PluginPlayerListener implements Listener } - /** - * 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; - } - - protected boolean hasPermission(Player player) { if (player.hasPermission(Plugin.permissionUse)) { return true; @@ -105,45 +82,4 @@ public class PluginPlayerListener implements Listener return false; } - - - protected Gate getGateAtPlayerLocation(PlayerMoveEvent e) { - Gate gate = null; - - Location playerLocation = e.getPlayer().getLocation(); - World playerWorld = playerLocation.getWorld(); - - Block blockTo = e.getTo().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() == false || !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) { - // do nothing - gate is closed - } - } - } - } - - return gate; - } - } diff --git a/src/org/mcteam/ancientgates/listeners/PluginPortalListener.java b/src/org/mcteam/ancientgates/listeners/PluginPortalListener.java new file mode 100644 index 0000000..db248cc --- /dev/null +++ b/src/org/mcteam/ancientgates/listeners/PluginPortalListener.java @@ -0,0 +1,26 @@ +package org.mcteam.ancientgates.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerPortalEvent; +import org.mcteam.ancientgates.Gate; + +public class PluginPortalListener extends BaseLocationListener implements Listener +{ + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerPortal(PlayerPortalEvent event) + { + if (event.isCancelled()) { + return; + } + + // Find the gate at the current location. + Gate gateAtLocation = getGateAtPlayerLocation(event); + + if (gateAtLocation != null) { + event.setCancelled(true); + } + } + +}