From d23a9b76500371825a1023c3cf1766986c1dfafc Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Mon, 3 Jun 2013 19:14:56 +0200 Subject: [PATCH] Added basic gate block sending mechanism. --- src/de/craftinc/gates/{gates => }/Gate.java | 5 +- .../gates/{gates => }/GatesManager.java | 5 +- src/de/craftinc/gates/Plugin.java | 40 +++++--- .../craftinc/gates/commands/BaseCommand.java | 4 +- .../gates/commands/CommandCreate.java | 4 +- .../craftinc/gates/commands/CommandList.java | 2 +- .../gates/commands/CommandRename.java | 2 +- .../listeners/PlayerChangedWorldListener.java | 19 ++++ .../gates/listeners/PlayerJoinListener.java | 17 ++++ .../gates/listeners/PlayerMoveListener.java | 10 +- .../listeners/PlayerRespawnListener.java | 22 +++++ .../listeners/PlayerTeleportListener.java | 21 ++++ .../{gates => }/persistence/LocationUtil.java | 2 +- .../persistence/MigrationUtil.java | 4 +- src/de/craftinc/gates/util/FloodUtil.java | 2 +- .../gates/util/GateBlockChangeSender.java | 98 +++++++++++++++++++ 16 files changed, 227 insertions(+), 30 deletions(-) rename src/de/craftinc/gates/{gates => }/Gate.java (98%) rename src/de/craftinc/gates/{gates => }/GatesManager.java (98%) create mode 100644 src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java create mode 100644 src/de/craftinc/gates/listeners/PlayerJoinListener.java create mode 100644 src/de/craftinc/gates/listeners/PlayerRespawnListener.java create mode 100644 src/de/craftinc/gates/listeners/PlayerTeleportListener.java rename src/de/craftinc/gates/{gates => }/persistence/LocationUtil.java (98%) rename src/de/craftinc/gates/{gates => }/persistence/MigrationUtil.java (90%) create mode 100644 src/de/craftinc/gates/util/GateBlockChangeSender.java diff --git a/src/de/craftinc/gates/gates/Gate.java b/src/de/craftinc/gates/Gate.java similarity index 98% rename from src/de/craftinc/gates/gates/Gate.java rename to src/de/craftinc/gates/Gate.java index 85570ad..025ece8 100644 --- a/src/de/craftinc/gates/gates/Gate.java +++ b/src/de/craftinc/gates/Gate.java @@ -1,8 +1,7 @@ -package de.craftinc.gates.gates; +package de.craftinc.gates; -import de.craftinc.gates.Plugin; import de.craftinc.gates.util.FloodUtil; -import de.craftinc.gates.gates.persistence.LocationUtil; +import de.craftinc.gates.persistence.LocationUtil; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.configuration.serialization.ConfigurationSerializable; diff --git a/src/de/craftinc/gates/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java similarity index 98% rename from src/de/craftinc/gates/gates/GatesManager.java rename to src/de/craftinc/gates/GatesManager.java index 57aa8bf..425fec9 100644 --- a/src/de/craftinc/gates/gates/GatesManager.java +++ b/src/de/craftinc/gates/GatesManager.java @@ -1,4 +1,4 @@ -package de.craftinc.gates.gates; +package de.craftinc.gates; import java.io.File; import java.io.FileWriter; @@ -7,8 +7,7 @@ import java.io.IOException; import java.util.*; import java.util.logging.Level; -import de.craftinc.gates.Plugin; -import de.craftinc.gates.gates.persistence.MigrationUtil; +import de.craftinc.gates.persistence.MigrationUtil; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.configuration.file.FileConfiguration; diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index f54ebbc..7876510 100644 --- a/src/de/craftinc/gates/Plugin.java +++ b/src/de/craftinc/gates/Plugin.java @@ -5,14 +5,13 @@ import java.util.logging.Level; import java.util.logging.Logger; -import de.craftinc.gates.gates.Gate; -import de.craftinc.gates.listeners.PlayerMoveListener; -import de.craftinc.gates.gates.GatesManager; +import de.craftinc.gates.listeners.*; import net.milkbowl.vault.permission.Permission; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.configuration.serialization.ConfigurationSerialization; +import org.bukkit.event.player.PlayerChangedWorldEvent; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; @@ -29,10 +28,15 @@ public class Plugin extends JavaPlugin private static Plugin instance; private static Permission permission; - private String baseCommand; - private PlayerMoveListener playerListener = new PlayerMoveListener(); - private List commands = new ArrayList(); - private GatesManager gatesManager = new GatesManager(); + protected String baseCommand; + protected List commands = new ArrayList(); + protected GatesManager gatesManager = new GatesManager(); + + protected PlayerMoveListener moveListener = new PlayerMoveListener(); + protected PlayerTeleportListener teleportListener = new PlayerTeleportListener(); + protected PlayerRespawnListener respawnListener = new PlayerRespawnListener(); + protected PlayerChangedWorldListener worldChangeListener = new PlayerChangedWorldListener(); + protected PlayerJoinListener joinListener = new PlayerJoinListener(); public Plugin() @@ -58,9 +62,9 @@ public class Plugin extends JavaPlugin { ConfigurationSerialization.registerClass(Gate.class); } - - - private void setupPermissions() + + + protected void setupPermissions() { if (getServer().getPluginManager().getPlugin("Vault") == null) { return; @@ -112,8 +116,7 @@ public class Plugin extends JavaPlugin // Register events - PluginManager pm = this.getServer().getPluginManager(); - pm.registerEvents(this.playerListener, this); + this.registerEventListeners(); // Load gates gatesManager.loadGatesFromDisk(); @@ -121,6 +124,19 @@ public class Plugin extends JavaPlugin log("Enabled"); } + + protected void registerEventListeners() + { + PluginManager pm = this.getServer().getPluginManager(); + + pm.registerEvents(this.moveListener, this); + pm.registerEvents(this.teleportListener, this); + pm.registerEvents(this.respawnListener, this); + pm.registerEvents(this.worldChangeListener, this); + pm.registerEvents(this.joinListener, this); + } + + // -------------------------------------------- // // Commands diff --git a/src/de/craftinc/gates/commands/BaseCommand.java b/src/de/craftinc/gates/commands/BaseCommand.java index e7143a3..4c63268 100644 --- a/src/de/craftinc/gates/commands/BaseCommand.java +++ b/src/de/craftinc/gates/commands/BaseCommand.java @@ -7,8 +7,8 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import de.craftinc.gates.gates.Gate; -import de.craftinc.gates.gates.GatesManager; +import de.craftinc.gates.Gate; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; import de.craftinc.gates.util.TextUtil; diff --git a/src/de/craftinc/gates/commands/CommandCreate.java b/src/de/craftinc/gates/commands/CommandCreate.java index 2994092..87e0192 100644 --- a/src/de/craftinc/gates/commands/CommandCreate.java +++ b/src/de/craftinc/gates/commands/CommandCreate.java @@ -3,8 +3,8 @@ package de.craftinc.gates.commands; import org.bukkit.ChatColor; import org.bukkit.Location; -import de.craftinc.gates.gates.Gate; -import de.craftinc.gates.gates.GatesManager; +import de.craftinc.gates.Gate; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; diff --git a/src/de/craftinc/gates/commands/CommandList.java b/src/de/craftinc/gates/commands/CommandList.java index 95ea0ea..ccd3dfb 100644 --- a/src/de/craftinc/gates/commands/CommandList.java +++ b/src/de/craftinc/gates/commands/CommandList.java @@ -8,7 +8,7 @@ import java.util.List; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import de.craftinc.gates.gates.Gate; +import de.craftinc.gates.Gate; import de.craftinc.gates.Plugin; import de.craftinc.gates.util.TextUtil; diff --git a/src/de/craftinc/gates/commands/CommandRename.java b/src/de/craftinc/gates/commands/CommandRename.java index 0c3ebdb..269776a 100644 --- a/src/de/craftinc/gates/commands/CommandRename.java +++ b/src/de/craftinc/gates/commands/CommandRename.java @@ -2,7 +2,7 @@ package de.craftinc.gates.commands; import org.bukkit.ChatColor; -import de.craftinc.gates.gates.GatesManager; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; diff --git a/src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java b/src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java new file mode 100644 index 0000000..4c3035e --- /dev/null +++ b/src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java @@ -0,0 +1,19 @@ +package de.craftinc.gates.listeners; + + +import de.craftinc.gates.util.GateBlockChangeSender; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerChangedWorldEvent; + + +public class PlayerChangedWorldListener implements Listener +{ + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerChangeWorld(PlayerChangedWorldEvent event) + { + System.out.println("location: " + event.getPlayer().getLocation()); + GateBlockChangeSender.updateGateBlocks(event.getPlayer()); + } +} diff --git a/src/de/craftinc/gates/listeners/PlayerJoinListener.java b/src/de/craftinc/gates/listeners/PlayerJoinListener.java new file mode 100644 index 0000000..0b81b88 --- /dev/null +++ b/src/de/craftinc/gates/listeners/PlayerJoinListener.java @@ -0,0 +1,17 @@ +package de.craftinc.gates.listeners; + + +import de.craftinc.gates.util.GateBlockChangeSender; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class PlayerJoinListener implements Listener +{ + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerJoin(PlayerJoinEvent event) + { + GateBlockChangeSender.updateGateBlocks(event.getPlayer()); + } +} diff --git a/src/de/craftinc/gates/listeners/PlayerMoveListener.java b/src/de/craftinc/gates/listeners/PlayerMoveListener.java index d2ee3c1..e491917 100644 --- a/src/de/craftinc/gates/listeners/PlayerMoveListener.java +++ b/src/de/craftinc/gates/listeners/PlayerMoveListener.java @@ -3,6 +3,7 @@ package de.craftinc.gates.listeners; import java.util.Calendar; import java.util.HashMap; +import de.craftinc.gates.util.GateBlockChangeSender; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -11,8 +12,8 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; -import de.craftinc.gates.gates.Gate; -import de.craftinc.gates.gates.GatesManager; +import de.craftinc.gates.Gate; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; @@ -26,6 +27,11 @@ public class PlayerMoveListener implements Listener if (event.isCancelled()) { return; } + + if (event.getFrom().getChunk() != event.getTo().getChunk()) { + GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getTo()); + } + GatesManager gateManager = Plugin.getPlugin().getGatesManager(); Gate gateAtLocation = gateManager.getGateAtLocation(event.getTo()); diff --git a/src/de/craftinc/gates/listeners/PlayerRespawnListener.java b/src/de/craftinc/gates/listeners/PlayerRespawnListener.java new file mode 100644 index 0000000..b6155e5 --- /dev/null +++ b/src/de/craftinc/gates/listeners/PlayerRespawnListener.java @@ -0,0 +1,22 @@ +package de.craftinc.gates.listeners; + + +import de.craftinc.gates.util.GateBlockChangeSender; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerRespawnEvent; + + +public class PlayerRespawnListener implements Listener +{ + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerRespawn(PlayerRespawnEvent event) + { + System.out.println("in onPlayerRespawn"); + System.out.println("player: " + event.getPlayer()); + System.out.println("position: " + event.getPlayer().getLocation()); + + GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getRespawnLocation()); + } +} diff --git a/src/de/craftinc/gates/listeners/PlayerTeleportListener.java b/src/de/craftinc/gates/listeners/PlayerTeleportListener.java new file mode 100644 index 0000000..b361c3b --- /dev/null +++ b/src/de/craftinc/gates/listeners/PlayerTeleportListener.java @@ -0,0 +1,21 @@ +package de.craftinc.gates.listeners; + + +import de.craftinc.gates.util.GateBlockChangeSender; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerTeleportEvent; + +public class PlayerTeleportListener implements Listener +{ + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerTeleport(PlayerTeleportEvent event) + { + if (event.isCancelled()) { + return; + } + + GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getTo()); + } +} diff --git a/src/de/craftinc/gates/gates/persistence/LocationUtil.java b/src/de/craftinc/gates/persistence/LocationUtil.java similarity index 98% rename from src/de/craftinc/gates/gates/persistence/LocationUtil.java rename to src/de/craftinc/gates/persistence/LocationUtil.java index 2273527..92dba84 100644 --- a/src/de/craftinc/gates/gates/persistence/LocationUtil.java +++ b/src/de/craftinc/gates/persistence/LocationUtil.java @@ -1,4 +1,4 @@ -package de.craftinc.gates.gates.persistence; +package de.craftinc.gates.persistence; import java.util.HashMap; import java.util.Map; diff --git a/src/de/craftinc/gates/gates/persistence/MigrationUtil.java b/src/de/craftinc/gates/persistence/MigrationUtil.java similarity index 90% rename from src/de/craftinc/gates/gates/persistence/MigrationUtil.java rename to src/de/craftinc/gates/persistence/MigrationUtil.java index 1efdb3a..b79ac37 100644 --- a/src/de/craftinc/gates/gates/persistence/MigrationUtil.java +++ b/src/de/craftinc/gates/persistence/MigrationUtil.java @@ -1,6 +1,6 @@ -package de.craftinc.gates.gates.persistence; +package de.craftinc.gates.persistence; -import de.craftinc.gates.gates.Gate; +import de.craftinc.gates.Gate; import org.bukkit.Location; import org.bukkit.Material; diff --git a/src/de/craftinc/gates/util/FloodUtil.java b/src/de/craftinc/gates/util/FloodUtil.java index 84280b8..eb552a1 100644 --- a/src/de/craftinc/gates/util/FloodUtil.java +++ b/src/de/craftinc/gates/util/FloodUtil.java @@ -13,7 +13,7 @@ import de.craftinc.gates.Plugin; public class FloodUtil { - private final static int frameBlockSearchLimit = 100; + private final static int frameBlockSearchLimit = 100; // TODO: move search radius into a config file / get value from config class private static final Set exp1 = new HashSet(); private static final Set exp2 = new HashSet(); diff --git a/src/de/craftinc/gates/util/GateBlockChangeSender.java b/src/de/craftinc/gates/util/GateBlockChangeSender.java new file mode 100644 index 0000000..17608ae --- /dev/null +++ b/src/de/craftinc/gates/util/GateBlockChangeSender.java @@ -0,0 +1,98 @@ +package de.craftinc.gates.util; + + +import de.craftinc.gates.Plugin; +import de.craftinc.gates.Gate; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + + +public class GateBlockChangeSender +{ + protected static final int searchRadius = 16; // TODO: move search radius into a config file / get value from config class + + + public static void updateGateBlocks(final Player player) + { + if (player == null) { + throw new IllegalArgumentException("'player' must not be 'null'!"); + } + + updateGateBlocks(player, player.getLocation()); + } + + + public static void updateGateBlocks(final Player player, final Location location) + { + if (player == null) { + throw new IllegalArgumentException("'player' must not be 'null'!"); + } + + if (location == null) { + throw new IllegalArgumentException("'location' must not be 'null'!"); + } + + Set gatesNearby = Plugin.getPlugin().getGatesManager().getNearbyGates(location.getChunk()); + + if (gatesNearby == null) { + return; // no gates nearby + } + + for (Gate g : gatesNearby) { + + if (!g.isOpen() || g.isHidden()) { + continue; + } + + for (Location l : g.getGateBlockLocations()) { + + if (l.getBlock().getType() == Material.AIR) { + player.sendBlockChange(l, Material.PORTAL, (byte)0); + } + } + } + } + + + + public static void updateGateBlocks(final Gate gate) + { + if (gate == null) { + throw new IllegalArgumentException("'gate must not be 'null'!"); + } + + Location gateLocation = gate.getLocation(); + ArrayList playersNearby = new ArrayList(); + + for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) { + + if (p.getLocation().distance(gateLocation) < searchRadius) { + playersNearby.add(p); + } + } + + Material material; + + if (gate.isOpen() && !gate.isHidden()) { + material = Material.PORTAL; + } + else { + material = Material.AIR; + } + + for (Player p : playersNearby) { + + for (Location l : gate.getGateBlockLocations()) { + + if (l.getBlock().getType() == Material.AIR) { // on server-side a gate is always made out of AIR + p.sendBlockChange(l, material, (byte)0); + } + } + } + } +}