mirror of https://github.com/Minestom/Minestom.git
Update args
This commit is contained in:
parent
3dfd37ec08
commit
5c8cf728c2
|
@ -19,36 +19,34 @@ public class ArgumentBlockState extends Argument<Block> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Block parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.getWord();
|
||||
public @NotNull Result<Block> parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.readWord();
|
||||
final int nbtIndex = input.indexOf("[");
|
||||
if (nbtIndex == 0)
|
||||
throw new ArgumentSyntaxException("No block type", input, NO_BLOCK);
|
||||
return Result.syntaxError("No block type", input, NO_BLOCK);
|
||||
|
||||
if (nbtIndex == -1) {
|
||||
// Only block name
|
||||
final Block block = Block.fromNamespaceId(input);
|
||||
if (block == null)
|
||||
throw new ArgumentSyntaxException("Invalid block type", input, INVALID_BLOCK);
|
||||
reader.consume();
|
||||
return block;
|
||||
return Result.syntaxError("Invalid block type", input, INVALID_BLOCK);
|
||||
return Result.success(block);
|
||||
} else {
|
||||
reader.consume();
|
||||
if (!input.endsWith("]"))
|
||||
throw new ArgumentSyntaxException("Property list need to end with ]", input, INVALID_PROPERTY);
|
||||
return Result.syntaxError("Property list need to end with ]", input, INVALID_PROPERTY);
|
||||
// Block state
|
||||
final String blockName = input.substring(0, nbtIndex);
|
||||
Block block = Block.fromNamespaceId(blockName);
|
||||
if (block == null)
|
||||
throw new ArgumentSyntaxException("Invalid block type", input, INVALID_BLOCK);
|
||||
return Result.syntaxError("Invalid block type", input, INVALID_BLOCK);
|
||||
|
||||
// Compute properties
|
||||
final String query = input.substring(nbtIndex);
|
||||
final var propertyMap = BlockUtils.parseProperties(query);
|
||||
try {
|
||||
return block.withProperties(propertyMap);
|
||||
return Result.success(block.withProperties(propertyMap));
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ArgumentSyntaxException("Invalid property values", input, INVALID_PROPERTY_VALUE);
|
||||
return Result.syntaxError("Invalid property values", input, INVALID_PROPERTY_VALUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
|||
import net.kyori.adventure.text.format.Style;
|
||||
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;
|
||||
|
||||
/**
|
||||
|
@ -22,22 +21,20 @@ public class ArgumentColor extends Argument<Style> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Style parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.getWord();
|
||||
public @NotNull Result<Style> parse(CommandReader reader) {
|
||||
final String input = reader.readWord();
|
||||
// check for colour
|
||||
NamedTextColor color = NamedTextColor.NAMES.value(input);
|
||||
if (color != null) {
|
||||
reader.consume();
|
||||
return Style.style(color);
|
||||
return Result.success(Style.style(color));
|
||||
}
|
||||
|
||||
// check for reset
|
||||
if (input.equals("reset")) {
|
||||
reader.consume();
|
||||
return Style.empty();
|
||||
return Result.success(Style.empty());
|
||||
}
|
||||
|
||||
throw new ArgumentSyntaxException("Undefined color", input, UNDEFINED_COLOR);
|
||||
return Result.syntaxError("Undefined color", input, UNDEFINED_COLOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -16,23 +16,20 @@ public class ArgumentComponent extends Argument<Component> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Component parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final char start = reader.getNextChar();
|
||||
if (start != '{') throw new ArgumentSyntaxException("Invalid component start", start+"", INVALID_JSON_ERROR);
|
||||
public @NotNull Result<Component> parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final char start = reader.peekNextChar();
|
||||
if (start != '{') Result.incompatibleType();
|
||||
final int end = reader.getClosingIndexOfJsonObject(0);
|
||||
|
||||
if (end == -1) {
|
||||
final String remaining = reader.getRemaining();
|
||||
reader.consume();
|
||||
throw new ArgumentSyntaxException("Invalid JSON", remaining, INVALID_JSON_ERROR);
|
||||
return Result.syntaxError("Invalid JSON", reader.getRemaining(), INVALID_JSON_ERROR);
|
||||
}
|
||||
|
||||
final String s = reader.get(end);
|
||||
reader.consume();
|
||||
final String s = reader.read(end);
|
||||
try {
|
||||
return GsonComponentSerializer.gson().deserialize(s);
|
||||
return Result.success(GsonComponentSerializer.gson().deserialize(s));
|
||||
} catch (Exception e) {
|
||||
throw new ArgumentSyntaxException("Invalid component", s, INVALID_JSON_ERROR);
|
||||
return Result.syntaxError("Invalid component", s, INVALID_JSON_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,13 +32,12 @@ public abstract class ArgumentRange<T extends Range<N>, N extends Number> extend
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.getWord();
|
||||
public @NotNull Result<T> parse(CommandReader reader) throws ArgumentSyntaxException {
|
||||
final String input = reader.readWord();
|
||||
try {
|
||||
final String[] split = input.split(Pattern.quote(".."), -1);
|
||||
|
||||
if (split.length == 2) {
|
||||
reader.consume();
|
||||
final N min;
|
||||
final N max;
|
||||
if (split[0].length() == 0 && split[1].length() > 0) {
|
||||
|
@ -47,8 +46,12 @@ public abstract class ArgumentRange<T extends Range<N>, N extends Number> extend
|
|||
max = parser.apply(split[1]);
|
||||
} else if (split[0].length() > 0 && split[1].length() == 0) {
|
||||
// Format NUMBER..
|
||||
min = parser.apply(split[0]);
|
||||
max = this.max;
|
||||
try {
|
||||
min = parser.apply(split[0]);
|
||||
max = this.max;
|
||||
} catch (NumberFormatException e) {
|
||||
return Result.incompatibleType();
|
||||
}
|
||||
} else if (split[0].length() > 0) {
|
||||
// Format NUMBER..NUMBER
|
||||
min = parser.apply(split[0]);
|
||||
|
@ -57,15 +60,15 @@ public abstract class ArgumentRange<T extends Range<N>, N extends Number> extend
|
|||
// Format ..
|
||||
throw new ArgumentSyntaxException("Invalid range format", input, FORMAT_ERROR);
|
||||
}
|
||||
return rangeConstructor.apply(min, max);
|
||||
return Result.success(rangeConstructor.apply(min, max));
|
||||
} else if (split.length == 1) {
|
||||
final N number = parser.apply(input);
|
||||
reader.consume();
|
||||
return rangeConstructor.apply(number, number);
|
||||
return Result.success(rangeConstructor.apply(number, number));
|
||||
} else {
|
||||
return Result.syntaxError("Invalid range format", input, FORMAT_ERROR);
|
||||
}
|
||||
} catch (NumberFormatException e2) {
|
||||
throw new ArgumentSyntaxException("Invalid number", input, FORMAT_ERROR);
|
||||
return Result.syntaxError("Invalid number", input, FORMAT_ERROR);
|
||||
}
|
||||
throw new ArgumentSyntaxException("Invalid range format", input, FORMAT_ERROR);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue