From d2d45176126b60544db000e80d05f8aa76cbd5d0 Mon Sep 17 00:00:00 2001 From: asofold Date: Fri, 10 Aug 2012 14:55:49 +0200 Subject: [PATCH] Fix and performance improvement for hooks. --- .../nocheatplus/hooks/NCPHookManager.java | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java b/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java index 27c32916..f0e83a4b 100644 --- a/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java +++ b/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java @@ -84,7 +84,7 @@ public final class NCPHookManager { } /** - * Add to the mapping for given check type (only). + * Add to the mapping for given check type, no extra actions or recursion. * * @param checkType * the check type @@ -102,8 +102,8 @@ public final class NCPHookManager { } /** - * Add hook to the hooksByChecks mappings, for the check type and if present, group type. Assumes that the hook - * already has been registered in the allHooks map. + * Add hook to the hooksByChecks mappings.
+ * Assumes that the hook already has been registered in the allHooks map. * * @param checkType * the check type @@ -111,12 +111,36 @@ public final class NCPHookManager { * the hook */ private static void addToMappings(final CheckType checkType, final NCPHook hook) { + if (checkType == CheckType.ALL){ + for (final CheckType refType : CheckType.values()){ + addToMapping(refType, hook); + } + return; + } addToMapping(checkType, hook); - if (checkType.group != null) - addToMapping(checkType.group, hook); + for (final CheckType refType : CheckType.values()){ + addToMappingsRecursively(checkType, refType, hook); + } } - + /** + * Add to mappings if checkType is a parent in the tree structure leading to refType. + * @param checkType + * @param refType + * @param hook + */ + private static void addToMappingsRecursively(final CheckType checkType, CheckType refType, final NCPHook hook) { + if (refType.group == null) + return; + else if (refType.group == checkType){ + addToMapping(refType, hook); + return; + } + else + addToMappingsRecursively(checkType, refType.group, hook); + } + + /** * Call the hooks for the specified check type and player. * * @param checkType @@ -377,29 +401,14 @@ public final class NCPHookManager { * @return if we should cancel the VL processing */ public static final boolean shouldCancelVLProcessing(final CheckType checkType, final Player player) { - // Checks for hooks registered for all events, only for the group and specifically for the check. - // A paradigm could be to return true as soon as one hook has returned true. - - // Check specific. + // Checks for hooks registered for this event, parent groups or ALL will be inserted into the list. + // Return true as soon as one hook returns true. + + // Test hooks, if present: final List hooksCheck = hooksByChecks.get(checkType); if (hooksCheck != null) if (applyHooks(checkType, player, hooksCheck)) return true; - - // Group specific. - if (checkType.group != null) { - final List hooksGroup = hooksByChecks.get(checkType.group); - if (hooksGroup != null) - if (applyHooks(checkType, player, hooksGroup)) - return true; - } - - // General (all). - final List hooksAll = hooksByChecks.get(CheckType.ALL); - if (hooksAll != null) - if (applyHooks(checkType, player, hooksAll)) - return true; - return false; } }