From 4d0df10f883c79007668e62cf4b663ecd51516dd Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Sun, 19 Aug 2018 13:01:08 +1000 Subject: [PATCH] Fixed sessions not saving on shutdown --- .../worldedit/bukkit/WorldEditPlugin.java | 2 +- .../worldedit/session/SessionManager.java | 64 +++++++++++-------- .../sk89q/worldedit/forge/ForgeWorldEdit.java | 4 +- .../worldedit/sponge/SpongeWorldEdit.java | 4 +- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 057742b92..ab0d08fba 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -150,7 +150,7 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter { @Override public void onDisable() { WorldEdit worldEdit = WorldEdit.getInstance(); - worldEdit.getSessionManager().clear(); + worldEdit.getSessionManager().unload(); worldEdit.getPlatformManager().unregister(server); if (config != null) { config.unload(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java index aa9b9e5df..929085bd5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java @@ -266,13 +266,50 @@ public class SessionManager { sessions.remove(getKey(owner)); } + /** + * Called to unload this session manager. + */ + public synchronized void unload() { + clear(); + } + /** * Remove all sessions. */ public synchronized void clear() { + saveChangedSessions(); sessions.clear(); } + private synchronized void saveChangedSessions() { + long now = System.currentTimeMillis(); + Iterator it = sessions.values().iterator(); + Map saveQueue = new HashMap<>(); + + while (it.hasNext()) { + SessionHolder stored = it.next(); + if (stored.key.isActive()) { + stored.lastActive = now; + + if (stored.session.compareAndResetDirty()) { + saveQueue.put(stored.key, stored.session); + } + } else { + if (now - stored.lastActive > EXPIRATION_GRACE) { + if (stored.session.compareAndResetDirty()) { + saveQueue.put(stored.key, stored.session); + } + + it.remove(); + } + } + } + + if (!saveQueue.isEmpty()) { + commit(saveQueue); + } + } + @Subscribe public void onConfigurationLoad(ConfigurationLoadEvent event) { LocalConfiguration config = event.getConfiguration(); @@ -302,32 +339,7 @@ public class SessionManager { @Override public void run() { synchronized (SessionManager.this) { - long now = System.currentTimeMillis(); - Iterator it = sessions.values().iterator(); - Map saveQueue = new HashMap<>(); - - while (it.hasNext()) { - SessionHolder stored = it.next(); - if (stored.key.isActive()) { - stored.lastActive = now; - - if (stored.session.compareAndResetDirty()) { - saveQueue.put(stored.key, stored.session); - } - } else { - if (now - stored.lastActive > EXPIRATION_GRACE) { - if (stored.session.compareAndResetDirty()) { - saveQueue.put(stored.key, stored.session); - } - - it.remove(); - } - } - } - - if (!saveQueue.isEmpty()) { - commit(saveQueue); - } + saveChangedSessions(); } } } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java index bf17292cd..963f2f748 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java @@ -136,7 +136,9 @@ public class ForgeWorldEdit { @EventHandler public void serverStopping(FMLServerStoppingEvent event) { - WorldEdit.getInstance().getPlatformManager().unregister(platform); + WorldEdit worldEdit = WorldEdit.getInstance(); + worldEdit.getSessionManager().unload(); + worldEdit.getPlatformManager().unregister(platform); } @EventHandler diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java index f2ba77982..d86e77c7c 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java @@ -147,7 +147,9 @@ public class SpongeWorldEdit { @Listener public void serverStopping(GameStoppingServerEvent event) { - WorldEdit.getInstance().getPlatformManager().unregister(platform); + WorldEdit worldEdit = WorldEdit.getInstance(); + worldEdit.getSessionManager().unload(); + worldEdit.getPlatformManager().unregister(platform); } @Listener