Fixed issue with gate destruction

Added StargateEvent API
This commit is contained in:
Steven Scott 2011-08-18 21:00:02 -07:00
parent 1b117c3132
commit f174d9003b
6 changed files with 130 additions and 7 deletions

View File

@ -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();
}

View File

@ -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);
}
@ -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<Portal> iter = Stargate.activeList.iterator(); iter.hasNext();) {
Portal p = iter.next();
if (!p.isActive()) continue;
if (time > p.getOpenTime() + Stargate.activeTime) {
p.deactivate();
iter.remove();

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}