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

70 lines
2.5 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;
2015-12-19 20:30:06 +01:00
import org.spongepowered.api.scheduler.Task;
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-30 16:25:16 +02:00
import com.plotsquared.sponge.SpongeMain;
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
@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());
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
@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-11-26 04:40:22 +01: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-11-26 04:40:22 +01: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();
builder.delayTicks(delay).execute(r).submit(SpongeMain.THIS.getPlugin());
}
2015-11-26 04:40:22 +01: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();
builder.async().delayTicks(delay).execute(r).submit(SpongeMain.THIS.getPlugin());
}
2015-11-26 04:40:22 +01: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) {
task.cancel();
}
}
2015-11-26 04:40:22 +01:00
}