Search in sources :

Example 1 with ChildCommandElementExecutor

use of org.spongepowered.api.command.args.ChildCommandElementExecutor in project SpongeCommon by SpongePowered.

the class SpongePaginationService method buildPaginationCommand.

private CommandSpec buildPaginationCommand() {
    final ActivePaginationCommandElement paginationElement = new ActivePaginationCommandElement(t("pagination-id"));
    CommandSpec next = CommandSpec.builder().description(t("Go to the next page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().nextPage();
        return CommandResult.success();
    }).build();
    CommandSpec prev = CommandSpec.builder().description(t("Go to the previous page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().previousPage();
        return CommandResult.success();
    }).build();
    CommandElement pageArgs = integer(t("page"));
    CommandExecutor pageExecutor = (src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().specificPage(args.<Integer>getOne("page").get());
        return CommandResult.success();
    };
    CommandSpec page = CommandSpec.builder().description(t("Go to a specific page")).arguments(pageArgs).executor(pageExecutor).build();
    // Fallback to page arguments
    ChildCommandElementExecutor childDispatcher = new ChildCommandElementExecutor(pageExecutor);
    childDispatcher.register(next, "next", "n");
    childDispatcher.register(prev, "prev", "p", "previous");
    childDispatcher.register(page, "page");
    // https://github.com/SpongePowered/SpongeAPI/issues/1272
    return CommandSpec.builder().arguments(paginationElement, firstParsing(childDispatcher, pageArgs)).executor(childDispatcher).description(t("Helper command for paginations occurring")).build();
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) SpongeImpl(org.spongepowered.common.SpongeImpl) Iterables(com.google.common.collect.Iterables) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cache(com.github.benmanes.caffeine.cache.Cache) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) ConcurrentMap(java.util.concurrent.ConcurrentMap) PaginationList(org.spongepowered.api.service.pagination.PaginationList) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Map(java.util.Map) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) CommandSource(org.spongepowered.api.command.CommandSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) TimeUnit(java.util.concurrent.TimeUnit) GenericArguments.integer(org.spongepowered.api.command.args.GenericArguments.integer) List(java.util.List) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) MapMaker(com.google.common.collect.MapMaker) SpongeCommonTranslationHelper.t(org.spongepowered.common.util.SpongeCommonTranslationHelper.t) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CommandElement(org.spongepowered.api.command.args.CommandElement)

Example 2 with ChildCommandElementExecutor

use of org.spongepowered.api.command.args.ChildCommandElementExecutor in project LanternServer by LanternPowered.

the class LanternPaginationService method buildPaginationCommand.

@SuppressWarnings("ConstantConditions")
private CommandSpec buildPaginationCommand() {
    // TODO Completely redo this once command refactor is out and PR changes to Sponge as well
    final ActivePaginationCommandElement paginationElement = new ActivePaginationCommandElement(t("pagination-id"));
    CommandSpec next = CommandSpec.builder().description(t("Go to the next page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().nextPage();
        return CommandResult.success();
    }).build();
    CommandSpec prev = CommandSpec.builder().description(t("Go to the previous page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().previousPage();
        return CommandResult.success();
    }).build();
    CommandElement pageArgs = integer(t("page"));
    CommandExecutor pageExecutor = (src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().specificPage(args.<Integer>getOne("page").get());
        return CommandResult.success();
    };
    CommandSpec page = CommandSpec.builder().description(t("Go to a specific page")).arguments(pageArgs).executor(pageExecutor).build();
    // Fallback to page arguments
    ChildCommandElementExecutor childDispatcher = new ChildCommandElementExecutor(pageExecutor);
    childDispatcher.register(next, "next", "n");
    childDispatcher.register(prev, "prev", "p", "previous");
    childDispatcher.register(page, "page");
    // https://github.com/SpongePowered/SpongeAPI/issues/1272
    return CommandSpec.builder().arguments(paginationElement, firstParsing(childDispatcher, pageArgs)).executor(childDispatcher).description(t("Helper command for paginations occurring")).build();
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) Iterables(com.google.common.collect.Iterables) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) ConcurrentMap(java.util.concurrent.ConcurrentMap) PaginationList(org.spongepowered.api.service.pagination.PaginationList) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Map(java.util.Map) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) UUID(java.util.UUID) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) GenericArguments.integer(org.spongepowered.api.command.args.GenericArguments.integer) List(java.util.List) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) Lantern(org.lanternpowered.server.game.Lantern) Optional(java.util.Optional) MapMaker(com.google.common.collect.MapMaker) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CommandElement(org.spongepowered.api.command.args.CommandElement)

Example 3 with ChildCommandElementExecutor

use of org.spongepowered.api.command.args.ChildCommandElementExecutor in project SpongeCommon by SpongePowered.

the class SpongeCommandFactory method createSpongeCommand.

