From 2a03b547c7714a130b2bc1407b12a8abd58a9069 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 3 Aug 2019 22:04:20 +0100 Subject: Some more reorganising --- .../java/me/lucko/spark/common/SparkPlatform.java | 8 ++-- .../java/me/lucko/spark/common/SparkPlugin.java | 56 ++++++++++++++++++++-- .../common/command/modules/ActivityLogModule.java | 2 +- .../spark/common/command/modules/HealthModule.java | 2 +- .../spark/common/command/modules/MemoryModule.java | 8 ++-- .../common/command/modules/SamplerModule.java | 4 +- .../spark/common/monitor/tick/TickMonitor.java | 6 +-- .../me/lucko/spark/common/util/RollingAverage.java | 3 +- 8 files changed, 68 insertions(+), 21 deletions(-) (limited to 'spark-common/src') 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 e99cb05..db5cb84 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 @@ -84,7 +84,7 @@ public class SparkPlatform { } this.commands = commandsBuilder.build(); - this.activityLog = new ActivityLog(plugin.getPluginFolder().resolve("activity.json")); + this.activityLog = new ActivityLog(plugin.getPluginDirectory().resolve("activity.json")); this.activityLog.load(); this.tickCounter = plugin.createTickCounter(); @@ -142,10 +142,10 @@ public class SparkPlatform { ); resp.replyPrefixed(TextComponent.builder("").color(TextColor.GRAY) .append(TextComponent.of("Use ")) - .append(TextComponent.builder("/" + getPlugin().getLabel() + " help") + .append(TextComponent.builder("/" + getPlugin().getCommandName() + " help") .color(TextColor.WHITE) .decoration(TextDecoration.UNDERLINED, true) - .clickEvent(ClickEvent.runCommand("/" + getPlugin().getLabel() + " help")) + .clickEvent(ClickEvent.runCommand("/" + getPlugin().getCommandName() + " help")) .build() ) .append(TextComponent.of(" to view usage information.")) @@ -204,7 +204,7 @@ public class SparkPlatform { .build() ); for (Command command : this.commands) { - String usage = "/" + getPlugin().getLabel() + " " + command.aliases().get(0); + String usage = "/" + getPlugin().getCommandName() + " " + command.aliases().get(0); ClickEvent clickEvent = ClickEvent.suggestCommand(usage); sender.reply(TextComponent.builder("") .append(TextComponent.builder(">").color(TextColor.GOLD).decoration(TextDecoration.BOLD, true).build()) 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 index 72bbfbc..7ccffab 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java @@ -26,20 +26,66 @@ import me.lucko.spark.common.sampler.TickCounter; import java.nio.file.Path; import java.util.stream.Stream; +/** + * Spark plugin interface + */ public interface SparkPlugin { + /** + * Gets the version of the plugin. + * + * @return the version + */ String getVersion(); - Path getPluginFolder(); + /** + * Gets the plugins storage/configuration directory. + * + * @return the plugin directory + */ + Path getPluginDirectory(); - String getLabel(); + /** + * Gets the name used for the plugin command. + * + * @return the plugin command name + */ + String getCommandName(); + /** + * Gets a {@link Stream} of the {@link CommandSender}s on the platform with the given + * permission. + * + * @param permission the permission + * @return the stream of command senders + */ Stream getSendersWithPermission(String permission); - void runAsync(Runnable r); + /** + * Executes the given {@link Runnable} asynchronously using the plugins scheduler. + * + * @param task the task + */ + void executeAsync(Runnable task); - ThreadDumper getDefaultThreadDumper(); + /** + * Gets the default {@link ThreadDumper} to be used by the plugin. + * + * @return the default thread dumper + */ + default ThreadDumper getDefaultThreadDumper() { + return ThreadDumper.ALL; + } - TickCounter createTickCounter(); + /** + * Creates a tick counter for the platform, if supported. + * + *

Returns {@code null} if the platform does not have "ticks"

+ * + * @return a new tick counter + */ + default TickCounter createTickCounter() { + return null; + } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java index 9c81f28..e09dc9d 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java @@ -108,7 +108,7 @@ public class ActivityLogModule implements CommandModule, RowRenderer { Pagination activityPagination = this.pagination.build( TextComponent.of("Recent spark activity", TextColor.GOLD), this, - value -> "/" + platform.getPlugin().getLabel() + " activity --page " + value + value -> "/" + platform.getPlugin().getCommandName() + " activity --page " + value ); activityPagination.render(log, page).forEach(resp::reply); }) diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/HealthModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/HealthModule.java index 963ffcd..86ab047 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/HealthModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/HealthModule.java @@ -91,7 +91,7 @@ public class HealthModule implements CommandModule { .argumentUsage("memory", null) .executor((platform, sender, resp, arguments) -> { resp.replyPrefixed(TextComponent.of("Generating server health report...")); - platform.getPlugin().runAsync(() -> { + platform.getPlugin().executeAsync(() -> { List report = new LinkedList<>(); report.add(TextComponent.empty()); diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java index 0033716..dcfb0c4 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java @@ -62,7 +62,7 @@ public class MemoryModule implements CommandModule { .aliases("heapsummary") .argumentUsage("run-gc-before", null) .executor((platform, sender, resp, arguments) -> { - platform.getPlugin().runAsync(() -> { + platform.getPlugin().executeAsync(() -> { if (arguments.boolFlag("run-gc-before")) { resp.broadcastPrefixed(TextComponent.of("Running garbage collector...")); System.gc(); @@ -110,8 +110,8 @@ public class MemoryModule implements CommandModule { .argumentUsage("run-gc-before", null) .argumentUsage("include-non-live", null) .executor((platform, sender, resp, arguments) -> { - platform.getPlugin().runAsync(() -> { - Path pluginFolder = platform.getPlugin().getPluginFolder(); + platform.getPlugin().executeAsync(() -> { + Path pluginFolder = platform.getPlugin().getPluginDirectory(); try { Files.createDirectories(pluginFolder); } catch (IOException e) { @@ -153,7 +153,7 @@ public class MemoryModule implements CommandModule { if (timeSinceLastReport > TimeUnit.SECONDS.toMillis(5)) { lastReport.set(System.currentTimeMillis()); - platform.getPlugin().runAsync(() -> { + platform.getPlugin().executeAsync(() -> { resp.broadcastPrefixed(TextComponent.builder("").color(TextColor.GRAY) .append(TextComponent.of("Compressed ")) .append(TextComponent.of(FormatUtil.formatBytes(progress), TextColor.GOLD)) diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java index fe7ffe0..9f14e6e 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java @@ -196,7 +196,7 @@ public class SamplerModule implements CommandModule { resp.broadcastPrefixed(TextComponent.of("Profiler now active!", TextColor.GOLD)); if (timeoutSeconds == -1) { - resp.broadcastPrefixed(TextComponent.of("Use '/" + platform.getPlugin().getLabel() + " sampler --stop' to stop profiling and upload the results.")); + resp.broadcastPrefixed(TextComponent.of("Use '/" + platform.getPlugin().getCommandName() + " sampler --stop' to stop profiling and upload the results.")); } else { resp.broadcastPrefixed(TextComponent.of("The results will be automatically returned after the profiler has been running for " + timeoutSeconds + " seconds.")); } @@ -246,7 +246,7 @@ public class SamplerModule implements CommandModule { } private void handleUpload(SparkPlatform platform, CommandResponseHandler resp, Sampler sampler) { - platform.getPlugin().runAsync(() -> { + platform.getPlugin().executeAsync(() -> { byte[] output = sampler.formCompressedDataPayload(resp.sender()); try { String key = SparkPlatform.BYTEBIN_CLIENT.postContent(output, SPARK_SAMPLER_MEDIA_TYPE, false).key(); diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TickMonitor.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TickMonitor.java index c20ce94..fa4b83c 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TickMonitor.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TickMonitor.java @@ -102,7 +102,7 @@ public abstract class TickMonitor implements TickCounter.TickTask, GarbageCollec // move onto the next state if (this.averageTickTime.getCount() >= 120) { - this.platform.getPlugin().runAsync(() -> { + this.platform.getPlugin().executeAsync(() -> { sendMessage(TextComponent.of("Analysis is now complete.", TextColor.GOLD)); sendMessage(TextComponent.builder("").color(TextColor.GRAY) .append(TextComponent.of(">", TextColor.WHITE)) @@ -145,7 +145,7 @@ public abstract class TickMonitor implements TickCounter.TickTask, GarbageCollec double percentageChange = (increase * 100d) / this.avg; if (percentageChange > this.percentageChangeThreshold) { - this.platform.getPlugin().runAsync(() -> { + this.platform.getPlugin().executeAsync(() -> { sendMessage(TextComponent.builder("").color(TextColor.GRAY) .append(TextComponent.of("Tick ")) .append(TextComponent.of("#" + getCurrentTick(), TextColor.DARK_GRAY)) @@ -179,7 +179,7 @@ public abstract class TickMonitor implements TickCounter.TickTask, GarbageCollec gcType = data.getGcAction(); } - this.platform.getPlugin().runAsync(() -> { + this.platform.getPlugin().executeAsync(() -> { sendMessage(TextComponent.builder("").color(TextColor.GRAY) .append(TextComponent.of("Tick ")) .append(TextComponent.of("#" + getCurrentTick(), TextColor.DARK_GRAY)) diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java index 5b1fd21..514876d 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java @@ -27,12 +27,13 @@ import java.util.Queue; public class RollingAverage { - private final Queue samples = new ArrayDeque<>(); + private final Queue samples; private final int size; private BigDecimal total = BigDecimal.ZERO; public RollingAverage(int size) { this.size = size; + this.samples = new ArrayDeque<>(this.size); } public void add(BigDecimal num) { -- cgit