diff options
author | Luck <git@lucko.me> | 2018-06-06 15:39:16 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2018-06-06 15:39:16 +0100 |
commit | 7d6808cbcfbb0f61f93e536d36968eeda5bd302c (patch) | |
tree | 3271db1ffa6e4d6c1fa5ea4ccc1335b1ac746f46 /spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java | |
parent | 38f0c12483e6eda79ca36dc829ef678a736d2cef (diff) | |
download | spark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.tar.gz spark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.tar.bz2 spark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.zip |
Convert to Gradle
Diffstat (limited to 'spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java')
-rw-r--r-- | spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java new file mode 100644 index 0000000..61a7690 --- /dev/null +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java @@ -0,0 +1,55 @@ +package me.lucko.spark.bukkit; + +import me.lucko.spark.profiler.TickCounter; + +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.LongAdder; + +public class BukkitTickCounter implements TickCounter, Runnable { + private final Plugin plugin; + private BukkitTask task; + + private final Set<Runnable> tasks = new HashSet<>(); + private final LongAdder tick = new LongAdder(); + + public BukkitTickCounter(Plugin plugin) { + this.plugin = plugin; + } + + @Override + public void run() { + this.tick.increment(); + for (Runnable r : this.tasks){ + r.run(); + } + } + + @Override + public void start() { + this.task = this.plugin.getServer().getScheduler().runTaskTimer(this.plugin, this, 1, 1); + } + + @Override + public void close() { + this.task.cancel(); + } + + @Override + public long getCurrentTick() { + return this.tick.longValue(); + } + + @Override + public void addTickTask(Runnable runnable) { + this.tasks.add(runnable); + } + + @Override + public void removeTickTask(Runnable runnable) { + this.tasks.remove(runnable); + } +} |