From 053ef06ee1ee6605115ed86d3744b551b8c8ed71 Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 11 Feb 2021 03:00:13 +0100 Subject: [PATCH] Cleanup (moved some packet methods to DeclareCommandsPacket) and added CommandResult#getInput --- .../server/command/CommandManager.java | 45 +++++-------------- .../command/builder/CommandDispatcher.java | 3 +- .../server/command/builder/CommandResult.java | 9 +++- .../command/builder/arguments/Argument.java | 6 +-- .../builder/arguments/ArgumentCommand.java | 2 +- .../builder/arguments/ArgumentLiteral.java | 4 +- .../builder/arguments/ArgumentWord.java | 4 +- .../server/play/DeclareCommandsPacket.java | 28 ++++++++++++ 8 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index f5ac20ca1..d7801cd0c 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -157,7 +157,7 @@ public final class CommandManager { player.callEvent(PlayerCommandEvent.class, playerCommandEvent); if (playerCommandEvent.isCancelled()) - return CommandResult.withType(CommandResult.Type.CANCELLED); + return CommandResult.of(CommandResult.Type.CANCELLED, command); command = playerCommandEvent.getCommand(); } @@ -178,13 +178,13 @@ public final class CommandManager { if (unknownCommandCallback != null) { this.unknownCommandCallback.apply(sender, command); } - return CommandResult.withType(CommandResult.Type.CANCELLED); + return CommandResult.of(CommandResult.Type.CANCELLED, command); } // Execute the legacy-command final String[] args = command.substring(command.indexOf(StringUtils.SPACE) + 1).split(StringUtils.SPACE); commandProcessor.process(sender, commandName, args); - return CommandResult.withType(CommandResult.Type.SUCCESS); + return CommandResult.of(CommandResult.Type.SUCCESS, command); } } } @@ -328,7 +328,8 @@ public final class CommandManager { for (String alias : command.getAliases()) { DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node(); - node.flags = getFlag(NodeType.LITERAL, false, true, false); + node.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, + false, true, false); node.name = alias; node.redirectedNode = mainNodeIndex; nodes.add(node); @@ -359,7 +360,8 @@ public final class CommandManager { // Server suggestion (ask_server) { DeclareCommandsPacket.Node tabNode = new DeclareCommandsPacket.Node(); - tabNode.flags = getFlag(NodeType.ARGUMENT, true, false, tracking); + tabNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.ARGUMENT, + true, false, tracking); tabNode.name = tracking ? "tab_completion" : "args"; tabNode.parser = "brigadier:string"; tabNode.properties = packetWriter -> packetWriter.writeVarInt(2); // Greedy phrase @@ -372,7 +374,8 @@ public final class CommandManager { } DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node(); - literalNode.flags = getFlag(NodeType.LITERAL, true, false, false); + literalNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, + true, false, false); literalNode.name = name; literalNode.children = new int[]{nodes.size() - 1}; @@ -517,37 +520,9 @@ public final class CommandManager { @NotNull private DeclareCommandsPacket.Node createMainNode(@NotNull String name, boolean executable) { DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node(); - literalNode.flags = getFlag(NodeType.LITERAL, executable, false, false); + literalNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, executable, false, false); literalNode.name = name; return literalNode; } - - public byte getFlag(@NotNull NodeType type, boolean executable, boolean redirect, boolean suggestionType) { - byte result = (byte) type.mask; - - if (executable) { - result |= 0x04; - } - - if (redirect) { - result |= 0x08; - } - - if (suggestionType) { - result |= 0x10; - } - return result; - } - - public enum NodeType { - ROOT(0), LITERAL(0b1), ARGUMENT(0b10), NONE(0x11); - - private final int mask; - - NodeType(int mask) { - this.mask = mask; - } - - } } diff --git a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java index bb3c0ddf5..dbc5c6c0e 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java +++ b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java @@ -86,7 +86,7 @@ public class CommandDispatcher { final Command command = findCommand(commandName); // Check if the command exists if (command == null) { - return CommandResult.withType(CommandResult.Type.UNKNOWN); + return CommandResult.of(CommandResult.Type.UNKNOWN, commandName); } // Removes the command's name + the space after @@ -94,6 +94,7 @@ public class CommandDispatcher { // Find the used syntax CommandResult result = new CommandResult(); + result.input = commandString; ParsedCommand parsedCommand = findParsedCommand(command, args); if (parsedCommand != null && parsedCommand.executor != null) { // Syntax found diff --git a/src/main/java/net/minestom/server/command/builder/CommandResult.java b/src/main/java/net/minestom/server/command/builder/CommandResult.java index 2267db0db..84fcbcbe3 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandResult.java +++ b/src/main/java/net/minestom/server/command/builder/CommandResult.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable; public class CommandResult { protected Type type = Type.UNKNOWN; + protected String input; protected ParsedCommand parsedCommand; protected CommandData commandData; @@ -14,6 +15,11 @@ public class CommandResult { return type; } + @NotNull + public String getInput() { + return input; + } + @Nullable public ParsedCommand getParsedCommand() { return parsedCommand; @@ -48,9 +54,10 @@ public class CommandResult { } @NotNull - public static CommandResult withType(@NotNull Type type) { + public static CommandResult of(@NotNull Type type, @NotNull String input) { CommandResult result = new CommandResult(); result.type = type; + result.input = input; return result; } diff --git a/src/main/java/net/minestom/server/command/builder/arguments/Argument.java b/src/main/java/net/minestom/server/command/builder/arguments/Argument.java index 1e8f5de25..2fa9a5013 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/Argument.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/Argument.java @@ -1,7 +1,5 @@ package net.minestom.server.command.builder.arguments; -import net.minestom.server.MinecraftServer; -import net.minestom.server.command.CommandManager; import net.minestom.server.command.builder.ArgumentCallback; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandExecutor; @@ -22,8 +20,6 @@ import org.jetbrains.annotations.Nullable; */ public abstract class Argument { - protected final static CommandManager COMMAND_MANAGER = MinecraftServer.getCommandManager(); - private final String id; private final boolean allowSpace; private final boolean useRemaining; @@ -96,7 +92,7 @@ public abstract class Argument { boolean executable, boolean redirect, boolean suggestion) { DeclareCommandsPacket.Node argumentNode = new DeclareCommandsPacket.Node(); - argumentNode.flags = COMMAND_MANAGER.getFlag(CommandManager.NodeType.ARGUMENT, executable, redirect, suggestion); + argumentNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.ARGUMENT, executable, redirect, suggestion); argumentNode.name = argument.getId(); return argumentNode; diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentCommand.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentCommand.java index 26c822f73..905602847 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentCommand.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentCommand.java @@ -13,7 +13,7 @@ public class ArgumentCommand extends Argument { public static final int INVALID_COMMAND_ERROR = 1; public ArgumentCommand(@NotNull String id) { - super(id, true, true); // TODO don't use remaining? + super(id, true, true); } @NotNull diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLiteral.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLiteral.java index 911759f78..beb284a5d 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLiteral.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLiteral.java @@ -1,6 +1,5 @@ package net.minestom.server.command.builder.arguments; -import net.minestom.server.command.CommandManager; import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; @@ -26,7 +25,8 @@ public class ArgumentLiteral extends Argument { @Override public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) { DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node(); - literalNode.flags = COMMAND_MANAGER.getFlag(CommandManager.NodeType.LITERAL, executable, false, false); + literalNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, + executable, false, false); literalNode.name = getId(); nodeMaker.setCurrentNodes(new DeclareCommandsPacket.Node[]{literalNode}); diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java index 9d7fadcf8..2b6d2dd10 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java @@ -1,6 +1,5 @@ package net.minestom.server.command.builder.arguments; -import net.minestom.server.command.CommandManager; import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; @@ -93,7 +92,8 @@ public class ArgumentWord extends Argument { for (int i = 0; i < nodes.length; i++) { DeclareCommandsPacket.Node argumentNode = new DeclareCommandsPacket.Node(); - argumentNode.flags = COMMAND_MANAGER.getFlag(CommandManager.NodeType.LITERAL, executable, false, false); + argumentNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, + executable, false, false); argumentNode.name = this.getRestrictions()[i]; wordConsumer.accept(argumentNode); nodes[i] = argumentNode; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java index d8e2ccb73..3d41b16c3 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java @@ -74,4 +74,32 @@ public class DeclareCommandsPacket implements ServerPacket { } + public static byte getFlag(@NotNull NodeType type, boolean executable, boolean redirect, boolean suggestionType) { + byte result = (byte) type.mask; + + if (executable) { + result |= 0x04; + } + + if (redirect) { + result |= 0x08; + } + + if (suggestionType) { + result |= 0x10; + } + return result; + } + + public enum NodeType { + ROOT(0), LITERAL(0b1), ARGUMENT(0b10), NONE(0x11); + + private final int mask; + + NodeType(int mask) { + this.mask = mask; + } + + } + }