From afa6f7ec8a48140721d95efbfab8f342004980ec Mon Sep 17 00:00:00 2001 From: Chipmunk <65827213+ChipmunkMC@users.noreply.github.com> Date: Sun, 12 Mar 2023 20:51:15 -0400 Subject: [PATCH] Refactor commands a bit --- .../chipmunk/chipmunkbot/command/Command.java | 15 ----- .../chipmunkbot/commands/EchoCommand.java | 11 ++-- .../chipmunkbot/commands/HelpCommand.java | 56 ++++++++++++++----- .../chipmunkbot/commands/InfoCommand.java | 13 ++--- .../chipmunkbot/commands/MusicCommand.java | 27 ++++----- .../chipmunkbot/commands/NetMsgCommand.java | 11 ++-- .../commands/ReconnectCommand.java | 11 ++-- .../chipmunkbot/commands/RunCommand.java | 11 ++-- .../chipmunkbot/commands/TestCommand.java | 11 ++-- .../chipmunkbot/plugins/CommandManager.java | 21 +++---- 10 files changed, 100 insertions(+), 87 deletions(-) delete mode 100644 src/main/java/land/chipmunk/chipmunkbot/command/Command.java diff --git a/src/main/java/land/chipmunk/chipmunkbot/command/Command.java b/src/main/java/land/chipmunk/chipmunkbot/command/Command.java deleted file mode 100644 index 364e670..0000000 --- a/src/main/java/land/chipmunk/chipmunkbot/command/Command.java +++ /dev/null @@ -1,15 +0,0 @@ -package land.chipmunk.chipmunkbot.command; - -import com.mojang.brigadier.tree.LiteralCommandNode; -import com.mojang.brigadier.builder.LiteralArgumentBuilder; -import lombok.Getter; -import lombok.AllArgsConstructor; -import lombok.NoArgsConstructor; - -@AllArgsConstructor -@NoArgsConstructor -public class Command { - @Getter private LiteralCommandNode node; - protected void node (LiteralCommandNode node) { this.node = node; } - protected void node (LiteralArgumentBuilder builder) { this.node = builder.build(); } -} diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/EchoCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/EchoCommand.java index 13449a0..c3043a2 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/EchoCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/EchoCommand.java @@ -6,18 +6,19 @@ import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; import static land.chipmunk.chipmunkbot.plugins.CommandManager.argument; import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import net.kyori.adventure.text.Component; -public class EchoCommand extends Command { - public EchoCommand () { - super(); +public class EchoCommand { + public static void register (CommandDispatcher dispatcher) { + final EchoCommand instance = new EchoCommand(); - this.node( + dispatcher.register( literal("echo") .then( argument("text", greedyString()) - .executes(this::echo) + .executes(instance::echo) ) ); } diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/HelpCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/HelpCommand.java index df6814f..f175645 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/HelpCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/HelpCommand.java @@ -6,22 +6,27 @@ import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; import static land.chipmunk.chipmunkbot.plugins.CommandManager.argument; import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; import static com.mojang.brigadier.arguments.StringArgumentType.getString; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.tree.CommandNode; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.JoinConfiguration; import java.util.List; import java.util.ArrayList; -public class HelpCommand extends Command { - public HelpCommand () { - super(); +public class HelpCommand { + public static void register (CommandDispatcher dispatcher) { + final HelpCommand instance = new HelpCommand(); - this.node( + dispatcher.register( literal("help") - .executes(this::sendCommandList) + .executes(instance::sendCommandList) + .then( + argument("command", greedyString()) + .executes(instance::sendUsage) + ) ); } @@ -29,30 +34,53 @@ public class HelpCommand extends Command { final CommandSource source = context.getSource(); final ChipmunkBot client = source.client(); - final CommandDispatcher dispatcher = client.commandManager().dispatcher(); + final CommandDispatcher dispatcher = client.commandManager().dispatcher(); source.sendOutput(generateCommandList(dispatcher), false); return 1; } + public int sendUsage (CommandContext context) throws CommandSyntaxException { + final CommandSource source = context.getSource(); + final ChipmunkBot client = source.client(); + + final String commandName = getString(context, "command"); + + final CommandDispatcher dispatcher = client.commandManager().dispatcher(); + for (CommandNode node : dispatcher.getRoot().getChildren()) { + if (!node.getName().equals(commandName)) continue; + source.sendOutput(generateUsages(dispatcher, node), false); + + return 1; + } + + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().create(); + } + public Component generateCommandList (CommandDispatcher dispatcher) { final List list = new ArrayList<>(); for (CommandNode node : dispatcher.getRoot().getChildren()) { final String name = node.getName(); - final List usages = new ArrayList<>(); - for (String usage : dispatcher.getAllUsage(node, null, false)) { - final String text = (name + " " + usage).trim(); - usages.add(Component.text(text)); - } - - final HoverEvent hoverEvent = HoverEvent.showText(Component.join(JoinConfiguration.separator(Component.newline()), usages)); + final Component usages = generateUsages(dispatcher, node); + final HoverEvent hoverEvent = HoverEvent.showText(usages); list.add(Component.text(name).hoverEvent(hoverEvent)); } return Component.translatable("Commands - %s", Component.join(JoinConfiguration.separator(Component.space()), list)); } + + public Component generateUsages (CommandDispatcher dispatcher, CommandNode node) { + final List usages = new ArrayList<>(); + + for (String usage : dispatcher.getAllUsage(node, null, true)) { + final String text = (node.getName() + " " + usage).trim(); + usages.add(Component.text(text)); + } + + return Component.join(JoinConfiguration.separator(Component.newline()), usages); + } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/InfoCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/InfoCommand.java index 4cb46e9..778fc6c 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/InfoCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/InfoCommand.java @@ -7,7 +7,6 @@ import static land.chipmunk.chipmunkbot.plugins.CommandManager.argument; import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; import static com.mojang.brigadier.arguments.StringArgumentType.getString; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.context.CommandContext; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -18,18 +17,18 @@ import java.net.InetAddress; import java.lang.management.*; import java.io.IOException; -public class InfoCommand extends Command { +public class InfoCommand { private static final String REPOSITORY_URL = "https://code.chipmunk.land/ChipmunkMC/chipmunkbot"; - public InfoCommand () { - super(); + public static void register (CommandDispatcher dispatcher) { + final InfoCommand instance = new InfoCommand(); - this.node( + dispatcher.register( literal("info") - .executes(this::sendBotInfo) + .executes(instance::sendBotInfo) .then( literal("server") - .executes(this::sendServerInfo) + .executes(instance::sendServerInfo) ) ); } diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java index 0e64fcf..a135aab 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java @@ -15,6 +15,7 @@ import static land.chipmunk.chipmunkbot.command.arguments.LocationArgumentType.g import static land.chipmunk.chipmunkbot.command.arguments.LocationArgumentType.getUrl; import static land.chipmunk.chipmunkbot.command.arguments.TimestampArgumentType.timestamp; import static com.mojang.brigadier.arguments.LongArgumentType.getLong; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; @@ -25,45 +26,45 @@ import java.nio.file.Path; import java.util.List; import java.util.ArrayList; -public class MusicCommand extends Command { +public class MusicCommand { private static SimpleCommandExceptionType NO_SONG_IS_CURRENTLY_PLAYING = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("No song is currently playing"))); private static SimpleCommandExceptionType OOB_TIMESTAMP = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("Invalid timestamp for the current song"))); private static SimpleCommandExceptionType DIRECTORY_DOES_NOT_EXIST = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("The specified directory does not exist"))); - public MusicCommand () { - super(); + public static void register (CommandDispatcher dispatcher) { + final MusicCommand instance = new MusicCommand(); Path root = Path.of(SongPlayer.SONG_DIR.getPath()); - this.node( + dispatcher.register( literal("music") .then( literal("play") .then( argument("location", location(root)) - .executes(this::play) + .executes(instance::play) ) ) - .then(literal("stop").executes(this::stop)) - .then(literal("skip").executes(this::skip)) - .then(literal("pause").executes(this::pause)) + .then(literal("stop").executes(instance::stop)) + .then(literal("skip").executes(instance::skip)) + .then(literal("pause").executes(instance::pause)) .then( literal("list") - .executes(c -> list(c, root)) + .executes(c -> instance.list(c, root)) .then( argument("location", filepath(root)) - .executes(c -> list(c, getPath(c, "location"))) + .executes(c -> instance.list(c, getPath(c, "location"))) ) ) .then( literal("loop") - .executes(this::toggleLoop) + .executes(instance::toggleLoop) .then( argument("count", integer()) - .executes(this::loop) + .executes(instance::loop) ) ) @@ -71,7 +72,7 @@ public class MusicCommand extends Command { literal("goto") .then( argument("timestamp", timestamp()) - .executes(this::gotoCommand) + .executes(instance::gotoCommand) ) ) ); diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/NetMsgCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/NetMsgCommand.java index 96e5a54..b5db754 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/NetMsgCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/NetMsgCommand.java @@ -7,6 +7,7 @@ import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; import static land.chipmunk.chipmunkbot.plugins.CommandManager.argument; import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.packet.PacketProtocol; @@ -15,15 +16,15 @@ import com.github.steveice10.mc.protocol.data.ProtocolState; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -public class NetMsgCommand extends Command { - public NetMsgCommand () { - super(); +public class NetMsgCommand { + public static void register (CommandDispatcher dispatcher) { + final NetMsgCommand instance = new NetMsgCommand(); - this.node( + dispatcher.register( literal("netmsg") .then( argument("message", greedyString()) - .executes(this::netmsg) + .executes(instance::netmsg) ) ); } diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java index e1600eb..68f058c 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java @@ -3,16 +3,17 @@ package land.chipmunk.chipmunkbot.commands; import land.chipmunk.chipmunkbot.ChipmunkBot; import land.chipmunk.chipmunkbot.command.*; import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import net.kyori.adventure.text.Component; -public class ReconnectCommand extends Command { - public ReconnectCommand () { - super(); +public class ReconnectCommand { + public static void register (CommandDispatcher dispatcher) { + final ReconnectCommand instance = new ReconnectCommand(); - this.node( + dispatcher.register( literal("reconnect") - .executes(this::reconnect) + .executes(instance::reconnect) ); } diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/RunCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/RunCommand.java index 234fb18..e613582 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/RunCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/RunCommand.java @@ -6,6 +6,7 @@ import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; import static land.chipmunk.chipmunkbot.plugins.CommandManager.argument; import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; @@ -13,15 +14,15 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import java.util.concurrent.CompletableFuture; -public class RunCommand extends Command { - public RunCommand () { - super(); +public class RunCommand { + public static void register (CommandDispatcher dispatcher) { + final RunCommand instance = new RunCommand(); - this.node( + dispatcher.register( literal("run") .then( argument("command", greedyString()) - .executes(this::run) + .executes(instance::run) ) ); } diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/TestCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/TestCommand.java index 53723e8..f1619d9 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/TestCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/TestCommand.java @@ -2,16 +2,17 @@ package land.chipmunk.chipmunkbot.commands; import land.chipmunk.chipmunkbot.command.*; import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import net.kyori.adventure.text.Component; -public class TestCommand extends Command { - public TestCommand () { - super(); +public class TestCommand { + public static void register (CommandDispatcher dispatcher) { + final TestCommand instance = new TestCommand(); - this.node( + dispatcher.register( literal("test") - .executes(this::helloWorld) + .executes(instance::helloWorld) ); } diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java index ba4d579..40d9c5d 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java @@ -1,7 +1,6 @@ package land.chipmunk.chipmunkbot.plugins; import land.chipmunk.chipmunkbot.ChipmunkBot; -import land.chipmunk.chipmunkbot.command.Command; import land.chipmunk.chipmunkbot.command.CommandSource; import land.chipmunk.chipmunkbot.command.ComponentMessage; import land.chipmunk.chipmunkbot.command.BuiltInExceptions; @@ -23,16 +22,6 @@ import lombok.Setter; public class CommandManager { private ChipmunkBot client; @Getter @Setter private CommandDispatcher dispatcher = new CommandDispatcher<>(); - private final Command[] commands = { - new TestCommand(), - new HelpCommand(), - new RunCommand(), - new EchoCommand(), - new InfoCommand(), - new ReconnectCommand(), - new NetMsgCommand(), - new MusicCommand() - }; static { // ? Is messing with static properties a good idea? @@ -42,8 +31,14 @@ public class CommandManager { public CommandManager (ChipmunkBot client) { this.client = client; - final RootCommandNode root = dispatcher.getRoot(); - for (Command command : commands) root.addChild(command.node()); + TestCommand.register(dispatcher); + HelpCommand.register(dispatcher); + RunCommand.register(dispatcher); + EchoCommand.register(dispatcher); + InfoCommand.register(dispatcher); + ReconnectCommand.register(dispatcher); + NetMsgCommand.register(dispatcher); + MusicCommand.register(dispatcher); } public static void sendException (CommandSource source, CommandSyntaxException exception) {