From 4a6b5902e342b86bda763a3f3629f73b282ce08b Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sat, 25 Feb 2012 17:41:44 +0100 Subject: [PATCH] update to current bukkit event handling system --- src/org/mcteam/ancientgates/Plugin.java | 18 +++-- .../listeners/PluginBlockListener.java | 52 +++++++----- .../listeners/PluginPlayerListener.java | 81 +++++++++++++------ 3 files changed, 99 insertions(+), 52 deletions(-) diff --git a/src/org/mcteam/ancientgates/Plugin.java b/src/org/mcteam/ancientgates/Plugin.java index 31c05c1..2947e0a 100644 --- a/src/org/mcteam/ancientgates/Plugin.java +++ b/src/org/mcteam/ancientgates/Plugin.java @@ -9,11 +9,12 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Location; + import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.event.Event; -import org.bukkit.plugin.PluginManager; + import org.bukkit.plugin.java.JavaPlugin; + import org.mcteam.ancientgates.commands.BaseCommand; import org.mcteam.ancientgates.commands.CommandClose; import org.mcteam.ancientgates.commands.CommandCreate; @@ -23,11 +24,14 @@ import org.mcteam.ancientgates.commands.CommandList; import org.mcteam.ancientgates.commands.CommandOpen; import org.mcteam.ancientgates.commands.CommandSetFrom; import org.mcteam.ancientgates.commands.CommandSetTo; + import org.mcteam.ancientgates.gson.Gson; import org.mcteam.ancientgates.gson.GsonBuilder; + import org.mcteam.ancientgates.listeners.PluginBlockListener; import org.mcteam.ancientgates.listeners.PluginPlayerListener; + public class Plugin extends JavaPlugin { public static Plugin instance; @@ -55,7 +59,8 @@ public class Plugin extends JavaPlugin { } @Override - public void onEnable() { + public void onEnable() + { // Add the commands commands.add(new CommandHelp()); commands.add(new CommandCreate()); @@ -74,10 +79,9 @@ public class Plugin extends JavaPlugin { Gate.load(); // Register events - PluginManager pm = this.getServer().getPluginManager(); - pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this); - pm.registerEvent(Event.Type.BLOCK_PHYSICS, this.blockListener, Event.Priority.Normal, this); - + getServer().getPluginManager().registerEvents(this.playerListener, this); + getServer().getPluginManager().registerEvents(this.blockListener, this); + log("Enabled"); } diff --git a/src/org/mcteam/ancientgates/listeners/PluginBlockListener.java b/src/org/mcteam/ancientgates/listeners/PluginBlockListener.java index f56d931..7fd4386 100644 --- a/src/org/mcteam/ancientgates/listeners/PluginBlockListener.java +++ b/src/org/mcteam/ancientgates/listeners/PluginBlockListener.java @@ -1,43 +1,53 @@ package org.mcteam.ancientgates.listeners; import org.bukkit.Material; + import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.event.block.BlockListener; -import org.bukkit.event.block.BlockPhysicsEvent; -public class PluginBlockListener extends BlockListener { - @Override - public void onBlockPhysics(BlockPhysicsEvent event) { - if (event.isCancelled()) { +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPhysicsEvent; +import org.bukkit.event.EventHandler; + + + + +public class PluginBlockListener implements Listener +{ + @EventHandler + public void onBlockPhysics(BlockPhysicsEvent event) + { + if (event.isCancelled()) return; - } - if (event.getBlock().getType() != Material.PORTAL) { + + if (event.getBlock().getType() != Material.PORTAL) return; - } - if (isBlockInPortal(event.getBlock())) { + + if (isBlockInPortal(event.getBlock())) event.setCancelled(true); - } + } - public boolean isBlockInPortal(Block block) { - if (block.getFace(BlockFace.UP).getType() == Material.AIR) { + + public boolean isBlockInPortal(Block block) + { + if (block.getRelative(BlockFace.UP).getType() == Material.AIR) return false; - } - if (block.getFace(BlockFace.DOWN).getType() == Material.AIR) { + + if (block.getRelative(BlockFace.DOWN).getType() == Material.AIR) return false; - } - if ( block.getFace(BlockFace.NORTH).getType() != Material.AIR && block.getFace(BlockFace.SOUTH).getType() != Material.AIR ) { - return true; - } - if ( block.getFace(BlockFace.WEST).getType() != Material.AIR && block.getFace(BlockFace.EAST).getType() != Material.AIR ) { + if ( block.getRelative(BlockFace.NORTH).getType() != Material.AIR && block.getRelative(BlockFace.SOUTH).getType() != Material.AIR ) return true; - } + + + if ( block.getRelative(BlockFace.WEST).getType() != Material.AIR && block.getRelative(BlockFace.EAST).getType() != Material.AIR ) + return true; + return false; } diff --git a/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java index 675d884..012588f 100644 --- a/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java +++ b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java @@ -4,66 +4,99 @@ import java.util.logging.Level; import org.bukkit.Chunk; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.World; + import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.event.player.PlayerListener; + +import org.bukkit.event.EventHandler; +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 extends PlayerListener { - @Override - public void onPlayerMove(PlayerMoveEvent event) { - if (event.isCancelled()) { +public class PluginPlayerListener implements Listener +{ + @EventHandler + public void onPlayerMove(PlayerMoveEvent event) + { + if (event.isCancelled()) return; - } Block blockTo = event.getTo().getBlock(); - Block blockToUp = blockTo.getFace(BlockFace.UP); + Block blockToUp = blockTo.getRelative(BlockFace.UP); - if (blockTo.getType() != Material.PORTAL && blockToUp.getType() != Material.PORTAL) { - return; - } + // Uncomment if you don't want portals to be always open. Portals then are only open, if the Material in the Portal is PORTAL + /* + if (blockTo.getType() != Material.PORTAL && blockToUp.getType() != Material.PORTAL) { + + return; + } + */ // Ok so a player walks into a portal block // Find the nearest gate! Gate nearestGate = null; Location playerLocation = event.getPlayer().getLocation(); - double shortestDistance = -1; + //double shortestDistance = -1; - for (Gate gate : Gate.getAll()) { - if ( gate.getFrom() == null || gate.getTo() == null) { + for (Gate gate : Gate.getAll()) + { + if ( gate.getFrom() == null || gate.getTo() == null) continue; - } - if ( ! gate.getFrom().getWorld().equals(playerLocation.getWorld())) { + + if ( ! gate.getFrom().getWorld().equals(playerLocation.getWorld())) continue; // We can only be close to gates in the same world - } + double distance = GeometryUtil.distanceBetweenLocations(playerLocation, gate.getFrom()); - if (distance > Conf.getGateSearchRadius()) { + if (distance > Conf.getGateSearchRadius()) continue; - } - if (shortestDistance == -1 || shortestDistance > distance) { + + Plugin.log(Level.ALL, "in gate search radius."); + for (Integer[] blockXYZ: gate.getGateBlocks()) + { + 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]) + ) + { + nearestGate = gate; + break; + } + } + + /*if (shortestDistance == -1 || shortestDistance > distance) { nearestGate = gate; shortestDistance = distance; - } + }*/ } - if (nearestGate != null) { + if (nearestGate != null) + { checkChunkLoad(nearestGate.getTo().getBlock()); - event.getPlayer().teleport(nearestGate.getTo()); - event.setTo(nearestGate.getTo()); + + 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(), + newYaw, playerLocation.getPitch() ); + + event.getPlayer().teleport(teleportToLocation); + event.setTo(teleportToLocation); } } + private void checkChunkLoad(Block b) { World w = b.getWorld();