aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2019-08-03 22:04:20 +0100
committerLuck <git@lucko.me>2019-08-03 22:04:20 +0100
commit2a03b547c7714a130b2bc1407b12a8abd58a9069 (patch)
treec4306958d814964a58c472b959b94afda5ee3800 /spark-common/src
parent4308eda6fd4ce3d637ad29b50f53d870ec1facbc (diff)
downloadspark-2a03b547c7714a130b2bc1407b12a8abd58a9069.tar.gz
spark-2a03b547c7714a130b2bc1407b12a8abd58a9069.tar.bz2
spark-2a03b547c7714a130b2bc1407b12a8abd58a9069.zip
Some more reorganising
Diffstat (limited to 'spark-common/src')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java8
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java56
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/HealthModule.java2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java8
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java4
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TickMonitor.java6
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java3
8 files changed, 68 insertions, 21 deletions
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<? extends CommandSender> 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.
+ *
+ * <p>Returns {@code null} if the platform does not have "ticks"</p>
+ *
+ * @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<Activity> {
Pagination<Activity> 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<Component> 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<BigDecimal> samples = new ArrayDeque<>();
+ private final Queue<BigDecimal> 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) {