Search in sources :

Example 1 with SingleArg

use of org.spongepowered.api.command.args.parsing.SingleArg in project SpongeAPI by SpongePowered.

the class CommandArgs method insertArg.

/**
 * Insert an arg as the next arg to be returned by {@link #next()}.
 *
 * @param value The argument to insert
 */
public void insertArg(String value) {
    int index = this.index < 0 ? 0 : this.args.get(this.index).getEndIdx();
    this.args.add(this.index + 1, new SingleArg(value, index, index));
}
Also used : SingleArg(org.spongepowered.api.command.args.parsing.SingleArg)

Example 2 with SingleArg

use of org.spongepowered.api.command.args.parsing.SingleArg in project SpongeAPI by SpongePowered.

the class GenericArgumentsTest method testGettingSinglePlayer.

@Test
public void testGettingSinglePlayer() throws Exception {
    CommandArgs args = new CommandArgs("test1", Lists.newArrayList(new SingleArg("test1", 0, 5)));
    CommandContext context = new CommandContext();
    CommandSource source = Mockito.mock(CommandSource.class);
    getPlayerElement().parse(source, args, context);
    assertEquals("test1", context.<Player>getOne(Text.of("player")).get().getName());
}
Also used : SingleArg(org.spongepowered.api.command.args.parsing.SingleArg) CommandSource(org.spongepowered.api.command.CommandSource) Test(org.junit.Test)

Example 3 with SingleArg

use of org.spongepowered.api.command.args.parsing.SingleArg in project SpongeAPI by SpongePowered.

the class GenericArgumentsTest method testGettingBothPlayers.

@Test
public void testGettingBothPlayers() throws Exception {
    CommandArgs args = new CommandArgs("test", Lists.newArrayList(new SingleArg("test", 0, 5)));
    CommandContext context = new CommandContext();
    CommandSource source = Mockito.mock(CommandSource.class);
    getPlayerElement().parse(source, args, context);
    Collection<Player> cpl = context.getAll(Text.of("player"));
    assertEquals(2, cpl.size());
    Collection<String> s = cpl.stream().map(User::getName).collect(Collectors.toList());
    assertTrue(s.contains("test1"));
    assertTrue(s.contains("test2"));
}
Also used : SingleArg(org.spongepowered.api.command.args.parsing.SingleArg) Player(org.spongepowered.api.entity.living.player.Player) CommandSource(org.spongepowered.api.command.CommandSource) Test(org.junit.Test)

Example 4 with SingleArg

use of org.spongepowered.api.command.args.parsing.SingleArg in project Nucleus by NucleusPowered.

the class AbstractCommand method getSuggestions.

@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
    List<SingleArg> singleArgs = Lists.newArrayList(tokeniser.tokenize(arguments, false));
    // If we end with a space - then we add another argument.
    if (arguments.isEmpty() || arguments.endsWith(" ")) {
        singleArgs.add(new SingleArg("", arguments.length() - 1, arguments.length() - 1));
    }
    final CommandArgs args = new CommandArgs(arguments, singleArgs);
    final List<String> options = Lists.newArrayList();
    CommandContext context = new CommandContext();
    // We don't care for the value.
    context.putArg(COMPLETION_ARG, true);
    // Subcommand
    Object state = args.getState();
    options.addAll(this.dispatcher.getSuggestions(source, arguments, targetPosition));
    args.setState(state);
    options.addAll(this.argumentParser.complete(source, args, context));
    return options.stream().distinct().collect(Collectors.toList());
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) SingleArg(org.spongepowered.api.command.args.parsing.SingleArg) CommandContext(org.spongepowered.api.command.args.CommandContext)

Aggregations

SingleArg (org.spongepowered.api.command.args.parsing.SingleArg)4 Test (org.junit.Test)2 CommandSource (org.spongepowered.api.command.CommandSource)2 CommandArgs (org.spongepowered.api.command.args.CommandArgs)1 CommandContext (org.spongepowered.api.command.args.CommandContext)1 Player (org.spongepowered.api.entity.living.player.Player)1