diff options
| author | Luck <git@lucko.me> | 2019-04-16 21:37:59 +0100 |
|---|---|---|
| committer | Luck <git@lucko.me> | 2019-04-16 21:37:59 +0100 |
| commit | ecd4cec8545460a4fc4ca65b911c2503a00cd8e7 (patch) | |
| tree | 62067383a1044abc3a09724e89c6e7c619e87ec0 | |
| parent | 8a61b404848ed8e3c27f06eb73239d37d4273240 (diff) | |
| download | spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.tar.gz spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.tar.bz2 spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.zip | |
Lots of refactoring, add tps command
40 files changed, 1159 insertions, 703 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 index 3b95399..44a69cb 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java @@ -20,8 +20,7 @@ package me.lucko.spark.bukkit; -import me.lucko.spark.sampler.TickCounter; - +import me.lucko.spark.common.sampler.TickCounter; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitTask; @@ -32,7 +31,7 @@ public class BukkitTickCounter implements TickCounter, Runnable { private final Plugin plugin; private BukkitTask task; - private final Set<Runnable> tasks = new HashSet<>(); + private final Set<TickTask> tasks = new HashSet<>(); private int tick = 0; public BukkitTickCounter(Plugin plugin) { @@ -41,8 +40,8 @@ public class BukkitTickCounter implements TickCounter, Runnable { @Override public void run() { - for (Runnable r : this.tasks) { - r.run(); + for (TickTask r : this.tasks) { + r.onTick(this); } this.tick++; } @@ -63,12 +62,12 @@ public class BukkitTickCounter implements TickCounter, Runnable { } @Override - public void addTickTask(Runnable runnable) { + public void addTickTask(TickTask runnable) { this.tasks.add(runnable); } @Override - public void removeTickTask(Runnable runnable) { + public void removeTickTask(TickTask 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 index f1395c4..1d6160a 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java @@ -21,100 +21,97 @@ package me.lucko.spark.bukkit; import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.sampler.ThreadDumper; -import me.lucko.spark.sampler.TickCounter; - +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.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; import java.nio.file.Path; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; -public class SparkBukkitPlugin extends JavaPlugin { +public class SparkBukkitPlugin extends JavaPlugin implements SparkPlugin<CommandSender> { - private final SparkPlatform<CommandSender> sparkPlatform = new SparkPlatform<CommandSender>() { + private final SparkPlatform<CommandSender> platform = new SparkPlatform<>(this); - private String colorize(String message) { - return ChatColor.translateAlternateColorCodes('&', message); - } + @Override + public void onEnable() { + this.platform.enable(); + } - private void broadcast(String msg) { - getServer().getConsoleSender().sendMessage(msg); - for (Player player : getServer().getOnlinePlayers()) { - if (player.hasPermission("spark")) { - player.sendMessage(msg); - } - } - } + @Override + public void onDisable() { + this.platform.disable(); + } - @Override - public String getVersion() { - return getDescription().getVersion(); + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!sender.hasPermission("spark")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; } - @Override - public Path getPluginFolder() { - return getDataFolder().toPath(); - } + this.platform.executeCommand(sender, args); + return true; + } - @Override - public String getLabel() { - return "spark"; + @Override + public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + if (!sender.hasPermission("spark")) { + return Collections.emptyList(); } + return this.platform.tabCompleteCommand(sender, args); + } - @Override - public void sendMessage(CommandSender sender, String message) { - sender.sendMessage(colorize(message)); - } + @Override + public String getVersion() { + return getDescription().getVersion(); + } - @Override - public void sendMessage(String message) { - String msg = colorize(message); - broadcast(msg); - } + @Override + public Path getPluginFolder() { + return getDataFolder().toPath(); + } - @Override - public void sendLink(String url) { - String msg = colorize("&7" + url); - broadcast(msg); - } + @Override + public String getLabel() { + return "spark"; + } - @Override - public void runAsync(Runnable r) { - getServer().getScheduler().runTaskAsynchronously(SparkBukkitPlugin.this, r); - } + @Override + public Set<CommandSender> getSenders() { + Set<CommandSender> senders = new HashSet<>(getServer().getOnlinePlayers()); + senders.add(getServer().getConsoleSender()); + return senders; + } - @Override - public ThreadDumper getDefaultThreadDumper() { - return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); - } + @Override + public void sendMessage(CommandSender sender, String message) { + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); + } - @Override - public TickCounter newTickCounter() { - return new BukkitTickCounter(SparkBukkitPlugin.this); - } - }; + @Override + public void sendLink(CommandSender sender, String url) { + sendMessage(sender, "&7" + url); + } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (!sender.hasPermission("spark")) { - sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); - return true; - } + public void runAsync(Runnable r) { + getServer().getScheduler().runTaskAsynchronously(SparkBukkitPlugin.this, r); + } - this.sparkPlatform.executeCommand(sender, args); - return true; + @Override + public ThreadDumper getDefaultThreadDumper() { + return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); } @Override - public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { - if (!sender.hasPermission("spark")) { - return Collections.emptyList(); - } - return this.sparkPlatform.tabCompleteCommand(sender, args); + public TickCounter createTickCounter() { + return new BukkitTickCounter(this); } } diff --git a/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/SparkBungeeCordPlugin.java b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/SparkBungeeCordPlugin.java index 6d23683..5c39d34 100644 --- a/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/SparkBungeeCordPlugin.java +++ b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/SparkBungeeCordPlugin.java @@ -21,95 +21,93 @@ package me.lucko.spark.bungeecord; import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.sampler.ThreadDumper; -import me.lucko.spark.sampler.TickCounter; - +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.TickCounter; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; -import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.TabExecutor; import java.nio.file.Path; import java.util.Collections; +import java.util.HashSet; +import java.util.Set; -public class SparkBungeeCordPlugin extends Plugin { +public class SparkBungeeCordPlugin extends Plugin implements SparkPlugin<CommandSender> { - private final SparkPlatform<CommandSender> sparkPlatform = new SparkPlatform<CommandSender>() { - private BaseComponent[] colorize(String message) { - return TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', message)); - } + private final SparkPlatform<CommandSender> platform = new SparkPlatform<>(this); - private void broadcast(BaseComponent... msg) { - getProxy().getConsole().sendMessage(msg); - for (ProxiedPlayer player : getProxy().getPlayers()) { - if (player.hasPermission("spark")) { - player.sendMessage(msg); - } - } - } + @Override + public void onEnable() { + this.platform.enable(); + getProxy().getPluginManager().registerCommand(this, new SparkCommand(this)); + } - @Override - public String getVersion() { - return getDescription().getVersion(); - } + @Override + public void onDisable() { + this.platform.disable(); + } - @Override - public Path getPluginFolder() { - return getDataFolder().toPath(); - } + @Override + public String getVersion() { + return getDescription().getVersion(); + } - @Override - public String getLabel() { - return "sparkb"; - } + @Override + public Path getPluginFolder() { + return getDataFolder().toPath(); + } - @Override - public void sendMessage(CommandSender sender, String message) { - sender.sendMessage(colorize(message)); - } + @Override + public String getLabel() { + return "sparkb"; + } - @Override - public void sendMessage(String message) { - broadcast(colorize(message)); - } + @Override + public Set<CommandSender> getSenders() { + Set<CommandSender> senders = new HashSet<>(getProxy().getPlayers()); + senders.add(getProxy().getConsole()); + return senders; + } - @Override - public void sendLink(String url) { - TextComponent component = new TextComponent(url); - component.setColor(ChatColor.GRAY); - component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)); - broadcast(component); - } + @Override + public void sendMessage(CommandSender sender, String message) { + sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', message))); + } - @Override - public void runAsync(Runnable r) { - getProxy().getScheduler().runAsync(SparkBungeeCordPlugin.this, r); - } + @Override + public void sendLink(CommandSender sender, String url) { + TextComponent component = new TextComponent(url); + component.setColor(ChatColor.GRAY); + component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)); + sender.sendMessage(component); + } - @Override - public ThreadDumper getDefaultThreadDumper() { - return ThreadDumper.ALL; - } + @Override + public void runAsync(Runnable r) { + getProxy().getScheduler().runAsync(SparkBungeeCordPlugin.this, r); + } - @Override - public TickCounter newTickCounter() { - throw new UnsupportedOperationException(); - } - }; + @Override + public ThreadDumper getDefaultThreadDumper() { + return ThreadDumper.ALL; + } @Override - public void onEnable() { - getProxy().getPluginManager().registerCommand(this, new SparkCommand()); + public TickCounter createTickCounter() { + return null; } - private final class SparkCommand extends Command implements TabExecutor { - public SparkCommand() { + private static final class SparkCommand extends Command implements TabExecutor { + private final SparkBungeeCordPlugin plugin; + + SparkCommand(SparkBungeeCordPlugin plugin) { super("sparkb", null, "sparkbungee"); + this.plugin = plugin; } @Override @@ -121,7 +119,7 @@ public class SparkBungeeCordPlugin extends Plugin { return; } - SparkBungeeCordPlugin.this.sparkPlatform.executeCommand(sender, args); + this.plugin.platform.executeCommand(sender, args); } @Override @@ -129,7 +127,7 @@ public class SparkBungeeCordPlugin extends Plugin { if (!sender.hasPermission("spark")) { return Collections.emptyList(); } - return SparkBungeeCordPlugin.this.sparkPlatform.tabCompleteCommand(sender, args); + return this.plugin.platform.tabCompleteCommand(sender, args); } } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java index ef21d1c..8eb4565 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java @@ -21,19 +21,20 @@ package me.lucko.spark.common; import com.google.common.collect.ImmutableList; - import me.lucko.spark.common.command.Arguments; import me.lucko.spark.common.command.Command; +import me.lucko.spark.common.command.CommandResponseHandler; import me.lucko.spark.common.command.modules.MemoryModule; +import me.lucko.spark.common.command.modules.MonitoringModule; import me.lucko.spark.common.command.modules.SamplerModule; import me.lucko.spark.common.command.modules.TickMonitoringModule; import me.lucko.spark.common.command.tabcomplete.CompletionSupplier; import me.lucko.spark.common.command.tabcomplete.TabCompleter; -import me.lucko.spark.sampler.ThreadDumper; -import me.lucko.spark.sampler.TickCounter; -import me.lucko.spark.util.BytebinClient; +import me.lucko.spark.common.monitor.tick.TpsCalculator; +import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.common.util.BytebinClient; +import okhttp3.OkHttpClient; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -41,52 +42,68 @@ import java.util.List; import java.util.stream.Collectors; /** - * Abstract command handling class used by all platforms. + * Abstract spark implementation used by all platforms. * * @param <S> the sender (e.g. CommandSender) type used by the platform */ -public abstract class SparkPlatform<S> { +public class SparkPlatform<S> { /** The URL of the viewer frontend */ public static final String VIEWER_URL = "https://sparkprofiler.github.io/#"; + /** The shared okhttp client */ + private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient(); /** The bytebin instance used by the platform */ - public static final BytebinClient BYTEBIN_CLIENT = new BytebinClient("https://bytebin.lucko.me/", "spark-plugin"); - - /** The prefix used in all messages */ - private static final String PREFIX = "&8[&fspark&8] &7"; - - private static <T> List<Command<T>> prepareCommands() { - ImmutableList.Builder<Command<T>> builder = ImmutableList.builder(); - new SamplerModule<T>().registerCommands(builder::add); - new TickMonitoringModule<T>().registerCommands(builder::add); - new MemoryModule<T>().registerCommands(builder::add); - return builder.build(); + public static final BytebinClient BYTEBIN_CLIENT = new BytebinClient(OK_HTTP_CLIENT, "https://bytebin.lucko.me/", "spark-plugin"); + + private final List<Command<S>> commands; + private final SparkPlugin<S> plugin; + + private final TickCounter tickCounter; + private final TpsCalculator tpsCalculator; + + public SparkPlatform(SparkPlugin<S> plugin) { + this.plugin = plugin; + + ImmutableList.Builder<Command<S>> commandsBuilder = ImmutableList.builder(); + new SamplerModule<S>().registerCommands(commandsBuilder::add); + new MonitoringModule<S>().registerCommands(commandsBuilder::add); + new TickMonitoringModule<S>().registerCommands(commandsBuilder::add); + new MemoryModule<S>().registerCommands(commandsBuilder::add); + this.commands = commandsBuilder.build(); + + this.tickCounter = plugin.createTickCounter(); + this.tpsCalculator = this.tickCounter != null ? new TpsCalculator() : null; + } + + public void enable() { + if (this.tickCounter != null) { + this.tickCounter.addTickTask(this.tpsCalculator); + this.tickCounter.start(); + } + } + + public void disable() { + if (this.tickCounter != null) { + this.tickCounter.close(); + } + } + + public SparkPlugin<S> getPlugin() { + return this.plugin; } - private final List<Command<S>> commands = prepareCommands(); - - // abstract methods implemented by each platform - public abstract String getVersion(); - public abstract Path getPluginFolder(); - public abstract String getLabel(); - public abstract void sendMessage(S sender, String message); - public abstract void sendMessage(String message); - public abstract void sendLink(String url); - public abstract void runAsync(Runnable r); - public abstract ThreadDumper getDefaultThreadDumper(); - public abstract TickCounter newTickCounter(); - - public void sendPrefixedMessage(S sender, String message) { - sendMessage(sender, PREFIX + message); + public TickCounter getTickCounter() { + return this.tickCounter; } - public void sendPrefixedMessage(String message) { - sendMessage(PREFIX + message); + public TpsCalculator getTpsCalculator() { + return this.tpsCalculator; } public void executeCommand(S sender, String[] args) { + CommandResponseHandler<S> resp = new CommandResponseHandler<>(this, sender); if (args.length == 0) { - sendUsage(sender); + sendUsage(resp); return; } @@ -96,15 +113,15 @@ public abstract class SparkPlatform<S> { for (Command<S> command : this.commands) { if (command.aliases().contains(alias)) { try { - command.executor().execute(this, sender, new Arguments(rawArgs)); + command.executor().execute(this, sender, resp, new Arguments(rawArgs)); } catch (IllegalArgumentException e) { - sendMessage(sender, "&c" + e.getMessage()); + resp.replyPrefixed("&c" + e.getMessage()); } return; } } - sendUsage(sender); + sendUsage(resp); } public List<String> tabCompleteCommand(S sender, String[] args) { @@ -127,15 +144,15 @@ public abstract class SparkPlatform<S> { return Collections.emptyList(); } - private void sendUsage(S sender) { - sendPrefixedMessage(sender, "&fspark &7v" + getVersion()); + private void sendUsage(CommandResponseHandler<S> sender) { + sender.replyPrefixed("&fspark &7v" + getPlugin().getVersion()); for (Command<S> command : this.commands) { - sendMessage(sender, "&b&l> &7/" + getLabel() + " " + command.aliases().get(0)); + sender.reply("&b&l> &7/" + getPlugin().getLabel() + " " + command.aliases().get(0)); for (Command.ArgumentInfo arg : command.arguments()) { if (arg.requiresParameter()) { - sendMessage(sender, " &8[&7--" + arg.argumentName() + "&8 <" + arg.parameterDescription() + ">]"); + sender.reply(" &8[&7--" + arg.argumentName() + "&8 <" + arg.parameterDescription() + ">]"); } else { - sendMessage(sender, " &8[&7--" + arg.argumentName() + "]"); + sender.reply(" &8[&7--" + arg.argumentName() + "]"); } } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java new file mode 100644 index 0000000..7a3a353 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java @@ -0,0 +1,49 @@ +/* + * 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 |
