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

The following examples show how to use com.mojang.brigadier.StringReader#canRead() . 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: EntityArgumentType_1_12_2.java    From multiconnect with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    if (!(context.getSource() instanceof CommandSource))
        return builder.buildFuture();

    StringReader reader = new StringReader(builder.getInput());
    reader.setCursor(builder.getStart());

    CompletableFuture<Suggestions> playerCompletions;
    if ((reader.canRead() && reader.peek() == '@') || !suggestPlayerNames) {
        playerCompletions = Suggestions.empty();
    } else {
        playerCompletions = ((CommandSource) context.getSource()).getCompletions((CommandContext<CommandSource>) context, builder.restart());
    }

    EntitySelectorParser parser = new EntitySelectorParser(reader, singleTarget, playersOnly);
    try {
        parser.parse();
    } catch (CommandSyntaxException ignore) {
    }
    CompletableFuture<Suggestions> selectorCompletions = parser.suggestor.apply(builder.restart());

    return CompletableFuture.allOf(playerCompletions, selectorCompletions)
            .thenCompose(v -> UnionArgumentType.mergeSuggestions(playerCompletions.join(), selectorCompletions.join()));
}
 
Example 2
Source File: LaserParticleData.java    From MiningGadgets with MIT License 6 votes vote down vote up
@Nonnull
@Override
public LaserParticleData deserialize(@Nonnull ParticleType<LaserParticleData> type, @Nonnull StringReader reader) throws CommandSyntaxException {
    reader.expect(' ');
    BlockState state = (new BlockStateParser(reader, false)).parse(false).getState();
    reader.expect(' ');
    float size = reader.readFloat();
    reader.expect(' ');
    float r = reader.readFloat();
    reader.expect(' ');
    float g = reader.readFloat();
    reader.expect(' ');
    float b = reader.readFloat();
    reader.expect(' ');
    float mam = reader.readFloat();
    boolean depth = true;
    if (reader.canRead()) {
        reader.expect(' ');
        depth = reader.readBoolean();
    }
    return new LaserParticleData(state, size, r, g, b, mam, depth);
}
 
Example 3
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 4
Source File: EntityArgumentType_1_12_2.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public Void parse(StringReader reader) throws CommandSyntaxException {
    if (reader.canRead() && reader.peek() == '@') {
        new EntitySelectorParser(reader, singleTarget, playersOnly).parse();
    } else {
        reader.readUnquotedString();
    }
    return null;
}
 
Example 5
Source File: CommandArgumentType.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public Custom_1_12_Argument parse(StringReader reader) throws CommandSyntaxException {
    ParseResults<?> results = dispatcher.parse(reader, null);
    reader.setCursor(results.getReader().getCursor());
    if (reader.canRead()) {
        if (results.getExceptions().size() == 1) {
            throw results.getExceptions().values().iterator().next();
        } else if (results.getContext().getRange().isEmpty()) {
            throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(reader);
        } else {
            throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader);
        }
    }
    return new Custom_1_12_Argument(new ArrayList<>(results.getContext().getArguments().values()));
}
 
Example 6
Source File: XPArgumentType.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public Integer parse(StringReader reader) throws CommandSyntaxException {
    int amount = reader.readInt();
    if (reader.canRead() && (reader.peek() == 'l' || reader.peek() == 'L'))
        reader.skip();
    return amount;
}
 
Example 7
Source File: PlayerParticleData.java    From MiningGadgets with MIT License 5 votes vote down vote up
@Nonnull
@Override
public PlayerParticleData deserialize(@Nonnull ParticleType<PlayerParticleData> type, @Nonnull StringReader reader) throws CommandSyntaxException {
    reader.expect(' ');
    String partType = reader.readString();
    reader.expect(' ');
    double targetX = reader.readDouble();
    reader.expect(' ');
    double targetY = reader.readDouble();
    reader.expect(' ');
    double targetZ = reader.readDouble();
    reader.expect(' ');
    float size = reader.readFloat();
    reader.expect(' ');
    float r = reader.readFloat();
    reader.expect(' ');
    float g = reader.readFloat();
    reader.expect(' ');
    float b = reader.readFloat();
    reader.expect(' ');
    float mam = reader.readFloat();
    boolean depth = true;
    if (reader.canRead()) {
        reader.expect(' ');
        depth = reader.readBoolean();
    }
    return new PlayerParticleData(partType, targetX, targetY, targetZ, size, r, g, b, mam, depth);
}
 
Example 8
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 9
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 10
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 11
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 12
Source File: ArgumentCommandNode.java    From brigadier with MIT License 5 votes vote down vote up
@Override
public boolean isValidInput(final String input) {
    try {
        final StringReader reader = new StringReader(input);
        type.parse(reader);
        return !reader.canRead() || reader.peek() == ' ';
    } catch (final CommandSyntaxException ignored) {
        return false;
    }
}
 
Example 13
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);
}