PlayerListener refactoring for real

This commit is contained in:
Tobias Ottenweller 2012-05-16 22:03:57 +02:00
parent d293eaf52d
commit 259d2cd3c4

View File

@ -7,6 +7,7 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
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;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -21,60 +22,41 @@ public class PluginPlayerListener implements Listener
@EventHandler(priority = EventPriority.NORMAL) @EventHandler(priority = EventPriority.NORMAL)
public void onPlayerMove(PlayerMoveEvent event) public void onPlayerMove(PlayerMoveEvent event)
{ {
if (event.isCancelled()) if (event.isCancelled()) {
return;
// check for permission
if (!event.getPlayer().hasPermission("ancientgates.use")) {
return; return;
} }
// Check for permission
if (!hasPermission(event.getPlayer())) {
return;
}
// Find the nearest gate! // Find the nearest gate!
Gate nearestGate = null; Gate gateAtLocation = getGateAtPlayerLocation(event);
if (gateAtLocation == null) {
return;
}
// Teleport the player
checkChunkLoad(gateAtLocation.getLocation().getBlock());
Location gateExit = gateAtLocation.getExit();
Location playerLocation = event.getPlayer().getLocation(); Location playerLocation = event.getPlayer().getLocation();
World playerWorld = playerLocation.getWorld();
Block blockTo = event.getTo().getBlock(); Float newYaw = gateExit.getYaw() - gateExit.getYaw() + playerLocation.getYaw();
Block blockToUp = blockTo.getRelative(BlockFace.UP);
Location teleportToLocation = new Location( gateExit.getWorld(),
for (Gate gate : Gate.getAll()) gateExit.getX(),
{ gateExit.getY(),
// Check if the gate is open and useable gateExit.getZ(),
World gateWorld = gate.getLocation().getWorld(); newYaw,
playerLocation.getPitch() );
if (gate.isOpen() == false || !gateWorld.equals(playerWorld)) {
continue;
}
// Check if the location matches
for (Location l: gate.getGateBlockLocations())
{
if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation()))
{
nearestGate = gate;
break;
}
}
}
if (nearestGate != null)
{
checkChunkLoad(nearestGate.getLocation().getBlock());
Float newYaw = nearestGate.getExit().getYaw() - nearestGate.getExit().getYaw() + playerLocation.getYaw();
Location teleportToLocation = new Location( nearestGate.getExit().getWorld(),
nearestGate.getExit().getX(),
nearestGate.getExit().getY(),
nearestGate.getExit().getZ(),
newYaw, playerLocation.getPitch() );
event.getPlayer().teleport(teleportToLocation); event.getPlayer().teleport(teleportToLocation);
event.setTo(teleportToLocation); event.setTo(teleportToLocation);
} }
}
private void checkChunkLoad(Block b) private void checkChunkLoad(Block b)
@ -91,9 +73,9 @@ public class PluginPlayerListener implements Listener
/** /**
* Does the same as the equal method of Location but ignores fitch and yaw. * Does the same as the equal method of Location but ignores pitch and yaw.
*/ */
private static boolean locationsAreAtSamePositions(final Location l1, final Location l2) protected boolean locationsAreAtSamePositions(final Location l1, final Location l2)
{ {
if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) { if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) {
return false; return false;
@ -110,4 +92,56 @@ public class PluginPlayerListener implements Listener
return true; return true;
} }
protected boolean hasPermission(Player player) {
if (player.hasPermission(Plugin.permissionUse)) {
return true;
}
if (player.hasPermission(Plugin.permissionAll)) {
return true;
}
return false;
}
protected Gate getGateAtPlayerLocation(PlayerMoveEvent e) {
Gate gate = null;
Location playerLocation = e.getPlayer().getLocation();
World playerWorld = playerLocation.getWorld();
Block blockTo = e.getTo().getBlock();
Block blockToUp = blockTo.getRelative(BlockFace.UP);
for (Gate g : Gate.getAll()) {
// Check if the gate is open and useable
World gateWorld = g.getLocation().getWorld();
if (g.isOpen() == false || !gateWorld.equals(playerWorld)) {
continue;
}
// Check if the location matches
for (Location l: g.getGateBlockLocations()) {
if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation())) {
// Check if the gate is still valid
g.validate();
if (g.isOpen()) {
gate = g;
break;
}
}
}
}
return gate;
}
} }