diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index 9271e1f..7efdf3c 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.ChunkLoadListener; import de.craftinc.gates.listeners.PlayerMoveListener; import net.milkbowl.vault.permission.Permission; @@ -29,6 +30,7 @@ public class Plugin extends JavaPlugin private String baseCommand; private PlayerMoveListener playerListener = new PlayerMoveListener(); + private ChunkLoadListener chunkLoadListener = new ChunkLoadListener(); private List commands = new ArrayList(); private GatesManager gatesManager = new GatesManager(); @@ -112,6 +114,7 @@ public class Plugin extends JavaPlugin // Register events PluginManager pm = this.getServer().getPluginManager(); pm.registerEvents(this.playerListener, this); + pm.registerEvents(this.chunkLoadListener, this); // Load gates gatesManager.loadGatesFromDisk(); @@ -119,6 +122,12 @@ public class Plugin extends JavaPlugin log("Enabled"); } + + public ChunkLoadListener getChunkLoadListener() + { + return chunkLoadListener; + } + // -------------------------------------------- // // Commands diff --git a/src/de/craftinc/gates/listeners/ChunkLoadListener.java b/src/de/craftinc/gates/listeners/ChunkLoadListener.java new file mode 100644 index 0000000..a4e30fa --- /dev/null +++ b/src/de/craftinc/gates/listeners/ChunkLoadListener.java @@ -0,0 +1,45 @@ +package de.craftinc.gates.listeners; + +import de.craftinc.gates.Plugin; +import de.craftinc.gates.util.TeleportRequest; +import org.bukkit.ChatColor; +import org.bukkit.Chunk; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.world.ChunkLoadEvent; + +import java.util.HashMap; + + +public class ChunkLoadListener implements Listener +{ + private HashMap pendingRequests = new HashMap(); + + @EventHandler(priority = EventPriority.NORMAL) + public void onChunkLoad(ChunkLoadEvent event) + { + Chunk c = event.getChunk(); + TeleportRequest request = pendingRequests.get(c); + + if (request != null) { + + pendingRequests.remove(c); + + Player p = request.getPlayer(); + p.teleport(request.getDestination()); + p.sendMessage(ChatColor.DARK_AQUA + "Thank you for traveling with Craft Inc. Gates."); + } + } + + + public void addTeleportRequest(TeleportRequest request) + { + if (request == null) { + throw new IllegalArgumentException("The request must not be null!"); + } + + this.pendingRequests.put(request.getDestination().getChunk(), request); + } +} diff --git a/src/de/craftinc/gates/listeners/PlayerMoveListener.java b/src/de/craftinc/gates/listeners/PlayerMoveListener.java index 584badb..4823ae4 100644 --- a/src/de/craftinc/gates/listeners/PlayerMoveListener.java +++ b/src/de/craftinc/gates/listeners/PlayerMoveListener.java @@ -2,13 +2,12 @@ package de.craftinc.gates.listeners; import java.util.Calendar; import java.util.HashMap; -import java.util.logging.Level; +import de.craftinc.gates.util.TeleportRequest; 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; @@ -60,41 +59,45 @@ public class PlayerMoveListener implements Listener 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."); + else { + this.teleportPlayer(event.getPlayer(), gateAtLocation); + } } - - - private void checkChunkLoad(Block b) + + + /** + * Teleports a player. This method will check if the destination chunk is loaded and will wait until the chunk + * is loaded before executing the teleportion event. + * @param p The player to teleport. + * @param g The gate to which exit the player will be teleported. + */ + private void teleportPlayer(Player p, Gate g) { - World w = b.getWorld(); - Chunk c = b.getChunk(); - - if (!w.isChunkLoaded(c)) - { - Plugin.log(Level.FINE, "Loading chunk: " + c.toString() + " on: " + w.toString()); + Location playerLocation = p.getLocation(); + Location exit = g.getExit(); + + Float newYaw = g.getExit().getYaw() - g.getLocation().getYaw() + playerLocation.getYaw(); + + Location teleportToLocation = new Location( g.getExit().getWorld(), + g.getExit().getX(), + g.getExit().getY(), + g.getExit().getZ(), + newYaw, + playerLocation.getPitch() + ); + + Chunk c = exit.getChunk(); + World w = exit.getWorld(); + + if (w.isChunkLoaded(c)) { + p.teleport(teleportToLocation); + p.sendMessage(ChatColor.DARK_AQUA + "Thank you for traveling with Craft Inc. Gates."); + } + else { + TeleportRequest request = new TeleportRequest(p, exit); + Plugin.getPlugin().getChunkLoadListener().addTeleportRequest(request); + w.loadChunk(c); } } diff --git a/src/de/craftinc/gates/util/TeleportRequest.java b/src/de/craftinc/gates/util/TeleportRequest.java new file mode 100644 index 0000000..e01a162 --- /dev/null +++ b/src/de/craftinc/gates/util/TeleportRequest.java @@ -0,0 +1,11 @@ +package de.craftinc.gates.util; + +/** + * Created with IntelliJ IDEA. + * User: tobi + * Date: 26.05.13 + * Time: 14:28 + * To change this template use File | Settings | File Templates. + */ +public class TeleportRequest { +}