diff --git a/src/fr/neatmonster/nocheatplus/hooks/IFirst.java b/src/fr/neatmonster/nocheatplus/hooks/IFirst.java new file mode 100644 index 00000000..2d869fb9 --- /dev/null +++ b/src/fr/neatmonster/nocheatplus/hooks/IFirst.java @@ -0,0 +1,10 @@ +package fr.neatmonster.nocheatplus.hooks; + +/** + * Indicate that an object is intended to be sorted in first. + * @author mc_dev + * + */ +public interface IFirst { + +} diff --git a/src/fr/neatmonster/nocheatplus/hooks/ILast.java b/src/fr/neatmonster/nocheatplus/hooks/ILast.java new file mode 100644 index 00000000..62018e49 --- /dev/null +++ b/src/fr/neatmonster/nocheatplus/hooks/ILast.java @@ -0,0 +1,10 @@ +package fr.neatmonster.nocheatplus.hooks; + +/** + * Indicate that an object is intended to be sorted in last. + * @author mc_dev + * + */ +public interface ILast { + +} diff --git a/src/fr/neatmonster/nocheatplus/hooks/IStats.java b/src/fr/neatmonster/nocheatplus/hooks/IStats.java index e3925490..31b2496c 100644 --- a/src/fr/neatmonster/nocheatplus/hooks/IStats.java +++ b/src/fr/neatmonster/nocheatplus/hooks/IStats.java @@ -2,7 +2,8 @@ package fr.neatmonster.nocheatplus.hooks; /** * Interface to indicate an object is just used to collect stats, - * it will get the violations first and can not cancel vl-processing. + * it can not cancel vl-processing. If ILast is implemented as well, + * this will be sorted after hooks that can cancel, otherwise before those. * @author mc_dev * */ diff --git a/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java b/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java index 6e50651e..4caa2ac9 100644 --- a/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java +++ b/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java @@ -3,6 +3,7 @@ package fr.neatmonster.nocheatplus.hooks; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.LinkedList; @@ -49,6 +50,25 @@ public final class NCPHookManager { /** Mapping the check types to the hooks. */ private static final Map> hooksByChecks = new HashMap>(); + + private static Comparator HookComparator = new Comparator() { + @Override + public int compare(final NCPHook o1, final NCPHook o2) { + final boolean s1 = o1 instanceof IStats; + final boolean f1 = o1 instanceof IFirst; + final boolean l1 = o1 instanceof ILast; + final boolean s2 = o2 instanceof IStats; + final boolean f2 = o2 instanceof IFirst; + final boolean l2 = o2 instanceof ILast; + if (s1 && !s2) return l1 ? 1 : -1; + else if (!s1 && s2) return l2 ? -1 : 1; + else if (l2) return -1; + else if (l1) return 1; + else if (f1) return -1; + else if (f2) return 1; + else return 0; + } + }; static{ // Fill the map to be sure that thread safety can be guaranteed. @@ -105,11 +125,12 @@ public final class NCPHookManager { private static void addToMapping(final CheckType checkType, final NCPHook hook) { final List hooks = hooksByChecks.get(checkType); if (!hooks.contains(hook)){ - if (hook instanceof IStats) hooks.add(0, hook); + if (!(hook instanceof ILast) && (hook instanceof IStats || hook instanceof IFirst)) hooks.add(0, hook); else hooks.add(hook); + Collections.sort(hooks, HookComparator); } } - + /** * Add hook to the hooksByChecks mappings.
* Assumes that the hook already has been registered in the allHooks map.