use of org.spongepowered.api.command.exception.ArgumentParseException in project SpongeCommon by SpongePowered.
the class SpongeStringReader method parseNBTString.
@Override
public String parseNBTString() throws ArgumentParseException {
final int startCursor = this.getCursor();
try {
new TagParser(this).readStruct();
} catch (final CommandSyntaxException ex) {
this.setCursor(startCursor);
throw new ArgumentParseException(SpongeAdventure.asAdventure(ex.getRawMessage()), ex.getInput(), ex.getCursor());
}
// this will be just after a }
final int endCursor = this.getCursor();
return this.input().substring(startCursor, endCursor);
}
use of org.spongepowered.api.command.exception.ArgumentParseException in project SpongeCommon by SpongePowered.
the class SpongeServerLocationValueParameter method parseValue.
@Override
@NonNull
public Optional<? extends ServerLocation> parseValue(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
final ArgumentReader.Immutable state = reader.immutable();
ServerWorld serverWorld;
try {
final ResourceKey resourceLocation = reader.parseResourceKey("minecraft");
serverWorld = SpongeCommon.game().server().worldManager().world(resourceLocation).orElseThrow(() -> reader.createException(Component.text("Could not get world with key \"" + resourceLocation.toString() + "\"")));
} catch (final ArgumentParseException e) {
final Optional<ServerLocation> location = cause.location();
if (location.isPresent()) {
// do this as late as possible to prevent expense of regex.
if (!SpongeServerLocationValueParameter.STARTS_WITH_NUMBER.matcher(state.remaining()).find()) {
throw e;
}
serverWorld = location.get().world();
} else {
throw e;
}
reader.setState(state);
}
try {
reader.skipWhitespace();
final Vec3 vec3d = SpongeServerLocationValueParameter.VEC_3_ARGUMENT.parse((StringReader) reader).getPosition((CommandSourceStack) cause);
return Optional.of(serverWorld.location(VecHelper.toVector3d(vec3d)));
} catch (final CommandSyntaxException e) {
throw reader.createException(Component.text(e.getMessage()));
}
}
use of org.spongepowered.api.command.exception.ArgumentParseException in project SpongeCommon by SpongePowered.
the class SpongeTargetBlockValueParameter method parseValue.
@Override
@NonNull
public Optional<? extends ServerLocation> parseValue(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
final Object root = cause.cause().root();
if (root instanceof Living) {
final Living living = (Living) root;
final Optional<RayTraceResult<@NonNull LocatableBlock>> rayTraceResult = RayTrace.block().sourceEyePosition(living).direction(living.headDirection()).limit(30).continueWhileBlock(RayTrace.onlyAir()).select(RayTrace.nonAir()).continueWhileEntity(r -> false).execute();
if (rayTraceResult.isPresent()) {
return rayTraceResult.map(x -> x.selectedObject().serverLocation());
}
throw reader.createException(Component.text("The cause root is not looking at a block!"));
}
throw reader.createException(Component.text("The cause root must be a Living!"));
}
use of org.spongepowered.api.command.exception.ArgumentParseException in project SpongeCommon by SpongePowered.
the class SpongeParameterValue method parseInternal.
private void parseInternal(final ArgumentReader.Mutable args, final CommandContext.Builder context) throws ArgumentParseException {
List<ArgumentParseException> currentExceptions = null;
final ArgumentReader.Immutable state = args.immutable();
final CommandContext.Builder.Transaction transaction = context.startTransaction();
for (final ValueParser<? extends T> parser : this.parsers) {
try {
T value = parser.parseValue(this.key, args, context).orElse(null);
if (this.modifier != null) {
value = this.modifier.modifyResult(this.key, args.immutable(), context, value).orElse(null);
}
if (value != null) {
context.putEntry(this.key, value);
}
context.commit(transaction);
// something parsed, so we exit.
return;
} catch (final ArgumentParseException ex) {
if (currentExceptions == null) {
currentExceptions = new ArrayList<>();
}
currentExceptions.add(ex);
args.setState(state);
context.rollback(transaction);
}
}
// If we get this far, we failed to parse, return the exceptions
if (currentExceptions == null) {
throw new CommandRuntimeException(new TextComponent("Could not parse element"));
// throw new ArgumentParseException(t("Could not parse element"), args.getInput(), args.cursor());
} else if (currentExceptions.size() == 1) {
throw currentExceptions.get(0);
} else {
final List<Component> errors = currentExceptions.stream().map(ArgumentParseException::superText).collect(Collectors.toList());
throw new ArgumentParseException(Component.join(Component.newline(), errors), args.input(), args.cursor());
}
}
Aggregations