/**
 * Create a new instance of the Sponge command structure.
 *
 * @return The newly created command
 */
public static CommandSpec createSpongeCommand() {
    final ChildCommandElementExecutor flagChildren = new ChildCommandElementExecutor(null);
    final ChildCommandElementExecutor nonFlagChildren = new ChildCommandElementExecutor(flagChildren);
    nonFlagChildren.register(createSpongeVersionCommand(), "version");
    nonFlagChildren.register(createSpongeBlockInfoCommand(), "blockInfo");
    nonFlagChildren.register(createSpongeEntityInfoCommand(), "entityInfo");
    nonFlagChildren.register(createSpongeAuditCommand(), "audit");
    nonFlagChildren.register(createSpongeHeapCommand(), "heap");
    nonFlagChildren.register(createSpongePluginsCommand(), "plugins");
    nonFlagChildren.register(createSpongeTimingsCommand(), "timings");
    nonFlagChildren.register(createSpongeWhichCommand(), "which");
    flagChildren.register(createSpongeChunksCommand(), "chunks");
    flagChildren.register(createSpongeConfigCommand(), "config");
    // TODO: Should these two be subcommands of config, and what is now config be set?
    flagChildren.register(createSpongeReloadCommand(), "reload");
    flagChildren.register(createSpongeSaveCommand(), "save");
    flagChildren.register(createSpongeTpsCommand(), "tps");
    SpongeImplHooks.registerAdditionalCommands(flagChildren, nonFlagChildren);
    return CommandSpec.builder().description(Text.of("General Sponge command")).extendedDescription(// TODO: Automatically generate from child executors (wait for help system on this)
    Text.of(// TODO: Automatically generate from child executors (wait for help system on this)
    "commands:\n", INDENT, title("chunks"), LONG_INDENT, "Prints chunk data for a specific dimension or world(s)\n", INDENT, title("conf"), LONG_INDENT, "Configure sponge settings\n", INDENT, title("heap"), LONG_INDENT, "Dump live JVM heap\n", INDENT, title("reload"), LONG_INDENT, "Reloads a global, dimension, or world config\n", INDENT, title("save"), LONG_INDENT, "Saves a global, dimension, or world config\n", INDENT, title("version"), LONG_INDENT, "Prints current Sponge version\n", INDENT, title("audit"), LONG_INDENT, "Audit mixin classes for implementation\n", INDENT, title("plugins"), LONG_INDENT, "List currently installed plugins\n", INDENT, title("which"), LONG_INDENT, "List plugins that own a specific command\n", INDENT, title("tps"), LONG_INDENT, "Provides TPS (ticks per second) data for loaded worlds\n", SpongeImplHooks.getAdditionalCommandDescriptions())).arguments(firstParsing(nonFlagChildren, flags().flag("-global", "g").valueFlag(world(Text.of("world")), "-world", "w").valueFlag(dimension(Text.of("dimension")), "-dimension", "d").buildWith(flagChildren))).executor(nonFlagChildren).build();
}
Also used : ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor)

Example 4 with ChildCommandElementExecutor

use of org.spongepowered.api.command.args.ChildCommandElementExecutor in project SpongeCommon by SpongePowered.

the class ScaledHealthTest method getHealthCommand.

private static CommandCallable getHealthCommand() {
    final ChildCommandElementExecutor flagChildren = new ChildCommandElementExecutor(null);
    final ChildCommandElementExecutor nonFlagChildren = new ChildCommandElementExecutor(flagChildren);
    nonFlagChildren.register(getShowHealth(), "show");
    nonFlagChildren.register(getSetHealthScale(), "setScale");
    nonFlagChildren.register(getSetHealth(), "setHealth");
    nonFlagChildren.register(getSetMaxHealth(), "setMax");
    return CommandSpec.builder().description(Text.of("ScaledHealth command")).extendedDescription(Text.of("commands:\n", "set a trail to you as a player")).arguments(firstParsing(nonFlagChildren)).executor(nonFlagChildren).build();
}
Also used : ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor)

Aggregations

ChildCommandElementExecutor (org.spongepowered.api.command.args.ChildCommandElementExecutor)4 ImmutableList (com.google.common.collect.ImmutableList)2 Iterables (com.google.common.collect.Iterables)2 MapMaker (com.google.common.collect.MapMaker)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Nullable (javax.annotation.Nullable)2 CommandResult (org.spongepowered.api.command.CommandResult)2 CommandSource (org.spongepowered.api.command.CommandSource)2 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)2 CommandArgs (org.spongepowered.api.command.args.CommandArgs)2 CommandContext (org.spongepowered.api.command.args.CommandContext)2 CommandElement (org.spongepowered.api.command.args.CommandElement)2 GenericArguments.firstParsing (org.spongepowered.api.command.args.GenericArguments.firstParsing)2