2015-07-30 16:25:16 +02:00
|
|
|
package com.plotsquared.sponge.util;
|
2015-07-28 13:38:49 +02:00
|
|
|
|
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
2015-07-30 16:25:16 +02:00
|
|
|
import com.plotsquared.sponge.SpongeMain;
|
2016-03-29 01:30:55 +02:00
|
|
|
import org.spongepowered.api.scheduler.Task;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2015-07-28 13:38:49 +02:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class SpongeTaskManager extends TaskManager {
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private final AtomicInteger i = new AtomicInteger();
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
private final HashMap<Integer, Task> tasks = new HashMap<>();
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
@Override
|
2016-03-29 01:30:55 +02:00
|
|
|
public int taskRepeat(Runnable runnable, int interval) {
|
|
|
|
int val = this.i.incrementAndGet();
|
|
|
|
Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
Task.Builder built = builder.delayTicks(interval).intervalTicks(interval).execute(runnable);
|
|
|
|
Task task = built.submit(SpongeMain.THIS.getPlugin());
|
|
|
|
this.tasks.put(val, task);
|
2015-07-28 13:38:49 +02:00
|
|
|
return val;
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-08-02 13:56:18 +02:00
|
|
|
@Override
|
2016-03-29 01:30:55 +02:00
|
|
|
public int taskRepeatAsync(Runnable runnable, int interval) {
|
|
|
|
int val = this.i.incrementAndGet();
|
|
|
|
Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
Task.Builder built = builder.delayTicks(interval).async().intervalTicks(interval).execute(runnable);
|
|
|
|
Task task = built.submit(SpongeMain.THIS.getPlugin());
|
|
|
|
this.tasks.put(val, task);
|
2015-08-02 13:56:18 +02:00
|
|
|
return val;
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
@Override
|
2016-03-29 01:30:55 +02:00
|
|
|
public void taskAsync(Runnable runnable) {
|
|
|
|
Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
builder.async().execute(runnable).submit(SpongeMain.THIS.getPlugin());
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
@Override
|
2016-03-29 01:30:55 +02:00
|
|
|
public void task(Runnable runnable) {
|
|
|
|
Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
builder.execute(runnable).submit(SpongeMain.THIS.getPlugin());
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
@Override
|
2016-03-29 01:30:55 +02:00
|
|
|
public void taskLater(Runnable runnable, int delay) {
|
|
|
|
Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
builder.delayTicks(delay).execute(runnable).submit(SpongeMain.THIS.getPlugin());
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
@Override
|
2016-03-29 01:30:55 +02:00
|
|
|
public void taskLaterAsync(Runnable runnable, int delay) {
|
|
|
|
Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
builder.async().delayTicks(delay).execute(runnable).submit(SpongeMain.THIS.getPlugin());
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
@Override
|
2016-03-29 01:30:55 +02:00
|
|
|
public void cancelTask(int i) {
|
|
|
|
Task task = this.tasks.remove(i);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (task != null) {
|
2015-07-28 13:38:49 +02:00
|
|
|
task.cancel();
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|