Make sure chunks are loaded before teleporting players.
This commit is contained in:
parent
30d15c141e
commit
a5b34458c4
@ -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<BaseCommand> commands = new ArrayList<BaseCommand>();
|
||||
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();
|
||||
@ -120,6 +123,12 @@ public class Plugin extends JavaPlugin
|
||||
}
|
||||
|
||||
|
||||
public ChunkLoadListener getChunkLoadListener()
|
||||
{
|
||||
return chunkLoadListener;
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------- //
|
||||
// Commands
|
||||
// -------------------------------------------- //
|
||||
|
45
src/de/craftinc/gates/listeners/ChunkLoadListener.java
Normal file
45
src/de/craftinc/gates/listeners/ChunkLoadListener.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
else {
|
||||
this.teleportPlayer(event.getPlayer(), gateAtLocation);
|
||||
}
|
||||
}
|
||||
|
||||
// Teleport the player
|
||||
checkChunkLoad(gateAtLocation.getLocation().getBlock());
|
||||
|
||||
Location gateExit = gateAtLocation.getExit();
|
||||
Location gateLocation = gateAtLocation.getLocation();
|
||||
Location playerLocation = event.getPlayer().getLocation();
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
Location playerLocation = p.getLocation();
|
||||
Location exit = g.getExit();
|
||||
|
||||
Float newYaw = gateExit.getYaw() - gateLocation.getYaw() + playerLocation.getYaw();
|
||||
Float newYaw = g.getExit().getYaw() - g.getLocation().getYaw() + playerLocation.getYaw();
|
||||
|
||||
Location teleportToLocation = new Location( gateExit.getWorld(),
|
||||
gateExit.getX(),
|
||||
gateExit.getY(),
|
||||
gateExit.getZ(),
|
||||
Location teleportToLocation = new Location( g.getExit().getWorld(),
|
||||
g.getExit().getX(),
|
||||
g.getExit().getY(),
|
||||
g.getExit().getZ(),
|
||||
newYaw,
|
||||
playerLocation.getPitch() );
|
||||
playerLocation.getPitch()
|
||||
);
|
||||
|
||||
event.getPlayer().teleport(teleportToLocation);
|
||||
event.setTo(teleportToLocation);
|
||||
Chunk c = exit.getChunk();
|
||||
World w = exit.getWorld();
|
||||
|
||||
event.getPlayer().sendMessage(ChatColor.DARK_AQUA + "Thank you for traveling with Craft Inc. Gates.");
|
||||
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);
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
11
src/de/craftinc/gates/util/TeleportRequest.java
Normal file
11
src/de/craftinc/gates/util/TeleportRequest.java
Normal 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 {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user