Search in sources :

Example 1 with CommandRuntimeException

use of net.minecraft.commands.CommandRuntimeException 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

ArrayList (java.util.ArrayList)1 List (java.util.List)1 CommandRuntimeException (net.minecraft.commands.CommandRuntimeException)1 TextComponent (net.minecraft.network.chat.TextComponent)1 ArgumentParseException (org.spongepowered.api.command.exception.ArgumentParseException)1 ArgumentReader (org.spongepowered.api.command.parameter.ArgumentReader)1