Search in sources :

Example 11 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandElementExecutor method complete.

@Override
public List<String> complete(final CommandSource src, CommandArgs args, CommandContext context) {
    List<String> completions = Lists.newArrayList();
    if (this.fallbackElements != null) {
        Object state = args.getState();
        completions.addAll(this.fallbackElements.complete(src, args, context));
        args.setState(state);
    }
    final Optional<String> commandComponent = args.nextIfPresent();
    if (!commandComponent.isPresent()) {
        return ImmutableList.copyOf(filterCommands(src));
    }
    if (args.hasNext()) {
        Optional<CommandMapping> child = this.dispatcher.get(commandComponent.get(), src);
        if (!child.isPresent()) {
            return ImmutableList.of();
        }
        if (child.get().getCallable() instanceof CommandSpec) {
            return ((CommandSpec) child.get().getCallable()).complete(src, args, context);
        }
        args.nextIfPresent();
        final String arguments = args.getRaw().substring(args.getRawPosition());
        while (args.hasNext()) {
            args.nextIfPresent();
        }
        try {
            return child.get().getCallable().getSuggestions(src, arguments, context.<Location<World>>getOne(CommandContext.TARGET_BLOCK_ARG).orElse(null));
        } catch (CommandException e) {
            Text eText = e.getText();
            if (eText != null) {
                src.sendMessage(error(eText));
            }
            return ImmutableList.of();
        }
    }
    completions.addAll(filterCommands(src).stream().filter(new StartsWithPredicate(commandComponent.get())).collect(ImmutableList.toImmutableList()));
    return completions;
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandMapping(org.spongepowered.api.command.CommandMapping) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) Location(org.spongepowered.api.world.Location)

Example 12 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandsTest method testSimpleChildCommandIsThrownOnErrorWhenSelected.

@Test
public void testSimpleChildCommandIsThrownOnErrorWhenSelected() throws CommandException {
    final AtomicBoolean parentExecuted = new AtomicBoolean();
    final AtomicBoolean childExecuted = new AtomicBoolean();
    final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder().arguments(GenericArguments.literal(Text.of("test"), "test")).executor((src, args) -> {
        childExecuted.set(true);
        return CommandResult.builder().successCount(1).build();
    }).build())).arguments(GenericArguments.literal(Text.of("t"), "child")).executor((src, args) -> {
        parentExecuted.set(true);
        return CommandResult.success();
    }).childArgumentParseExceptionFallback(false).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "parent");
    try {
        execute.process(mock(CommandSource.class), "parent child");
    } catch (ArgumentParseException ex) {
    // ignored - we check this with the booleans
    }
    assertFalse(childExecuted.get());
    assertFalse(parentExecuted.get());
}
Also used : SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) ImmutableMap(com.google.common.collect.ImmutableMap) Game(org.spongepowered.api.Game) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Assert.assertFalse(org.junit.Assert.assertFalse) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) TestHooks(org.spongepowered.api.util.test.TestHooks) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) TestPlainTextSerializer(org.spongepowered.api.text.TestPlainTextSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) Test(org.junit.Test)

Example 13 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandsTest method testEmptyChildrenWorks.

@Test
public void testEmptyChildrenWorks() throws CommandException {
    final AtomicBoolean parent = new AtomicBoolean();
    final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.<List<String>, CommandSpec>of()).executor((s, c) -> {
        parent.set(true);
        return CommandResult.success();
    }).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "emptyparent");
    execute.process(mock(CommandSource.class), "emptyparent");
    assertTrue(parent.get());
}
Also used : SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) ImmutableMap(com.google.common.collect.ImmutableMap) Game(org.spongepowered.api.Game) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Assert.assertFalse(org.junit.Assert.assertFalse) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) TestHooks(org.spongepowered.api.util.test.TestHooks) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) TestPlainTextSerializer(org.spongepowered.api.text.TestPlainTextSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) Test(org.junit.Test)

Example 14 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project Skree by Skelril.

the class ZoneMeCommand method aquireSpec.

