diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java index eb1838cd..6eed742a 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java @@ -45,6 +45,7 @@ import fr.neatmonster.nocheatplus.compat.MCAccess; import fr.neatmonster.nocheatplus.compat.MCAccessFactory; import fr.neatmonster.nocheatplus.components.ComponentWithName; import fr.neatmonster.nocheatplus.components.INeedConfig; +import fr.neatmonster.nocheatplus.components.MCAccessHolder; import fr.neatmonster.nocheatplus.components.NCPListener; import fr.neatmonster.nocheatplus.components.NameSetPermState; import fr.neatmonster.nocheatplus.components.NoCheatPlusAPI; @@ -253,31 +254,40 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI { private int dataManTaskId = -1; - /** - * Interfaces checked for managed listeners: IHaveMethodOrder (method), ComponentWithName (tag)
- */ @Override - public void addComponent(final Object obj) { - allComponents.add(obj); + public boolean addComponent(final Object obj) { + if (allComponents.contains(obj)) return false; + boolean added = false; if (obj instanceof Listener) { addListener((Listener) obj); + added = true; } if (obj instanceof INotifyReload) { notifyReload.add((INotifyReload) obj); if (obj instanceof INeedConfig) { ((INeedConfig) obj).onReload(); } + added = true; } if (obj instanceof TickListener){ TickTask.addTickListener((TickListener) obj); + added = true; } if (obj instanceof PermStateReceiver){ // No immediate update done. permStateReceivers.add((PermStateReceiver) obj); + added = true; + } + if (obj instanceof MCAccessHolder){ + // Add to allComponents. + ((MCAccessHolder) obj).setMCAccess(getMCAccess()); + added = true; } // Also add to DataManager, which will pick what it needs. // TODO: This is fishy in principle, something more concise? - dataMan.addComponent(obj); + if (dataMan.addComponent(obj)) added = true; + if (added) allComponents.add(obj); + return added; } /** @@ -361,6 +371,7 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI { TickTask.setLocked(true); TickTask.purge(); TickTask.cancel(); + TickTask.removeAllTickListeners(); // (Keep the tick task locked!) // Stop metrics task. @@ -502,6 +513,7 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI { public void onReload() { final ConfigFile config = ConfigManager.getConfigFile(); // Initialize BlockProperties + initMCAccess(config); initBlockProperties(config); // Reset Command protection. undoCommandChanges(); @@ -619,11 +631,24 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI { } /** - * Reset MCAccess and (re-) initialize BlockProperties, including config. + * Re-setup MCAccess and pass it to MCAccessHolder components. + * @param config + */ + protected void initMCAccess(final ConfigFile config) { + // Reset MCAccess. + NoCheatPlus.mcAccess = null; + final MCAccess mcAccess = getMCAccess(); + for (final Object obj : this.allComponents){ + if (obj instanceof MCAccessHolder){ + ((MCAccessHolder) obj).setMCAccess(mcAccess); + } + } + } + + /** + * Initialize BlockProperties, including config. */ protected void initBlockProperties(ConfigFile config){ - // Reset MCAccess. - mcAccess = null; // Set up BlockProperties. BlockProperties.init(getMCAccess()); BlockProperties.applyConfig(config, ConfPaths.COMPATIBILITY_BLOCKS); diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/Check.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/Check.java index 7bcb7ccb..c5c291e1 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/Check.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/Check.java @@ -9,6 +9,7 @@ import fr.neatmonster.nocheatplus.NoCheatPlus; import fr.neatmonster.nocheatplus.actions.ActionList; import fr.neatmonster.nocheatplus.actions.ParameterName; import fr.neatmonster.nocheatplus.compat.MCAccess; +import fr.neatmonster.nocheatplus.components.MCAccessHolder; import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager; import fr.neatmonster.nocheatplus.hooks.NCPHookManager; import fr.neatmonster.nocheatplus.logging.LogUtil; @@ -27,9 +28,9 @@ import fr.neatmonster.nocheatplus.utilities.TickTask; * MMMMMMMMMMM */ /** - * The parent class of all checks. + * The parent class of all checks. Don't let this implement Listener without knowing that this might be registered as component with NCP before the check-listeners. */ -public abstract class Check { +public abstract class Check implements MCAccessHolder{ /** The execution histories of each check. */ protected static Map histories = new HashMap(); @@ -50,7 +51,7 @@ public abstract class Check { /** The type. */ protected final CheckType type; - protected final MCAccess mcAccess; + protected MCAccess mcAccess; /** * Instantiates a new check. @@ -183,4 +184,15 @@ public abstract class Check { } return !NCPExemptionManager.isExempted(player, type); } + + @Override + public void setMCAccess(MCAccess mcAccess) { + this.mcAccess = mcAccess; + } + + @Override + public MCAccess getMCAccess() { + return mcAccess; + } + } diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/CheckListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/CheckListener.java index 2d3a9285..dd737b71 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/CheckListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/CheckListener.java @@ -2,6 +2,7 @@ package fr.neatmonster.nocheatplus.checks; import fr.neatmonster.nocheatplus.NoCheatPlus; import fr.neatmonster.nocheatplus.compat.MCAccess; +import fr.neatmonster.nocheatplus.components.MCAccessHolder; import fr.neatmonster.nocheatplus.components.NCPListener; /** @@ -10,11 +11,11 @@ import fr.neatmonster.nocheatplus.components.NCPListener; * @author mc_dev * */ -public class CheckListener extends NCPListener{ +public class CheckListener extends NCPListener implements MCAccessHolder{ /** Check group / type which this listener is for. */ protected final CheckType checkType; - protected final MCAccess mcAccess; + protected MCAccess mcAccess; public CheckListener(){ this(null); @@ -30,4 +31,25 @@ public class CheckListener extends NCPListener{ final String part = super.getComponentName(); return checkType == null ? part : part + "_" + checkType.name(); } + + @Override + public void setMCAccess(MCAccess mcAccess) { + this.mcAccess = mcAccess; + } + + @Override + public MCAccess getMCAccess() { + return mcAccess; + } + + /** + * Convenience method to add checks as components to NCP. + * @param check + * @return + */ + protected C addCheck(C check){ + // Could also set up a map from check type to check, etc. + NoCheatPlus.getAPI().addComponent(check); + return check; + } } diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java index 0b2ebdc4..14a263c5 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java @@ -44,22 +44,22 @@ import fr.neatmonster.nocheatplus.utilities.TickTask; public class BlockBreakListener extends CheckListener { /** The direction check. */ - private final Direction direction = new Direction(); + private final Direction direction = addCheck(new Direction()); /** The fast break check (per block breaking speed). */ - private final FastBreak fastBreak = new FastBreak(); + private final FastBreak fastBreak = addCheck(new FastBreak()); /** The frequency check (number of blocks broken) */ - private final Frequency frequency = new Frequency(); + private final Frequency frequency = addCheck(new Frequency()); /** The no swing check. */ - private final NoSwing noSwing = new NoSwing(); + private final NoSwing noSwing = addCheck(new NoSwing()); /** The reach check. */ - private final Reach reach = new Reach(); + private final Reach reach = addCheck(new Reach()); /** The wrong block check. */ - private final WrongBlock wrongBlock = new WrongBlock(); + private final WrongBlock wrongBlock = addCheck(new WrongBlock()); private boolean isInstaBreak = false; diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockinteract/BlockInteractListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockinteract/BlockInteractListener.java index 36487620..8fc08b48 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockinteract/BlockInteractListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockinteract/BlockInteractListener.java @@ -35,10 +35,10 @@ import fr.neatmonster.nocheatplus.checks.CheckType; public class BlockInteractListener extends CheckListener { /** The direction check. */ - private final Direction direction = new Direction(); + private final Direction direction = addCheck(new Direction()); /** The reach check. */ - private final Reach reach = new Reach(); + private final Reach reach = addCheck(new Reach()); public BlockInteractListener(){ super(CheckType.BLOCKINTERACT); diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java index be513e46..5a9b0671 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java @@ -46,19 +46,19 @@ import fr.neatmonster.nocheatplus.utilities.BlockProperties; public class BlockPlaceListener extends CheckListener { /** The direction check. */ - private final Direction direction = new Direction(); + private final Direction direction = addCheck(new Direction()); /** The fast place check. */ - private final FastPlace fastPlace = new FastPlace(); + private final FastPlace fastPlace = addCheck(new FastPlace()); /** The no swing check. */ - private final NoSwing noSwing = new NoSwing(); + private final NoSwing noSwing = addCheck(new NoSwing()); /** The reach check. */ - private final Reach reach = new Reach(); + private final Reach reach = addCheck(new Reach()); /** The speed check. */ - private final Speed speed = new Speed(); + private final Speed speed = addCheck(new Speed()); public BlockPlaceListener(){ super(CheckType.BLOCKPLACE); diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java index 3592df86..c9ec9aaa 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java @@ -41,22 +41,22 @@ public class ChatListener extends CheckListener implements INotifyReload { // Checks. /** Captcha handler. */ - private final Captcha captcha = new Captcha(); + private final Captcha captcha = addCheck(new Captcha()); /** The color check. */ - private final Color color = new Color(); + private final Color color = addCheck(new Color()); /** Commands repetition check. */ - private final Commands commands = new Commands(); + private final Commands commands = addCheck(new Commands()); /** Logins check (global) */ - private final Logins logins = new Logins(); + private final Logins logins = addCheck(new Logins()); /** Chat message check. */ - private final Text text = new Text(); + private final Text text = addCheck(new Text()); /** Relogging check. */ - private final Relog relog = new Relog(); + private final Relog relog = addCheck(new Relog()); // Auxiliary stuff. diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/combined/CombinedListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/combined/CombinedListener.java index ca519ee7..60977367 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/combined/CombinedListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/combined/CombinedListener.java @@ -22,13 +22,12 @@ import fr.neatmonster.nocheatplus.utilities.TickTask; */ public class CombinedListener extends CheckListener { - protected final Improbable improbable; + protected final Improbable improbable = addCheck(new Improbable()); - protected final MunchHausen munchHausen = new MunchHausen(); + protected final MunchHausen munchHausen = addCheck(new MunchHausen()); public CombinedListener(){ super(CheckType.COMBINED); - this.improbable = new Improbable(); } /** diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/fight/FightListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/fight/FightListener.java index 215580fa..a7e56a74 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/fight/FightListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/fight/FightListener.java @@ -41,28 +41,28 @@ import fr.neatmonster.nocheatplus.utilities.TickTask; public class FightListener extends CheckListener { /** The angle check. */ - private final Angle angle = new Angle(); + private final Angle angle = addCheck(new Angle()); /** The critical check. */ - private final Critical critical = new Critical(); + private final Critical critical = addCheck(new Critical()); /** The direction check. */ - private final Direction direction = new Direction(); + private final Direction direction = addCheck(new Direction()); /** The god mode check. */ - private final GodMode godMode = new GodMode(); + private final GodMode godMode = addCheck(new GodMode()); /** The knockback check. */ - private final Knockback knockback = new Knockback(); + private final Knockback knockback = addCheck(new Knockback()); /** The no swing check. */ - private final NoSwing noSwing = new NoSwing(); + private final NoSwing noSwing = addCheck(new NoSwing()); /** The reach check. */ - private final Reach reach = new Reach(); + private final Reach reach = addCheck(new Reach()); /** The self hit check */ - private final SelfHit selfHit = new SelfHit(); + private final SelfHit selfHit = addCheck(new SelfHit()); /** The speed check. */ private final Speed speed = new Speed(); diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java index 2e8b40d7..d82daf59 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java @@ -50,18 +50,18 @@ import fr.neatmonster.nocheatplus.checks.combined.Improbable; public class InventoryListener extends CheckListener { /** The drop check. */ - private final Drop drop = new Drop(); + private final Drop drop = addCheck(new Drop()); /** The fast click check. */ - private final FastClick fastClick = new FastClick(); + private final FastClick fastClick = addCheck(new FastClick()); /** The instant bow check. */ - private final InstantBow instantBow = new InstantBow(); + private final InstantBow instantBow = addCheck(new InstantBow()); /** The instant eat check. */ - private final InstantEat instantEat = new InstantEat(); + private final InstantEat instantEat = addCheck(new InstantEat()); - protected final Items items = new Items(); + protected final Items items = addCheck(new Items()); public InventoryListener(){ super(CheckType.INVENTORY); diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/moving/MovingListener.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/moving/MovingListener.java index 7466f8b9..22d8dc61 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/moving/MovingListener.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/checks/moving/MovingListener.java @@ -197,28 +197,28 @@ public class MovingListener extends CheckListener implements TickListener, IRemo /** The instance of NoCheatPlus. */ - private final NoCheatPlus plugin = (NoCheatPlus) Bukkit.getPluginManager().getPlugin( - "NoCheatPlus"); + private final NoCheatPlus plugin = (NoCheatPlus) Bukkit.getPluginManager().getPlugin("NoCheatPlus"); + /** The no fall check. **/ public final NoFall noFall = new NoFall(); /** The creative fly check. */ - private final CreativeFly creativeFly = new CreativeFly(); + private final CreativeFly creativeFly = addCheck(new CreativeFly()); /** The more packets check. */ - private final MorePackets morePackets = new MorePackets(); + private final MorePackets morePackets = addCheck(new MorePackets()); /** The more packets vehicle check. */ - private final MorePacketsVehicle morePacketsVehicle = new MorePacketsVehicle(); + private final MorePacketsVehicle morePacketsVehicle = addCheck(new MorePacketsVehicle()); /** The survival fly check. */ - private final SurvivalFly survivalFly = new SurvivalFly(); + private final SurvivalFly survivalFly = addCheck(new SurvivalFly()); /** The Passable (simple no-clip) check.*/ - private final Passable passable = new Passable(); + private final Passable passable = addCheck(new Passable()); /** Combined check but handled here (subject to change!) */ - private final BedLeave bedLeave = new BedLeave(); + private final BedLeave bedLeave = addCheck(new BedLeave()); /** * Unused instances.
diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/ComponentRegistry.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/ComponentRegistry.java index b62fb562..96e8fb1a 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/ComponentRegistry.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/ComponentRegistry.java @@ -3,8 +3,8 @@ package fr.neatmonster.nocheatplus.components; /** * A ComponentRegistry allows registering components, that then are delegated to where they belong.
* Notes: - *
  • Implementations should somehow specify when components can be registered and when they are unregistered automatically.
  • *
  • Implementations should somehow specify what components can be registered.
  • + *
  • Implementations should somehow specify if/when they are unregistered automatically.
  • * @author mc_dev * */ @@ -14,8 +14,9 @@ public interface ComponentRegistry { * like Listener, INotifyReload, INeedConfig.
    * For the NoCheatPlus instance this must be done after the configuration has been initialized. * @param obj + * @return If (newly) added. Adding an already present component should do nothing. */ - public void addComponent(final Object obj); + public boolean addComponent(final Object obj); /** * Remove a registered component.
    diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/MCAccessHolder.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/MCAccessHolder.java new file mode 100644 index 00000000..4686cb69 --- /dev/null +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/MCAccessHolder.java @@ -0,0 +1,23 @@ +package fr.neatmonster.nocheatplus.components; + +import fr.neatmonster.nocheatplus.compat.MCAccess; + +/** + * MCAccessHolder will be updated automatically with the current MCAccess. + *
    How to name this... + * @author mc_dev + * + */ +public interface MCAccessHolder { + /** + * Set access. + * @param mcAccess + */ + public void setMCAccess(MCAccess mcAccess); + + /** + * Getter. + * @return + */ + public MCAccess getMCAccess(); +} diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/NoCheatPlusAPI.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/NoCheatPlusAPI.java index 378d2aff..250eb541 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/NoCheatPlusAPI.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/components/NoCheatPlusAPI.java @@ -3,9 +3,10 @@ package fr.neatmonster.nocheatplus.components; /** * ComponentRegistry: - *
  • Supported components: Listener, TickListener, PermStateReceiver, INotifyReload, INeedConfig, IRemoveData
  • + *
  • Supported components: Listener, TickListener, PermStateReceiver, INotifyReload, INeedConfig, IRemoveData, MCAccessHolder
  • *
  • Registering components should be done during onEnable or any time while the plugin is enabled, all components will be unregistered in onDisable.
  • *
  • References to all components will be held until onDisable is finished.
  • + *
  • Interfaces checked for managed listeners: IHaveMethodOrder (method), ComponentWithName (tag)
  • * @author mc_dev * */ diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java index bf89a8ee..0aaeb5a5 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java @@ -314,9 +314,18 @@ public class DataManager implements Listener, INotifyReload, INeedConfig, Compon @Override - public void addComponent(Object obj) { + public boolean addComponent(Object obj) { if (obj instanceof IRemoveData) { - iRemoveData.add((IRemoveData) obj); + if (iRemoveData.contains(obj)){ + return false; + } + else{ + iRemoveData.add((IRemoveData) obj); + return true; + } + } + else{ + return true; } } diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/utilities/TickTask.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/utilities/TickTask.java index 84a2bbaa..91b65381 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/utilities/TickTask.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/utilities/TickTask.java @@ -190,6 +190,15 @@ public class TickTask implements Runnable { } } + /** + * Remove all of them. + */ + public static void removeAllTickListeners() { + synchronized (tickListeners) { + tickListeners.clear(); + } + } + /** * Get the tasks tick count. It is increased with every server tick.
    * NOTE: Can be called from other threads.