mirror of https://github.com/Minestom/Minestom.git
Update args
This commit is contained in:
parent
6811e39137
commit
80bfe05e16
|
@ -10,27 +10,22 @@ import org.jetbrains.annotations.NotNull;
|
|||
* Example: true
|
||||
*/
|
||||
public class ArgumentBoolean extends Argument<Boolean> {
|
||||
|
||||
public static final int NOT_BOOLEAN_ERROR = 1;
|
||||
|
||||
public ArgumentBoolean(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Boolean parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String word = reader.getWord();
|
||||
public @NotNull Result<Boolean> parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String word = reader.readWord();
|
||||
|
||||
if (word.equalsIgnoreCase("true")) {
|
||||
reader.consume();
|
||||
return true;
|
||||
return Result.success(true);
|
||||
}
|
||||
if (word.equalsIgnoreCase("false")) {
|
||||
reader.consume();
|
||||
return false;
|
||||
return Result.success(false);
|
||||
}
|
||||
|
||||
throw new ArgumentSyntaxException("Not a boolean", word, NOT_BOOLEAN_ERROR);
|
||||
return Result.incompatibleType();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,26 +1,22 @@
|
|||
package net.minestom.server.command.builder.arguments;
|
||||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ArgumentLiteral extends Argument<String> {
|
||||
|
||||
public static final int INVALID_VALUE_ERROR = 1;
|
||||
|
||||
public ArgumentLiteral(@NotNull String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String word = reader.getWord();
|
||||
public @NotNull Result<String> parse(CommandReader reader) {
|
||||
final String word = reader.readWord();
|
||||
|
||||
if (!word.equals(getId()))
|
||||
throw new ArgumentSyntaxException("Invalid literal value", word, INVALID_VALUE_ERROR);
|
||||
return Result.incompatibleType();
|
||||
|
||||
reader.consume();
|
||||
return word;
|
||||
return Result.success(word);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,7 +22,7 @@ public class ArgumentComponent extends Argument<Component> {
|
|||
final int end = reader.getClosingIndexOfJsonObject(0);
|
||||
|
||||
if (end == -1) {
|
||||
return Result.syntaxError("Invalid JSON", reader.getRemaining(), INVALID_JSON_ERROR);
|
||||
return Result.syntaxError("Invalid JSON", reader.readRemaining(), INVALID_JSON_ERROR);
|
||||
}
|
||||
|
||||
final String s = reader.read(end);
|
||||
|
|
|
@ -2,7 +2,6 @@ package net.minestom.server.command.builder.arguments.minecraft;
|
|||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
@ -25,22 +24,22 @@ public class ArgumentNbtCompoundTag extends Argument<NBTCompound> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull NBTCompound parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
public @NotNull Result<NBTCompound> parse(CommandReader reader) {
|
||||
int end = reader.getClosingIndexOfJsonObject(0);
|
||||
if (end == -1) {
|
||||
throw new ArgumentSyntaxException("Invalid NBT", "", INVALID_NBT);
|
||||
return Result.syntaxError("Invalid NBT", "", INVALID_NBT);
|
||||
} else {
|
||||
final String input = reader.get(end);
|
||||
reader.consume();
|
||||
final String input = reader.read(end);
|
||||
try {
|
||||
NBT nbt = new SNBTParser(new StringReader(input)).parse();
|
||||
|
||||
if (!(nbt instanceof NBTCompound))
|
||||
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
||||
|
||||
return (NBTCompound) nbt;
|
||||
if (nbt instanceof NBTCompound compound) {
|
||||
return Result.success(compound);
|
||||
} else {
|
||||
return Result.syntaxError("Not a compound", input, INVALID_NBT);
|
||||
}
|
||||
} catch (NBTException e) {
|
||||
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
||||
return Result.syntaxError("NBTCompound is invalid", input, INVALID_NBT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package net.minestom.server.command.builder.arguments.minecraft;
|
|||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTException;
|
||||
|
@ -26,20 +25,19 @@ public class ArgumentNbtTag extends Argument<NBT> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull NBT parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
public @NotNull Result<NBT> parse(CommandReader reader) {
|
||||
int end = reader.getClosingIndexOfJsonObject(0);
|
||||
if (end == -1) {
|
||||
end = reader.getClosingIndexOfJsonArray(0);
|
||||
}
|
||||
if (end == -1) {
|
||||
throw new ArgumentSyntaxException("Invalid NBT", "", INVALID_NBT);
|
||||
return Result.syntaxError("Invalid NBT", "", INVALID_NBT);
|
||||
} else {
|
||||
final String input = reader.get(end);
|
||||
reader.consume();
|
||||
final String input = reader.read(end);
|
||||
try {
|
||||
return new SNBTParser(new StringReader(input)).parse();
|
||||
return Result.success(new SNBTParser(new StringReader(input)).parse());
|
||||
} catch (NBTException e) {
|
||||
throw new ArgumentSyntaxException("Invalid NBT", input, INVALID_NBT);
|
||||
return Result.syntaxError("Invalid NBT", input, INVALID_NBT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package net.minestom.server.command.builder.arguments.minecraft.registry;
|
|||
|
||||
import net.minestom.server.command.CommandReader;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class ArgumentRegistry<T> extends Argument<T> {
|
||||
|
@ -16,13 +15,13 @@ public abstract class ArgumentRegistry<T> extends Argument<T> {
|
|||
public abstract T getRegistry(@NotNull String value);
|
||||
|
||||
@Override
|
||||
public @NotNull T parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.getWord();
|
||||
public @NotNull Result<T> parse(CommandReader reader) {
|
||||
final String input = reader.readWord();
|
||||
final T registryValue = getRegistry(input);
|
||||
if (registryValue == null)
|
||||
throw new ArgumentSyntaxException("Registry value is invalid", input, INVALID_NAME);
|
||||
return Result.syntaxError("Registry value is invalid", input, INVALID_NAME);
|
||||
//fixme check vanilla incompatible/syntax error and in every other arg too
|
||||
|
||||
reader.consume();
|
||||
return registryValue;
|
||||
return Result.success(registryValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,10 +41,10 @@ public class ArgumentNumber<T extends Number> extends Argument<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final char start = reader.getNextChar();
|
||||
if ((start < '0' || start > '9') && start != '-') throw new ArgumentSyntaxException("Numbers cannot start with", start+"", NOT_NUMBER_ERROR);
|
||||
final String input = reader.getWord();
|
||||
public @NotNull Result<T> parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final char start = reader.peekNextChar();
|
||||
if ((start < '0' || start > '9') && start != '-') return Result.incompatibleType();
|
||||
final String input = reader.readWord();
|
||||
try {
|
||||
final T value;
|
||||
final int radix = getRadix(input);
|
||||
|
@ -53,19 +53,18 @@ public class ArgumentNumber<T extends Number> extends Argument<T> {
|
|||
} else {
|
||||
value = radixParser.apply(parseValue(input), radix);
|
||||
}
|
||||
reader.consume();
|
||||
|
||||
// Check range
|
||||
if (hasMin && comparator.compare(value, min) < 0) {
|
||||
throw new ArgumentSyntaxException("Input is lower than the minimum allowed value", input, TOO_LOW_ERROR);
|
||||
return Result.syntaxError("Input is lower than the minimum allowed value", input, TOO_LOW_ERROR);
|
||||
}
|
||||
if (hasMax && comparator.compare(value, max) > 0) {
|
||||
throw new ArgumentSyntaxException("Input is higher than the maximum allowed value", input, TOO_HIGH_ERROR);
|
||||
return Result.syntaxError("Input is higher than the maximum allowed value", input, TOO_HIGH_ERROR);
|
||||
}
|
||||
|
||||
return value;
|
||||
return Result.success(value);
|
||||
} catch (NumberFormatException | NullPointerException e) {
|
||||
throw new ArgumentSyntaxException("Input is not a number, or it's invalid for the given type", input, NOT_NUMBER_ERROR);
|
||||
return Result.syntaxError("Input is not a number, or it's invalid for the given type", input, NOT_NUMBER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue