PlotSquared/src/main/java/com/plotsquared/sponge/util/SpongeTaskManager.java

71 lines
2.4 KiB
Java
Raw Normal View History

2015-07-30 16:25:16 +02:00
package com.plotsquared.sponge.util;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.spongepowered.api.service.scheduler.Task;
import org.spongepowered.api.service.scheduler.TaskBuilder;
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-30 16:25:16 +02:00
import com.plotsquared.sponge.SpongeMain;
public class SpongeTaskManager extends TaskManager {
private AtomicInteger i = new AtomicInteger();
private HashMap<Integer, Task> tasks = new HashMap<>();
@Override
public int taskRepeat(Runnable r, int interval) {
int val = i.incrementAndGet();
TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
TaskBuilder built = builder.delay(interval).interval(interval).execute(r);
Task task = built.submit(SpongeMain.THIS.getPlugin());
tasks.put(val, task);
return val;
}
2015-08-02 13:56:18 +02:00
@Override
public int taskRepeatAsync(Runnable r, int interval) {
int val = i.incrementAndGet();
TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
2015-08-02 13:56:18 +02:00
TaskBuilder built = builder.delay(interval).async().interval(interval).execute(r);
Task task = built.submit(SpongeMain.THIS.getPlugin());
tasks.put(val, task);
return val;
}
@Override
public void taskAsync(Runnable r) {
TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
2015-07-30 16:25:16 +02:00
builder.async().execute(r).submit(SpongeMain.THIS.getPlugin());
}
@Override
public void task(Runnable r) {
TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
2015-07-30 16:25:16 +02:00
builder.execute(r).submit(SpongeMain.THIS.getPlugin());
}
@Override
public void taskLater(Runnable r, int delay) {
TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
2015-07-30 16:25:16 +02:00
builder.delay(delay).execute(r).submit(SpongeMain.THIS.getPlugin());
}
@Override
public void taskLaterAsync(Runnable r, int delay) {
TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
2015-07-30 16:25:16 +02:00
builder.async().delay(delay).execute(r).submit(SpongeMain.THIS.getPlugin());
}
@Override
public void cancelTask(int i) {
Task task = tasks.remove(i);
if (task != null) {
task.cancel();
}
}
}