diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:02 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:02 +0100 |
commit | 5e168847c2624b767deb9da310ecfdf169e0f43c (patch) | |
tree | 56b76556a6837fff20b800d5a22218286a975581 /dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java | |
parent | 60503351a30a91985b95ee8aa64e163ef084b34b (diff) |
Add dicore3-core, without really irrelevant packages
Diffstat (limited to 'dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java')
-rw-r--r-- | dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java b/dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java new file mode 100644 index 0000000..b400cce --- /dev/null +++ b/dicore3/core/src/main/java/io/dico/dicore/task/BaseTask.java @@ -0,0 +1,108 @@ +package io.dico.dicore.task; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +import java.util.NoSuchElementException; + +public abstract class BaseTask<T> { + private boolean running = false; + private Integer taskId = null; + private long workTime = 5L; + private int workCount; + + public void start(Plugin plugin, int delay, int period, long workTime) { + doStartChecks(); + this.workTime = workTime; + workCount = 0; + running = true; + + if (delay == -1) { + run(); + if (!running) { + return; + } + delay = period; + } + + taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this::run, delay, period); + } + + public void startImmediately(Plugin plugin, int period, long workTime) { + start(plugin, -1, period, workTime); + } + + protected void doStartChecks() { + if (isRunning()) { + throw new IllegalStateException("Can't start when already running"); + } + } + + public void start(Plugin plugin) { + start(plugin, -1, 20, 5L); + } + + protected void onFinish(boolean early) { + } + + protected long getWorkTime() { + return workTime; + } + + protected abstract boolean process(T object); + + private void run() { + workCount++; + final long stop = System.currentTimeMillis() + getWorkTime(); + do { + if (!processNext()) { + return; + } + } while (System.currentTimeMillis() < stop); + } + + public int getTaskId() { + return running ? taskId : -1; + } + + public int getWorkCount() { + return workCount; + } + + public boolean isRunning() { + return running; + } + + protected abstract T supply() throws NoSuchElementException; + + private void cancelTask(boolean early) { + if (taskId != null) { + Bukkit.getScheduler().cancelTask(taskId); + } + running = false; + taskId = null; + onFinish(early); + } + + private boolean processNext() { + T object; + try { + object = supply(); + } catch (NoSuchElementException e) { + cancelTask(false); + return false; + } + + try { + if (process(object)) { + return true; + } + } catch (RuntimeException e) { + e.printStackTrace(); + } + + cancelTask(true); + return false; + } + +} |