mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 17:11:58 +01:00
Added ArgumentLiteral
This commit is contained in:
parent
c6e15d4bda
commit
c11151360b
@ -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<String> {
|
||||
|
||||
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};
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user