public static CommandSpec aquireSpec() {
    ZoneService service = Sponge.getServiceManager().provide(ZoneService.class).get();
    Map<String, String> options = service.getManagerNames().stream().collect(Collectors.toMap(a -> a, a -> a));
    return CommandSpec.builder().description(Text.of("Create a zone")).permission("skree.zone.zoneme").arguments(onlyOne(choices(Text.of("zone"), options))).executor(new ZoneMeCommand()).build();
}
Also used : ZoneService(com.skelril.skree.service.ZoneService) CommandResult(org.spongepowered.api.command.CommandResult) ZoneService(com.skelril.skree.service.ZoneService) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) Collectors(java.util.stream.Collectors) GenericArguments.choices(org.spongepowered.api.command.args.GenericArguments.choices) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) GenericArguments.onlyOne(org.spongepowered.api.command.args.GenericArguments.onlyOne) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Map(java.util.Map) ZoneStatus(com.skelril.skree.service.internal.zone.ZoneStatus) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors)

Example 15 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method gameStarted.

@Listener
public void gameStarted(GameStartedServerEvent event) {
    CommandSpec key = CommandSpec.builder().description(Text.of("Get a key for a specified crate.")).arguments(new CrateElement(Text.of("type")), GenericArguments.playerOrSource(Text.of("player")), GenericArguments.optional(GenericArguments.integer(Text.of("quantity")))).permission("huskycrates.key").executor(new Key()).build();
    CommandSpec keyAll = CommandSpec.builder().description(Text.of("Give everyone a specified amount of keys for a crate.")).arguments(new CrateElement(Text.of("type")), GenericArguments.optional(GenericArguments.integer(Text.of("quantity")))).permission("huskycrates.keyAll").executor(new KeyAll()).build();
    CommandSpec wand = CommandSpec.builder().description(Text.of("Give yourself an entity wand for crates.")).arguments(new CrateElement(Text.of("type"))).permission("huskycrates.wand").executor(new Wand()).build();
    CommandSpec chest = CommandSpec.builder().description(Text.of("Get the placeable crate item.")).permission("huskycrates.chest").arguments(new CrateElement(Text.of("type")), GenericArguments.playerOrSource(Text.of("player")), GenericArguments.optional(GenericArguments.integer(Text.of("quantity")))).executor(new Chest()).build();
    CommandSpec crateSpec = CommandSpec.builder().description(Text.of("Main crates command")).child(key, "key").child(chest, "chest").child(keyAll, "keyAll").child(wand, "wand").arguments(GenericArguments.optional(GenericArguments.remainingRawJoinedStrings(Text.of("")))).executor(new Crate(this)).build();
    scheduler = Sponge.getScheduler();
    genericCause = Cause.of(NamedCause.of("PluginContainer", pC));
    Sponge.getCommandManager().register(this, crateSpec, "crate");
    logger.info("Crates has been started.");
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec) VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) PhysicalCrate(pw.codehusky.huskycrates.crate.PhysicalCrate) CrateElement(pw.codehusky.huskycrates.commands.elements.CrateElement) Listener(org.spongepowered.api.event.Listener)

Aggregations

CommandSpec (org.spongepowered.api.command.spec.CommandSpec)28 Text (org.spongepowered.api.text.Text)18 CommandContext (org.spongepowered.api.command.args.CommandContext)14 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)14 List (java.util.List)12 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)12 GenericArguments (org.spongepowered.api.command.args.GenericArguments)12 ImmutableList (com.google.common.collect.ImmutableList)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CommandResult (org.spongepowered.api.command.CommandResult)10 Lists (com.google.common.collect.Lists)9 Sponge (org.spongepowered.api.Sponge)9 CommandException (org.spongepowered.api.command.CommandException)9 Test (org.junit.Test)8 CommandSource (org.spongepowered.api.command.CommandSource)8 Player (org.spongepowered.api.entity.living.player.Player)8 SimpleDispatcher (org.spongepowered.api.command.dispatcher.SimpleDispatcher)7 TextColors (org.spongepowered.api.text.format.TextColors)7 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6