From 778461da16381985e3e2ad530f7bf46aa6861d2b Mon Sep 17 00:00:00 2001 From: asofold Date: Mon, 10 Sep 2012 15:33:18 +0200 Subject: [PATCH] Rework actions execution, simplify + optimize, add metrics count asynchronously. --- .../neatmonster/nocheatplus/checks/Check.java | 19 +++--- .../checks/DelayedActionsExecution.java | 63 ------------------- .../nocheatplus/checks/ViolationData.java | 45 ++++++++++++- .../nocheatplus/utilities/TickTask.java | 12 ++-- 4 files changed, 62 insertions(+), 77 deletions(-) delete mode 100644 src/fr/neatmonster/nocheatplus/checks/DelayedActionsExecution.java diff --git a/src/fr/neatmonster/nocheatplus/checks/Check.java b/src/fr/neatmonster/nocheatplus/checks/Check.java index 985a773c..c2d61070 100644 --- a/src/fr/neatmonster/nocheatplus/checks/Check.java +++ b/src/fr/neatmonster/nocheatplus/checks/Check.java @@ -9,6 +9,7 @@ import fr.neatmonster.nocheatplus.actions.ParameterName; import fr.neatmonster.nocheatplus.actions.types.ActionList; import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager; import fr.neatmonster.nocheatplus.hooks.NCPHookManager; +import fr.neatmonster.nocheatplus.metrics.MetricsData; import fr.neatmonster.nocheatplus.players.ExecutionHistory; import fr.neatmonster.nocheatplus.utilities.TickTask; @@ -118,17 +119,21 @@ public abstract class Check { if (NCPHookManager.shouldCancelVLProcessing(violationData)) // One of the hooks has decided to cancel the VL processing, return false. return false; - - final DelayedActionsExecution delayedActions = new DelayedActionsExecution(violationData); + + // Add this failed check to the Metrics data (async!). + MetricsData.addFailed(violationData.check.type); + + final boolean hasCancel = violationData.hasCancel(); - if (isMainThread) return delayedActions.execute(); - else{ - TickTask.requestActionsExecution(delayedActions); - return delayedActions.hasCancel(); + if (isMainThread) return violationData.executeActions(); + else if (violationData.applicableActions.length > 0){ + // Only schedule if there is something to schedule. + if (!hasCancel || violationData.applicableActions.length > 1) + TickTask.requestActionsExecution(violationData); } // (Design change: Permission checks are moved to cached permissions, lazily updated.) - + return hasCancel; } /** diff --git a/src/fr/neatmonster/nocheatplus/checks/DelayedActionsExecution.java b/src/fr/neatmonster/nocheatplus/checks/DelayedActionsExecution.java deleted file mode 100644 index 0c6f2aa2..00000000 --- a/src/fr/neatmonster/nocheatplus/checks/DelayedActionsExecution.java +++ /dev/null @@ -1,63 +0,0 @@ -package fr.neatmonster.nocheatplus.checks; - -import fr.neatmonster.nocheatplus.actions.Action; -import fr.neatmonster.nocheatplus.actions.types.CancelAction; -import fr.neatmonster.nocheatplus.metrics.MetricsData; - -/** - * For scheduling actions execution. This does not check the NCPHookManager. - *
- * Not put to ViolationData itself for the possibility of adding other data (might be considered though). - * @author mc_dev - * - */ -public class DelayedActionsExecution { - - protected final ViolationData violationData; - protected final Action[] actions; - - public DelayedActionsExecution(final ViolationData violationData) { - this.violationData = violationData; - actions = violationData.getActions(); - } - - /** - * Execute actions and return if cancel. - * @return - */ - public boolean execute(){ - try { - - ViolationHistory.getHistory(violationData.player).log(violationData.check.getClass().getName(), violationData.addedVL); - - // Add this failed check to the Metrics data. - MetricsData.addFailed(violationData.check.type); - - // TODO: the time is taken here, which makes sense for delay, but otherwise ? - final long time = System.currentTimeMillis() / 1000L; - boolean cancel = false; - for (final Action action : actions) - if (Check.getHistory(violationData.player).executeAction(violationData, action, time)) - // The execution history said it really is time to execute the action, find out what it is and do - // what is needed. - if (action.execute(violationData)) cancel = true; - - return cancel; - } catch (final Exception e) { - e.printStackTrace(); - // On exceptions cancel events. - return true; - } - } - - /** - * Check if the actions contain a cancel. - * @return - */ - public boolean hasCancel(){ - for (final Action action : actions){ - if (action instanceof CancelAction) return true; - } - return false; - } -} diff --git a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java index 34b2ce22..0332aee0 100644 --- a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java +++ b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java @@ -4,6 +4,7 @@ import org.bukkit.entity.Player; import fr.neatmonster.nocheatplus.actions.Action; import fr.neatmonster.nocheatplus.actions.types.ActionList; +import fr.neatmonster.nocheatplus.actions.types.CancelAction; /* * M""MMMMM""M oo dP dP oo M""""""'YMM dP @@ -24,6 +25,9 @@ public class ViolationData { /** The actions to be executed. */ public final ActionList actions; + + /** The actions applicable for the violation level. */ + public final Action[] applicableActions; /** The violation level added. */ public final double addedVL; @@ -58,6 +62,7 @@ public class ViolationData { this.vL = vL; this.addedVL = addedVL; this.actions = actions; + this.applicableActions = actions.getActions(vL); } /** @@ -66,6 +71,44 @@ public class ViolationData { * @return the actions */ public Action[] getActions() { - return actions.getActions(vL); + return applicableActions; } + + /** + * Execute actions and return if cancel. Does add it to history. + * @return + */ + public boolean executeActions(){ + try { + + ViolationHistory.getHistory(player).log(check.getClass().getName(), addedVL); + + // TODO: the time is taken here, which makes sense for delay, but otherwise ? + final long time = System.currentTimeMillis() / 1000L; + boolean cancel = false; + for (final Action action : getActions()) + if (Check.getHistory(player).executeAction(this, action, time)) + // The execution history said it really is time to execute the action, find out what it is and do + // what is needed. + if (action.execute(this)) cancel = true; + + return cancel; + } catch (final Exception e) { + e.printStackTrace(); + // On exceptions cancel events. + return true; + } + } + + /** + * Check if the actions contain a cancel. + * @return + */ + public boolean hasCancel(){ + for (final Action action : applicableActions){ + if (action instanceof CancelAction) return true; + } + return false; + } + } diff --git a/src/fr/neatmonster/nocheatplus/utilities/TickTask.java b/src/fr/neatmonster/nocheatplus/utilities/TickTask.java index 619d4ae8..75122a20 100644 --- a/src/fr/neatmonster/nocheatplus/utilities/TickTask.java +++ b/src/fr/neatmonster/nocheatplus/utilities/TickTask.java @@ -11,8 +11,8 @@ import org.bukkit.entity.Player; import fr.neatmonster.nocheatplus.NoCheatPlus; import fr.neatmonster.nocheatplus.checks.CheckType; -import fr.neatmonster.nocheatplus.checks.DelayedActionsExecution; import fr.neatmonster.nocheatplus.checks.ICheckData; +import fr.neatmonster.nocheatplus.checks.ViolationData; /** * Task to run every tick, to update permissions and execute actions, and maybe later for extended lag measurement. @@ -46,7 +46,7 @@ public class TickTask implements Runnable { private static final Set permissionUpdates = Collections.synchronizedSet(new HashSet(50)); /** Actions to execute. */ - public static final List delayedActions = Collections.synchronizedList(new LinkedList()); + public static final List delayedActions = Collections.synchronizedList(new LinkedList()); /** Task id of the running TickTask */ protected static int taskId = -1; @@ -67,17 +67,17 @@ public class TickTask implements Runnable { } private void executeActions() { - final List copyActions = new LinkedList(); + final List copyActions = new LinkedList(); synchronized (delayedActions) { copyActions.addAll(delayedActions); delayedActions.clear(); } - for (final DelayedActionsExecution actions : copyActions){ - actions.execute(); + for (final ViolationData violationData : copyActions){ + violationData.executeActions(); } } - public static void requestActionsExecution(final DelayedActionsExecution actions) { + public static void requestActionsExecution(final ViolationData actions) { delayedActions.add(actions); }