update to current bukkit event handling system

This commit is contained in:
Tobias Ottenweller 2012-02-25 17:41:44 +01:00
parent 0b6f573928
commit 4a6b5902e3
3 changed files with 99 additions and 52 deletions

View File

@ -9,11 +9,12 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.mcteam.ancientgates.commands.BaseCommand; import org.mcteam.ancientgates.commands.BaseCommand;
import org.mcteam.ancientgates.commands.CommandClose; import org.mcteam.ancientgates.commands.CommandClose;
import org.mcteam.ancientgates.commands.CommandCreate; import org.mcteam.ancientgates.commands.CommandCreate;
@ -23,11 +24,14 @@ import org.mcteam.ancientgates.commands.CommandList;
import org.mcteam.ancientgates.commands.CommandOpen; import org.mcteam.ancientgates.commands.CommandOpen;
import org.mcteam.ancientgates.commands.CommandSetFrom; import org.mcteam.ancientgates.commands.CommandSetFrom;
import org.mcteam.ancientgates.commands.CommandSetTo; import org.mcteam.ancientgates.commands.CommandSetTo;
import org.mcteam.ancientgates.gson.Gson; import org.mcteam.ancientgates.gson.Gson;
import org.mcteam.ancientgates.gson.GsonBuilder; import org.mcteam.ancientgates.gson.GsonBuilder;
import org.mcteam.ancientgates.listeners.PluginBlockListener; import org.mcteam.ancientgates.listeners.PluginBlockListener;
import org.mcteam.ancientgates.listeners.PluginPlayerListener; import org.mcteam.ancientgates.listeners.PluginPlayerListener;
public class Plugin extends JavaPlugin { public class Plugin extends JavaPlugin {
public static Plugin instance; public static Plugin instance;
@ -55,7 +59,8 @@ public class Plugin extends JavaPlugin {
} }
@Override @Override
public void onEnable() { public void onEnable()
{
// Add the commands // Add the commands
commands.add(new CommandHelp()); commands.add(new CommandHelp());
commands.add(new CommandCreate()); commands.add(new CommandCreate());
@ -74,10 +79,9 @@ public class Plugin extends JavaPlugin {
Gate.load(); Gate.load();
// Register events // Register events
PluginManager pm = this.getServer().getPluginManager(); getServer().getPluginManager().registerEvents(this.playerListener, this);
pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this); getServer().getPluginManager().registerEvents(this.blockListener, this);
pm.registerEvent(Event.Type.BLOCK_PHYSICS, this.blockListener, Event.Priority.Normal, this);
log("Enabled"); log("Enabled");
} }

View File

@ -1,43 +1,53 @@
package org.mcteam.ancientgates.listeners; package org.mcteam.ancientgates.listeners;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPhysicsEvent;
public class PluginBlockListener extends BlockListener { import org.bukkit.event.Listener;
@Override import org.bukkit.event.block.BlockPhysicsEvent;
public void onBlockPhysics(BlockPhysicsEvent event) { import org.bukkit.event.EventHandler;
if (event.isCancelled()) {
public class PluginBlockListener implements Listener
{
@EventHandler
public void onBlockPhysics(BlockPhysicsEvent event)
{
if (event.isCancelled())
return; return;
}
if (event.getBlock().getType() != Material.PORTAL) {
if (event.getBlock().getType() != Material.PORTAL)
return; return;
}
if (isBlockInPortal(event.getBlock())) {
if (isBlockInPortal(event.getBlock()))
event.setCancelled(true); event.setCancelled(true);
}
} }
public boolean isBlockInPortal(Block block) {
if (block.getFace(BlockFace.UP).getType() == Material.AIR) { public boolean isBlockInPortal(Block block)
{
if (block.getRelative(BlockFace.UP).getType() == Material.AIR)
return false; return false;
}
if (block.getFace(BlockFace.DOWN).getType() == Material.AIR) {
if (block.getRelative(BlockFace.DOWN).getType() == Material.AIR)
return false; return false;
}
if ( block.getFace(BlockFace.NORTH).getType() != Material.AIR && block.getFace(BlockFace.SOUTH).getType() != Material.AIR ) {
return true;
}
if ( block.getFace(BlockFace.WEST).getType() != Material.AIR && block.getFace(BlockFace.EAST).getType() != Material.AIR ) { if ( block.getRelative(BlockFace.NORTH).getType() != Material.AIR && block.getRelative(BlockFace.SOUTH).getType() != Material.AIR )
return true; return true;
}
if ( block.getRelative(BlockFace.WEST).getType() != Material.AIR && block.getRelative(BlockFace.EAST).getType() != Material.AIR )
return true;
return false; return false;
} }

View File

@ -4,66 +4,99 @@ import java.util.logging.Level;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
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.event.player.PlayerListener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.mcteam.ancientgates.Conf; import org.mcteam.ancientgates.Conf;
import org.mcteam.ancientgates.Gate; import org.mcteam.ancientgates.Gate;
import org.mcteam.ancientgates.Plugin; import org.mcteam.ancientgates.Plugin;
import org.mcteam.ancientgates.util.GeometryUtil; import org.mcteam.ancientgates.util.GeometryUtil;
public class PluginPlayerListener extends PlayerListener { public class PluginPlayerListener implements Listener
@Override {
public void onPlayerMove(PlayerMoveEvent event) { @EventHandler
if (event.isCancelled()) { public void onPlayerMove(PlayerMoveEvent event)
{
if (event.isCancelled())
return; return;
}
Block blockTo = event.getTo().getBlock(); Block blockTo = event.getTo().getBlock();
Block blockToUp = blockTo.getFace(BlockFace.UP); Block blockToUp = blockTo.getRelative(BlockFace.UP);
if (blockTo.getType() != Material.PORTAL && blockToUp.getType() != Material.PORTAL) { // Uncomment if you don't want portals to be always open. Portals then are only open, if the Material in the Portal is PORTAL
return; /*
} if (blockTo.getType() != Material.PORTAL && blockToUp.getType() != Material.PORTAL) {
return;
}
*/
// Ok so a player walks into a portal block // Ok so a player walks into a portal block
// Find the nearest gate! // Find the nearest gate!
Gate nearestGate = null; Gate nearestGate = null;
Location playerLocation = event.getPlayer().getLocation(); Location playerLocation = event.getPlayer().getLocation();
double shortestDistance = -1; //double shortestDistance = -1;
for (Gate gate : Gate.getAll()) { for (Gate gate : Gate.getAll())
if ( gate.getFrom() == null || gate.getTo() == null) { {
if ( gate.getFrom() == null || gate.getTo() == null)
continue; continue;
}
if ( ! gate.getFrom().getWorld().equals(playerLocation.getWorld())) {
if ( ! gate.getFrom().getWorld().equals(playerLocation.getWorld()))
continue; // We can only be close to gates in the same world continue; // We can only be close to gates in the same world
}
double distance = GeometryUtil.distanceBetweenLocations(playerLocation, gate.getFrom()); double distance = GeometryUtil.distanceBetweenLocations(playerLocation, gate.getFrom());
if (distance > Conf.getGateSearchRadius()) { if (distance > Conf.getGateSearchRadius())
continue; continue;
}
if (shortestDistance == -1 || shortestDistance > distance) {
Plugin.log(Level.ALL, "in gate search radius.");
for (Integer[] blockXYZ: gate.getGateBlocks())
{
if ( (blockTo.getX() == blockXYZ[0] || blockToUp.getX() == blockXYZ[0]) &&
(blockTo.getY() == blockXYZ[1] || blockToUp.getY() == blockXYZ[1]) &&
(blockTo.getZ() == blockXYZ[2] || blockToUp.getZ() == blockXYZ[2])
)
{
nearestGate = gate;
break;
}
}
/*if (shortestDistance == -1 || shortestDistance > distance) {
nearestGate = gate; nearestGate = gate;
shortestDistance = distance; shortestDistance = distance;
} }*/
} }
if (nearestGate != null) { if (nearestGate != null)
{
checkChunkLoad(nearestGate.getTo().getBlock()); checkChunkLoad(nearestGate.getTo().getBlock());
event.getPlayer().teleport(nearestGate.getTo());
event.setTo(nearestGate.getTo()); Float newYaw = nearestGate.getFrom().getYaw() - nearestGate.getTo().getYaw() + playerLocation.getYaw();
Location teleportToLocation = new Location( nearestGate.getTo().getWorld(),
nearestGate.getTo().getX(),
nearestGate.getTo().getY(),
nearestGate.getTo().getZ(),
newYaw, playerLocation.getPitch() );
event.getPlayer().teleport(teleportToLocation);
event.setTo(teleportToLocation);
} }
} }
private void checkChunkLoad(Block b) private void checkChunkLoad(Block b)
{ {
World w = b.getWorld(); World w = b.getWorld();