Introduce uninitialize for sessions (#1940)

* Introduce uninitialize for sessions

* Add FlagValueChangeHandler#onClearValue
This commit is contained in:
Joni Aromaa 2022-09-18 18:18:08 +03:00 committed by GitHub
parent f1f1e8aa7d
commit bd1d772faa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 0 deletions

View File

@ -146,6 +146,7 @@ public class BukkitWorldGuardPlatform implements WorldGuardPlatform {
@Override
public void unload() {
sessionManager.shutdown();
configuration.unload();
regionContainer.shutdown();
}

View File

@ -148,6 +148,8 @@ public class PlayerMoveListener extends AbstractListener {
if (loc != null) {
player.teleport(BukkitAdapter.adapt(loc));
}
session.uninitialize(localPlayer);
}
private class EntityMountListener implements Listener {

View File

@ -100,4 +100,11 @@ public class BukkitSessionManager extends AbstractSessionManager implements Runn
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);
}
}
}

View File

@ -120,6 +120,21 @@ public class Session {
}
}
/**
* 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.
*

View File

@ -45,6 +45,12 @@ public abstract class FlagValueChangeHandler<T> extends Handler {
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<ProtectedRegion> entered, Set<ProtectedRegion> exited, MoveType moveType) {
if (entered.isEmpty() && exited.isEmpty()
@ -74,4 +80,8 @@ public abstract class FlagValueChangeHandler<T> extends Handler {
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);
}
}

View File

@ -74,6 +74,16 @@ public abstract class Handler {
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.
*