aboutsummaryrefslogtreecommitdiff
path: root/spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2020-01-11 19:24:30 +0000
committerLuck <git@lucko.me>2020-01-11 19:24:30 +0000
commit9e066d1052643b2be270b4039a102260abb41ebb (patch)
tree31bddf86d03d723f4ccdcdcccb67ca9711bb8b5c /spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java
parent47c65e9c64ec387f845eac5784d86d9685d8ea71 (diff)
downloadspark-9e066d1052643b2be270b4039a102260abb41ebb.tar.gz
spark-9e066d1052643b2be270b4039a102260abb41ebb.tar.bz2
spark-9e066d1052643b2be270b4039a102260abb41ebb.zip
Implement PaperTickCounter using server tick event instead of scheduler
Diffstat (limited to 'spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java')
-rw-r--r--spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java75
1 files changed, 75 insertions, 0 deletions
diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java
new file mode 100644
index 0000000..e545687
--- /dev/null
+++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java
@@ -0,0 +1,75 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.bukkit;
+
+import com.destroystokyo.paper.event.server.ServerTickStartEvent;
+import me.lucko.spark.common.sampler.TickCounter;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.Listener;
+import org.bukkit.plugin.Plugin;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class PaperTickCounter implements TickCounter, Listener {
+ private final Plugin plugin;
+
+ private final Set<TickTask> tasks = new HashSet<>();
+ private int tick = 0;
+
+ public PaperTickCounter(Plugin plugin) {
+ this.plugin = plugin;
+ }
+
+ @EventHandler
+ public void onServerTickEvent(ServerTickStartEvent e) {
+ for (TickTask r : this.tasks) {
+ r.onTick(this);
+ }
+ this.tick++;
+ }
+
+ @Override
+ public void start() {
+ this.plugin.getServer().getPluginManager().registerEvents(this, this.plugin);
+ }
+
+ @Override
+ public void close() {
+ HandlerList.unregisterAll(this);
+ }
+
+ @Override
+ public int getCurrentTick() {
+ return this.tick;
+ }
+
+ @Override
+ public void addTickTask(TickTask runnable) {
+ this.tasks.add(runnable);
+ }
+
+ @Override
+ public void removeTickTask(TickTask runnable) {
+ this.tasks.remove(runnable);
+ }
+}