aboutsummaryrefslogtreecommitdiff
path: root/spark-bukkit/src
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2018-06-06 15:39:16 +0100
committerLuck <git@lucko.me>2018-06-06 15:39:16 +0100
commit7d6808cbcfbb0f61f93e536d36968eeda5bd302c (patch)
tree3271db1ffa6e4d6c1fa5ea4ccc1335b1ac746f46 /spark-bukkit/src
parent38f0c12483e6eda79ca36dc829ef678a736d2cef (diff)
downloadspark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.tar.gz
spark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.tar.bz2
spark-7d6808cbcfbb0f61f93e536d36968eeda5bd302c.zip
Convert to Gradle
Diffstat (limited to 'spark-bukkit/src')
-rw-r--r--spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java55
-rw-r--r--spark-bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java73
-rw-r--r--spark-bukkit/src/main/resources/plugin.yml10
3 files changed, 138 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);
+ }
+}
diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java
new file mode 100644
index 0000000..dc432c5
--- /dev/null
+++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java
@@ -0,0 +1,73 @@
+package me.lucko.spark.bukkit;
+
+import me.lucko.spark.common.CommandHandler;
+import me.lucko.spark.profiler.ThreadDumper;
+import me.lucko.spark.profiler.TickCounter;
+
+import org.bukkit.ChatColor;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.java.JavaPlugin;
+
+public class SparkBukkitPlugin extends JavaPlugin {
+
+ private final CommandHandler<CommandSender> commandHandler = new CommandHandler<CommandSender>() {
+
+ private String colorize(String message) {
+ return ChatColor.translateAlternateColorCodes('&', message);
+ }
+
+ private void broadcast(String msg) {
+ getServer().getConsoleSender().sendMessage(msg);
+ for (Player player : getServer().getOnlinePlayers()) {
+ if (player.hasPermission("spark.profiler")) {
+ player.sendMessage(msg);
+ }
+ }
+ }
+
+ @Override
+ protected void sendMessage(CommandSender sender, String message) {
+ sender.sendMessage(colorize(message));
+ }
+
+ @Override
+ protected void sendMessage(String message) {
+ String msg = colorize(message);
+ broadcast(msg);
+ }
+
+ @Override
+ protected void sendLink(String url) {
+ String msg = colorize("&7" + url);
+ broadcast(msg);
+ }
+
+ @Override
+ protected void runAsync(Runnable r) {
+ getServer().getScheduler().runTaskAsynchronously(SparkBukkitPlugin.this, r);
+ }
+
+ @Override
+ protected ThreadDumper getDefaultThreadDumper() {
+ return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()});
+ }
+
+ @Override
+ protected TickCounter newTickCounter() {
+ return new BukkitTickCounter(SparkBukkitPlugin.this);
+ }
+ };
+
+ @Override
+ public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
+ if (!sender.hasPermission("spark.profiler")) {
+ sender.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
+ return true;
+ }
+
+ this.commandHandler.handleCommand(sender, args);
+ return true;
+ }
+}
diff --git a/spark-bukkit/src/main/resources/plugin.yml b/spark-bukkit/src/main/resources/plugin.yml
new file mode 100644
index 0000000..94c61a3
--- /dev/null
+++ b/spark-bukkit/src/main/resources/plugin.yml
@@ -0,0 +1,10 @@
+name: spark
+version: ${pluginVersion}
+description: Spark is a CPU profiling plugin based on sk89q's WarmRoast profiler
+authors: [Luck, sk89q]
+main: me.lucko.spark.bukkit.SparkBukkitPlugin
+
+commands:
+ spark:
+ description: Main plugin command
+ aliases: [profiler] \ No newline at end of file