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 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;
|
2015-07-28 13:38:49 +02:00
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
public class SpongeTaskManager extends TaskManager
|
|
|
|
{
|
|
|
|
|
|
|
|
private final AtomicInteger i = new AtomicInteger();
|
|
|
|
|
|
|
|
private final HashMap<Integer, Task> tasks = new HashMap<>();
|
2015-07-28 13:38:49 +02:00
|
|
|
|
|
|
|
@Override
|
2015-09-11 12:09:22 +02:00
|
|
|
public int taskRepeat(final Runnable r, final int interval)
|
|
|
|
{
|
|
|
|
final int val = i.incrementAndGet();
|
|
|
|
final TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
final TaskBuilder built = builder.delay(interval).interval(interval).execute(r);
|
|
|
|
final Task task = built.submit(SpongeMain.THIS.getPlugin());
|
2015-07-28 13:38:49 +02:00
|
|
|
tasks.put(val, task);
|
|
|
|
return val;
|
|
|
|
}
|
2015-09-11 12:09:22 +02:00
|
|
|
|
2015-08-02 13:56:18 +02:00
|
|
|
@Override
|
2015-09-11 12:09:22 +02:00
|
|
|
public int taskRepeatAsync(final Runnable r, final int interval)
|
|
|
|
{
|
|
|
|
final int val = i.incrementAndGet();
|
|
|
|
final TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
|
|
|
final TaskBuilder built = builder.delay(interval).async().interval(interval).execute(r);
|
|
|
|
final Task task = built.submit(SpongeMain.THIS.getPlugin());
|
2015-08-02 13:56:18 +02:00
|
|
|
tasks.put(val, task);
|
|
|
|
return val;
|
|
|
|
}
|
2015-07-28 13:38:49 +02:00
|
|
|
|
|
|
|
@Override
|
2015-09-11 12:09:22 +02:00
|
|
|
public void taskAsync(final Runnable r)
|
|
|
|
{
|
|
|
|
final TaskBuilder 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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-11 12:09:22 +02:00
|
|
|
public void task(final Runnable r)
|
|
|
|
{
|
|
|
|
final TaskBuilder 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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-11 12:09:22 +02:00
|
|
|
public void taskLater(final Runnable r, final int delay)
|
|
|
|
{
|
|
|
|
final TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
2015-07-30 16:25:16 +02:00
|
|
|
builder.delay(delay).execute(r).submit(SpongeMain.THIS.getPlugin());
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-11 12:09:22 +02:00
|
|
|
public void taskLaterAsync(final Runnable r, final int delay)
|
|
|
|
{
|
|
|
|
final 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());
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-11 12:09:22 +02:00
|
|
|
public void cancelTask(final int i)
|
|
|
|
{
|
|
|
|
final Task task = tasks.remove(i);
|
|
|
|
if (task != null)
|
|
|
|
{
|
2015-07-28 13:38:49 +02:00
|
|
|
task.cancel();
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 12:09:22 +02:00
|
|
|
|
2015-07-28 13:38:49 +02:00
|
|
|
}
|