diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index 3ff1259..681fdc0 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -50,7 +50,7 @@ public class Portal { this.signDrawer = new PortalSignDrawer(this); this.portalOpener = new PortalOpener(this, destination); this.structure = new PortalStructure(this, gate, button); - this.portalActivator = portalOpener.getPortalOpener(); + this.portalActivator = portalOpener.getPortalActivator(); } /** @@ -167,7 +167,7 @@ public class Portal { * @return

The name of this portal's destination portal

*/ public String getDestinationName() { - return portalOpener.getPortalOpener().getDestinationName(); + return portalOpener.getPortalActivator().getDestinationName(); } /** diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java b/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java index 3a26834..995479a 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalOpener.java @@ -33,123 +33,145 @@ public class PortalOpener { } /** - * Gets whether this portal activator's portal is currently open + * Gets whether this portal opener's portal is currently open * - * @return

Whether this portal activator's portal is open

+ * @return

Whether this portal opener's portal is open

*/ public boolean isOpen() { return isOpen || portal.getOptions().isAlwaysOn(); } /** - * Sets the time when this portal was activated + * Sets the time when this portal was activated/opened * - * @param activatedTime

Unix timestamp when portal was activated

+ * @param activatedTime

Unix timestamp when portal was activated/opened

*/ public void setActivatedTime(long activatedTime) { this.activatedTime = activatedTime; } /** - * Gets the destinations this portal activator has available + * Gets the portal activator belonging to this portal opener * - * @return

The available destinations

+ * @return

The portal activator belonging to this portal opener

*/ - public PortalActivator getPortalOpener() { + public PortalActivator getPortalActivator() { return this.portalActivator; } /** - * Open this portal + * Open this portal opener's portal * - * @param force

Whether to force this portal open, even if it's already open for some player

+ * @param force

Whether to force the portal open, even if it's already open for some player

*/ public void openPortal(boolean force) { openPortal(null, force); } /** - * Open this portal + * Open this portal opener's portal * - * @param force

Whether to force this portal open, even if it's already open for some player

+ * @param force

Whether to force the portal open, even if it's already open for some player

*/ public void openPortal(Player openFor, boolean force) { - //Call the StargateOpenEvent + //Call the StargateOpenEvent to allow the opening to be cancelled StargateOpenEvent event = new StargateOpenEvent(openFor, portal, force); Stargate.server.getPluginManager().callEvent(event); if (event.isCancelled() || (isOpen() && !event.getForce())) { return; } - //Change the opening blocks to the correct type + //Get the material to change the opening to Material openType = portal.getGate().getPortalOpenBlock(); + //Adjust orientation if applicable Axis axis = (openType.createBlockData() instanceof Orientable) ? portal.getLocation().getRotationAxis() : null; + + //Change the entrance blocks to the correct type for (BlockLocation inside : portal.getStructure().getEntrances()) { Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, openType, axis)); } + //Update the portal state to make is actually open updatePortalOpenState(openFor); } /** - * Updates this portal to be recognized as open and opens its destination portal + * Updates this portal opener's portal to be recognized as open and opens its destination portal * - * @param openFor

The player to open this portal for

+ * @param openFor

The player to open this portal opener's portal for

*/ private void updatePortalOpenState(Player openFor) { //Update the open state of this portal isOpen = true; activatedTime = System.currentTimeMillis() / 1000; + + //Change state from active to open Stargate.openPortalsQueue.add(portal); Stargate.activePortalsQueue.remove(portal); + PortalOptions options = portal.getOptions(); - //Open remote portal - if (!options.isAlwaysOn()) { - player = openFor; + //If this portal is always open, opening the destination is not necessary + if (options.isAlwaysOn()) { + return; + } - Portal destination = portal.getPortalActivator().getDestination(); - //Only open destination if it's not-fixed or points at this portal - if (!options.isRandom() && destination != null && (!destination.getOptions().isFixed() || - destination.getDestinationName().equalsIgnoreCase(portal.getName())) && !destination.isOpen()) { - destination.getPortalOpener().openPortal(openFor, false); - destination.getPortalActivator().setDestination(portal); - if (destination.getStructure().isVerified()) { - destination.drawSign(); - } + //Update the player the portal is open for + this.player = openFor; + + Portal destination = portal.getPortalActivator().getDestination(); + if (destination == null) { + return; + } + + boolean thisIsDestination = destination.getDestinationName().equalsIgnoreCase(portal.getName()); + //Only open destination if it's not-fixed or points at this portal, and is not already open + if (!options.isRandom() && (!destination.getOptions().isFixed() || thisIsDestination) && !destination.isOpen()) { + //Open the destination portal + destination.getPortalOpener().openPortal(openFor, false); + //Set the destination portal to this opener's portal + destination.getPortalActivator().setDestination(portal); + + //Update the destination's sign if it's verified + if (destination.getStructure().isVerified()) { + destination.drawSign(); } } } /** - * Closes this portal + * Closes this portal opener's portal * - * @param force

Whether to force this portal closed, even if it's set as always on

+ * @param force

Whether to force the portal closed, even if it's set as always on

*/ public void closePortal(boolean force) { + //No need to close a portal which is already closed if (!isOpen) { return; } - //Call the StargateCloseEvent + + //Call the StargateCloseEvent to allow other plugins to cancel the closing, or change whether to force it closed StargateCloseEvent event = new StargateCloseEvent(portal, force); Stargate.server.getPluginManager().callEvent(event); if (event.isCancelled()) { return; } - force = event.getForce(); - //Only close always-open if forced to - if (portal.getOptions().isAlwaysOn() && !force) { + //Only close an always-open portal if forced to + if (portal.getOptions().isAlwaysOn() && !event.getForce()) { return; } - //Close this gate, then the dest gate. + //Close the portal by requesting the opening blocks to change Material closedType = portal.getGate().getPortalClosedBlock(); - for (BlockLocation inside : portal.getStructure().getEntrances()) { - Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, closedType, null)); + for (BlockLocation entrance : portal.getStructure().getEntrances()) { + Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(entrance, closedType, null)); } + //Update the portal state to make it actually closed updatePortalClosedState(); + + //Finally, deactivate the portal portalActivator.deactivate(); } @@ -157,44 +179,49 @@ public class PortalOpener { * Updates this portal to be recognized as closed and closes its destination portal */ private void updatePortalClosedState() { - //Update the closed state of this portal + //Unset the stored player and set the portal to closed player = null; isOpen = false; + + //Un-mark the portal as active and open Stargate.openPortalsQueue.remove(portal); Stargate.activePortalsQueue.remove(portal); - //Close remote portal + //Close the destination portal if not always open if (!portal.getOptions().isAlwaysOn()) { - Portal end = portal.getPortalActivator().getDestination(); + Portal destination = portal.getPortalActivator().getDestination(); - if (end != null && end.isOpen()) { - //Clear its destination first - end.getPortalActivator().deactivate(); - end.getPortalOpener().closePortal(false); + if (destination != null && destination.isOpen()) { + //De-activate and close the destination portal + destination.getPortalActivator().deactivate(); + destination.getPortalOpener().closePortal(false); } } } /** - * Gets whether this portal is open for the given player + * Gets whether this portal opener's portal is open for the given player * * @param player

The player to check portal state for

- * @return

True if this portal is open to the given player

+ * @return

True if this portal opener's portal is open to the given player

*/ public boolean isOpenFor(Player player) { + //If closed, it's closed for everyone if (!isOpen) { return false; } + //If always on, or player is null which only happens with an always on portal, allow the player to pass if (portal.getOptions().isAlwaysOn() || this.player == null) { return true; } + //If the player is the player which the portal opened for, allow it to pass return player != null && player.getName().equalsIgnoreCase(this.player.getName()); } /** - * Gets the time this portal activator's portal opened + * Gets the time this portal opener's portal was activated/opened * - * @return

The time this portal activator's portal opened

+ * @return

The time this portal opener's portal was activated/opened

*/ public long getActivatedTime() { return activatedTime;