From bd1d772faa22d48f5d619b070908cb06600e45fd Mon Sep 17 00:00:00 2001 From: Joni Aromaa Date: Sun, 18 Sep 2022 18:18:08 +0300 Subject: [PATCH] Introduce uninitialize for sessions (#1940) * Introduce uninitialize for sessions * Add FlagValueChangeHandler#onClearValue --- .../bukkit/BukkitWorldGuardPlatform.java | 1 + .../bukkit/listener/PlayerMoveListener.java | 2 ++ .../bukkit/session/BukkitSessionManager.java | 7 +++++++ .../com/sk89q/worldguard/session/Session.java | 15 +++++++++++++++ .../session/handler/FlagValueChangeHandler.java | 10 ++++++++++ .../sk89q/worldguard/session/handler/Handler.java | 10 ++++++++++ 6 files changed, 45 insertions(+) diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldGuardPlatform.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldGuardPlatform.java index 50e4ba0a..69ef8656 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldGuardPlatform.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldGuardPlatform.java @@ -146,6 +146,7 @@ public void load() { @Override public void unload() { + sessionManager.shutdown(); configuration.unload(); regionContainer.shutdown(); } diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/PlayerMoveListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/PlayerMoveListener.java index 45185bad..925ecaa1 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/PlayerMoveListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/PlayerMoveListener.java @@ -148,6 +148,8 @@ public void onPlayerQuit(PlayerQuitEvent event) { if (loc != null) { player.teleport(BukkitAdapter.adapt(loc)); } + + session.uninitialize(localPlayer); } private class EntityMountListener implements Listener { diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/session/BukkitSessionManager.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/session/BukkitSessionManager.java index 24270c53..da23d6bf 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/session/BukkitSessionManager.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/session/BukkitSessionManager.java @@ -100,4 +100,11 @@ public boolean isUsingTimings() { public void setUsingTimings(boolean useTimings) { this.useTimings = useTimings; } + + public void shutdown() { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + get(localPlayer).uninitialize(localPlayer); + } + } } diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/session/Session.java b/worldguard-core/src/main/java/com/sk89q/worldguard/session/Session.java index b9109c8d..f1f3166b 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/session/Session.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/session/Session.java @@ -120,6 +120,21 @@ public void initialize(LocalPlayer player) { } } + /** + * Uninitialize the session. + * + * @param player The player + */ + public void uninitialize(LocalPlayer player) { + RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); + Location location = player.getLocation(); + ApplicableRegionSet set = query.getApplicableRegions(location); + + for (Handler handler : handlers.values()) { + handler.uninitialize(player, location, set); + } + } + /** * Tick the session. * diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/FlagValueChangeHandler.java b/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/FlagValueChangeHandler.java index acc87bbc..065b4191 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/FlagValueChangeHandler.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/FlagValueChangeHandler.java @@ -45,6 +45,12 @@ public final void initialize(LocalPlayer player, Location current, ApplicableReg onInitialValue(player, set, lastValue); } + @Override + public final void uninitialize(LocalPlayer player, Location current, ApplicableRegionSet set) { + onClearValue(player, set); + lastValue = null; + } + @Override public boolean onCrossBoundary(LocalPlayer player, Location from, Location to, ApplicableRegionSet toSet, Set entered, Set exited, MoveType moveType) { if (entered.isEmpty() && exited.isEmpty() @@ -74,4 +80,8 @@ public boolean onCrossBoundary(LocalPlayer player, Location from, Location to, A protected abstract boolean onAbsentValue(LocalPlayer player, Location from, Location to, ApplicableRegionSet toSet, T lastValue, MoveType moveType); + protected void onClearValue(LocalPlayer player, ApplicableRegionSet set) { + Location current = player.getLocation(); + onAbsentValue(player, current, current, set, lastValue, MoveType.OTHER_NON_CANCELLABLE); + } } diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/Handler.java b/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/Handler.java index def30c46..92962de2 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/Handler.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/session/handler/Handler.java @@ -74,6 +74,16 @@ public Session getSession() { public void initialize(LocalPlayer player, Location current, ApplicableRegionSet set) { } + /** + * Called when the session should clear its caches. + * + * @param player The player + * @param current The player's current location + * @param set The regions for the current location + */ + public void uninitialize(LocalPlayer player, Location current, ApplicableRegionSet set) { + } + /** * Called when {@link Session#testMoveTo(LocalPlayer, Location, MoveType)} is called. *