private static void init() {
if (APIScheduler.tasks.isEmpty()) {
APIScheduler.task = Bukkit.getScheduler().runTaskTimer(Main.getInstance(), new Runnable() {
@Override
public void run() {
if (APIScheduler.toRemove.isEmpty()) {
APIScheduler.tasks.removeAll((APIScheduler.toRemove));
APIScheduler.toRemove.clear();
}
if (APIScheduler.tasks.isEmpty()) {
APIScheduler.task.cancel();
APIScheduler.task = null;
}
for (ScheduledTask task : (APIScheduler.tasks)) {
APIScheduler.tasks.remove(task);
if (task.aux == task.value * task.unit.getTicks()) {
if (task.runnable.iterations >= 0 && task.runnable.aux == task.runnable.iterations) {
task.runnable.run();
task.cancel();
continue;
}
task.aux = 1;
task.runnable.run();
task.runnable.aux++;
}
else {
task.aux++;
}
APIScheduler.tasks.add(task);
}
}
}, 0L, 1L);
}
}
public static ScheduledTask startTask(TaskRunnable runnable, int value, SchedulerUnit unit) {
return startAutoTask(runnable, -1, value, unit, false);
}
public static ScheduledTask startDelayedTask(TaskRunnable runnable, int value, SchedulerUnit unit) {
return startAutoTask(runnable, 1, value, unit, false);
}
public static ScheduledTask startAutoTask(TaskRunnable runnable, int iterations, int value, SchedulerUnit unit) {
return startAutoTask(runnable, iterations, value, unit, false);
}
private static ScheduledTask startAutoTask(TaskRunnable runnable, int iterations, int value, SchedulerUnit unit, boolean async) {
ScheduledTask localTask = new ScheduledTask(runnable, value, unit);
runnable.task = localTask;
runnable.iterations = iterations;
if (iterations != 1) {
localTask.runnable.run();
}
if (async) {
APIScheduler.asyncTasks.add(localTask);
if (APIScheduler.asyncTask == null) {
init();
}
}
else {
APIScheduler.tasks.add(localTask);
if (APIScheduler.task == null) {
init();
}
}
return localTask;
}
static {
APIScheduler.task = null;
APIScheduler.asyncTask = null;
APIScheduler.tasks = new HashSet<ScheduledTask>();
APIScheduler.asyncTasks = new HashSet<ScheduledTask>();
APIScheduler.toRemove = new HashSet<ScheduledTask>();
}
public abstract static class TaskRunnable implements Runnable
{
private ScheduledTask task;
private int aux;
private int iterations;
public TaskRunnable() {
this.aux = 1;
this.iterations = -1;
}
public void cancel() {
this.task.cancel();
}
public int getAux() {
return this.aux;
}
}
public enum SchedulerUnit
{
TICK(1),
SECOND(20),
MINUTE(1200),
HOUR(72000),
DAY(1728000);
Pergunta
Frogguer
o código
Link para o comentário
Compartilhar em outros sites
10 respostass a esta questão
Posts Recomendados