Java Code Examples for com.mojang.brigadier.StringReader

The following examples show how to use com.mojang.brigadier.StringReader. These examples are extracted from open source projects. 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 Project: multiconnect   Source File: AchievementArgumentType.java    License: 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 Project: multiconnect   Source File: EntityArgumentType_1_12_2.java    License: 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 3
Source Project: fabric-carpet   Source File: ShapeDispatcher.java    License: 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 4
Source Project: fabric-carpet   Source File: NBTSerializableValue.java    License: 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 5
Source Project: fabric-carpet   Source File: NBTSerializableValue.java    License: 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 6
Source Project: MiningGadgets   Source File: LaserParticleData.java    License: 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 7
Source Project: Chimera   Source File: VectorType.java    License: 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 8
Source Project: brigadier   Source File: CommandNode.java    License: 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 Project: multiconnect   Source File: TPArgumentType.java    License: MIT License 5 votes vote down vote up
private static boolean isCoordinateArg(StringReader reader) {
    if (reader.peek() == '~') {
        reader.skip();
        if (reader.peek() == ' ')
            return true;
    }
    try {
        reader.readDouble();
    } catch (CommandSyntaxException e) {
        return false;
    }
    return reader.peek() == ' ';
}
 
Example 10
Source Project: multiconnect   Source File: ParticleArgumentType_1_12_2.java    License: 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 11
Source Project: multiconnect   Source File: UnionArgumentType.java    License: 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 12
Source Project: multiconnect   Source File: ItemArgumentType_1_12_2.java    License: 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 13
Source Project: multiconnect   Source File: EntityArgumentType_1_12_2.java    License: 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 14
Source Project: brigadier   Source File: FloatArgumentTypeTest.java    License: MIT License 5 votes vote down vote up
@Test
public void parse_tooSmall() throws Exception {
    final StringReader reader = new StringReader("-5");
    try {
        floatArg(0, 100).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.floatTooLow()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example 15
Source Project: multiconnect   Source File: CommandArgumentType.java    License: 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 16
Source Project: multiconnect   Source File: CommandArgumentType.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    CommandDispatcher<S> dispatcher = ((CommandDispatcher<S>) this.dispatcher);
    StringReader reader = new StringReader(builder.getInput());
    reader.setCursor(builder.getStart());
    ParseResults<S> results = dispatcher.parse(reader, context.getSource());
    return dispatcher.getCompletionSuggestions(results);
}
 
Example 17
Source Project: brigadier   Source File: DoubleArgumentTypeTest.java    License: MIT License 5 votes vote down vote up
@Test
public void parse_tooSmall() throws Exception {
    final StringReader reader = new StringReader("-5");
    try {
        doubleArg(0, 100).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.doubleTooLow()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example 18
Source Project: multiconnect   Source File: XPArgumentType.java    License: 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 19
Source Project: fabric-carpet   Source File: ParticleDisplay.java    License: MIT License 5 votes vote down vote up
private static ParticleEffect parseParticle(String name)
{
    try
    {
        return ParticleArgumentType.readParameters(new StringReader(name));
    }
    catch (CommandSyntaxException e)
    {
        throw new RuntimeException("No such particle: "+name);
    }
}
 
Example 20
Source Project: fabric-carpet   Source File: NBTSerializableValue.java    License: MIT License 5 votes vote down vote up
public NBTSerializableValue(String nbtString)
{
    nbtSupplier = () ->
    {
        try
        {
            return (new StringNbtReader(new StringReader(nbtString))).parseTag();
        }
        catch (CommandSyntaxException e)
        {
            throw new InternalExpressionException("Incorrect NBT data: "+nbtString);
        }
    };
    owned = true;
}
 
Example 21
Source Project: fabric-carpet   Source File: NBTSerializableValue.java    License: MIT License 5 votes vote down vote up
public static NBTSerializableValue parseString(String nbtString)
{
    Tag tag;
    try
    {
        tag = (new StringNbtReader(new StringReader(nbtString))).parseTag();
    }
    catch (CommandSyntaxException e)
    {
       throw new InternalExpressionException("Incorrect NBT tag: nbtString");
    }
    NBTSerializableValue value = new NBTSerializableValue(tag);
    value.nbtString = nbtString;
    return value;
}
 
Example 22
Source Project: brigadier   Source File: IntegerArgumentTypeTest.java    License: MIT License 5 votes vote down vote up
@Test
public void parse_tooSmall() throws Exception {
    final StringReader reader = new StringReader("-5");
    try {
        integer(0, 100).parse(reader);
        fail();
    } catch (final CommandSyntaxException ex) {
        assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooLow()));
        assertThat(ex.getCursor(), is(0));
    }
}
 
Example 23
Source Project: MiningGadgets   Source File: PlayerParticleData.java    License: 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 24
Source Project: Chimera   Source File: Readers.java    License: 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 25
Source Project: Chimera   Source File: Readers.java    License: 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 26
Source Project: Chimera   Source File: Readers.java    License: 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 27
Source Project: Chimera   Source File: DispatcherCommandTest.java    License: MIT License 5 votes vote down vote up
@Test
void execute() throws CommandSyntaxException {
    doReturn(true).when(command).testPermission(sender);
    var captor = ArgumentCaptor.forClass(StringReader.class);
    
    assertTrue(command.execute(sender, "command", "a", "b"));
    verify(dispatcher).execute(captor.capture(), eq(sender));
    
    var reader = captor.getValue();
    
    assertEquals("/command a b", reader.getString());
    assertEquals("command a b", reader.getRemaining());
}
 
Example 28
Source Project: Chimera   Source File: DispatcherCommandTest.java    License: MIT License 5 votes vote down vote up
@Test
void execute_CommandSyntaxException() throws CommandSyntaxException {
    doReturn(true).when(command).testPermission(sender);
    doThrow(new CommandSyntaxException(null, new LiteralMessage("message"), "abc", 2)).when(dispatcher).execute(any(StringReader.class), any(CommandSender.class));
    
    assertTrue(command.execute(sender, "command", "a", "b"));
    verify(listener, times(2)).sendFailureMessage(any());
}
 
Example 29
Source Project: Chimera   Source File: DispatcherCommandTest.java    License: MIT License 5 votes vote down vote up
@Test
void execute_Exception() throws CommandSyntaxException {
    doReturn(true).when(command).testPermission(sender);
    doThrow(RuntimeException.class).when(dispatcher).execute(any(StringReader.class), any(CommandSender.class));
    
    assertTrue(command.execute(sender, "command", "a", "b"));
    verify(listener, times(1)).sendFailureMessage(any());
}
 
Example 30
Source Project: Chimera   Source File: VectorTypeTest.java    License: MIT License 5 votes vote down vote up
@ParameterizedTest
@CsvSource({"'   -1.4  3.6', -1.4, 3.6", "' -1.4  3.6   2.7', -1.4, 3.6"})
void parse_2D_vector(String line, double x, double z) throws CommandSyntaxException {
    var vector = VectorType.FLAT.parse(new StringReader(line));
    
    assertEquals(x, vector.getX(), 0);
    assertEquals(0, vector.getY(), 0);
    assertEquals(z, vector.getZ(), 0);
}