From 4501169db1e72bbe71ca4668d338fa87f1467be8 Mon Sep 17 00:00:00 2001 From: asofold Date: Thu, 13 Sep 2012 11:03:17 +0200 Subject: [PATCH] TickTask: add tick count, randomize method ordering. --- .../nocheatplus/utilities/TickTask.java | 109 ++++++++++++------ 1 file changed, 76 insertions(+), 33 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/utilities/TickTask.java b/src/fr/neatmonster/nocheatplus/utilities/TickTask.java index 75122a20..61ac05ae 100644 --- a/src/fr/neatmonster/nocheatplus/utilities/TickTask.java +++ b/src/fr/neatmonster/nocheatplus/utilities/TickTask.java @@ -16,6 +16,9 @@ import fr.neatmonster.nocheatplus.checks.ViolationData; /** * Task to run every tick, to update permissions and execute actions, and maybe later for extended lag measurement. + * + *
+ * The permissions updates and actions execution is meant for use by the asynchronously run checks, not for normal use. * @author mc_dev * */ @@ -51,22 +54,18 @@ public class TickTask implements Runnable { /** Task id of the running TickTask */ protected static int taskId = -1; - /** - * Access method to request permisison updates. - * @param playerName - * @param checkType - */ - public static void requestPermissionUpdate(final String playerName, final CheckType checkType){ - permissionUpdates.add(new PermissionUpdateEntry(playerName, checkType)); - } - - public static void cancel(){ - if (taskId == -1) return; - Bukkit.getScheduler().cancelTask(taskId); - taskId = -1; - } + protected static int tick = 0; - private void executeActions() { + + ////////////////////////////////////////////////////////////// + // Special static methods, usually not called from outside. + ////////////////////////////////////////////////////////////// + + /** + * Force executing actions.
+ * Note: Only call from the main thread! + */ + public void executeActions() { final List copyActions = new LinkedList(); synchronized (delayedActions) { copyActions.addAll(delayedActions); @@ -77,25 +76,9 @@ public class TickTask implements Runnable { } } - public static void requestActionsExecution(final ViolationData actions) { - delayedActions.add(actions); - } - - @Override - public void run() { - // The isEmpty checks are faster than synchronizing fully always, the actions get delayed one tick at most. - if (!delayedActions.isEmpty()) executeActions(); - if (!permissionUpdates.isEmpty()) updatePermissions(); - } - - public static int start(final NoCheatPlus plugin){ - cancel(); - taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new TickTask(), 1, 1); - return taskId; - } - /** - * Only call from the main thread! + * Force a permissions update.
+ * Note: Only call from the main thread! */ public static void updatePermissions() { final List copyPermissions = new LinkedList(); @@ -115,5 +98,65 @@ public class TickTask implements Runnable { } } + + /////////////////////////////////// + // Public static access methods + /////////////////////////////////// + + /** + * Access method to request permission updates.
+ * NOTE: Thread safe. + * @param playerName + * @param checkType + */ + public static void requestPermissionUpdate(final String playerName, final CheckType checkType){ + permissionUpdates.add(new PermissionUpdateEntry(playerName, checkType)); + } + + /** + * Request actions execution.
+ * NOTE: Thread safe. + * @param actions + */ + public static void requestActionsExecution(final ViolationData actions) { + delayedActions.add(actions); + } + + /** + * Get the tasks tick count. It is increased with every server tick.
+ * NOTE: Can be called from other threads. + * @return The current tick count. + */ + public static final int getTick(){ + return tick; + } + + //////////////////////////////////////// + // Public methods for internal use. + //////////////////////////////////////// + + public static int start(final NoCheatPlus plugin){ + cancel(); + taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new TickTask(), 1, 1); + return taskId; + } + + public static void cancel(){ + if (taskId == -1) return; + Bukkit.getScheduler().cancelTask(taskId); + taskId = -1; + } + + ////////////////////////// + // Instance methods + ////////////////////////// + + @Override + public void run() { + tick ++; + // The isEmpty checks are faster than synchronizing fully always, the actions get delayed one tick at most. + if (!delayedActions.isEmpty()) executeActions(); + if (!permissionUpdates.isEmpty()) updatePermissions(); + } }