package net.knarcraft.stargate.thread;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.portal.Portal;
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
*/
public class StarGateThread implements Runnable {
@Override
public void run() {
long time = System.currentTimeMillis() / 1000;
closeOpenPortals(time);
deactivateActivePortals(time);
}
/**
* Closes portals which are open and have timed out
*
* @param time
The current time
*/
private void closeOpenPortals(long time) {
List closedPortals = new ArrayList<>();
Queue openPortalsQueue = Stargate.getStargateConfig().getOpenPortalsQueue();
for (Portal portal : openPortalsQueue) {
//Skip always open and non-open gates
if (portal.getOptions().isAlwaysOn() || !portal.isOpen()) {
continue;
}
if (time > portal.getTriggeredTime() + Stargate.getGateConfig().getOpenTime()) {
portal.getPortalOpener().closePortal(false);
closedPortals.add(portal);
}
}
openPortalsQueue.removeAll(closedPortals);
}
/**
* De-activates portals which are active and have timed out
*
* @param time The current time
*/
private void deactivateActivePortals(long time) {
List deactivatedPortals = new ArrayList<>();
Queue activePortalsQueue = Stargate.getStargateConfig().getActivePortalsQueue();
for (Portal portal : activePortalsQueue) {
//Skip portals which aren't active
if (!portal.getPortalActivator().isActive()) {
continue;
}
if (time > portal.getTriggeredTime() + Stargate.getGateConfig().getActiveTime()) {
portal.getPortalActivator().deactivate();
deactivatedPortals.add(portal);
}
}
activePortalsQueue.removeAll(deactivatedPortals);
}
}