Java Code Examples for com.mojang.brigadier.StringReader#getCursor()

The following examples show how to use com.mojang.brigadier.StringReader#getCursor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AchievementArgumentType.java    From multiconnect with MIT License 6 votes vote down vote up
@Override
public Advancement parse(StringReader reader) throws CommandSyntaxException {
    int start = reader.getCursor();

    String achievementName = reader.readUnquotedString();
    if (!achievementName.startsWith("achievement.")) {
        reader.setCursor(start);
        throw NO_SUCH_ACHIEVEMENT_EXCEPTION.createWithContext(reader, achievementName);
    }

    Advancement achievement = Achievements_1_11_2.ACHIEVEMENTS.get(achievementName.substring("achievement.".length()));
    if (achievement == null) {
        reader.setCursor(start);
        throw NO_SUCH_ACHIEVEMENT_EXCEPTION.createWithContext(reader, achievementName);
    }

    return achievement;
}
 
Example 2
Source File: CommandNode.java    From brigadier with MIT License 6 votes vote down vote up
public Collection<? extends CommandNode<S>> getRelevantNodes(final StringReader input) {
    if (literals.size() > 0) {
        final int cursor = input.getCursor();
        while (input.canRead() && input.peek() != ' ') {
            input.skip();
        }
        final String text = input.getString().substring(cursor, input.getCursor());
        input.setCursor(cursor);
        final LiteralCommandNode<S> literal = literals.get(text);
        if (literal != null) {
            return Collections.singleton(literal);
        } else {
            return arguments.values();
        }
    } else {
        return arguments.values();
    }
}
 
Example 3
Source File: Readers.java    From Chimera with MIT License 5 votes vote down vote up
public static String until(StringReader reader, Predicate<Character> end) {
    var start = reader.getCursor();
    while (reader.canRead() && !end.test(reader.peek())) {
        reader.skip();
    }
    
    return reader.getString().substring(start, reader.getCursor());
}
 
Example 4
Source File: FloatArgumentType.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public Float parse(final StringReader reader) throws CommandSyntaxException {
    final int start = reader.getCursor();
    final float result = reader.readFloat();
    if (result < minimum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooLow().createWithContext(reader, result, minimum);
    }
    if (result > maximum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooHigh().createWithContext(reader, result, maximum);
    }
    return result;
}
 
Example 5
Source File: DoubleArgumentType.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public Double parse(final StringReader reader) throws CommandSyntaxException {
    final int start = reader.getCursor();
    final double result = reader.readDouble();
    if (result < minimum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooLow().createWithContext(reader, result, minimum);
    }
    if (result > maximum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooHigh().createWithContext(reader, result, maximum);
    }
    return result;
}
 
Example 6
Source File: LongArgumentType.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public Long parse(final StringReader reader) throws CommandSyntaxException {
    final int start = reader.getCursor();
    final long result = reader.readLong();
    if (result < minimum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.longTooLow().createWithContext(reader, result, minimum);
    }
    if (result > maximum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.longTooHigh().createWithContext(reader, result, maximum);
    }
    return result;
}
 
Example 7
Source File: IntegerArgumentType.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public Integer parse(final StringReader reader) throws CommandSyntaxException {
    final int start = reader.getCursor();
    final int result = reader.readInt();
    if (result < minimum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooLow().createWithContext(reader, result, minimum);
    }
    if (result > maximum) {
        reader.setCursor(start);
        throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh().createWithContext(reader, result, maximum);
    }
    return result;
}
 
Example 8
Source File: ArgumentCommandNode.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException {
    final int start = reader.getCursor();
    final T result = type.parse(reader);
    final ParsedArgument<S, T> parsed = new ParsedArgument<>(start, reader.getCursor(), result);

    contextBuilder.withArgument(name, parsed);
    contextBuilder.withNode(this, parsed.getRange());
}
 
Example 9
Source File: LiteralCommandNode.java    From brigadier with MIT License 5 votes vote down vote up
private int parse(final StringReader reader) {
    final int start = reader.getCursor();
    if (reader.canRead(literal.length())) {
        final int end = start + literal.length();
        if (reader.getString().substring(start, end).equals(literal)) {
            reader.setCursor(end);
            if (!reader.canRead() || reader.peek() == ' ') {
                return end;
            } else {
                reader.setCursor(start);
            }
        }
    }
    return -1;
}
 
