From f174d9003b05419e7822dafa4dc822f6e0233603 Mon Sep 17 00:00:00 2001 From: Steven Scott Date: Thu, 18 Aug 2011 21:00:02 -0700 Subject: [PATCH] Fixed issue with gate destruction Added StargateEvent API --- src/net/TheDgtl/Stargate/Portal.java | 19 +++++++- src/net/TheDgtl/Stargate/Stargate.java | 11 ++--- .../Stargate/event/StargateCloseEvent.java | 11 +++++ .../TheDgtl/Stargate/event/StargateEvent.java | 43 +++++++++++++++++++ .../Stargate/event/StargateListener.java | 28 ++++++++++++ .../Stargate/event/StargateOpenEvent.java | 25 +++++++++++ 6 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 src/net/TheDgtl/Stargate/event/StargateCloseEvent.java create mode 100644 src/net/TheDgtl/Stargate/event/StargateEvent.java create mode 100644 src/net/TheDgtl/Stargate/event/StargateListener.java create mode 100644 src/net/TheDgtl/Stargate/event/StargateOpenEvent.java diff --git a/src/net/TheDgtl/Stargate/Portal.java b/src/net/TheDgtl/Stargate/Portal.java index 94eacc1..cb9fbdc 100644 --- a/src/net/TheDgtl/Stargate/Portal.java +++ b/src/net/TheDgtl/Stargate/Portal.java @@ -9,6 +9,9 @@ import java.util.Iterator; import java.util.Scanner; import java.util.logging.Level; +import net.TheDgtl.Stargate.event.StargateCloseEvent; +import net.TheDgtl.Stargate.event.StargateOpenEvent; + import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -215,6 +218,12 @@ public class Portal { } public boolean open(Player openFor, boolean force) { + // Call the StargateOpenEvent + StargateOpenEvent event = new StargateOpenEvent(openFor, this, force); + Stargate.server.getPluginManager().callEvent(event); + if (event.isCancelled()) return false; + force = event.getForce(); + if (isOpen() && !force) return false; getWorld().loadChunk(getWorld().getChunkAt(topLeft.getBlock())); @@ -245,7 +254,14 @@ public class Portal { } public void close(boolean force) { - if (isAlwaysOn() && !force) return; // Never close an always open gate + if (!isOpen) return; + // Call the StargateCloseEvent + StargateCloseEvent event = new StargateCloseEvent(this, force); + Stargate.server.getPluginManager().callEvent(event); + if (event.isCancelled()) return; + force = event.getForce(); + + if (isAlwaysOn() && !force) return; // Only close always-open if forced // Close this gate, then the dest gate. for (Blox inside : getEntrances()) { @@ -722,7 +738,6 @@ public class Portal { Stargate.debug("createPortal", "h = " + hidden + " a = " + alwaysOn + " p = " + priv + " f = " + free + " b = " + backwards); if ((network.length() < 1) || (network.length() > 11)) { - Stargate.debug("createPortal", "Network name too long. Shortening"); network = Stargate.getDefaultNetwork(); } diff --git a/src/net/TheDgtl/Stargate/Stargate.java b/src/net/TheDgtl/Stargate/Stargate.java index bf62d6b..c1285a8 100644 --- a/src/net/TheDgtl/Stargate/Stargate.java +++ b/src/net/TheDgtl/Stargate/Stargate.java @@ -348,6 +348,7 @@ public class Stargate extends JavaPlugin { // Can access all networks if (hasPerm(player, "stargate.network")) return true; // Can access this network + Stargate.debug("canAccessNetwork", "stargate.network." + network); if (hasPerm(player, "stargate.network." + network)) return true; return false; } @@ -615,7 +616,7 @@ public class Stargate extends JavaPlugin { } } - Stargate.sendMessage(player, teleMsg); + Stargate.sendMessage(player, teleMsg, false); destination.teleport(player, portal, event); portal.close(false); } @@ -634,7 +635,7 @@ public class Stargate extends JavaPlugin { event.setUseItemInHand(Result.DENY); event.setUseInteractedBlock(Result.DENY); - if (!Stargate.canAccessNetwork(player, portal.getNetwork())) { + if (!Stargate.canAccessNetwork(player, portal.getNetwork())) { Stargate.sendMessage(player, denyMsg); return; } @@ -664,9 +665,6 @@ public class Stargate extends JavaPlugin { if (block.getType() == Material.WALL_SIGN) { Portal portal = Portal.getByBlock(block); if (portal == null) return; - // Cancel item use - event.setUseItemInHand(Result.DENY); - event.setUseInteractedBlock(Result.DENY); if (!Stargate.canAccessNetwork(player, portal.getNetwork())) { Stargate.sendMessage(player, denyMsg); @@ -735,6 +733,7 @@ public class Stargate extends JavaPlugin { if (!Stargate.canDestroy(player, portal)) { Stargate.sendMessage(player, "Permission Denied"); + Stargate.log.info("[Stargate] " + player.getName() + " tried to destroy gate"); event.setCancelled(true); return; } @@ -921,6 +920,7 @@ public class Stargate extends JavaPlugin { Portal p = iter.next(); // Skip always open gates if (p.isAlwaysOn()) continue; + if (!p.isOpen()) continue; if (time > p.getOpenTime() + Stargate.openTime) { p.close(false); iter.remove(); @@ -929,6 +929,7 @@ public class Stargate extends JavaPlugin { // Deactivate active portals for (Iterator iter = Stargate.activeList.iterator(); iter.hasNext();) { Portal p = iter.next(); + if (!p.isActive()) continue; if (time > p.getOpenTime() + Stargate.activeTime) { p.deactivate(); iter.remove(); diff --git a/src/net/TheDgtl/Stargate/event/StargateCloseEvent.java b/src/net/TheDgtl/Stargate/event/StargateCloseEvent.java new file mode 100644 index 0000000..8cdd0ff --- /dev/null +++ b/src/net/TheDgtl/Stargate/event/StargateCloseEvent.java @@ -0,0 +1,11 @@ +package net.TheDgtl.Stargate.event; + +import net.TheDgtl.Stargate.Portal; + +public class StargateCloseEvent extends StargateEvent { + private static final long serialVersionUID = -4382967941863636023L; + + public StargateCloseEvent(Portal portal, boolean force) { + super("StargateCloseEvent", portal, force); + } +} diff --git a/src/net/TheDgtl/Stargate/event/StargateEvent.java b/src/net/TheDgtl/Stargate/event/StargateEvent.java new file mode 100644 index 0000000..ab95c2b --- /dev/null +++ b/src/net/TheDgtl/Stargate/event/StargateEvent.java @@ -0,0 +1,43 @@ +package net.TheDgtl.Stargate.event; + +import net.TheDgtl.Stargate.Portal; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; + +public class StargateEvent extends Event implements Cancellable { + private static final long serialVersionUID = -5079274654178040431L; + protected Portal portal; + protected boolean cancelled; + protected boolean force; + + public StargateEvent(String event, Portal portal, boolean force) { + super (event); + this.portal = portal; + this.cancelled = false; + this.force = force; + } + + public Portal getPortal() { + return portal; + } + + public boolean getForce() { + return force; + } + + public void setForce(boolean force) { + this.force = force; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + +} diff --git a/src/net/TheDgtl/Stargate/event/StargateListener.java b/src/net/TheDgtl/Stargate/event/StargateListener.java new file mode 100644 index 0000000..0ac3d29 --- /dev/null +++ b/src/net/TheDgtl/Stargate/event/StargateListener.java @@ -0,0 +1,28 @@ +package net.TheDgtl.Stargate.event; + +import org.bukkit.event.CustomEventListener; +import org.bukkit.event.Event; +import org.bukkit.event.Listener; + +public class StargateListener extends CustomEventListener implements Listener { + public StargateListener() { + + } + + public void onStargateOpen(StargateOpenEvent event) { + + } + + public void onStargateClose(StargateCloseEvent event) { + + } + + @Override + public void onCustomEvent(Event event) { + if (event instanceof StargateOpenEvent) { + onStargateOpen((StargateOpenEvent)event); + } else if (event instanceof StargateCloseEvent) { + onStargateClose((StargateCloseEvent)event); + } + } +} diff --git a/src/net/TheDgtl/Stargate/event/StargateOpenEvent.java b/src/net/TheDgtl/Stargate/event/StargateOpenEvent.java new file mode 100644 index 0000000..6932c63 --- /dev/null +++ b/src/net/TheDgtl/Stargate/event/StargateOpenEvent.java @@ -0,0 +1,25 @@ +package net.TheDgtl.Stargate.event; + +import net.TheDgtl.Stargate.Portal; + +import org.bukkit.entity.Player; + +public class StargateOpenEvent extends StargateEvent { + private static final long serialVersionUID = -2804865767733660648L; + Player player; + + public StargateOpenEvent(Player player, Portal portal, boolean force) { + super ("StargateOpenEvent", portal, force); + + this.player = player; + this.portal = portal; + } + + /** + * Return the player than opened the gate. + * @return player than opened the gate + */ + public Player getPlayer() { + return player; + } +}