From 87e56742d443f6f4399da7b39ae1c0fd3ccf2a84 Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sun, 13 May 2012 18:18:04 +0200 Subject: [PATCH] refactoring: make plugin work for testing (no persistence yet) --- src/org/mcteam/ancientgates/Plugin.java | 22 +++--- .../listeners/PluginPlayerListener.java | 76 +++++++++++-------- .../ancientgates/util/LocationSerializer.java | 19 +++++ 3 files changed, 75 insertions(+), 42 deletions(-) create mode 100644 src/org/mcteam/ancientgates/util/LocationSerializer.java diff --git a/src/org/mcteam/ancientgates/Plugin.java b/src/org/mcteam/ancientgates/Plugin.java index 4e42c1d..3f5a13f 100644 --- a/src/org/mcteam/ancientgates/Plugin.java +++ b/src/org/mcteam/ancientgates/Plugin.java @@ -66,13 +66,7 @@ public class Plugin extends JavaPlugin commands.add(new CommandInfo()); commands.add(new CommandHide()); commands.add(new CommandUnhide()); - - // Ensure basefolder exists! - this.getDataFolder().mkdirs(); - - // Load from disc - Conf.load(); - Gate.load(); + // Register events PluginManager pm = this.getServer().getPluginManager(); @@ -106,8 +100,10 @@ public class Plugin extends JavaPlugin return true; } - public void handleCommand(CommandSender sender, List parameters) { - if (parameters.size() == 0) { + public void handleCommand(CommandSender sender, List parameters) + { + if (parameters.size() == 0) + { this.commands.get(0).execute(sender, parameters); return; } @@ -115,14 +111,16 @@ public class Plugin extends JavaPlugin String commandName = parameters.get(0).toLowerCase(); parameters.remove(0); - for (BaseCommand fcommand : this.commands) { - if (fcommand.getAliases().contains(commandName)) { + for (BaseCommand fcommand : this.commands) + { + if (fcommand.getAliases().contains(commandName)) + { fcommand.execute(sender, parameters); return; } } - sender.sendMessage(Conf.colorSystem+"Unknown gate-command \""+commandName+"\". Try "+Conf.colorCommand+"/"+getBaseCommand()+" help"); + sender.sendMessage("Unknown gate-command \"" + commandName + "\". Try " + "/" + getBaseCommand() + " help"); } // -------------------------------------------- // diff --git a/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java index 36f2f53..860406a 100644 --- a/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java +++ b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java @@ -5,7 +5,6 @@ import java.util.logging.Level; 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.event.EventHandler; @@ -13,53 +12,48 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; -import org.mcteam.ancientgates.Conf; import org.mcteam.ancientgates.Gate; import org.mcteam.ancientgates.Plugin; -import org.mcteam.ancientgates.util.GeometryUtil; public class PluginPlayerListener implements Listener { - @EventHandler(priority = EventPriority.NORMAL) public void onPlayerMove(PlayerMoveEvent event) { if (event.isCancelled()) return; - if (!(event.getPlayer().hasPermission("ancientgates.use") || sender.hasPermission(permissionInfo) || sender.hasPermission(permissionManage))) + // check for permission + if (!event.getPlayer().hasPermission("ancientgates.use")) { return; + } + + + // Find the nearest gate! + Gate nearestGate = null; + + Location playerLocation = event.getPlayer().getLocation(); + World playerWorld = playerLocation.getWorld(); Block blockTo = event.getTo().getBlock(); Block blockToUp = blockTo.getRelative(BlockFace.UP); - // Find the nearest gate! - Gate nearestGate = null; - Location playerLocation = event.getPlayer().getLocation(); for (Gate gate : Gate.getAll()) { - if (gate.getFrom() == null || - gate.getTo() == null || - gate.isOpen() == false || - !gate.getFrom().getWorld().equals(playerLocation.getWorld())) - { + // Check if the gate is open and useable + World gateWorld = gate.getLocation().getWorld(); + + if (gate.getLocation() == null || gate.getExit() == null || gate.isOpen() == false || !gateWorld.equals(playerWorld)) { continue; } - - double distance = GeometryUtil.distanceBetweenLocations(playerLocation, gate.getFrom()); - if (distance > Conf.getGateSearchRadius()) - continue; - - Plugin.log(Level.ALL, "in gate search radius of " + gate.getId()); - - for (Integer[] blockXYZ: gate.getGateBlocks()) + + // Check if the location matches + for (Location l: gate.getGateBlockLocations()) { - if ((blockTo.getX() == blockXYZ[0] || blockToUp.getX() == blockXYZ[0]) && - (blockTo.getY() == blockXYZ[1] || blockToUp.getY() == blockXYZ[1]) && - (blockTo.getZ() == blockXYZ[2] || blockToUp.getZ() == blockXYZ[2])) + if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation())) { nearestGate = gate; break; @@ -69,12 +63,12 @@ public class PluginPlayerListener implements Listener if (nearestGate != null) { - checkChunkLoad(nearestGate.getTo().getBlock()); - Float newYaw = nearestGate.getFrom().getYaw() - nearestGate.getTo().getYaw() + playerLocation.getYaw(); - Location teleportToLocation = new Location( nearestGate.getTo().getWorld(), - nearestGate.getTo().getX(), - nearestGate.getTo().getY(), - nearestGate.getTo().getZ(), + checkChunkLoad(nearestGate.getLocation().getBlock()); + Float newYaw = nearestGate.getExit().getYaw() - nearestGate.getExit().getYaw() + playerLocation.getYaw(); + Location teleportToLocation = new Location( nearestGate.getExit().getWorld(), + nearestGate.getExit().getX(), + nearestGate.getExit().getY(), + nearestGate.getExit().getZ(), newYaw, playerLocation.getPitch() ); event.getPlayer().teleport(teleportToLocation); @@ -94,4 +88,26 @@ public class PluginPlayerListener implements Listener w.loadChunk(c); } } + + + /** + * Does the same as the equal method of Location but ignores fitch and yaw. + */ + private 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 (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/util/LocationSerializer.java b/src/org/mcteam/ancientgates/util/LocationSerializer.java new file mode 100644 index 0000000..9a6eb2d --- /dev/null +++ b/src/org/mcteam/ancientgates/util/LocationSerializer.java @@ -0,0 +1,19 @@ +package org.mcteam.ancientgates.util; + +import java.util.Map; + +import org.bukkit.Location; + +public class LocationSerializer +{ + public static Map serializeLocation(Location l) + { + return null; + } + + + public static Location deserializeLocation(Map map) + { + return null; + } +}