diff options
Diffstat (limited to 'spark-bukkit/src')
-rw-r--r-- | spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java | 17 | ||||
-rw-r--r-- | spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java | 75 |
2 files changed, 91 insertions, 1 deletions
diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java index edeeb50..0eca719 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java @@ -130,6 +130,21 @@ public class BukkitSparkPlugin extends JavaPlugin implements SparkPlugin { @Override public TickCounter createTickCounter() { - return new BukkitTickCounter(this); + if (classExists("com.destroystokyo.paper.event.server.ServerTickStartEvent")) { + getLogger().info("Using Paper ServerTickStartEvent for tick monitoring"); + return new PaperTickCounter(this); + } else { + getLogger().info("Using Bukkit scheduler for tick monitoring"); + return new BukkitTickCounter(this); + } + } + + private static boolean classExists(String className) { + try { + Class.forName(className); + return true; + } catch (ClassNotFoundException e) { + return false; + } } } 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); + } +} |