introduction of plugin portal listener
This commit is contained in:
parent
8375a390cc
commit
bbe4b29073
@ -20,6 +20,7 @@ import org.mcteam.ancientgates.commands.*;
|
|||||||
|
|
||||||
import org.mcteam.ancientgates.listeners.PluginBlockListener;
|
import org.mcteam.ancientgates.listeners.PluginBlockListener;
|
||||||
import org.mcteam.ancientgates.listeners.PluginPlayerListener;
|
import org.mcteam.ancientgates.listeners.PluginPlayerListener;
|
||||||
|
import org.mcteam.ancientgates.listeners.PluginPortalListener;
|
||||||
|
|
||||||
|
|
||||||
public class Plugin extends JavaPlugin
|
public class Plugin extends JavaPlugin
|
||||||
@ -33,6 +34,7 @@ public class Plugin extends JavaPlugin
|
|||||||
|
|
||||||
public PluginPlayerListener playerListener = new PluginPlayerListener();
|
public PluginPlayerListener playerListener = new PluginPlayerListener();
|
||||||
public PluginBlockListener blockListener = new PluginBlockListener();
|
public PluginBlockListener blockListener = new PluginBlockListener();
|
||||||
|
public PluginPortalListener portalListener = new PluginPortalListener();
|
||||||
|
|
||||||
private String baseCommand;
|
private String baseCommand;
|
||||||
|
|
||||||
@ -88,6 +90,7 @@ public class Plugin extends JavaPlugin
|
|||||||
PluginManager pm = this.getServer().getPluginManager();
|
PluginManager pm = this.getServer().getPluginManager();
|
||||||
pm.registerEvents(this.playerListener, this);
|
pm.registerEvents(this.playerListener, this);
|
||||||
pm.registerEvents(this.blockListener, this);
|
pm.registerEvents(this.blockListener, this);
|
||||||
|
pm.registerEvents(this.portalListener, this);
|
||||||
|
|
||||||
// Load gates
|
// Load gates
|
||||||
loadGates();
|
loadGates();
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,6 @@ import org.bukkit.Chunk;
|
|||||||
import org.bukkit.Location;
|
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.entity.Player;
|
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;
|
||||||
@ -17,7 +16,7 @@ import org.mcteam.ancientgates.Gate;
|
|||||||
import org.mcteam.ancientgates.Plugin;
|
import org.mcteam.ancientgates.Plugin;
|
||||||
|
|
||||||
|
|
||||||
public class PluginPlayerListener implements Listener
|
public class PluginPlayerListener extends BaseLocationListener implements Listener
|
||||||
{
|
{
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void onPlayerMove(PlayerMoveEvent event)
|
public void onPlayerMove(PlayerMoveEvent event)
|
||||||
@ -31,8 +30,8 @@ public class PluginPlayerListener implements Listener
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the nearest gate!
|
// Find the gate at the current location.
|
||||||
Gate gateAtLocation = getGateAtPlayerLocation(event);
|
Gate gateAtLocation = getValidGateAtPlayerLocation(event);
|
||||||
|
|
||||||
|
|
||||||
if (gateAtLocation == null) {
|
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) {
|
protected boolean hasPermission(Player player) {
|
||||||
if (player.hasPermission(Plugin.permissionUse)) {
|
if (player.hasPermission(Plugin.permissionUse)) {
|
||||||
return true;
|
return true;
|
||||||
@ -105,45 +82,4 @@ public class PluginPlayerListener implements Listener
|
|||||||
|
|
||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user