Adds missing comments to the world event listener and adds faster gate unloading

Replaces the clear all + load all with a method which just removes all
relevant portals directly. This should be faster, especially with many gates and worlds
This commit is contained in:
Kristian Knarvik 2021-02-20 16:21:18 +01:00
parent 1d642bfcf2
commit a475e8d8b1
2 changed files with 60 additions and 13 deletions

View File

@ -684,7 +684,7 @@ public class PortalHandler {
}
/**
* Clears all loaded gates and gate data
* Clears all loaded gates and gate data from all worlds
*/
public static void clearGates() {
lookupBlocks.clear();
@ -695,6 +695,44 @@ public class PortalHandler {
allPortalsNet.clear();
}
/**
* Clears all gates loaded in a given world
*
* @param world <p>The world containing the portals to clear</p>
*/
public static void clearGates(World world) {
//This is necessary
List<Portal> portalsToRemove = new ArrayList<>();
allPortals.forEach((portal) -> {
if (portal.getWorld().equals(world)) {
portalsToRemove.add(portal);
}
});
clearGates(portalsToRemove);
}
/**
* Clears a given list of portals from all relevant variables
*
* @param portalsToRemove <p>A list of portals to remove</p>
*/
private static void clearGates(List<Portal> portalsToRemove) {
List<String> portalNames = new ArrayList<>();
portalsToRemove.forEach((portal) -> portalNames.add(portal.getName()));
lookupBlocks.keySet().removeIf((key) -> portalsToRemove.contains(lookupBlocks.get(key)));
lookupNamesNet.keySet().forEach((network) -> lookupNamesNet.get(network).keySet().removeIf((key) ->
portalsToRemove.contains(lookupNamesNet.get(network).get(key))));
//Remove any networks with no portals
lookupNamesNet.keySet().removeIf((key) -> lookupNamesNet.get(key).isEmpty());
lookupEntrances.keySet().removeIf((key) -> portalsToRemove.contains(lookupEntrances.get(key)));
lookupControls.keySet().removeIf((key) -> portalsToRemove.contains(lookupControls.get(key)));
allPortals.removeIf(portalsToRemove::contains);
allPortalsNet.keySet().forEach((network) -> allPortalsNet.get(network).removeIf(portalNames::contains));
//Remove any networks with no portals
allPortalsNet.keySet().removeIf((network) -> allPortalsNet.get(network).isEmpty());
}
/**
* Loads all gates for the given world
*

View File

@ -8,28 +8,37 @@ import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
/**
* This listener listens for the loading and unloading of worlds to load and unload stargates
*/
@SuppressWarnings("unused")
public class WorldEventListener implements Listener {
/**
* This listener listens for the loading of a world and loads all all gates from the world if not already loaded
*
* @param event <p>The triggered world load event</p>
*/
@EventHandler
public void onWorldLoad(WorldLoadEvent event) {
if (!Stargate.managedWorlds.contains(event.getWorld().getName())
&& PortalHandler.loadAllGates(event.getWorld())) {
if (!Stargate.managedWorlds.contains(event.getWorld().getName()) &&
PortalHandler.loadAllGates(event.getWorld())) {
Stargate.managedWorlds.add(event.getWorld().getName());
}
}
// We need to reload all gates on world unload, boo
/**
* This listener listens for the unloading of a world
*
* @param event <p>The triggered world unload event</p>
*/
@EventHandler
public void onWorldUnload(WorldUnloadEvent event) {
Stargate.debug("onWorldUnload", "Reloading all Stargates");
World w = event.getWorld();
if (Stargate.managedWorlds.contains(w.getName())) {
Stargate.managedWorlds.remove(w.getName());
PortalHandler.clearGates();
for (World world : Stargate.server.getWorlds()) {
World world = event.getWorld();
if (Stargate.managedWorlds.contains(world.getName())) {
PortalHandler.loadAllGates(world);
}
}
Stargate.managedWorlds.remove(world.getName());
PortalHandler.clearGates(world);
}
}
}