Example 10
Source File: LiteralCommandNode.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException {
    final int start = reader.getCursor();
    final int end = parse(reader);
    if (end > -1) {
        contextBuilder.withNode(this, StringRange.between(start, end));
        return;
    }

    throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect().createWithContext(reader, literal);
}
 
Example 11
Source File: Readers.java    From Chimera with MIT License 5 votes vote down vote up
public static String until(StringReader reader, char... delimiters) {
    var start = reader.getCursor();
    while (reader.canRead() && !contains(delimiters, reader.peek())) {
        reader.skip();
    }
    
    return reader.getString().substring(start, reader.getCursor());
}
 
Example 12
Source File: Readers.java    From Chimera with MIT License 5 votes vote down vote up
public static String until(StringReader reader, char delimiter) {
    var start = reader.getCursor();
    while (reader.canRead() && reader.peek() != delimiter) {
        reader.skip();
    }
    
    return reader.getString().substring(start, reader.getCursor());
}
 
Example 13
Source File: ItemArgumentType_1_12_2.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public Item parse(StringReader reader) throws CommandSyntaxException {
    int start = reader.getCursor();
    Identifier id = Identifier.fromCommandInput(reader);
    if (!Registry.ITEM.containsId(id)) {
        reader.setCursor(start);
        throw ItemStringReader.ID_INVALID_EXCEPTION.createWithContext(reader, id);
    }
    Item item = Registry.ITEM.get(id);
    if (!isValidItem(item)) {
        reader.setCursor(start);
        throw ItemStringReader.ID_INVALID_EXCEPTION.createWithContext(reader, id);
    }
    return item;
}
 
Example 14
Source File: UnionArgumentType.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public Either<L, R> parse(StringReader reader) throws CommandSyntaxException {
    int start = reader.getCursor();
    try {
        return Either.left(left.parse(reader));
    } catch (CommandSyntaxException leftError) {
        reader.setCursor(start);
        try {
            return Either.right(right.parse(reader));
        } catch (CommandSyntaxException rightError) {
            reader.setCursor(start);
            throw leftError;
        }
    }
}
 
Example 15
Source File: ParticleArgumentType_1_12_2.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public ParticleType<?> parse(StringReader reader) throws CommandSyntaxException {
    int start = reader.getCursor();
    Identifier id = Identifier.fromCommandInput(reader);
    if (!Registry.PARTICLE_TYPE.containsId(id)) {
        reader.setCursor(start);
        throw ParticleArgumentType.UNKNOWN_PARTICLE_EXCEPTION.createWithContext(reader, id);
    }
    return Registry.PARTICLE_TYPE.get(id);
}
 
