From f051a29659c659d9b491bced1acf3cac59074f88 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 6 Jun 2022 19:30:51 +0100 Subject: Refactor Minestom plugin and fix tab complete (#209) Co-authored-by: HSGamer --- .../spark/minestom/MinestomClassSourceLookup.java | 1 + .../spark/minestom/MinestomCommandSender.java | 11 +- .../lucko/spark/minestom/MinestomPlatformInfo.java | 1 + .../spark/minestom/MinestomPlayerPingProvider.java | 2 + .../spark/minestom/MinestomSparkExtension.java | 151 ----------------- .../lucko/spark/minestom/MinestomSparkPlugin.java | 183 +++++++++++++++++++++ .../me/lucko/spark/minestom/MinestomTickHook.java | 7 +- .../lucko/spark/minestom/MinestomTickReporter.java | 7 +- 8 files changed, 201 insertions(+), 162 deletions(-) delete mode 100644 spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkExtension.java create mode 100644 spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkPlugin.java (limited to 'spark-minestom/src/main/java/me/lucko/spark/minestom') diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.java index a3cf04c..252060e 100644 --- a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.java +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.java @@ -21,6 +21,7 @@ package me.lucko.spark.minestom; import me.lucko.spark.common.util.ClassSourceLookup; + import net.minestom.server.MinecraftServer; import net.minestom.server.extensions.Extension; import net.minestom.server.extensions.ExtensionClassLoader; diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java index 3fc1a82..da46224 100644 --- a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java @@ -21,6 +21,7 @@ package me.lucko.spark.minestom; import me.lucko.spark.common.command.sender.AbstractCommandSender; + import net.kyori.adventure.text.Component; import net.minestom.server.command.CommandSender; import net.minestom.server.command.ConsoleSender; @@ -35,12 +36,12 @@ public class MinestomCommandSender extends AbstractCommandSender @Override public String getName() { - if (delegate instanceof Player player) { + if (this.delegate instanceof Player player) { return player.getUsername(); - } else if (delegate instanceof ConsoleSender) { + } else if (this.delegate instanceof ConsoleSender) { return "Console"; }else { - return "unknown:" + delegate.getClass().getSimpleName(); + return "unknown:" + this.delegate.getClass().getSimpleName(); } } @@ -54,11 +55,11 @@ public class MinestomCommandSender extends AbstractCommandSender @Override public void sendMessage(Component message) { - delegate.sendMessage(message); + this.delegate.sendMessage(message); } @Override public boolean hasPermission(String permission) { - return delegate.hasPermission(permission); + return this.delegate.hasPermission(permission); } } diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java index 64dea89..add258a 100644 --- a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java @@ -21,6 +21,7 @@ package me.lucko.spark.minestom; import me.lucko.spark.common.platform.PlatformInfo; + import net.minestom.server.MinecraftServer; public class MinestomPlatformInfo implements PlatformInfo { diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java index 8fb42e0..0b50c0a 100644 --- a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java @@ -21,7 +21,9 @@ package me.lucko.spark.minestom; import com.google.common.collect.ImmutableMap; + import me.lucko.spark.common.monitor.ping.PlayerPingProvider; + import net.minestom.server.MinecraftServer; import net.minestom.server.entity.Player; diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkExtension.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkExtension.java deleted file mode 100644 index a99f49c..0000000 --- a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkExtension.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * 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 warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.lucko.spark.minestom; - -import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.common.SparkPlugin; -import me.lucko.spark.common.command.sender.CommandSender; -import me.lucko.spark.common.monitor.ping.PlayerPingProvider; -import me.lucko.spark.common.platform.PlatformInfo; -import me.lucko.spark.common.tick.TickHook; -import me.lucko.spark.common.tick.TickReporter; -import me.lucko.spark.common.util.ClassSourceLookup; -import net.minestom.server.MinecraftServer; -import net.minestom.server.command.builder.Command; -import net.minestom.server.command.builder.arguments.ArgumentStringArray; -import net.minestom.server.command.builder.arguments.ArgumentType; -import net.minestom.server.command.builder.suggestion.SuggestionEntry; -import net.minestom.server.extensions.Extension; -import net.minestom.server.timer.ExecutionType; - -import java.nio.file.Path; -import java.util.logging.Level; -import java.util.stream.Stream; - -public class MinestomSparkExtension extends Extension implements SparkPlugin { - private SparkPlatform platform; - private MinestomSparkCommand command; - - @Override - public String getVersion() { - return getOrigin().getVersion(); - } - - @Override - public Path getPluginDirectory() { - return getDataDirectory(); - } - - @Override - public String getCommandName() { - return "spark"; - } - - @Override - public Stream getCommandSenders() { - return Stream.concat( - MinecraftServer.getConnectionManager().getOnlinePlayers().stream(), - Stream.of(MinecraftServer.getCommandManager().getConsoleSender()) - ).map(MinestomCommandSender::new); - } - - @Override - public void executeAsync(Runnable task) { - MinecraftServer.getSchedulerManager().scheduleNextTick(task, ExecutionType.ASYNC); - } - - @Override - public void log(Level level, String msg) { - if (level == Level.INFO) { - this.getLogger().info(msg); - } else if (level == Level.WARNING) { - this.getLogger().warn(msg); - } else if (level == Level.SEVERE) { - this.getLogger().error(msg); - } else { - throw new IllegalArgumentException(level.getName()); - } - } - - @Override - public PlatformInfo getPlatformInfo() { - return new MinestomPlatformInfo(); - } - - @Override - public ClassSourceLookup createClassSourceLookup() { - return new MinestomClassSourceLookup(); - } - - @Override - public PlayerPingProvider createPlayerPingProvider() { - return new MinestomPlayerPingProvider(); - } - - @Override - public TickReporter createTickReporter() { - return new MinestomTickReporter(); - } - - @Override - public TickHook createTickHook() { - return new MinestomTickHook(); - } - - @Override - public void initialize() { - this.platform = new SparkPlatform(this); - this.platform.enable(); - this.command = new MinestomSparkCommand(this); - MinecraftServer.getCommandManager().register(command); - } - - @Override - public void terminate() { - this.platform.disable(); - MinecraftServer.getCommandManager().unregister(command); - } - - private static final class MinestomSparkCommand extends Command { - public MinestomSparkCommand(MinestomSparkExtension extension) { - super("spark", "sparkms"); - setDefaultExecutor((sender, context) -> extension.platform.executeCommand(new MinestomCommandSender(sender), new String[0])); - ArgumentStringArray arrayArgument = ArgumentType.StringArray("query"); - arrayArgument.setSuggestionCallback((sender, context, suggestion) -> { - String[] args = context.get(arrayArgument); - if (args == null) { - args = new String[0]; - } - Iterable suggestionEntries = extension.platform.tabCompleteCommand(new MinestomCommandSender(sender), args); - for (String suggestionEntry : suggestionEntries) { - suggestion.addEntry(new SuggestionEntry(suggestionEntry)); - } - }); - addSyntax((sender, context) -> { - String[] args = context.get(arrayArgument); - if (args == null) { - args = new String[0]; - } - extension.platform.executeCommand(new MinestomCommandSender(sender), args); - }, arrayArgument); - } - } -} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkPlugin.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkPlugin.java new file mode 100644 index 0000000..2b43cae --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkPlugin.java @@ -0,0 +1,183 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.minestom; + +import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.monitor.ping.PlayerPingProvider; +import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.tick.TickHook; +import me.lucko.spark.common.tick.TickReporter; +import me.lucko.spark.common.util.ClassSourceLookup; + +import net.minestom.server.MinecraftServer; +import net.minestom.server.command.CommandSender; +import net.minestom.server.command.builder.Command; +import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.CommandExecutor; +import net.minestom.server.command.builder.arguments.ArgumentStringArray; +import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.suggestion.Suggestion; +import net.minestom.server.command.builder.suggestion.SuggestionCallback; +import net.minestom.server.command.builder.suggestion.SuggestionEntry; +import net.minestom.server.extensions.Extension; +import net.minestom.server.timer.ExecutionType; + +import org.jetbrains.annotations.NotNull; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.logging.Level; +import java.util.stream.Stream; + +public class MinestomSparkPlugin extends Extension implements SparkPlugin { + private SparkPlatform platform; + private MinestomSparkCommand command; + + @Override + public void initialize() { + this.platform = new SparkPlatform(this); + this.platform.enable(); + this.command = new MinestomSparkCommand(this.platform); + MinecraftServer.getCommandManager().register(this.command); + } + + @Override + public void terminate() { + this.platform.disable(); + MinecraftServer.getCommandManager().unregister(this.command); + } + + @Override + public String getVersion() { + return getOrigin().getVersion(); + } + + @Override + public Path getPluginDirectory() { + return getDataDirectory(); + } + + @Override + public String getCommandName() { + return "spark"; + } + + @Override + public Stream getCommandSenders() { + return Stream.concat( + MinecraftServer.getConnectionManager().getOnlinePlayers().stream(), + Stream.of(MinecraftServer.getCommandManager().getConsoleSender()) + ).map(MinestomCommandSender::new); + } + + @Override + public void executeAsync(Runnable task) { + MinecraftServer.getSchedulerManager().scheduleNextTick(task, ExecutionType.ASYNC); + } + + @Override + public void log(Level level, String msg) { + if (level == Level.INFO) { + this.getLogger().info(msg); + } else if (level == Level.WARNING) { + this.getLogger().warn(msg); + } else if (level == Level.SEVERE) { + this.getLogger().error(msg); + } else { + throw new IllegalArgumentException(level.getName()); + } + } + + @Override + public PlatformInfo getPlatformInfo() { + return new MinestomPlatformInfo(); + } + + @Override + public ClassSourceLookup createClassSourceLookup() { + return new MinestomClassSourceLookup(); + } + + @Override + public PlayerPingProvider createPlayerPingProvider() { + return new MinestomPlayerPingProvider(); + } + + @Override + public TickReporter createTickReporter() { + return new MinestomTickReporter(); + } + + @Override + public TickHook createTickHook() { + return new MinestomTickHook(); + } + + private static final class MinestomSparkCommand extends Command implements CommandExecutor, SuggestionCallback { + private final SparkPlatform platform; + + public MinestomSparkCommand(SparkPlatform platform) { + super("spark"); + this.platform = platform; + + ArgumentStringArray arrayArgument = ArgumentType.StringArray("args"); + arrayArgument.setSuggestionCallback(this); + + addSyntax(this, arrayArgument); + setDefaultExecutor((sender, context) -> platform.executeCommand(new MinestomCommandSender(sender), new String[0])); + } + + // execute + @Override + public void apply(@NotNull CommandSender sender, @NotNull CommandContext context) { + String[] args = processArgs(context, false); + if (args == null) { + return; + } + + this.platform.executeCommand(new MinestomCommandSender(sender), args); + } + + // tab complete + @Override + public void apply(@NotNull CommandSender sender, @NotNull CommandContext context, @NotNull Suggestion suggestion) { + String[] args = processArgs(context, true); + if (args == null) { + return; + } + + Iterable suggestionEntries = this.platform.tabCompleteCommand(new MinestomCommandSender(sender), args); + for (String suggestionEntry : suggestionEntries) { + suggestion.addEntry(new SuggestionEntry(suggestionEntry)); + } + } + + private static String [] processArgs(CommandContext context, boolean tabComplete) { + String[] split = context.getInput().split(" ", tabComplete ? -1 : 0); + if (split.length == 0 || !split[0].equals("/spark") && !split[0].equals("spark")) { + return null; + } + + return Arrays.copyOfRange(split, 1, split.length); + } + } +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java index e5a1895..44c1c2d 100644 --- a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java @@ -21,6 +21,7 @@ package me.lucko.spark.minestom; import me.lucko.spark.common.tick.AbstractTickHook; + import net.minestom.server.MinecraftServer; import net.minestom.server.timer.Task; import net.minestom.server.timer.TaskSchedule; @@ -30,7 +31,7 @@ public class MinestomTickHook extends AbstractTickHook { @Override public void start() { - task = MinecraftServer.getSchedulerManager() + this.task = MinecraftServer.getSchedulerManager() .buildTask(this::onTick) .delay(TaskSchedule.tick(1)) .repeat(TaskSchedule.tick(1)) @@ -39,8 +40,8 @@ public class MinestomTickHook extends AbstractTickHook { @Override public void close() { - if (task != null) { - task.cancel(); + if (this.task != null) { + this.task.cancel(); } } } diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.java index dbb09ea..ae25f92 100644 --- a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.java +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.java @@ -21,6 +21,7 @@ package me.lucko.spark.minestom; import me.lucko.spark.common.tick.AbstractTickReporter; + import net.minestom.server.MinecraftServer; import net.minestom.server.event.Event; import net.minestom.server.event.EventNode; @@ -32,16 +33,16 @@ public class MinestomTickReporter extends AbstractTickReporter { private final EventNode node = EventNode.all("sparkTickReporter-" + UUID.randomUUID()); public MinestomTickReporter() { - node.addListener(ServerTickMonitorEvent.class, event -> onTick(event.getTickMonitor().getTickTime())); + this.node.addListener(ServerTickMonitorEvent.class, event -> onTick(event.getTickMonitor().getTickTime())); } @Override public void start() { - MinecraftServer.getGlobalEventHandler().addChild(node); + MinecraftServer.getGlobalEventHandler().addChild(this.node); } @Override public void close() { - MinecraftServer.getGlobalEventHandler().removeChild(node); + MinecraftServer.getGlobalEventHandler().removeChild(this.node); } } -- cgit