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

View File

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

View File

@ -4,65 +4,98 @@ import java.util.logging.Level;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
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.mcteam.ancientgates.Conf;
import org.mcteam.ancientgates.Gate;
import org.mcteam.ancientgates.Plugin;
import org.mcteam.ancientgates.util.GeometryUtil;
public class PluginPlayerListener extends PlayerListener {
@Override
public void onPlayerMove(PlayerMoveEvent event) {
if (event.isCancelled()) {
public class PluginPlayerListener implements Listener
{
@EventHandler
public void onPlayerMove(PlayerMoveEvent event)
{
if (event.isCancelled())
return;
}
Block blockTo = event.getTo().getBlock();
Block blockToUp = blockTo.getFace(BlockFace.UP);
Block blockToUp = blockTo.getRelative(BlockFace.UP);
// Uncomment if you don't want portals to be always open. Portals then are only open, if the Material in the Portal is PORTAL
/*
if (blockTo.getType() != Material.PORTAL && blockToUp.getType() != Material.PORTAL) {
return;
}
*/
// Ok so a player walks into a portal block
// Find the nearest gate!
Gate nearestGate = null;
Location playerLocation = event.getPlayer().getLocation();
double shortestDistance = -1;
//double shortestDistance = -1;
for (Gate gate : Gate.getAll()) {
if ( gate.getFrom() == null || gate.getTo() == null) {
for (Gate gate : Gate.getAll())
{
if ( gate.getFrom() == null || gate.getTo() == null)
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
}
double distance = GeometryUtil.distanceBetweenLocations(playerLocation, gate.getFrom());
if (distance > Conf.getGateSearchRadius()) {
if (distance > Conf.getGateSearchRadius())
continue;
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) {
/*if (shortestDistance == -1 || shortestDistance > distance) {
nearestGate = gate;
shortestDistance = distance;
}*/
}
if (nearestGate != null)
{
checkChunkLoad(nearestGate.getTo().getBlock());
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);
}
}
if (nearestGate != null) {
checkChunkLoad(nearestGate.getTo().getBlock());
event.getPlayer().teleport(nearestGate.getTo());
event.setTo(nearestGate.getTo());
}
}
private void checkChunkLoad(Block b)
{