From c11151360b5d436fbbc28de2a5e84cec6808d38b Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 11 Feb 2021 00:14:29 +0100 Subject: [PATCH] Added ArgumentLiteral --- .../builder/arguments/ArgumentLiteral.java | 40 +++++++++++++++++++ .../builder/arguments/ArgumentType.java | 4 ++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/net/minestom/server/command/builder/arguments/ArgumentLiteral.java 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 new file mode 100644 index 000000000..9331b2afe --- /dev/null +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLiteral.java @@ -0,0 +1,40 @@ +package net.minestom.server.command.builder.arguments; + +import net.minestom.server.command.CommandManager; +import net.minestom.server.command.builder.exception.ArgumentSyntaxException; +import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; + +public class ArgumentLiteral extends Argument { + + public static final int SPACE_ERROR = 1; + public static final int INVALID_VALUE_ERROR = 2; + + public ArgumentLiteral(@NotNull String id) { + super(id); + } + + @NotNull + @Override + public String parse(@NotNull String input) throws ArgumentSyntaxException { + if (input.contains(StringUtils.SPACE)) + throw new ArgumentSyntaxException("Literals cannot contain space character", input, SPACE_ERROR); + + // Check restrictions (acting as literal) + if (!input.equals(getId())) + throw new ArgumentSyntaxException("Invalid literal value", input, INVALID_VALUE_ERROR); + + return input; + } + + @NotNull + @Override + public DeclareCommandsPacket.Node[] toNodes(boolean executable) { + DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node(); + literalNode.flags = COMMAND_MANAGER.getFlag(CommandManager.NodeType.LITERAL, executable, false, false); + literalNode.name = getId(); + + return new DeclareCommandsPacket.Node[]{literalNode}; + } +} diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java index 92aa547f1..7edd1d957 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java @@ -18,6 +18,10 @@ import org.jetbrains.annotations.NotNull; */ public class ArgumentType { + public static ArgumentLiteral Literal(@NotNull String id) { + return new ArgumentLiteral(id); + } + public static ArgumentBoolean Boolean(@NotNull String id) { return new ArgumentBoolean(id); }