Add configuration if to manage listeners (change needs restart).

This commit is contained in:
asofold 2012-11-08 10:43:44 +01:00
parent 2a364b8cb3
commit 1235f7acd0
3 changed files with 54 additions and 22 deletions

View File

@ -39,6 +39,7 @@ import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile;
import fr.neatmonster.nocheatplus.config.ConfigManager;
import fr.neatmonster.nocheatplus.config.DefaultConfig;
import fr.neatmonster.nocheatplus.event.IHaveMethodOrder;
import fr.neatmonster.nocheatplus.event.ListenerManager;
import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager;
import fr.neatmonster.nocheatplus.metrics.Metrics;
@ -177,15 +178,14 @@ public class NoCheatPlus extends JavaPlugin implements Listener, NoCheatPlusAPI
protected List<CommandProtectionEntry> changedCommands = null;
protected final ListenerManager listenerManager = new ListenerManager(this, false);
private final ListenerManager listenerManager = new ListenerManager(this, false);
private boolean manageListeners = true;
@Override
public void addComponent(final Object obj) {
if (obj instanceof Listener) {
final Listener listener = (Listener) obj;
// Bukkit.getPluginManager().registerEvents(listener, this);
listenerManager.registerAllEventHandlers(listener, "NoCheatPlus");
listeners.add(listener);
addListener((Listener) obj);
}
if (obj instanceof INotifyReload) {
notifyReload.add((INotifyReload) obj);
@ -196,6 +196,29 @@ public class NoCheatPlus extends JavaPlugin implements Listener, NoCheatPlusAPI
dataMan.addComponent(obj);
}
private void addListener(final Listener listener) {
if (manageListeners){
listenerManager.registerAllEventHandlers(listener, "NoCheatPlus");
listeners.add(listener);
}
else{
Bukkit.getPluginManager().registerEvents(listener, this);
if (listener instanceof IHaveMethodOrder){
// TODO: Might log the order too, might prevent registration ?
// TODO: Alternative: queue listeners and register after startup (!)
CheckUtils.logWarning("[NoCheatPlus] Listener demands registration order, but listeners are not managed: " + listener.getClass().getName());
}
}
}
/**
* Test if NCP uses the ListenerManager at all.
* @return If so.
*/
public boolean doesManageListeners(){
return manageListeners;
}
@Override
public void removeComponent(final Object obj) {
if (obj instanceof Listener){
@ -293,10 +316,17 @@ public class NoCheatPlus extends JavaPlugin implements Listener, NoCheatPlusAPI
BlockProperties.applyConfig(config, ConfPaths.COMPATIBILITY_BLOCKS); // Temp probably,
// List the events listeners and register.
// Bukkit.getPluginManager().registerEvents(this, this);
manageListeners = config.getBoolean(ConfPaths.MISCELLANEOUS_MANAGELISTENERS);
if (manageListeners) {
listenerManager.setRegisterDirectly(true);
listenerManager.registerAllWithBukkit();
listenerManager.registerAllEventHandlers(this, "NoCheatPlus");
}
else{
// Just for safety.
listenerManager.setRegisterDirectly(false);
listenerManager.clear();
}
addListener(this);
for (final Object obj : new Object[]{
NCPExemptionManager.getListener(),
dataMan,

View File

@ -50,6 +50,7 @@ public abstract class ConfPaths {
public static final String MISCELLANEOUS_CHECKFORUPDATES = MISCELLANEOUS + "checkforupdates";
public static final String MISCELLANEOUS_UPDATETIMEOUT = MISCELLANEOUS + "updatetimeout";
public static final String MISCELLANEOUS_REPORTTOMETRICS = MISCELLANEOUS + "reporttometrics";
public static final String MISCELLANEOUS_MANAGELISTENERS = MISCELLANEOUS + "managelisteners";
private static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY = MISCELLANEOUS + "nomovedtooquickly.";
public static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY_ENABLED = MISCELLANEOUS_NOMOVEDTOOQUICKLY + "enabled";
public static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY_USEPROXY = MISCELLANEOUS_NOMOVEDTOOQUICKLY + "useproxy";

View File

@ -60,6 +60,7 @@ public class DefaultConfig extends ConfigFile {
set(ConfPaths.MISCELLANEOUS_ALLOWCLIENTMODS, false);
set(ConfPaths.MISCELLANEOUS_OPINCONSOLEONLY, false);
set(ConfPaths.MISCELLANEOUS_PROTECTPLUGINS, true);
set(ConfPaths.MISCELLANEOUS_MANAGELISTENERS, true);
// set(ConfPaths.MISCELLANEOUS_CHECKFORUPDATES, true);
set(ConfPaths.MISCELLANEOUS_REPORTTOMETRICS, true);