diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index e23fb96..9271e1f 100644 --- a/src/de/craftinc/gates/Plugin.java +++ b/src/de/craftinc/gates/Plugin.java @@ -5,6 +5,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import de.craftinc.gates.listeners.PlayerMoveListener; import net.milkbowl.vault.permission.Permission; import org.bukkit.command.Command; @@ -15,7 +16,6 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import de.craftinc.gates.commands.*; -import de.craftinc.gates.listeners.PluginPlayerListener; public class Plugin extends JavaPlugin @@ -28,7 +28,7 @@ public class Plugin extends JavaPlugin private static Permission permission; private String baseCommand; - private PluginPlayerListener playerListener = new PluginPlayerListener(); + private PlayerMoveListener playerListener = new PlayerMoveListener(); private List commands = new ArrayList(); private GatesManager gatesManager = new GatesManager(); diff --git a/src/de/craftinc/gates/listeners/PlayerMoveListener.java b/src/de/craftinc/gates/listeners/PlayerMoveListener.java new file mode 100644 index 0000000..cf97b85 --- /dev/null +++ b/src/de/craftinc/gates/listeners/PlayerMoveListener.java @@ -0,0 +1,111 @@ +package de.craftinc.gates.listeners; + +import java.util.Calendar; +import java.util.HashMap; +import java.util.logging.Level; + +import org.bukkit.ChatColor; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +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; + + +public class PlayerMoveListener implements Listener +{ + protected HashMap lastBorderMessage = new HashMap(); + + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerMove(PlayerMoveEvent event) + { + if (event.isCancelled()) { + return; + } + + GatesManager gateManager = Plugin.getPlugin().getGatesManager(); + Gate gateAtLocation = gateManager.getGateAtLocation(event.getTo()); + + if (gateAtLocation == null) { + return; + } + + // Check for permission + if (!hasPermission(event.getPlayer(), gateAtLocation)) { + + String playerName = event.getPlayer().getName(); + + if (playerName == null) { + return; + } + + // get the current time + Long now = Calendar.getInstance().getTimeInMillis(); + + // do not display messages more often than once per second + if (!this.lastBorderMessage.containsKey(playerName) || this.lastBorderMessage.get(playerName) < now - 10000L) { + event.getPlayer().sendMessage(ChatColor.RED + "You are not allowed to use this gate!"); + this.lastBorderMessage.put(playerName, now); + } + + return; + } + + // Teleport the player + checkChunkLoad(gateAtLocation.getLocation().getBlock()); + + Location gateExit = gateAtLocation.getExit(); + Location gateLocation = gateAtLocation.getLocation(); + Location playerLocation = event.getPlayer().getLocation(); + + Float newYaw = gateExit.getYaw() - gateLocation.getYaw() + playerLocation.getYaw(); + + Location teleportToLocation = new Location( gateExit.getWorld(), + gateExit.getX(), + gateExit.getY(), + gateExit.getZ(), + newYaw, + playerLocation.getPitch() ); + + event.getPlayer().teleport(teleportToLocation); + event.setTo(teleportToLocation); + + event.getPlayer().sendMessage(ChatColor.DARK_AQUA + "Thank you for traveling with Craft Inc. Gates."); + } + + + private void checkChunkLoad(Block b) + { + World w = b.getWorld(); + Chunk c = b.getChunk(); + + if (!w.isChunkLoaded(c)) + { + Plugin.log(Level.FINE, "Loading chunk: " + c.toString() + " on: " + w.toString()); + w.loadChunk(c); + } + } + + + protected boolean hasPermission(Player player, Gate gate) + { + if (Plugin.getPermission() == null) // fallback: use the standard bukkit permission system + { + return player.hasPermission(Plugin.permissionUse); + } + else { + boolean permAtLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse); + boolean permAtExit = Plugin.getPermission().has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse); + + return permAtLocation && permAtExit; + } + } +}