Add a method to unregister handlers.

This commit is contained in:
wizjany 2016-11-27 20:00:46 -05:00
parent deb283c44b
commit 104cdc023d

View File

@ -24,22 +24,8 @@
import com.sk89q.guavabackport.cache.LoadingCache; import com.sk89q.guavabackport.cache.LoadingCache;
import com.sk89q.worldguard.bukkit.BukkitUtil; import com.sk89q.worldguard.bukkit.BukkitUtil;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.session.handler.EntryFlag; import com.sk89q.worldguard.session.handler.*;
import com.sk89q.worldguard.session.handler.ExitFlag;
import com.sk89q.worldguard.session.handler.FarewellFlag;
import com.sk89q.worldguard.session.handler.FeedFlag;
import com.sk89q.worldguard.session.handler.GameModeFlag;
import com.sk89q.worldguard.session.handler.GodMode;
import com.sk89q.worldguard.session.handler.GreetingFlag;
import com.sk89q.worldguard.session.handler.Handler;
import com.sk89q.worldguard.session.handler.Handler.Factory; import com.sk89q.worldguard.session.handler.Handler.Factory;
import com.sk89q.worldguard.session.handler.HealFlag;
import com.sk89q.worldguard.session.handler.InvincibilityFlag;
import com.sk89q.worldguard.session.handler.NotifyEntryFlag;
import com.sk89q.worldguard.session.handler.NotifyExitFlag;
import com.sk89q.worldguard.session.handler.TimeLockFlag;
import com.sk89q.worldguard.session.handler.WaterBreathing;
import com.sk89q.worldguard.session.handler.WeatherLockFlag;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -48,10 +34,14 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -93,7 +83,7 @@ public Session load(CacheKey key) throws Exception {
public SessionManager(WorldGuardPlugin plugin) { public SessionManager(WorldGuardPlugin plugin) {
checkNotNull(plugin, "plugin"); checkNotNull(plugin, "plugin");
this.plugin = plugin; this.plugin = plugin;
registerDefaultHandlers(); handlers.addAll(defaultHandlers);
} }
/** /**
@ -148,23 +138,25 @@ public void resetState(Player player) {
private LinkedList<Factory<? extends Handler>> handlers = new LinkedList<Factory<? extends Handler>>(); private LinkedList<Factory<? extends Handler>> handlers = new LinkedList<Factory<? extends Handler>>();
private void registerDefaultHandlers() { private static final Set<Factory<? extends Handler>> defaultHandlers = new HashSet<Factory<? extends Handler>>();
// our list shouldn't be getting concurrently modified static {
// so we can safely order by providing null for 'after' Factory<?>[] factories = {
registerHandler(HealFlag.FACTORY, null); FeedFlag.FACTORY,
registerHandler(FeedFlag.FACTORY, null); FeedFlag.FACTORY,
registerHandler(NotifyEntryFlag.FACTORY, null); NotifyEntryFlag.FACTORY,
registerHandler(NotifyExitFlag.FACTORY, null); NotifyExitFlag.FACTORY,
registerHandler(EntryFlag.FACTORY, null); EntryFlag.FACTORY,
registerHandler(ExitFlag.FACTORY, null); ExitFlag.FACTORY,
registerHandler(FarewellFlag.FACTORY, null); FarewellFlag.FACTORY,
registerHandler(GreetingFlag.FACTORY, null); GreetingFlag.FACTORY,
registerHandler(GameModeFlag.FACTORY, null); GameModeFlag.FACTORY,
registerHandler(InvincibilityFlag.FACTORY, null); InvincibilityFlag.FACTORY,
registerHandler(TimeLockFlag.FACTORY, null); TimeLockFlag.FACTORY,
registerHandler(WeatherLockFlag.FACTORY, null); WeatherLockFlag.FACTORY,
registerHandler(GodMode.FACTORY, null); GodMode.FACTORY,
registerHandler(WaterBreathing.FACTORY, null); WaterBreathing.FACTORY
};
defaultHandlers.addAll(Arrays.asList(factories));
} }
/** /**
@ -181,10 +173,12 @@ private void registerDefaultHandlers() {
* @param after the handler factory to insert the first handler after, to ensure a specific order when creating new sessions * @param after the handler factory to insert the first handler after, to ensure a specific order when creating new sessions
* *
* @return {@code true} (as specified by {@link Collection#add}) * @return {@code true} (as specified by {@link Collection#add})
* {@code false} if {@param after} is not registered * {@code false} if {@param after} is not registered, or {@param factory} is null
*/ */
public boolean registerHandler(Factory<? extends Handler> factory, @Nullable Factory<? extends Handler> after) { public boolean registerHandler(Factory<? extends Handler> factory, @Nullable Factory<? extends Handler> after) {
if (factory == null) return false; if (factory == null) return false;
WorldGuardPlugin.inst().getLogger().log(Level.INFO, "Registering session handler "
+ factory.getClass().getEnclosingClass().getName());
if (after == null) { if (after == null) {
handlers.add(factory); handlers.add(factory);
} else { } else {
@ -196,6 +190,28 @@ public boolean registerHandler(Factory<? extends Handler> factory, @Nullable Fac
return true; return true;
} }
/**
* Unregister a handler.
*
* This will prevent it from being added to newly created sessions only. Existing
* sessions with the handler will continue to use it.
*
* Will return false if the handler was not registered to begin with.
*
* @param factory the handler factory to unregister
* @return true if the handler was registered and is now unregistered, false otherwise
*/
public boolean unregisterHandler(Factory<? extends Handler> factory) {
if (defaultHandlers.contains(factory)) {
WorldGuardPlugin.inst().getLogger().log(Level.WARNING, "Someone is unregistering a default WorldGuard handler: "
+ factory.getClass().getEnclosingClass().getName() + ". This may cause parts of WorldGuard to stop functioning");
} else {
WorldGuardPlugin.inst().getLogger().log(Level.INFO, "Unregistering session handler "
+ factory.getClass().getEnclosingClass().getName());
}
return handlers.remove(factory);
}
/** /**
* Create a session for a player. * Create a session for a player.
* *