Fixes comments for the PortalOpener class

This commit is contained in:
Kristian Knarvik 2021-10-21 00:27:42 +02:00
parent 4b42d4914a
commit 8eff9ae5e6
2 changed files with 76 additions and 49 deletions

View File

@ -50,7 +50,7 @@ public class Portal {
this.signDrawer = new PortalSignDrawer(this); this.signDrawer = new PortalSignDrawer(this);
this.portalOpener = new PortalOpener(this, destination); this.portalOpener = new PortalOpener(this, destination);
this.structure = new PortalStructure(this, gate, button); this.structure = new PortalStructure(this, gate, button);
this.portalActivator = portalOpener.getPortalOpener(); this.portalActivator = portalOpener.getPortalActivator();
} }
/** /**
@ -167,7 +167,7 @@ public class Portal {
* @return <p>The name of this portal's destination portal</p> * @return <p>The name of this portal's destination portal</p>
*/ */
public String getDestinationName() { public String getDestinationName() {
return portalOpener.getPortalOpener().getDestinationName(); return portalOpener.getPortalActivator().getDestinationName();
} }
/** /**

View File

@ -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 <p>Whether this portal activator's portal is open</p> * @return <p>Whether this portal opener's portal is open</p>
*/ */
public boolean isOpen() { public boolean isOpen() {
return isOpen || portal.getOptions().isAlwaysOn(); return isOpen || portal.getOptions().isAlwaysOn();
} }
/** /**
* Sets the time when this portal was activated * Sets the time when this portal was activated/opened
* *
* @param activatedTime <p>Unix timestamp when portal was activated</p> * @param activatedTime <p>Unix timestamp when portal was activated/opened</p>
*/ */
public void setActivatedTime(long activatedTime) { public void setActivatedTime(long activatedTime) {
this.activatedTime = activatedTime; this.activatedTime = activatedTime;
} }
/** /**
* Gets the destinations this portal activator has available * Gets the portal activator belonging to this portal opener
* *
* @return <p>The available destinations</p> * @return <p>The portal activator belonging to this portal opener</p>
*/ */
public PortalActivator getPortalOpener() { public PortalActivator getPortalActivator() {
return this.portalActivator; return this.portalActivator;
} }
/** /**
* Open this portal * Open this portal opener's portal
* *
* @param force <p>Whether to force this portal open, even if it's already open for some player</p> * @param force <p>Whether to force the portal open, even if it's already open for some player</p>
*/ */
public void openPortal(boolean force) { public void openPortal(boolean force) {
openPortal(null, force); openPortal(null, force);
} }
/** /**
* Open this portal * Open this portal opener's portal
* *
* @param force <p>Whether to force this portal open, even if it's already open for some player</p> * @param force <p>Whether to force the portal open, even if it's already open for some player</p>
*/ */
public void openPortal(Player openFor, boolean force) { 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); StargateOpenEvent event = new StargateOpenEvent(openFor, portal, force);
Stargate.server.getPluginManager().callEvent(event); Stargate.server.getPluginManager().callEvent(event);
if (event.isCancelled() || (isOpen() && !event.getForce())) { if (event.isCancelled() || (isOpen() && !event.getForce())) {
return; return;
} }
//Change the opening blocks to the correct type //Get the material to change the opening to
Material openType = portal.getGate().getPortalOpenBlock(); Material openType = portal.getGate().getPortalOpenBlock();
//Adjust orientation if applicable
Axis axis = (openType.createBlockData() instanceof Orientable) ? portal.getLocation().getRotationAxis() : null; Axis axis = (openType.createBlockData() instanceof Orientable) ? portal.getLocation().getRotationAxis() : null;
//Change the entrance blocks to the correct type
for (BlockLocation inside : portal.getStructure().getEntrances()) { for (BlockLocation inside : portal.getStructure().getEntrances()) {
Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, openType, axis)); Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, openType, axis));
} }
//Update the portal state to make is actually open
updatePortalOpenState(openFor); 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 <p>The player to open this portal for</p> * @param openFor <p>The player to open this portal opener's portal for</p>
*/ */
private void updatePortalOpenState(Player openFor) { private void updatePortalOpenState(Player openFor) {
//Update the open state of this portal //Update the open state of this portal
isOpen = true; isOpen = true;
activatedTime = System.currentTimeMillis() / 1000; activatedTime = System.currentTimeMillis() / 1000;
//Change state from active to open
Stargate.openPortalsQueue.add(portal); Stargate.openPortalsQueue.add(portal);
Stargate.activePortalsQueue.remove(portal); Stargate.activePortalsQueue.remove(portal);
PortalOptions options = portal.getOptions(); PortalOptions options = portal.getOptions();
//Open remote portal //If this portal is always open, opening the destination is not necessary
if (!options.isAlwaysOn()) { if (options.isAlwaysOn()) {
player = openFor; return;
}
Portal destination = portal.getPortalActivator().getDestination(); //Update the player the portal is open for
//Only open destination if it's not-fixed or points at this portal this.player = openFor;
if (!options.isRandom() && destination != null && (!destination.getOptions().isFixed() ||
destination.getDestinationName().equalsIgnoreCase(portal.getName())) && !destination.isOpen()) { Portal destination = portal.getPortalActivator().getDestination();
destination.getPortalOpener().openPortal(openFor, false); if (destination == null) {
destination.getPortalActivator().setDestination(portal); return;
if (destination.getStructure().isVerified()) { }
destination.drawSign();
} 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 <p>Whether to force this portal closed, even if it's set as always on</p> * @param force <p>Whether to force the portal closed, even if it's set as always on</p>
*/ */
public void closePortal(boolean force) { public void closePortal(boolean force) {
//No need to close a portal which is already closed
if (!isOpen) { if (!isOpen) {
return; 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); StargateCloseEvent event = new StargateCloseEvent(portal, force);
Stargate.server.getPluginManager().callEvent(event); Stargate.server.getPluginManager().callEvent(event);
if (event.isCancelled()) { if (event.isCancelled()) {
return; return;
} }
force = event.getForce();
//Only close always-open if forced to //Only close an always-open portal if forced to
if (portal.getOptions().isAlwaysOn() && !force) { if (portal.getOptions().isAlwaysOn() && !event.getForce()) {
return; return;
} }
//Close this gate, then the dest gate. //Close the portal by requesting the opening blocks to change
Material closedType = portal.getGate().getPortalClosedBlock(); Material closedType = portal.getGate().getPortalClosedBlock();
for (BlockLocation inside : portal.getStructure().getEntrances()) { for (BlockLocation entrance : portal.getStructure().getEntrances()) {
Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, closedType, null)); Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(entrance, closedType, null));
} }
//Update the portal state to make it actually closed
updatePortalClosedState(); updatePortalClosedState();
//Finally, deactivate the portal
portalActivator.deactivate(); portalActivator.deactivate();
} }
@ -157,44 +179,49 @@ public class PortalOpener {
* Updates this portal to be recognized as closed and closes its destination portal * Updates this portal to be recognized as closed and closes its destination portal
*/ */
private void updatePortalClosedState() { private void updatePortalClosedState() {
//Update the closed state of this portal //Unset the stored player and set the portal to closed
player = null; player = null;
isOpen = false; isOpen = false;
//Un-mark the portal as active and open
Stargate.openPortalsQueue.remove(portal); Stargate.openPortalsQueue.remove(portal);
Stargate.activePortalsQueue.remove(portal); Stargate.activePortalsQueue.remove(portal);
//Close remote portal //Close the destination portal if not always open
if (!portal.getOptions().isAlwaysOn()) { if (!portal.getOptions().isAlwaysOn()) {
Portal end = portal.getPortalActivator().getDestination(); Portal destination = portal.getPortalActivator().getDestination();
if (end != null && end.isOpen()) { if (destination != null && destination.isOpen()) {
//Clear its destination first //De-activate and close the destination portal
end.getPortalActivator().deactivate(); destination.getPortalActivator().deactivate();
end.getPortalOpener().closePortal(false); 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 <p>The player to check portal state for</p> * @param player <p>The player to check portal state for</p>
* @return <p>True if this portal is open to the given player</p> * @return <p>True if this portal opener's portal is open to the given player</p>
*/ */
public boolean isOpenFor(Player player) { public boolean isOpenFor(Player player) {
//If closed, it's closed for everyone
if (!isOpen) { if (!isOpen) {
return false; 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) { if (portal.getOptions().isAlwaysOn() || this.player == null) {
return true; 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()); 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 <p>The time this portal activator's portal opened</p> * @return <p>The time this portal opener's portal was activated/opened</p>
*/ */
public long getActivatedTime() { public long getActivatedTime() {
return activatedTime; return activatedTime;