Renames activatedTime to triggeredTime and makes some public fields private

Makes the active portals queue and open portals queue and languageLoader fields private
Cleans the StargateThread a bit
Renames activatedTime to triggeredTime as the dual use (open and activate) made the name confusing
This commit is contained in:
Kristian Knarvik 2021-10-29 17:22:58 +02:00
parent 5d84e1d78a
commit 0237f45046
7 changed files with 116 additions and 48 deletions

View File

@ -16,7 +16,6 @@ import net.knarcraft.stargate.listener.PortalEventListener;
import net.knarcraft.stargate.listener.TeleportEventListener;
import net.knarcraft.stargate.listener.VehicleEventListener;
import net.knarcraft.stargate.listener.WorldEventListener;
import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler;
import net.knarcraft.stargate.portal.PortalRegistry;
import net.knarcraft.stargate.thread.BlockChangeThread;
@ -35,17 +34,17 @@ import java.io.File;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The main class of the Stargate plugin
*/
@SuppressWarnings("unused")
public class Stargate extends JavaPlugin {
//Used for changing gate open/closed material.
public static final Queue<BlockChangeRequest> blockChangeRequestQueue = new LinkedList<>();
public static final ConcurrentLinkedQueue<Portal> openPortalsQueue = new ConcurrentLinkedQueue<>();
public static final ConcurrentLinkedQueue<Portal> activePortalsQueue = new ConcurrentLinkedQueue<>();
private static final Queue<BlockChangeRequest> blockChangeRequestQueue = new LinkedList<>();
private static final Queue<ChunkUnloadRequest> chunkUnloadQueue = new PriorityQueue<>();
private static Logger logger;
@ -76,6 +75,26 @@ public class Stargate extends JavaPlugin {
super(loader, descriptionFile, dataFolder, file);
}
/**
* Adds a block change request to the request queue
*
* @param request <p>The request to add</p>
*/
public static void addBlockChangeRequest(BlockChangeRequest request) {
if (request != null) {
blockChangeRequestQueue.add(request);
}
}
/**
* Gets the queue containing block change requests
*
* @return <p>A block change request queue</p>
*/
public static Queue<BlockChangeRequest> getBlockChangeRequestQueue() {
return blockChangeRequestQueue;
}
/**
* Gets the sender for sending messages to players
*

View File

@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;
@ -29,13 +30,13 @@ import java.util.logging.Logger;
*/
public final class StargateConfig {
public final ConcurrentLinkedQueue<Portal> activePortalsQueue = new ConcurrentLinkedQueue<>();
public final ConcurrentLinkedQueue<Portal> openPortalsQueue = new ConcurrentLinkedQueue<>();
private final Queue<Portal> activePortalsQueue = new ConcurrentLinkedQueue<>();
private final Queue<Portal> openPortalsQueue = new ConcurrentLinkedQueue<>();
private final HashSet<String> managedWorlds = new HashSet<>();
private StargateGateConfig stargateGateConfig;
private MessageSender messageSender;
public final LanguageLoader languageLoader;
private final LanguageLoader languageLoader;
private EconomyConfig economyConfig;
private final Logger logger;
@ -89,6 +90,28 @@ public final class StargateConfig {
setupVaultEconomy();
}
/**
* Gets the queue of open portals
*
* <p>The open portals queue is used to close open portals after some time has passed</p>
*
* @return <p>The open portals queue</p>
*/
public Queue<Portal> getOpenPortalsQueue() {
return openPortalsQueue;
}
/**
* Gets the queue of active portals
*
* <p>The active portals queue is used to de-activate portals after some time has passed</p>
*
* @return <p>The active portals queue</p>
*/
public Queue<Portal> getActivePortalsQueue() {
return activePortalsQueue;
}
/**
* Gets whether debugging is enabled
*
@ -127,10 +150,10 @@ public final class StargateConfig {
//Perform all block change requests to prevent mismatch if a gate's open-material changes. Changing the
// closed-material still requires a restart.
BlockChangeRequest firstElement = Stargate.blockChangeRequestQueue.peek();
BlockChangeRequest firstElement = Stargate.getBlockChangeRequestQueue().peek();
while (firstElement != null) {
BlockChangeThread.pollQueue();
firstElement = Stargate.blockChangeRequestQueue.peek();
firstElement = Stargate.getBlockChangeRequestQueue().peek();
}
//Store the old enable bungee state in case it changes

View File

@ -125,15 +125,15 @@ public class Portal {
}
/**
* Gets the time this portal was activated/opened
* Gets the time this portal was triggered (activated/opened)
*
* <p>The time is given in the equivalent of a Unix timestamp. It's used to decide when a portal times out and
* automatically closes.</p>
* automatically closes/deactivates.</p>
*
* @return <p>The time this portal was activated/opened</p>
* @return <p>The time this portal was triggered (activated/opened)</p>
*/
public long getActivatedTime() {
return portalOpener.getActivatedTime();
public long getTriggeredTime() {
return portalOpener.getTriggeredTime();
}
/**

View File

@ -131,7 +131,7 @@ public class PortalActivator {
this.destinations.clear();
//Adds the active gate to the active queue to allow it to be remotely deactivated
Stargate.activePortalsQueue.add(portal);
Stargate.getStargateConfig().getActivePortalsQueue().add(portal);
//Set the given player as the active player
activePlayer = player;
@ -166,7 +166,7 @@ public class PortalActivator {
StargateActivateEvent event = new StargateActivateEvent(portal, player, destinations, destination);
Stargate.server.getPluginManager().callEvent(event);
if (event.isCancelled()) {
Stargate.activePortalsQueue.remove(portal);
Stargate.getStargateConfig().getActivePortalsQueue().remove(portal);
return false;
}
@ -189,7 +189,7 @@ public class PortalActivator {
}
//Un-mark the portal as activated
Stargate.activePortalsQueue.remove(portal);
Stargate.getStargateConfig().getActivePortalsQueue().remove(portal);
//For a fixed gate, the destinations and the sign never really change, but at the same time, fixed gates are
// never really activated, so in theory, this check should be redundant.
@ -261,7 +261,7 @@ public class PortalActivator {
//Update the activated time to allow it to be deactivated after a timeout, and re-draw the sign to show the
// selected destination
opener.setActivatedTime(System.currentTimeMillis() / 1000);
opener.setTriggeredTime(System.currentTimeMillis() / 1000);
portal.drawSign();
}

View File

@ -17,7 +17,7 @@ public class PortalOpener {
private boolean isOpen = false;
private final Portal portal;
private long activatedTime;
private long triggeredTime;
private Player player;
private final PortalActivator portalActivator;
@ -42,12 +42,12 @@ public class PortalOpener {
}
/**
* Sets the time when this portal was activated/opened
* Sets the time when this portal was triggered (activated/opened)
*
* @param activatedTime <p>Unix timestamp when portal was activated/opened</p>
* @param triggeredTime <p>Unix timestamp when portal was triggered</p>
*/
public void setActivatedTime(long activatedTime) {
this.activatedTime = activatedTime;
public void setTriggeredTime(long triggeredTime) {
this.triggeredTime = triggeredTime;
}
/**
@ -88,7 +88,7 @@ public class PortalOpener {
//Change the entrance blocks to the correct type
for (BlockLocation inside : portal.getStructure().getEntrances()) {
Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, openType, axis));
Stargate.addBlockChangeRequest(new BlockChangeRequest(inside, openType, axis));
}
//Update the portal state to make is actually open
@ -103,11 +103,11 @@ public class PortalOpener {
private void updatePortalOpenState(Player openFor) {
//Update the open state of this portal
isOpen = true;
activatedTime = System.currentTimeMillis() / 1000;
triggeredTime = System.currentTimeMillis() / 1000;
//Change state from active to open
Stargate.openPortalsQueue.add(portal);
Stargate.activePortalsQueue.remove(portal);
Stargate.getStargateConfig().getOpenPortalsQueue().add(portal);
Stargate.getStargateConfig().getActivePortalsQueue().remove(portal);
PortalOptions options = portal.getOptions();
@ -165,7 +165,7 @@ public class PortalOpener {
//Close the portal by requesting the opening blocks to change
Material closedType = portal.getGate().getPortalClosedBlock();
for (BlockLocation entrance : portal.getStructure().getEntrances()) {
Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(entrance, closedType, null));
Stargate.addBlockChangeRequest(new BlockChangeRequest(entrance, closedType, null));
}
//Update the portal state to make it actually closed
@ -184,8 +184,8 @@ public class PortalOpener {
isOpen = false;
//Un-mark the portal as active and open
Stargate.openPortalsQueue.remove(portal);
Stargate.activePortalsQueue.remove(portal);
Stargate.getStargateConfig().getOpenPortalsQueue().remove(portal);
Stargate.getStargateConfig().getActivePortalsQueue().remove(portal);
//Close the destination portal if not always open
if (!portal.getOptions().isAlwaysOn()) {
@ -219,12 +219,12 @@ public class PortalOpener {
}
/**
* Gets the time this portal opener's portal was activated/opened
* Gets the time this portal opener's portal was triggered (activated/opened)
*
* @return <p>The time this portal opener's portal was activated/opened</p>
* @return <p>The time this portal opener's portal was triggered</p>
*/
public long getActivatedTime() {
return activatedTime;
public long getTriggeredTime() {
return triggeredTime;
}
}

View File

@ -30,7 +30,7 @@ public class BlockChangeThread implements Runnable {
*/
public static void pollQueue() {
//Abort if there's no work to be done
BlockChangeRequest blockChangeRequest = Stargate.blockChangeRequestQueue.poll();
BlockChangeRequest blockChangeRequest = Stargate.getBlockChangeRequestQueue().poll();
if (blockChangeRequest == null) {
return;
}

View File

@ -3,7 +3,9 @@ package net.knarcraft.stargate.thread;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.portal.Portal;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
/**
* This class contains the function used to close servers which should no longer be open/active
@ -13,29 +15,53 @@ public class StarGateThread implements Runnable {
@Override
public void run() {
long time = System.currentTimeMillis() / 1000;
//Close open portals
for (Iterator<Portal> iterator = Stargate.openPortalsQueue.iterator(); iterator.hasNext(); ) {
Portal portal = iterator.next();
closeOpenPortals(time);
deactivateActivePortals(time);
}
/**
* Closes portals which are open and have timed out
*
* @param time <p>The current time</p>
*/
private void closeOpenPortals(long time) {
List<Portal> closedPortals = new ArrayList<>();
Queue<Portal> openPortalsQueue = Stargate.getStargateConfig().getOpenPortalsQueue();
for (Portal portal : openPortalsQueue) {
//Skip always open and non-open gates
if (portal.getOptions().isAlwaysOn() || !portal.isOpen()) {
if (portal.getOptions().isAlwaysOn() || portal.getOptions().isRandom() || portal.getOptions().isBungee() ||
!portal.isOpen()) {
continue;
}
if (time > portal.getActivatedTime() + Stargate.getGateConfig().getOpenTime()) {
if (time > portal.getTriggeredTime() + Stargate.getGateConfig().getOpenTime()) {
portal.getPortalOpener().closePortal(false);
iterator.remove();
closedPortals.add(portal);
}
}
//Deactivate active portals
for (Iterator<Portal> iterator = Stargate.activePortalsQueue.iterator(); iterator.hasNext(); ) {
Portal portal = iterator.next();
openPortalsQueue.removeAll(closedPortals);
}
/**
* De-activates portals which are active and have timed out
*
* @param time <p>The current time</p>
*/
private void deactivateActivePortals(long time) {
List<Portal> deactivatedPortals = new ArrayList<>();
Queue<Portal> activePortalsQueue = Stargate.getStargateConfig().getActivePortalsQueue();
for (Portal portal : activePortalsQueue) {
//Skip portals which aren't active
if (!portal.getPortalActivator().isActive()) {
continue;
}
if (time > portal.getActivatedTime() + Stargate.getGateConfig().getActiveTime()) {
if (time > portal.getTriggeredTime() + Stargate.getGateConfig().getActiveTime()) {
portal.getPortalActivator().deactivate();
iterator.remove();
deactivatedPortals.add(portal);
}
}
activePortalsQueue.removeAll(deactivatedPortals);
}
}