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;
}
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());
}
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());
}
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();
}
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.");
}
Aggregations