diff options
| author | lucko <git@lucko.me> | 2022-12-27 09:17:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-27 09:17:54 +0000 |
| commit | e5b278047ccb7bc6b301d787474c51d162911867 (patch) | |
| tree | 11bba64e8f28ce8b83adc05252b75f17e2ccbf6a /spark-common/src | |
| parent | 4a16a1a2f4eb09f706b4a541e3d31618de29420b (diff) | |
| parent | 1075665def4a41cf0064255a6da1d1a652f5d473 (diff) | |
| download | spark-e5b278047ccb7bc6b301d787474c51d162911867.tar.gz spark-e5b278047ccb7bc6b301d787474c51d162911867.tar.bz2 spark-e5b278047ccb7bc6b301d787474c51d162911867.zip | |
Merge pull request #284 from embeddedt/forge-1.7.10
Align 1.7.10 with master
Diffstat (limited to 'spark-common/src')
69 files changed, 3957 insertions, 1001 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 0ef4556..dae04ff 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 @@ -45,17 +45,18 @@ import me.lucko.spark.common.monitor.ping.PingStatistics; import me.lucko.spark.common.monitor.ping.PlayerPingProvider; import me.lucko.spark.common.monitor.tick.TickStatistics; import me.lucko.spark.common.platform.PlatformStatisticsProvider; +import me.lucko.spark.common.sampler.BackgroundSamplerManager; +import me.lucko.spark.common.sampler.SamplerContainer; +import me.lucko.spark.common.sampler.source.ClassSourceLookup; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.common.util.BytebinClient; -import me.lucko.spark.common.util.ClassSourceLookup; import me.lucko.spark.common.util.Configuration; import me.lucko.spark.common.util.TemporaryFiles; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; -import okhttp3.OkHttpClient; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -64,6 +65,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -74,13 +76,11 @@ import java.util.stream.Collectors; import static net.kyori.adventure.text.Component.space; import static net.kyori.adventure.text.Component.text; -import static net.kyori.adventure.text.format.NamedTextColor.DARK_GRAY; import static net.kyori.adventure.text.format.NamedTextColor.GOLD; import static net.kyori.adventure.text.format.NamedTextColor.GRAY; import static net.kyori.adventure.text.format.NamedTextColor.RED; import static net.kyori.adventure.text.format.NamedTextColor.WHITE; import static net.kyori.adventure.text.format.TextDecoration.BOLD; -import static net.kyori.adventure.text.format.TextDecoration.UNDERLINED; /** * Abstract spark implementation used by all platforms. @@ -91,15 +91,17 @@ public class SparkPlatform { private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss"); private final SparkPlugin plugin; + private final TemporaryFiles temporaryFiles; private final Configuration configuration; private final String viewerUrl; - private final OkHttpClient httpClient; private final BytebinClient bytebinClient; private final boolean disableResponseBroadcast; private final List<CommandModule> commandModules; private final List<Command> commands; private final ReentrantLock commandExecuteLock = new ReentrantLock(true); private final ActivityLog activityLog; + private final SamplerContainer samplerContainer; + private final BackgroundSamplerManager backgroundSamplerManager; private final TickHook tickHook; private final TickReporter tickReporter; private final TickStatistics tickStatistics; @@ -112,13 +114,12 @@ public class SparkPlatform { public SparkPlatform(SparkPlugin plugin) { this.plugin = plugin; + this.temporaryFiles = new TemporaryFiles(this.plugin.getPluginDirectory().resolve("tmp")); this.configuration = new Configuration(this.plugin.getPluginDirectory().resolve("config.json")); this.viewerUrl = this.configuration.getString("viewerUrl", "https://spark.lucko.me/"); String bytebinUrl = this.configuration.getString("bytebinUrl", "https://bytebin.lucko.me/"); - - this.httpClient = new OkHttpClient(); - this.bytebinClient = new BytebinClient(this.httpClient, bytebinUrl, "spark-plugin"); + this.bytebinClient = new BytebinClient(bytebinUrl, "spark-plugin"); this.disableResponseBroadcast = this.configuration.getBoolean("disableResponseBroadcast", false); @@ -140,6 +141,9 @@ public class SparkPlatform { this.activityLog = new ActivityLog(plugin.getPluginDirectory().resolve("activity.json")); this.activityLog.load(); + this.samplerContainer = new SamplerContainer(); + this.backgroundSamplerManager = new BackgroundSamplerManager(this, this.configuration); + this.tickHook = plugin.createTickHook(); this.tickReporter = plugin.createTickReporter(); this.tickStatistics = this.tickHook != null || this.tickReporter != null ? new TickStatistics() : null; @@ -178,6 +182,8 @@ public class SparkPlatform { SparkApi api = new SparkApi(this); this.plugin.registerApi(api); SparkApi.register(api); + + this.backgroundSamplerManager.initialise(); } public void disable() { @@ -195,20 +201,21 @@ public class SparkPlatform { module.close(); } - SparkApi.unregister(); + this.samplerContainer.close(); - TemporaryFiles.deleteTemporaryFiles(); + SparkApi.unregister(); - // shutdown okhttp - // see: https://github.com/square/okhttp/issues/4029 - this.httpClient.dispatcher().executorService().shutdown(); - this.httpClient.connectionPool().evictAll(); + this.temporaryFiles.deleteTemporaryFiles(); } public SparkPlugin getPlugin() { return this.plugin; } + public TemporaryFiles getTemporaryFiles() { + return this.temporaryFiles; + } + public Configuration getConfiguration() { return this.configuration; } @@ -233,6 +240,14 @@ public class SparkPlatform { return this.activityLog; } + public SamplerContainer getSamplerContainer() { + return this.samplerContainer; + } + + public BackgroundSamplerManager getBackgroundSamplerManager() { + return this.backgroundSamplerManager; + } + public TickHook getTickHook() { return this.tickHook; } @@ -366,14 +381,15 @@ public class SparkPlatform { .append(text("v" + getPlugin().getVersion(), GRAY)) .build() ); + + String helpCmd = "/" + getPlugin().getCommandName() + " help"; resp.replyPrefixed(text() .color(GRAY) - .append(text("Use ")) + .append(text("Run ")) .append(text() - .content("/" + getPlugin().getCommandName() + " help") + .content(helpCmd) .color(WHITE) - .decoration(UNDERLINED, true) - .clickEvent(ClickEvent.runCommand("/" + getPlugin().getCommandName() + " help")) + .clickEvent(ClickEvent.runCommand(helpCmd)) .build() ) .append(text(" to view usage information.")) @@ -389,7 +405,7 @@ public class SparkPlatform { if (command.aliases().contains(alias)) { resp.setCommandPrimaryAlias(command.primaryAlias()); try { - command.executor().execute(this, sender, resp, new Arguments(rawArgs)); + command.executor().execute(this, sender, resp, new Arguments(rawArgs, command.allowSubCommand())); } catch (Arguments.ParseException e) { resp.replyPrefixed(text(e.getMessage(), RED)); } @@ -437,35 +453,53 @@ public class SparkPlatform { ); for (Command command : commands) { String usage = "/" + getPlugin().getCommandName() + " " + command.primaryAlias(); - ClickEvent clickEvent = ClickEvent.suggestCommand(usage); - sender.reply(text() - .append(text(">", GOLD, BOLD)) - .append(space()) - .append(text().content(usage).color(GRAY).clickEvent(clickEvent).build()) - .build() - ); - for (Command.ArgumentInfo arg : command.arguments()) { - if (arg.requiresParameter()) { + + if (command.allowSubCommand()) { + Map<String, List<Command.ArgumentInfo>> argumentsBySubCommand = command.arguments().stream() + .collect(Collectors.groupingBy(Command.ArgumentInfo::subCommandName, LinkedHashMap::new, Collectors.toList())); + + argumentsBySubCommand.forEach((subCommand, arguments) -> { + String subCommandUsage = usage + " " + subCommand; + sender.reply(text() - .content(" ") - .append(text("[", DARK_GRAY)) - .append(text("--" + arg.argumentName(), GRAY)) + .append(text(">", GOLD, BOLD)) .append(space()) - .append(text("<" + arg.parameterDescription() + ">", DARK_GRAY)) - .append(text("]", DARK_GRAY)) - .build() - ); - } else { - sender.reply(text() - .content(" ") - .append(text("[", DARK_GRAY)) - .append(text("--" + arg.argumentName(), GRAY)) - .append(text("]", DARK_GRAY)) + .append(text().content(subCommandUsage).color(GRAY).clickEvent(ClickEvent.suggestCommand(subCommandUsage)).build()) .build() ); + + for (Command.ArgumentInfo arg : arguments) { + if (arg.argumentName().isEmpty()) { + continue; + } + sender.reply(arg.toComponent(" ")); + } + }); + } else { + sender.reply(text() + .append(text(">", GOLD, BOLD)) + .append(space()) + .append(text().content(usage).color(GRAY).clickEvent(ClickEvent.suggestCommand(usage)).build()) + .build() + ); + + for (Command.ArgumentInfo arg : command.arguments()) { + sender.reply(arg.toComponent(" ")); } } } + + sender.reply(Component.empty()); + sender.replyPrefixed(text() + .append(text("For full usage information, please go to: ")) + .append(text() + .content("https://spark.lucko.me/docs/Command-Usage") + .color(WHITE) + .clickEvent(ClickEvent.openUrl("https://spark.lucko.me/docs/Command-Usage")) + .build() + ) + .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 b817df1..b7aef2a 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 @@ -23,14 +23,19 @@ package me.lucko.spark.common; import me.lucko.spark.api.Spark; import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.common.monitor.ping.PlayerPingProvider; +import me.lucko.spark.common.platform.MetadataProvider; import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.platform.serverconfig.ServerConfigProvider; +import me.lucko.spark.common.platform.world.WorldInfoProvider; import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.source.ClassSourceLookup; +import me.lucko.spark.common.sampler.source.SourceMetadata; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; -import me.lucko.spark.common.util.ClassSourceLookup; import java.nio.file.Path; +import java.util.Collection; +import java.util.Collections; import java.util.logging.Level; import java.util.stream.Stream; @@ -75,6 +80,15 @@ public interface SparkPlugin { void executeAsync(Runnable task); /** + * Executes the given {@link Runnable} on the server/client main thread. + * + * @param task the task + */ + default void executeSync(Runnable task) { + throw new UnsupportedOperationException(); + } + + /** * Print to the plugin logger. * * @param level the log level @@ -123,6 +137,15 @@ public interface SparkPlugin { } /** + * Gets a list of known sources (plugins/mods) on the platform. + * + * @return a list of sources + */ + default Collection<SourceMetadata> getKnownSources() { + return Collections.emptyList(); + } + + /** * Creates a player ping provider function. * * <p>Returns {@code null} if the platform does not support querying player pings</p> @@ -139,7 +162,25 @@ public interface SparkPlugin { * @return the server config provider function */ default ServerConfigProvider createServerConfigProvider() { - return ServerConfigProvider.NO_OP; + return null; + } + + /** + * Creates a metadata provider for the platform. + * + * @return the platform extra metadata provider + */ + default MetadataProvider createExtraMetadataProvider() { + return null; + } + + /** + * Creates a world info provider. + * + * @return the world info provider function + */ + default WorldInfoProvider createWorldInfoProvider() { + return WorldInfoProvider.NO_OP; } /** diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/Arguments.java b/spark-common/src/main/java/me/lucko/spark/common/command/Arguments.java index 17c49e2..ad8c777 100644 --- a/spark-common/src/main/java/me/lucko/spar |
