From ec937d0e077012134545c79cd749e7c7f4d3a0c5 Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 23 Nov 2018 11:40:18 +1100 Subject: SPIGOT-4472: Add Consumer scheduler methods --- .../craftbukkit/scheduler/CraftAsyncDebugger.java | 4 +- .../craftbukkit/scheduler/CraftAsyncTask.java | 2 +- .../craftbukkit/scheduler/CraftScheduler.java | 87 +++++++++++++++------- .../bukkit/craftbukkit/scheduler/CraftTask.java | 37 +++++++-- 4 files changed, 92 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncDebugger.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncDebugger.java index d80ae50d..c3321312 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncDebugger.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncDebugger.java @@ -7,9 +7,9 @@ class CraftAsyncDebugger { private CraftAsyncDebugger next = null; private final int expiry; private final Plugin plugin; - private final Class clazz; + private final Class clazz; - CraftAsyncDebugger(final int expiry, final Plugin plugin, final Class clazz) { + CraftAsyncDebugger(final int expiry, final Plugin plugin, final Class clazz) { this.expiry = expiry; this.plugin = plugin; this.clazz = clazz; diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java index fb50a4a7..26ca44c5 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncTask.java @@ -13,7 +13,7 @@ class CraftAsyncTask extends CraftTask { private final LinkedList workers = new LinkedList(); private final Map runners; - CraftAsyncTask(final Map runners, final Plugin plugin, final Runnable task, final int id, final long delay) { + CraftAsyncTask(final Map runners, final Plugin plugin, final Object task, final int id, final long delay) { super(plugin, task, id, delay); this.runners = runners; } diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java index 7b4dab91..b5c22e20 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java @@ -12,6 +12,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; import java.util.logging.Level; import org.apache.commons.lang.Validate; @@ -88,45 +89,89 @@ public class CraftScheduler implements BukkitScheduler { RECENT_TICKS = 30; } + @Override public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task) { return this.scheduleSyncDelayedTask(plugin, task, 0L); } + @Override public BukkitTask runTask(Plugin plugin, Runnable runnable) { return runTaskLater(plugin, runnable, 0L); } + @Override + public void runTask(Plugin plugin, Consumer task) throws IllegalArgumentException { + runTaskLater(plugin, task, 0L); + } + @Deprecated + @Override public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task) { return this.scheduleAsyncDelayedTask(plugin, task, 0L); } + @Override public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable runnable) { return runTaskLaterAsynchronously(plugin, runnable, 0L); } + @Override + public void runTaskAsynchronously(Plugin plugin, Consumer task) throws IllegalArgumentException { + runTaskLaterAsynchronously(plugin, task, 0L); + } + + @Override public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { return this.scheduleSyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING); } + @Override public BukkitTask runTaskLater(Plugin plugin, Runnable runnable, long delay) { return runTaskTimer(plugin, runnable, delay, CraftTask.NO_REPEATING); } + @Override + public void runTaskLater(Plugin plugin, Consumer task, long delay) throws IllegalArgumentException { + runTaskTimer(plugin, task, delay, CraftTask.NO_REPEATING); + } + @Deprecated + @Override public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { return this.scheduleAsyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING); } + @Override public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long delay) { return runTaskTimerAsynchronously(plugin, runnable, delay, CraftTask.NO_REPEATING); } + @Override + public void runTaskLaterAsynchronously(Plugin plugin, Consumer task, long delay) throws IllegalArgumentException { + runTaskTimerAsynchronously(plugin, task, delay, CraftTask.NO_REPEATING); + } + + @Override + public void runTaskTimerAsynchronously(Plugin plugin, Consumer task, long delay, long period) throws IllegalArgumentException { + runTaskTimerAsynchronously(plugin, (Object) task, delay, CraftTask.NO_REPEATING); + } + + @Override public int scheduleSyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) { return runTaskTimer(plugin, runnable, delay, period).getTaskId(); } + @Override public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) { + return runTaskTimer(plugin, (Object) runnable, delay, period); + } + + @Override + public void runTaskTimer(Plugin plugin, Consumer task, long delay, long period) throws IllegalArgumentException { + runTaskTimer(plugin, (Object) task, delay, period); + } + + public BukkitTask runTaskTimer(Plugin plugin, Object runnable, long delay, long period) { validate(plugin, runnable); if (delay < 0L) { delay = 0; @@ -140,11 +185,17 @@ public class CraftScheduler implements BukkitScheduler { } @Deprecated + @Override public int scheduleAsyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) { return runTaskTimerAsynchronously(plugin, runnable, delay, period).getTaskId(); } + @Override public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) { + return runTaskTimerAsynchronously(plugin, (Object) runnable, delay, period); + } + + public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Object runnable, long delay, long period) { validate(plugin, runnable); if (delay < 0L) { delay = 0; @@ -157,6 +208,7 @@ public class CraftScheduler implements BukkitScheduler { return handle(new CraftAsyncTask(runners, plugin, runnable, nextId(), period), delay); } + @Override public Future callSyncMethod(final Plugin plugin, final Callable task) { validate(plugin, task); final CraftFuture future = new CraftFuture(task, plugin, nextId()); @@ -164,6 +216,7 @@ public class CraftScheduler implements BukkitScheduler { return future; } + @Override public void cancelTask(final int taskId) { if (taskId <= 0) { return; @@ -205,6 +258,7 @@ public class CraftScheduler implements BukkitScheduler { } } + @Override public void cancelTasks(final Plugin plugin) { Validate.notNull(plugin, "Cannot cancel tasks of null plugin"); final CraftTask task = new CraftTask( @@ -243,34 +297,7 @@ public class CraftScheduler implements BukkitScheduler { } } - public void cancelAllTasks() { - final CraftTask task = new CraftTask( - new Runnable() { - public void run() { - Iterator it = CraftScheduler.this.runners.values().iterator(); - while (it.hasNext()) { - CraftTask task = it.next(); - task.cancel0(); - if (task.isSync()) { - it.remove(); - } - } - CraftScheduler.this.pending.clear(); - CraftScheduler.this.temp.clear(); - } - }); - handle(task, 0L); - for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) { - if (taskPending == task) { - break; - } - taskPending.cancel0(); - } - for (CraftTask runner : runners.values()) { - runner.cancel0(); - } - } - + @Override public boolean isCurrentlyRunning(final int taskId) { final CraftTask task = runners.get(taskId); if (task == null) { @@ -285,6 +312,7 @@ public class CraftScheduler implements BukkitScheduler { } } + @Override public boolean isQueued(final int taskId) { if (taskId <= 0) { return false; @@ -298,6 +326,7 @@ public class CraftScheduler implements BukkitScheduler { return task != null && task.getPeriod() >= CraftTask.NO_REPEATING; } + @Override public List getActiveWorkers() { final ArrayList workers = new ArrayList(); for (final CraftTask taskObj : runners.values()) { @@ -314,6 +343,7 @@ public class CraftScheduler implements BukkitScheduler { return workers; } + @Override public List getPendingTasks() { final ArrayList truePending = new ArrayList(); for (CraftTask task = head.getNext(); task != null; task = task.getNext()) { @@ -407,6 +437,7 @@ public class CraftScheduler implements BukkitScheduler { private static void validate(final Plugin plugin, final Object task) { Validate.notNull(plugin, "Plugin cannot be null"); Validate.notNull(task, "Task cannot be null"); + Validate.isTrue(task instanceof Runnable || task instanceof Consumer, "Task must be Runnable or Consumer"); if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register task while disabled"); } diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java index a3ec2301..adacb60a 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftTask.java @@ -1,5 +1,6 @@ package org.bukkit.craftbukkit.scheduler; +import java.util.function.Consumer; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitTask; @@ -23,7 +24,8 @@ class CraftTask implements BukkitTask, Runnable { */ private volatile long period; private long nextRun; - private final Runnable task; + private final Runnable rTask; + private final Consumer cTask; private final Plugin plugin; private final int id; @@ -31,31 +33,51 @@ class CraftTask implements BukkitTask, Runnable { this(null, null, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING); } - CraftTask(final Runnable task) { + CraftTask(final Object task) { this(null, task, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING); } - CraftTask(final Plugin plugin, final Runnable task, final int id, final long period) { + CraftTask(final Plugin plugin, final Object task, final int id, final long period) { this.plugin = plugin; - this.task = task; + if (task instanceof Runnable) { + this.rTask = (Runnable) task; + this.cTask = null; + } else if (task instanceof Consumer) { + this.cTask = (Consumer) task; + this.rTask = null; + } else if (task == null) { + // Head task + this.rTask = null; + this.cTask = null; + } else { + throw new AssertionError("Illegal task class " + task); + } this.id = id; this.period = period; } + @Override public final int getTaskId() { return id; } + @Override public final Plugin getOwner() { return plugin; } + @Override public boolean isSync() { return true; } + @Override public void run() { - task.run(); + if (rTask != null) { + rTask.run(); + } else { + cTask.accept(this); + } } long getPeriod() { @@ -82,8 +104,8 @@ class CraftTask implements BukkitTask, Runnable { this.next = next; } - Class getTaskClass() { - return task.getClass(); + Class getTaskClass() { + return (rTask != null) ? rTask.getClass() : cTask.getClass(); } @Override @@ -91,6 +113,7 @@ class CraftTask implements BukkitTask, Runnable { return (period == CraftTask.CANCEL); } + @Override public void cancel() { Bukkit.getScheduler().cancelTask(id); } -- cgit v1.2.3