com.mojang.brigadier.StringReader Java Examples

The following examples show how to use com.mojang.brigadier.StringReader. 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: NBTSerializableValue.java    From fabric-carpet with MIT License 6 votes vote down vote up
private static NbtPathArgumentType.NbtPath cachePath(String arg)
{
    NbtPathArgumentType.NbtPath res = pathCache.get(arg);
    if (res != null)
        return res;
    try
    {
        res = NbtPathArgumentType.nbtPath().parse(new StringReader(arg));
    }
    catch (CommandSyntaxException exc)
    {
        throw new InternalExpressionException("Incorrect nbt path: "+arg);
    }
    if (pathCache.size() > 1024)
        pathCache.clear();
    pathCache.put(arg, res);
    return res;
}
 
Example #2
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 6 votes vote down vote up
public static ItemStackArgument parseItem(String itemString, CompoundTag customTag)
{
    try
    {
        ItemStackArgument res = itemCache.get(itemString);
        if (res != null)
            if (customTag == null)
                return res;
            else
                return new ItemStackArgument(res.getItem(), customTag);

        ItemStringReader parser = (new ItemStringReader(new StringReader(itemString), false)).consume();
        res = new ItemStackArgument(parser.getItem(), parser.getTag());
        itemCache.put(itemString, res);
        if (itemCache.size()>64000)
            itemCache.clear();
        if (customTag == null)
            return res;
        else
            return new ItemStackArgument(res.getItem(), customTag);
    }
    catch (CommandSyntaxException e)
    {
        throw new InternalExpressionException("Incorrect item: "+itemString);
    }
}
 
Example #3
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 #4
Source File: VectorType.java    From Chimera with MIT License 6 votes vote down vote up
@Override
public Vector parse(StringReader reader) throws CommandSyntaxException {
    var vector = new Vector();
    
    reader.skipWhitespace();
    vector.setX(reader.readDouble());
    
    if (cubic) {
        reader.skipWhitespace();
        vector.setY(reader.readDouble());
    }
    
    reader.skipWhitespace();
    vector.setZ(reader.readDouble());
    
    return vector;
}
 
Example #5
Source File: ShapeDispatcher.java    From fabric-carpet with MIT License 6 votes vote down vote up
public static ParticleEffect getParticleData(String name)
{
    ParticleEffect particle = particleCache.get(name);
    if (particle != null)
        return particle;
    try
    {
        particle = ParticleArgumentType.readParameters(new StringReader(name));
    }
    catch (CommandSyntaxException e)
    {
        throw new InternalExpressionException("No such particle: "+name);
    }
    particleCache.put(name, particle);
    return particle;
}
 
Example #6
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 #7
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 #8
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 #9
Source File: StringArgumentTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void testParseWord() throws Exception {
    final StringReader reader = mock(StringReader.class);
    when(reader.readUnquotedString()).thenReturn("hello");
    assertThat(word().parse(reader), equalTo("hello"));
    verify(reader).readUnquotedString();
}
 
