Add Argument#parse

This commit is contained in:
TheMode 2021-07-13 01:07:02 +02:00
parent 838472b6c5
commit c643ecaeb1
6 changed files with 37 additions and 5 deletions

View File

@ -66,16 +66,28 @@ public abstract class Argument<T> {
this(id, false, false);
}
/**
* Parses an argument, using {@link Argument#getId()} as the input
*
* @param argument the argument, with the input as id
* @param <T> the result type
* @return the parsed result
* @throws ArgumentSyntaxException if the argument cannot be parsed due to a fault input (argument id)
*/
@ApiStatus.Experimental
public static <T> @NotNull T parse(@NotNull Argument<T> argument) throws ArgumentSyntaxException {
return argument.parse(argument.getId());
}
/**
* Parses the given input, and throw an {@link ArgumentSyntaxException}
* if the input cannot be convert to {@code T}
* if the input cannot be converted to {@code T}
*
* @param input the argument to parse
* @return the parsed argument
* @throws ArgumentSyntaxException if {@code value} is not valid
*/
@NotNull
public abstract T parse(@NotNull String input) throws ArgumentSyntaxException;
public abstract @NotNull T parse(@NotNull String input) throws ArgumentSyntaxException;
/**
* Turns the argument into a list of nodes for command dispatching. Make sure to set the Node's parser.

View File

@ -43,7 +43,10 @@ public class ArgumentString extends Argument<String> {
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}
@NotNull
/**
* @deprecated use {@link Argument#parse(Argument)}
*/
@Deprecated
public static String staticParse(@NotNull String input) throws ArgumentSyntaxException {
// Return if not quoted

View File

@ -31,6 +31,10 @@ public class ArgumentBlockState extends Argument<Block> {
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}
/**
* @deprecated use {@link Argument#parse(Argument)}
*/
@Deprecated
public static Block staticParse(@NotNull String input) throws ArgumentSyntaxException {
final int nbtIndex = input.indexOf("[");
if (nbtIndex == 0)

View File

@ -7,10 +7,10 @@ import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.GameMode;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.entity.EntityFinder;
import net.minestom.server.utils.math.IntRange;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
@ -90,6 +90,10 @@ public class ArgumentEntity extends Argument<EntityFinder> {
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}
/**
* @deprecated use {@link Argument#parse(Argument)}
*/
@Deprecated
@NotNull
public static EntityFinder staticParse(@NotNull String input,
boolean onlySingleEntity, boolean onlyPlayers) throws ArgumentSyntaxException {

View File

@ -1,6 +1,7 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.math.IntRange;
@ -25,6 +26,10 @@ public class ArgumentIntRange extends ArgumentRange<IntRange> {
return staticParse(input);
}
/**
* @deprecated use {@link Argument#parse(Argument)}
*/
@Deprecated
@NotNull
public static IntRange staticParse(@NotNull String input) throws ArgumentSyntaxException {
try {

View File

@ -44,6 +44,10 @@ public class ArgumentItemStack extends Argument<ItemStack> {
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}
/**
* @deprecated use {@link Argument#parse(Argument)}
*/
@Deprecated
public static ItemStack staticParse(@NotNull String input) throws ArgumentSyntaxException {
final int nbtIndex = input.indexOf("{");