SPIGOT-4472: Add Consumer scheduler methods

This commit is contained in:
md_5 2018-11-23 11:40:18 +11:00
parent f489f0f745
commit ec937d0e07
4 changed files with 92 additions and 38 deletions

View file

@ -7,9 +7,9 @@ class CraftAsyncDebugger {
private CraftAsyncDebugger next = null; private CraftAsyncDebugger next = null;
private final int expiry; private final int expiry;
private final Plugin plugin; private final Plugin plugin;
private final Class<? extends Runnable> clazz; private final Class<?> clazz;
CraftAsyncDebugger(final int expiry, final Plugin plugin, final Class<? extends Runnable> clazz) { CraftAsyncDebugger(final int expiry, final Plugin plugin, final Class<?> clazz) {
this.expiry = expiry; this.expiry = expiry;
this.plugin = plugin; this.plugin = plugin;
this.clazz = clazz; this.clazz = clazz;

View file

@ -13,7 +13,7 @@ class CraftAsyncTask extends CraftTask {
private final LinkedList<BukkitWorker> workers = new LinkedList<BukkitWorker>(); private final LinkedList<BukkitWorker> workers = new LinkedList<BukkitWorker>();
private final Map<Integer, CraftTask> runners; private final Map<Integer, CraftTask> runners;
CraftAsyncTask(final Map<Integer, CraftTask> runners, final Plugin plugin, final Runnable task, final int id, final long delay) { CraftAsyncTask(final Map<Integer, CraftTask> runners, final Plugin plugin, final Object task, final int id, final long delay) {
super(plugin, task, id, delay); super(plugin, task, id, delay);
this.runners = runners; this.runners = runners;
} }

View file

@ -12,6 +12,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
@ -88,45 +89,89 @@ public class CraftScheduler implements BukkitScheduler {
RECENT_TICKS = 30; RECENT_TICKS = 30;
} }
@Override
public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task) { public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task) {
return this.scheduleSyncDelayedTask(plugin, task, 0L); return this.scheduleSyncDelayedTask(plugin, task, 0L);
} }
@Override
public BukkitTask runTask(Plugin plugin, Runnable runnable) { public BukkitTask runTask(Plugin plugin, Runnable runnable) {
return runTaskLater(plugin, runnable, 0L); return runTaskLater(plugin, runnable, 0L);
} }
@Override
public void runTask(Plugin plugin, Consumer<BukkitTask> task) throws IllegalArgumentException {
runTaskLater(plugin, task, 0L);
}
@Deprecated @Deprecated
@Override
public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task) { public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task) {
return this.scheduleAsyncDelayedTask(plugin, task, 0L); return this.scheduleAsyncDelayedTask(plugin, task, 0L);
} }
@Override
public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable runnable) { public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable runnable) {
return runTaskLaterAsynchronously(plugin, runnable, 0L); return runTaskLaterAsynchronously(plugin, runnable, 0L);
} }
@Override
public void runTaskAsynchronously(Plugin plugin, Consumer<BukkitTask> task) throws IllegalArgumentException {
runTaskLaterAsynchronously(plugin, task, 0L);
}
@Override
public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) {
return this.scheduleSyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING); return this.scheduleSyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING);
} }
@Override
public BukkitTask runTaskLater(Plugin plugin, Runnable runnable, long delay) { public BukkitTask runTaskLater(Plugin plugin, Runnable runnable, long delay) {
return runTaskTimer(plugin, runnable, delay, CraftTask.NO_REPEATING); return runTaskTimer(plugin, runnable, delay, CraftTask.NO_REPEATING);
} }
@Override
public void runTaskLater(Plugin plugin, Consumer<BukkitTask> task, long delay) throws IllegalArgumentException {
runTaskTimer(plugin, task, delay, CraftTask.NO_REPEATING);
}
@Deprecated @Deprecated
@Override
public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) {
return this.scheduleAsyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING); return this.scheduleAsyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING);
} }
@Override
public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long delay) { public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long delay) {
return runTaskTimerAsynchronously(plugin, runnable, delay, CraftTask.NO_REPEATING); return runTaskTimerAsynchronously(plugin, runnable, delay, CraftTask.NO_REPEATING);
} }
@Override
public void runTaskLaterAsynchronously(Plugin plugin, Consumer<BukkitTask> task, long delay) throws IllegalArgumentException {
runTaskTimerAsynchronously(plugin, task, delay, CraftTask.NO_REPEATING);
}
@Override
public void runTaskTimerAsynchronously(Plugin plugin, Consumer<BukkitTask> 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) { public int scheduleSyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) {
return runTaskTimer(plugin, runnable, delay, period).getTaskId(); return runTaskTimer(plugin, runnable, delay, period).getTaskId();
} }
@Override
public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) { 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<BukkitTask> 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); validate(plugin, runnable);
if (delay < 0L) { if (delay < 0L) {
delay = 0; delay = 0;
@ -140,11 +185,17 @@ public class CraftScheduler implements BukkitScheduler {
} }
@Deprecated @Deprecated
@Override
public int scheduleAsyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) { public int scheduleAsyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) {
return runTaskTimerAsynchronously(plugin, runnable, delay, period).getTaskId(); return runTaskTimerAsynchronously(plugin, runnable, delay, period).getTaskId();
} }
@Override
public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) { 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); validate(plugin, runnable);
if (delay < 0L) { if (delay < 0L) {
delay = 0; delay = 0;
@ -157,6 +208,7 @@ public class CraftScheduler implements BukkitScheduler {
return handle(new CraftAsyncTask(runners, plugin, runnable, nextId(), period), delay); return handle(new CraftAsyncTask(runners, plugin, runnable, nextId(), period), delay);
} }
@Override
public <T> Future<T> callSyncMethod(final Plugin plugin, final Callable<T> task) { public <T> Future<T> callSyncMethod(final Plugin plugin, final Callable<T> task) {
validate(plugin, task); validate(plugin, task);
final CraftFuture<T> future = new CraftFuture<T>(task, plugin, nextId()); final CraftFuture<T> future = new CraftFuture<T>(task, plugin, nextId());
@ -164,6 +216,7 @@ public class CraftScheduler implements BukkitScheduler {
return future; return future;
} }
@Override
public void cancelTask(final int taskId) { public void cancelTask(final int taskId) {
if (taskId <= 0) { if (taskId <= 0) {
return; return;
@ -205,6 +258,7 @@ public class CraftScheduler implements BukkitScheduler {
} }
} }
@Override
public void cancelTasks(final Plugin plugin) { public void cancelTasks(final Plugin plugin) {
Validate.notNull(plugin, "Cannot cancel tasks of null plugin"); Validate.notNull(plugin, "Cannot cancel tasks of null plugin");
final CraftTask task = new CraftTask( final CraftTask task = new CraftTask(
@ -243,34 +297,7 @@ public class CraftScheduler implements BukkitScheduler {
} }
} }
public void cancelAllTasks() { @Override
final CraftTask task = new CraftTask(
new Runnable() {
public void run() {
Iterator<CraftTask> 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();
}
}
public boolean isCurrentlyRunning(final int taskId) { public boolean isCurrentlyRunning(final int taskId) {
final CraftTask task = runners.get(taskId); final CraftTask task = runners.get(taskId);
if (task == null) { if (task == null) {
@ -285,6 +312,7 @@ public class CraftScheduler implements BukkitScheduler {
} }
} }
@Override
public boolean isQueued(final int taskId) { public boolean isQueued(final int taskId) {
if (taskId <= 0) { if (taskId <= 0) {
return false; return false;
@ -298,6 +326,7 @@ public class CraftScheduler implements BukkitScheduler {
return task != null && task.getPeriod() >= CraftTask.NO_REPEATING; return task != null && task.getPeriod() >= CraftTask.NO_REPEATING;
} }
@Override
public List<BukkitWorker> getActiveWorkers() { public List<BukkitWorker> getActiveWorkers() {
final ArrayList<BukkitWorker> workers = new ArrayList<BukkitWorker>(); final ArrayList<BukkitWorker> workers = new ArrayList<BukkitWorker>();
for (final CraftTask taskObj : runners.values()) { for (final CraftTask taskObj : runners.values()) {
@ -314,6 +343,7 @@ public class CraftScheduler implements BukkitScheduler {
return workers; return workers;
} }
@Override
public List<BukkitTask> getPendingTasks() { public List<BukkitTask> getPendingTasks() {
final ArrayList<CraftTask> truePending = new ArrayList<CraftTask>(); final ArrayList<CraftTask> truePending = new ArrayList<CraftTask>();
for (CraftTask task = head.getNext(); task != null; task = task.getNext()) { 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) { private static void validate(final Plugin plugin, final Object task) {
Validate.notNull(plugin, "Plugin cannot be null"); Validate.notNull(plugin, "Plugin cannot be null");
Validate.notNull(task, "Task 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()) { if (!plugin.isEnabled()) {
throw new IllegalPluginAccessException("Plugin attempted to register task while disabled"); throw new IllegalPluginAccessException("Plugin attempted to register task while disabled");
} }

View file

@ -1,5 +1,6 @@
package org.bukkit.craftbukkit.scheduler; package org.bukkit.craftbukkit.scheduler;
import java.util.function.Consumer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@ -23,7 +24,8 @@ class CraftTask implements BukkitTask, Runnable {
*/ */
private volatile long period; private volatile long period;
private long nextRun; private long nextRun;
private final Runnable task; private final Runnable rTask;
private final Consumer<BukkitTask> cTask;
private final Plugin plugin; private final Plugin plugin;
private final int id; private final int id;
@ -31,31 +33,51 @@ class CraftTask implements BukkitTask, Runnable {
this(null, null, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING); 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); 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.plugin = plugin;
this.task = task; if (task instanceof Runnable) {
this.rTask = (Runnable) task;
this.cTask = null;
} else if (task instanceof Consumer) {
this.cTask = (Consumer<BukkitTask>) 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.id = id;
this.period = period; this.period = period;
} }
@Override
public final int getTaskId() { public final int getTaskId() {
return id; return id;
} }
@Override
public final Plugin getOwner() { public final Plugin getOwner() {
return plugin; return plugin;
} }
@Override
public boolean isSync() { public boolean isSync() {
return true; return true;
} }
@Override
public void run() { public void run() {
task.run(); if (rTask != null) {
rTask.run();
} else {
cTask.accept(this);
}
} }
long getPeriod() { long getPeriod() {
@ -82,8 +104,8 @@ class CraftTask implements BukkitTask, Runnable {
this.next = next; this.next = next;
} }
Class<? extends Runnable> getTaskClass() { Class<?> getTaskClass() {
return task.getClass(); return (rTask != null) ? rTask.getClass() : cTask.getClass();
} }
@Override @Override
@ -91,6 +113,7 @@ class CraftTask implements BukkitTask, Runnable {
return (period == CraftTask.CANCEL); return (period == CraftTask.CANCEL);
} }
@Override
public void cancel() { public void cancel() {
Bukkit.getScheduler().cancelTask(id); Bukkit.getScheduler().cancelTask(id);
} }