Search in sources :

Example 6 with ArgumentParseException

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);
}
Also used : TagParser(net.minecraft.nbt.TagParser) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 7 with ArgumentParseException

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()));
    }
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) Optional(java.util.Optional) Vec3(net.minecraft.world.phys.Vec3) StringReader(com.mojang.brigadier.StringReader) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) ResourceKey(org.spongepowered.api.ResourceKey) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 8 with ArgumentParseException

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!"));
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) LocatableBlock(org.spongepowered.api.world.LocatableBlock) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) CommandCause(org.spongepowered.api.command.CommandCause) Component(net.kyori.adventure.text.Component) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) ResourceKey(org.spongepowered.api.ResourceKey) CommandContext(org.spongepowered.api.command.parameter.CommandContext) ResourceKeyedZeroAdvanceValueParameter(org.spongepowered.common.command.brigadier.argument.ResourceKeyedZeroAdvanceValueParameter) Optional(java.util.Optional) RayTrace(org.spongepowered.api.util.blockray.RayTrace) Living(org.spongepowered.api.entity.living.Living) RayTraceResult(org.spongepowered.api.util.blockray.RayTraceResult) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Living(org.spongepowered.api.entity.living.Living) NonNull(org.checkerframework.checker.nullness.qual.NonNull) RayTraceResult(org.spongepowered.api.util.blockray.RayTraceResult) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 9 with ArgumentParseException

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());
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ArrayList(java.util.ArrayList) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) ArrayList(java.util.ArrayList) List(java.util.List) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) CommandRuntimeException(net.minecraft.commands.CommandRuntimeException)

Aggregations

ArgumentParseException (org.spongepowered.api.command.exception.ArgumentParseException)9 ArgumentReader (org.spongepowered.api.command.parameter.ArgumentReader)7 Optional (java.util.Optional)6 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 ResourceKey (org.spongepowered.api.ResourceKey)5 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)4 StringReader (com.mojang.brigadier.StringReader)3 List (java.util.List)3 Component (net.kyori.adventure.text.Component)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 UUID (java.util.UUID)2 Collectors (java.util.stream.Collectors)2 CommandSourceStack (net.minecraft.commands.CommandSourceStack)2 CommandCause (org.spongepowered.api.command.CommandCause)2 CommandException (org.spongepowered.api.command.exception.CommandException)2 CommandContext (org.spongepowered.api.command.parameter.CommandContext)2 Entity (org.spongepowered.api.entity.Entity)2 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)2