Example #10
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 #11
Source File: LiteralCommandNodeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void testParseSimilar() throws Exception {
    final StringReader reader = new StringReader("foobar");
    try {
        node.parse(reader, contextBuilder);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example #12
Source File: StringArgumentTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void testParseString() throws Exception {
    final StringReader reader = mock(StringReader.class);
    when(reader.readString()).thenReturn("hello world");
    assertThat(string().parse(reader), equalTo("hello world"));
    verify(reader).readString();
}
 
Example #13
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 #14
Source File: SimpleCommandSyntaxExceptionTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void createWithContext() throws Exception {
    final SimpleCommandExceptionType type = new SimpleCommandExceptionType(new LiteralMessage("error"));
    final StringReader reader = new StringReader("Foo bar");
    reader.setCursor(5);
    final CommandSyntaxException exception = type.createWithContext(reader);
    assertThat(exception.getType(), is(type));
    assertThat(exception.getInput(), is("Foo bar"));
    assertThat(exception.getCursor(), is(5));
}
 
Example #15
Source File: DynamicCommandSyntaxExceptionTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void createWithContext() throws Exception {
    final StringReader reader = new StringReader("Foo bar");
    reader.setCursor(5);
    final CommandSyntaxException exception = type.createWithContext(reader, "World");
    assertThat(exception.getType(), is(type));
    assertThat(exception.getInput(), is("Foo bar"));
    assertThat(exception.getCursor(), is(5));
}
 
Example #16
Source File: ArgumentCommandNodeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void testParse() throws Exception {
    final StringReader reader = new StringReader("123 456");
    node.parse(reader, contextBuilder);

    assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
    assertThat(contextBuilder.getArguments().get("foo").getResult(), is(123));
}
 
Example #17
Source File: FloatArgumentTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void parse_tooBig() throws Exception {
    final StringReader reader = new StringReader("5");
    try {
        floatArg(-100, 0).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooHigh()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example #18
Source File: LiteralCommandNodeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void testParseInvalid() throws Exception {
    final StringReader reader = new StringReader("bar");
    try {
        node.parse(reader, contextBuilder);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example #19
Source File: EnchantmentTypeTest.java    From Chimera with MIT License 5 votes vote down vote up
@Test
void parse() throws CommandSyntaxException {
    Enchantment enchantment = mock(Enchantment.class);
    EnchantmentType.ENCHANTMENTS.put("channeling", enchantment);
            
    assertEquals(enchantment, type.parse(new StringReader("CHANNELING")));
}
 
Example #20
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 #21
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 #22
Source File: LongArgumentTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void parse_tooSmall() throws Exception {
    final StringReader reader = new StringReader("-5");
    try {
        longArg(0, 100).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.longTooLow()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example #23
Source File: HexColorArgument.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
@Override
public HexColor parse(StringReader reader) throws CommandSyntaxException {
    String value = reader.getRemaining().split(" ")[0].toLowerCase();
    if (value.isEmpty()) {
        throw EXPECTED_HEX_COLOR.createWithContext(reader);
    }

    HexColor color = HexColor.from(value);
    if (color != null) {
        reader.setCursor(reader.getCursor() + 7);
        return color;
    }

    throw INVALID_HEX_COLOR.createWithContext(reader, value);
}
 
Example #24
Source File: DoubleArgumentTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void parse_tooBig() throws Exception {
    final StringReader reader = new StringReader("5");
    try {
        doubleArg(-100, 0).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooHigh()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example #25
Source File: LongArgumentTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void parse_tooBig() throws Exception {
    final StringReader reader = new StringReader("5");
    try {
        longArg(-100, 0).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.longTooHigh()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example #26
Source File: IntegerArgumentTypeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void parse_tooBig() throws Exception {
    final StringReader reader = new StringReader("5");
    try {
        integer(-100, 0).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example #27
Source File: PointTypeTest.java    From Chimera with MIT License 5 votes vote down vote up
@ParameterizedTest
@CsvSource("'^1 2 3', 1 ^2 3")
void parse_throws_exception(String line) throws CommandSyntaxException {
    assertEquals(
        "Cannot mix world and local coordinates (everything must either use ^ or not)",
        assertThrows(CommandSyntaxException.class, () -> type.parse(new StringReader(line))).getRawMessage().toString()
    );
}
 
Example #28
Source File: PlayerTypeTest.java    From Chimera with MIT License 5 votes vote down vote up
@Test
void parse_throws_exception() throws CommandSyntaxException {
    assertEquals(
        "Unknown player: invalid",
        assertThrows(CommandSyntaxException.class, () -> type.parse(new StringReader("invalid"))).getRawMessage().toString()
    );
}
 
Example #29
Source File: EnchantmentTypeTest.java    From Chimera with MIT License 5 votes vote down vote up
@Test
void parse_throws_exception() throws CommandSyntaxException {
    assertEquals(
        "Unknown enchantment: invalid",
        assertThrows(CommandSyntaxException.class, () -> type.parse(new StringReader("invalid"))).getRawMessage().toString()
    );
}
 
Example #30
Source File: WorldTypeTest.java    From Chimera with MIT License 5 votes vote down vote up
@Test
void parse_throws_exception() throws CommandSyntaxException {
    assertEquals(
        "Unknown world: \"something\" at position 9: something<--[HERE]", 
        assertThrows(CommandSyntaxException.class, () -> type.parse(new StringReader("something"))).getMessage()
    );
}