Example 16
Source File: BlockStateArgumentType_1_12_2.java    From multiconnect with MIT License 4 votes vote down vote up
@Override
public Custom_1_12_Argument parse(StringReader reader) throws CommandSyntaxException {
    List<ParsedArgument<?, ?>> result = new ArrayList<>();

    int start = reader.getCursor();
    Identifier id = Identifier.fromCommandInput(reader);
    if (!Registry.BLOCK.containsId(id)) {
        reader.setCursor(start);
        throw BlockArgumentParser.INVALID_BLOCK_ID_EXCEPTION.createWithContext(reader, id);
    }
    Block block = Registry.BLOCK.get(id);
    if (!isValidBlock(block)) {
        reader.setCursor(start);
        throw BlockArgumentParser.INVALID_BLOCK_ID_EXCEPTION.createWithContext(reader, id);
    }

    result.add(new ParsedArgument<>(start, reader.getCursor(), block));
    if (!reader.canRead())
        return new Custom_1_12_Argument(result);

    reader.expect(' ');
    start = reader.getCursor();
    try {
        int meta;
        if (!test && reader.peek() == '*') {
            reader.skip();
            meta = -1;
        } else {
            meta = reader.readInt();
        }
        if (meta >= (test ? -1 : 0) && meta < 16 && (!reader.canRead() || reader.peek() == ' ')) {
            result.add(new ParsedArgument<>(start, reader.getCursor(), meta));
            return new Custom_1_12_Argument(result);
        }
    } catch (CommandSyntaxException ignore) {
    }
    reader.setCursor(start);
    if ("default".equals(reader.readUnquotedString())) {
        result.add(new ParsedArgument<>(start, reader.getCursor(), 0));
        return new Custom_1_12_Argument(result);
    }

    reader.setCursor(start);

    List<String> properties = BlockStateReverseFlattening.OLD_PROPERTIES.getOrDefault(id, Collections.emptyList());
    Set<String> alreadySeen = new HashSet<>();
    while (reader.canRead() && reader.peek() != ' ') {
        int propStart = reader.getCursor();
        String property = reader.readUnquotedString();
        if (alreadySeen.contains(property)) {
            reader.setCursor(propStart);
            throw BlockArgumentParser.DUPLICATE_PROPERTY_EXCEPTION.createWithContext(reader, id, property);
        }
        if (!properties.contains(property)) {
            reader.setCursor(propStart);
            throw BlockArgumentParser.UNKNOWN_PROPERTY_EXCEPTION.createWithContext(reader, id, property);
        }
        alreadySeen.add(property);
        reader.expect('=');
        int valueStart = reader.getCursor();
        String value = reader.readUnquotedString();
        if (!BlockStateReverseFlattening.OLD_PROPERTY_VALUES.get(Pair.of(id, property)).contains(value)) {
            reader.setCursor(valueStart);
            throw BlockArgumentParser.INVALID_PROPERTY_EXCEPTION.createWithContext(reader, id, property, value);
        }
        if (reader.canRead() && reader.peek() != ' ')
            reader.expect(',');
    }

    result.add(new ParsedArgument<>(start, reader.getCursor(), null));
    return new Custom_1_12_Argument(result);
}
 
Example 17
Source File: TPArgumentType.java    From multiconnect with MIT License 4 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    List<CompletableFuture<Suggestions>> suggestions = new ArrayList<>();

    String[] args = builder.getRemaining().split(" ", -1);
    if (args.length == 1) {
        suggestions.add(blockPos.listSuggestions(context, builder.restart()));
        suggestions.add(victim.listSuggestions(context, builder.restart()));
        suggestions.add(target.listSuggestions(context, builder.restart()));
    } else {
        StringReader reader = new StringReader(builder.getInput());
        reader.setCursor(builder.getStart());

        boolean victimPresent = !isCoordinateArg(reader);
        if (victimPresent) {
            reader.setCursor(builder.getStart());
            try {
                victim.parse(reader);
            } catch (CommandSyntaxException e) {
                return builder.buildFuture();
            }
            if (reader.peek() != ' ')
                return builder.buildFuture();
        }
        reader.skip();
        if (args.length == 2) {
            if (victimPresent) {
                suggestions.add(blockPos.listSuggestions(context, builder.createOffset(reader.getCursor())));
                suggestions.add(target.listSuggestions(context, builder.createOffset(reader.getCursor())));
            } else {
                suggestions.add(blockPos.listSuggestions(context, builder.restart()));
            }
        } else {
            int argStart = victimPresent ? reader.getCursor() : builder.getStart();
            if (!isCoordinateArg(reader))
                return builder.buildFuture();
            if (args.length <= (victimPresent ? 4 : 3)) {
                suggestions.add(blockPos.listSuggestions(context, builder.createOffset(argStart)));
            } else {
                if (victimPresent) {
                    reader.skip();
                    isCoordinateArg(reader);
                }
                reader.skip();
                isCoordinateArg(reader);
                reader.skip();
                suggestions.add(CommandSource.suggestMatching(new String[] {"~", "~ ~"}, builder.createOffset(reader.getCursor())));
            }
        }
    }

    return CompletableFuture.allOf(suggestions.toArray(new CompletableFuture[0]))
            .thenApply(v -> Suggestions.merge(builder.getInput(), suggestions.stream().map(CompletableFuture::join).collect(Collectors.toList())));
}