2015-07-30 16:25:16 +02:00
|
|
|
package com.plotsquared.sponge.util;
|
2015-07-28 13:38:49 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
import org.spongepowered.api.service.scheduler.Task;
|
|
|
|
|
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
2015-07-30 16:25:16 +02:00
|
|
|
import com.plotsquared.sponge.SpongeMain;
|
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
|
2015-09-13 06:04:31 +02:00
|
|
|
public int taskRepeat(final Runnable r, final int interval) {
|
2015-09-11 12:09:22 +02:00
|
|
|
final int val = i.incrementAndGet();
|
2015-11-26 04:40:22 +01:00
|
|
|
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
final Task.Builder built = builder.delayTicks(interval).intervalTicks(interval).execute(r);
|
2015-09-11 12:09:22 +02:00
|
|
|
final Task task = built.submit(SpongeMain.THIS.getPlugin());
|
2015-07-28 13:38:49 +02:00
|
|
|
tasks.put(val, task);
|
|
|
|
return val;
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-08-02 13:56:18 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public int taskRepeatAsync(final Runnable r, final int interval) {
|
2015-09-11 12:09:22 +02:00
|
|
|
final int val = i.incrementAndGet();
|
2015-11-26 04:40:22 +01:00
|
|
|
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
final Task.Builder built = builder.delayTicks(interval).async().intervalTicks(interval).execute(r);
|
2015-09-11 12:09:22 +02:00
|
|
|
final Task task = built.submit(SpongeMain.THIS.getPlugin());
|
2015-08-02 13:56:18 +02:00
|
|
|
tasks.put(val, task);
|
|
|
|
return val;
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public void taskAsync(final Runnable r) {
|
2015-11-26 04:40:22 +01:00
|
|
|
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
2015-07-30 16:25:16 +02:00
|
|
|
builder.async().execute(r).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
|
2015-09-13 06:04:31 +02:00
|
|
|
public void task(final Runnable r) {
|
2015-11-26 04:40:22 +01:00
|
|
|
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
2015-07-30 16:25:16 +02:00
|
|
|
builder.execute(r).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
|
2015-09-13 06:04:31 +02:00
|
|
|
public void taskLater(final Runnable r, final int delay) {
|
2015-11-26 04:40:22 +01:00
|
|
|
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
2015-11-15 03:30:52 +01:00
|
|
|
builder.delayTicks(delay).execute(r).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
|
2015-09-13 06:04:31 +02:00
|
|
|
public void taskLaterAsync(final Runnable r, final int delay) {
|
2015-11-26 04:40:22 +01:00
|
|
|
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
2015-11-15 03:30:52 +01:00
|
|
|
builder.async().delayTicks(delay).execute(r).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
|
2015-09-13 06:04:31 +02:00
|
|
|
public void cancelTask(final int i) {
|
2015-09-11 12:09:22 +02:00
|
|
|
final Task task = 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
|
|
|
}
|