Make sure chunks are loaded before teleporting players.

This commit is contained in:
Tobias Ottenweller 2013-05-26 14:52:04 +02:00
parent 30d15c141e
commit a5b34458c4
4 changed files with 102 additions and 34 deletions

View File

@ -5,6 +5,7 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import de.craftinc.gates.listeners.ChunkLoadListener;
import de.craftinc.gates.listeners.PlayerMoveListener; import de.craftinc.gates.listeners.PlayerMoveListener;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
@ -29,6 +30,7 @@ public class Plugin extends JavaPlugin
private String baseCommand; private String baseCommand;
private PlayerMoveListener playerListener = new PlayerMoveListener(); private PlayerMoveListener playerListener = new PlayerMoveListener();
private ChunkLoadListener chunkLoadListener = new ChunkLoadListener();
private List<BaseCommand> commands = new ArrayList<BaseCommand>(); private List<BaseCommand> commands = new ArrayList<BaseCommand>();
private GatesManager gatesManager = new GatesManager(); private GatesManager gatesManager = new GatesManager();
@ -112,6 +114,7 @@ public class Plugin extends JavaPlugin
// Register events // Register events
PluginManager pm = this.getServer().getPluginManager(); PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(this.playerListener, this); pm.registerEvents(this.playerListener, this);
pm.registerEvents(this.chunkLoadListener, this);
// Load gates // Load gates
gatesManager.loadGatesFromDisk(); gatesManager.loadGatesFromDisk();
@ -120,6 +123,12 @@ public class Plugin extends JavaPlugin
} }
public ChunkLoadListener getChunkLoadListener()
{
return chunkLoadListener;
}
// -------------------------------------------- // // -------------------------------------------- //
// Commands // Commands
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -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<Chunk, TeleportRequest> pendingRequests = new HashMap<Chunk, TeleportRequest>();
@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);
}
}

View File

@ -2,13 +2,12 @@ package de.craftinc.gates.listeners;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Level;
import de.craftinc.gates.util.TeleportRequest;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; 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!"); event.getPlayer().sendMessage(ChatColor.RED + "You are not allowed to use this gate!");
this.lastBorderMessage.put(playerName, now); this.lastBorderMessage.put(playerName, now);
} }
return;
} }
else {
// Teleport the player this.teleportPlayer(event.getPlayer(), gateAtLocation);
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) /**
* 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(); Location playerLocation = p.getLocation();
Chunk c = b.getChunk(); 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);
if (!w.isChunkLoaded(c))
{
Plugin.log(Level.FINE, "Loading chunk: " + c.toString() + " on: " + w.toString());
w.loadChunk(c); w.loadChunk(c);
} }
} }

View File

@ -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 {
}