introduction of plugin portal listener

This commit is contained in:
Tobias Ottenweller 2012-05-17 17:43:52 +02:00
parent 8375a390cc
commit bbe4b29073
4 changed files with 127 additions and 67 deletions

View File

@ -20,6 +20,7 @@ import org.mcteam.ancientgates.commands.*;
import org.mcteam.ancientgates.listeners.PluginBlockListener;
import org.mcteam.ancientgates.listeners.PluginPlayerListener;
import org.mcteam.ancientgates.listeners.PluginPortalListener;
public class Plugin extends JavaPlugin
@ -33,6 +34,7 @@ public class Plugin extends JavaPlugin
public PluginPlayerListener playerListener = new PluginPlayerListener();
public PluginBlockListener blockListener = new PluginBlockListener();
public PluginPortalListener portalListener = new PluginPortalListener();
private String baseCommand;
@ -88,6 +90,7 @@ public class Plugin extends JavaPlugin
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(this.playerListener, this);
pm.registerEvents(this.blockListener, this);
pm.registerEvents(this.portalListener, this);
// Load gates
loadGates();

View File

@ -0,0 +1,95 @@
package org.mcteam.ancientgates.listeners;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.player.PlayerMoveEvent;
import org.mcteam.ancientgates.Gate;
public abstract class BaseLocationListener
{
protected Gate getValidGateAtPlayerLocation(PlayerMoveEvent e) {
Gate gate = null;
Location playerLocation = e.getPlayer().getLocation();
World playerWorld = playerLocation.getWorld();
Block blockTo = e.getFrom().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
try {
g.validate();
gate = g;
break;
}
catch (Exception e2) {
// do nothing - gate is closed
}
}
}
}
return gate;
}
protected Gate getGateAtPlayerLocation(PlayerMoveEvent e) {
Gate gate = null;
Block blockTo = e.getFrom().getBlock();
Block blockToUp = blockTo.getRelative(BlockFace.UP);
System.out.println(blockTo.getLocation().getWorld().getName());
for (Gate g : Gate.getAll()) {
// Check if the location matches
for (Location l: g.getGateBlockLocations()) {
if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation())) {
gate = g;
}
}
}
return gate;
}
/**
* Does the same as the equal method of Location but ignores pitch and yaw.
*/
protected boolean locationsAreAtSamePositions(final Location l1, final Location l2)
{
if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) {
return false;
}
if (Double.doubleToLongBits(l1.getX()) != Double.doubleToLongBits(l2.getX())) {
return false;
}
if (Double.doubleToLongBits(l1.getY()) != Double.doubleToLongBits(l2.getY())) {
return false;
}
if (Double.doubleToLongBits(l1.getZ()) != Double.doubleToLongBits(l2.getZ())) {
return false;
}
return true;
}
}

View File

@ -6,7 +6,6 @@ import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -17,7 +16,7 @@ import org.mcteam.ancientgates.Gate;
import org.mcteam.ancientgates.Plugin;
public class PluginPlayerListener implements Listener
public class PluginPlayerListener extends BaseLocationListener implements Listener
{
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerMove(PlayerMoveEvent event)
@ -31,8 +30,8 @@ public class PluginPlayerListener implements Listener
return;
}
// Find the nearest gate!
Gate gateAtLocation = getGateAtPlayerLocation(event);
// Find the gate at the current location.
Gate gateAtLocation = getValidGateAtPlayerLocation(event);
if (gateAtLocation == null) {
@ -72,28 +71,6 @@ public class PluginPlayerListener implements Listener
}
/**
* Does the same as the equal method of Location but ignores pitch and yaw.
*/
protected boolean locationsAreAtSamePositions(final Location l1, final Location l2)
{
if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) {
return false;
}
if (Double.doubleToLongBits(l1.getX()) != Double.doubleToLongBits(l2.getX())) {
return false;
}
if (Double.doubleToLongBits(l1.getY()) != Double.doubleToLongBits(l2.getY())) {
return false;
}
if (Double.doubleToLongBits(l1.getZ()) != Double.doubleToLongBits(l2.getZ())) {
return false;
}
return true;
}
protected boolean hasPermission(Player player) {
if (player.hasPermission(Plugin.permissionUse)) {
return true;
@ -105,45 +82,4 @@ public class PluginPlayerListener implements Listener
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
try {
g.validate();
gate = g;
break;
}
catch (Exception e2) {
// do nothing - gate is closed
}
}
}
}
return gate;
}
}

View File

@ -0,0 +1,26 @@
package org.mcteam.ancientgates.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPortalEvent;
import org.mcteam.ancientgates.Gate;
public class PluginPortalListener extends BaseLocationListener implements Listener
{
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerPortal(PlayerPortalEvent event)
{
if (event.isCancelled()) {
return;
}
// Find the gate at the current location.
Gate gateAtLocation = getGateAtPlayerLocation(event);
if (gateAtLocation != null) {
event.setCancelled(true);
}
}
}