Add DisableListener component and use for DataManager and Improbable.

This commit is contained in:
asofold 2013-09-19 21:00:57 +02:00
parent 3e85a92af6
commit 41e1e8017b
5 changed files with 45 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import fr.neatmonster.nocheatplus.actions.ParameterName;
import fr.neatmonster.nocheatplus.checks.Check;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.ViolationData;
import fr.neatmonster.nocheatplus.components.DisableListener;
import fr.neatmonster.nocheatplus.utilities.TickTask;
/**
@ -15,7 +16,7 @@ import fr.neatmonster.nocheatplus.utilities.TickTask;
* @author mc_dev
*
*/
public class Improbable extends Check {
public class Improbable extends Check implements DisableListener{
private static Improbable instance = null;
@ -85,4 +86,9 @@ public class Improbable extends Check {
return cancel;
}
@Override
public void onDisable() {
instance = null;
}
}

View File

@ -0,0 +1,13 @@
package fr.neatmonster.nocheatplus.components;
/**
* Component to listen to plugin/onDisable.
* @author mc_dev
*
*/
public interface DisableListener {
/**
* Called in the plugin in onDisable, before unregistration of all components.
*/
public void onDisable();
}

View File

@ -4,7 +4,7 @@ package fr.neatmonster.nocheatplus.components;
/**
* ComponentRegistry:
* <li>Supported components: Listener, TickListener, PermStateReceiver, INotifyReload, INeedConfig, IRemoveData, MCAccessHolder, ConsistencyChecker, JoinLeaveListener</li>
* <li>Supported components: Listener, TickListener, PermStateReceiver, INotifyReload, INeedConfig, IRemoveData, MCAccessHolder, ConsistencyChecker, JoinLeaveListener, DisableListener</li>
* <li>ComponentRegistry instances will be registered as sub registries unless you use the addComponent(Object, boolean) method appropriately. </li>
* <li>IHoldSubComponents instances will be registered in the next tick (scheduled task), those added within onEnable will get registered after looping in onEnable.</li>
* <li>JoinLeaveListeners are called on EventPriority.LOW.</li>

View File

@ -36,6 +36,7 @@ import fr.neatmonster.nocheatplus.checks.moving.MovingConfig;
import fr.neatmonster.nocheatplus.components.ComponentRegistry;
import fr.neatmonster.nocheatplus.components.ComponentWithName;
import fr.neatmonster.nocheatplus.components.ConsistencyChecker;
import fr.neatmonster.nocheatplus.components.DisableListener;
import fr.neatmonster.nocheatplus.components.IHaveCheckType;
import fr.neatmonster.nocheatplus.components.INeedConfig;
import fr.neatmonster.nocheatplus.components.INotifyReload;
@ -60,7 +61,7 @@ import fr.neatmonster.nocheatplus.utilities.StringUtil;
*
*/
@SetupOrder(priority = -80)
public class DataManager implements Listener, INotifyReload, INeedConfig, ComponentRegistry<IRemoveData>, ComponentWithName, ConsistencyChecker{
public class DataManager implements Listener, INotifyReload, INeedConfig, ComponentRegistry<IRemoveData>, ComponentWithName, ConsistencyChecker, DisableListener{
protected static DataManager instance = null;
@ -418,6 +419,7 @@ public class DataManager implements Listener, INotifyReload, INeedConfig, Compon
/**
* Cleanup method, removes all data and config, but does not call ConfigManager.cleanup.
*/
@Override
public void onDisable() {
clearData(CheckType.ALL);
iRemoveData.clear();

View File

@ -51,6 +51,7 @@ import fr.neatmonster.nocheatplus.compat.MCAccessFactory;
import fr.neatmonster.nocheatplus.components.ComponentRegistry;
import fr.neatmonster.nocheatplus.components.ComponentWithName;
import fr.neatmonster.nocheatplus.components.ConsistencyChecker;
import fr.neatmonster.nocheatplus.components.DisableListener;
import fr.neatmonster.nocheatplus.components.IHoldSubComponents;
import fr.neatmonster.nocheatplus.components.INeedConfig;
import fr.neatmonster.nocheatplus.components.INotifyReload;
@ -183,6 +184,8 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
/** Queued sub component holders, emptied on the next tick usually. */
protected final List<IHoldSubComponents> subComponentholders = new ArrayList<IHoldSubComponents>(20);
private final List<DisableListener> disableListeners = new ArrayList<DisableListener>();
/** All registered components. */
protected Set<Object> allComponents = new LinkedHashSet<Object>(50);
@ -431,6 +434,10 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
joinLeaveListeners.add((JoinLeaveListener) obj);
added = true;
}
if (obj instanceof DisableListener) {
disableListeners.add((DisableListener) obj);
added = true;
}
// Add to sub registries.
for (final ComponentRegistry<?> registry : subRegistries){
@ -511,6 +518,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
if (obj instanceof JoinLeaveListener){
joinLeaveListeners.remove((JoinLeaveListener) obj);
}
if (obj instanceof DisableListener) {
disableListeners.remove(obj);
}
// Remove sub registries.
if (obj instanceof ComponentRegistry<?>){
@ -584,8 +594,16 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
NCPExemptionManager.clear();
// Data cleanup.
if (verbose) LogUtil.logInfo("[NoCheatPlus] Cleanup DataManager...");
dataMan.onDisable();
if (verbose) LogUtil.logInfo("[NoCheatPlus] onDisable calls (include DataManager cleanup)...");
for (final DisableListener dl : disableListeners) {
try {
dl.onDisable();
} catch (Throwable t) {
// bad :)
LogUtil.logSevere("DisableListener (" + dl.getClass().getName() + "): " + t.getClass().getSimpleName() + " / " + t.getMessage());
LogUtil.logSevere(t);
}
}
// Hooks:
// (Expect external plugins to unregister their hooks on their own.)
@ -721,6 +739,7 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
initBlockProperties(config);
// Initialize data manager.
disableListeners.add(0, dataMan);
dataMan.onEnable();
// Allow entries to TickTask (just in case).