From 77216f0799f76b1c986028547d3a52497acbb2d5 Mon Sep 17 00:00:00 2001 From: asofold Date: Wed, 17 Oct 2012 18:31:44 +0200 Subject: [PATCH] Add Extended information about violations to hooks. --- .../nocheatplus/checks/IViolationInfo.java | 29 +++++++++++++++++++ .../nocheatplus/checks/ViolationData.java | 15 ++++++++-- .../nocheatplus/hooks/AbstractNCPHook.java | 26 ++++++++++++++++- .../neatmonster/nocheatplus/hooks/IStats.java | 10 +++++++ .../nocheatplus/hooks/NCPHook.java | 5 +++- .../nocheatplus/hooks/NCPHookManager.java | 15 ++++++---- 6 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 src/fr/neatmonster/nocheatplus/checks/IViolationInfo.java create mode 100644 src/fr/neatmonster/nocheatplus/hooks/IStats.java diff --git a/src/fr/neatmonster/nocheatplus/checks/IViolationInfo.java b/src/fr/neatmonster/nocheatplus/checks/IViolationInfo.java new file mode 100644 index 00000000..65538b23 --- /dev/null +++ b/src/fr/neatmonster/nocheatplus/checks/IViolationInfo.java @@ -0,0 +1,29 @@ +package fr.neatmonster.nocheatplus.checks; + +/** + * Access interface for extended information about violations. + * @author mc_dev + * + */ +public interface IViolationInfo { + /** + * Get the violation level just added by this violation. + * @return + */ + public double getAddedVl(); + /** + * Get the total violation level the player has right now. This is not the value shown with "/ncp info ", but the value used for actions. + * @return + */ + public double getTotalVl(); + /** + * Check if the actions contain a cancel action. + * @return + */ + boolean hasCancel(); + /** + * Check if any of the actions needs parameters. + * @return If true, actions are likely to contian command or logging actions. + */ + boolean needsParameters(); + } diff --git a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java index b170809d..f3af881b 100644 --- a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java +++ b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java @@ -24,7 +24,7 @@ import fr.neatmonster.nocheatplus.actions.types.CancelAction; * * @author asofold */ -public class ViolationData { +public class ViolationData implements IViolationInfo{ /** The actions to be executed. */ public final ActionList actions; @@ -118,6 +118,7 @@ public class ViolationData { * Check if the actions contain a cancel. * @return */ + @Override public boolean hasCancel(){ for (final Action action : applicableActions){ if (action instanceof CancelAction) return true; @@ -151,8 +152,18 @@ public class ViolationData { if (parameters != null) parameters.put(parameterName, value); } + @Override public boolean needsParameters() { return parameters != null; } - + + @Override + public double getAddedVl() { + return addedVL; + } + + @Override + public double getTotalVl() { + return vL; + } } diff --git a/src/fr/neatmonster/nocheatplus/hooks/AbstractNCPHook.java b/src/fr/neatmonster/nocheatplus/hooks/AbstractNCPHook.java index 77af3398..20884b9c 100644 --- a/src/fr/neatmonster/nocheatplus/hooks/AbstractNCPHook.java +++ b/src/fr/neatmonster/nocheatplus/hooks/AbstractNCPHook.java @@ -1,5 +1,10 @@ package fr.neatmonster.nocheatplus.hooks; +import org.bukkit.entity.Player; + +import fr.neatmonster.nocheatplus.checks.CheckType; +import fr.neatmonster.nocheatplus.checks.IViolationInfo; + /* * MMP"""""""MM dP dP dP * M' .mmmm MM 88 88 88 @@ -24,4 +29,23 @@ package fr.neatmonster.nocheatplus.hooks; * * @author asofold */ -public abstract class AbstractNCPHook implements NCPHook {} \ No newline at end of file +public abstract class AbstractNCPHook implements NCPHook { + /** + * + * @deprecated See new signature in NCPHook. + * @param checkType + * @param player + * @return + */ + public boolean onCheckFailure(CheckType checkType, Player player){ + // Implemented because of API change. + return false; + } + + @Override + public boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) { + // Kept for compatibility reasons. + return onCheckFailure(checkType, player); + } + +} \ No newline at end of file diff --git a/src/fr/neatmonster/nocheatplus/hooks/IStats.java b/src/fr/neatmonster/nocheatplus/hooks/IStats.java new file mode 100644 index 00000000..e3925490 --- /dev/null +++ b/src/fr/neatmonster/nocheatplus/hooks/IStats.java @@ -0,0 +1,10 @@ +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. + * @author mc_dev + * + */ +public class IStats { +} diff --git a/src/fr/neatmonster/nocheatplus/hooks/NCPHook.java b/src/fr/neatmonster/nocheatplus/hooks/NCPHook.java index a431361d..7d81f2fb 100644 --- a/src/fr/neatmonster/nocheatplus/hooks/NCPHook.java +++ b/src/fr/neatmonster/nocheatplus/hooks/NCPHook.java @@ -3,6 +3,7 @@ package fr.neatmonster.nocheatplus.hooks; import org.bukkit.entity.Player; import fr.neatmonster.nocheatplus.checks.CheckType; +import fr.neatmonster.nocheatplus.checks.IViolationInfo; /* * M"""""""`YM MM'""""'YMM MM"""""""`YM M""MMMMM""MM dP @@ -47,7 +48,9 @@ public interface NCPHook { * the check that failed * @param player * the player that failed the check + * @param info + * Extended information about the violations. * @return if we need to cancel the check failure processing */ - public boolean onCheckFailure(CheckType checkType, Player player); + public boolean onCheckFailure(CheckType checkType, Player player, IViolationInfo info); } diff --git a/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java b/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java index 71b866e2..fe7d1b82 100644 --- a/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java +++ b/src/fr/neatmonster/nocheatplus/hooks/NCPHookManager.java @@ -14,6 +14,7 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import fr.neatmonster.nocheatplus.checks.CheckType; +import fr.neatmonster.nocheatplus.checks.IViolationInfo; import fr.neatmonster.nocheatplus.checks.ViolationData; /* @@ -103,8 +104,10 @@ public final class NCPHookManager { */ private static void addToMapping(final CheckType checkType, final NCPHook hook) { final List hooks = hooksByChecks.get(checkType); - if (!hooks.contains(hook)) - hooks.add(hook); + if (!hooks.contains(hook)){ + if (hook instanceof IStats) hooks.add(0, hook); + else hooks.add(hook); + } } /** @@ -160,11 +163,11 @@ public final class NCPHookManager { * the hooks * @return true, if a hook as decided to cancel the VL processing */ - private static final boolean applyHooks(final CheckType checkType, final Player player, final List hooks) { + private static final boolean applyHooks(final CheckType checkType, final Player player, final IViolationInfo info, final List hooks) { for (int i = 0; i < hooks.size(); i++) { final NCPHook hook = hooks.get(i); try { - if (hook.onCheckFailure(checkType, player)) + if (hook.onCheckFailure(checkType, player, info)) return true; } catch (final Throwable t) { // TODO: maybe distinguish some exceptions here (interrupted ?). @@ -411,11 +414,11 @@ public final class NCPHookManager { if (!hooksCheck.isEmpty()){ if (APIUtils.needsSynchronization(type)){ synchronized (hooksCheck) { - return applyHooks(type, violationData.player, hooksCheck); + return applyHooks(type, violationData.player, violationData, hooksCheck); } } else{ - return applyHooks(type, violationData.player, hooksCheck); + return applyHooks(type, violationData.player, violationData, hooksCheck); } } return false;