Search in sources :

Example 1 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec 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 CommandSpec

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

the class CooldownTrackerTest method onInit.

@Listener
public void onInit(GameInitializationEvent event) {
    final CommandSpec test = CommandSpec.builder().executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
        }
        final Player player = (Player) src;
        final CooldownTracker cooldownTracker = player.getCooldownTracker();
        final ItemType itemType = player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty()).getType();
        if (!cooldownTracker.hasCooldown(itemType)) {
            player.sendMessage(Text.of(TextColors.GRAY, "The item type in your hand is not on cooldown!"));
        } else {
            player.sendMessage(Text.of(TextColors.GRAY, "The cooldown remaining for the item type in your hand is ", TextColors.GOLD, cooldownTracker.getCooldown(itemType).orElse(0), TextColors.GRAY, " tick(s)."));
            player.sendMessage(Text.of(TextColors.GRAY, "This item type has ", TextColors.GOLD, new DecimalFormat("#.00").format(cooldownTracker.getFractionRemaining(itemType).orElse(0.0) * 100) + "%", TextColors.GRAY, " of its cooldown remaining."));
        }
        return CommandResult.success();
    }).build();
    final CommandSpec set = CommandSpec.builder().executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
        }
        final Player player = (Player) src;
        final int cooldown = args.<Integer>getOne("cooldown").orElse(10);
        player.getCooldownTracker().setCooldown(player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty()).getType(), cooldown);
        player.sendMessage(Text.of(TextColors.GRAY, "You have given the item type in your hand a cooldown of ", TextColors.GOLD, cooldown, TextColors.GRAY, " tick(s)."));
        return CommandResult.success();
    }).arguments(GenericArguments.integer(Text.of("cooldown"))).build();
    final CommandSpec enable = CommandSpec.builder().executor(((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to execute this command!"));
        }
        final Player player = (Player) src;
        if (!this.enabled.remove(player.getUniqueId())) {
            this.enabled.add(player.getUniqueId());
            src.sendMessage(Text.of(TextColors.GOLD, "You have enabled the cooldown listeners!"));
        } else {
            src.sendMessage(Text.of(TextColors.GOLD, "You have disabled the cooldown listeners!"));
        }
        return CommandResult.success();
    })).build();
    Sponge.getCommandManager().register(this, CommandSpec.builder().executor(((src, args) -> {
        src.sendMessage(Text.of(TextColors.GOLD, "Use cooldown set|test|enable"));
        return CommandResult.success();
    })).child(test, "test").child(set, "set").child(enable, "enable", "disable").build(), "cooldowns");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) DecimalFormat(java.text.DecimalFormat) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) UUID(java.util.UUID) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) HashSet(java.util.HashSet) CooldownTracker(org.spongepowered.api.entity.living.player.CooldownTracker) CooldownEvent(org.spongepowered.api.event.entity.living.humanoid.player.CooldownEvent) Plugin(org.spongepowered.api.plugin.Plugin) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) Order(org.spongepowered.api.event.Order) HandTypes(org.spongepowered.api.data.type.HandTypes) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) ItemType(org.spongepowered.api.item.ItemType) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ItemType(org.spongepowered.api.item.ItemType) DecimalFormat(java.text.DecimalFormat) CommandException(org.spongepowered.api.command.CommandException) CooldownTracker(org.spongepowered.api.entity.living.player.CooldownTracker) Listener(org.spongepowered.api.event.Listener)

Example 3 with CommandSpec

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

the class InventoryQueryTest method onInitialization.

@Listener
public void onInitialization(GameInitializationEvent e) {
    CommandSpec inventoryType = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory hotbar = inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class));
        src.sendMessage(Text.of("You have ", hotbar.totalItems(), " items in your hotbar."));
        return CommandResult.success();
    }).build();
    CommandSpec itemType = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory sticks = inventory.query(QueryOperationTypes.ITEM_TYPE.of(ItemTypes.STICK));
        src.sendMessage(Text.of("You have ", sticks.totalItems(), " sticks in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackGeneral = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        ItemStack lapis = ItemStack.of(ItemTypes.DYE, 4);
        lapis.offer(Keys.DYE_COLOR, DyeColors.BLUE);
        Inventory lapisItems = inventory.query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(lapis));
        src.sendMessage(Text.of("You have ", lapisItems.totalItems(), " lapis lazuli in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackSpecific = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        ItemStack lapis = ItemStack.of(ItemTypes.DYE, 4);
        lapis.offer(Keys.DYE_COLOR, DyeColors.BLUE);
        Inventory lapisItems = inventory.query(QueryOperationTypes.ITEM_STACK_EXACT.of(lapis));
        src.sendMessage(Text.of("You have ", lapisItems.size(), " stacks of 4 lapis lazuli in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackCustom = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory evenCountStacks = inventory.query(QueryOperationTypes.ITEM_STACK_CUSTOM.of(x -> x.getQuantity() > 0 && x.getQuantity() % 2 == 0));
        src.sendMessage(Text.of("You have ", evenCountStacks.size(), " stacks with an even number of items in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec inventoryProperty = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory slots = ((PlayerInventory) inventory).getHotbar().query(QueryOperationTypes.INVENTORY_PROPERTY.of(new SlotIndex(3, Property.Operator.LESS)));
        src.sendMessage(Text.of("You have ", slots.totalItems(), " items in the first 3 slots of your hotbar."));
        return CommandResult.success();
    }).build();
    CommandSpec inventoryTranslation = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory slots = ((PlayerInventory) inventory).getHotbar().query(QueryOperationTypes.INVENTORY_TRANSLATION.of(Sponge.getRegistry().getTranslationById("slot.name").get()));
        src.sendMessage(Text.of("You have ", slots.totalItems(), " items in your hotbar."));
        return CommandResult.success();
    }).build();
    Sponge.getCommandManager().register(this, CommandSpec.builder().child(inventoryType, "inventorytype").child(itemType, "itemtype").child(itemStackGeneral, "itemstackgeneral").child(itemStackSpecific, "itemstackspecific").child(itemStackCustom, "itemstackcustom").child(inventoryProperty, "inventoryproperty").child(inventoryTranslation, "inventorytranslation").build(), "invquery");
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) CommandResult(org.spongepowered.api.command.CommandResult) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) Keys(org.spongepowered.api.data.key.Keys) CommandSource(org.spongepowered.api.command.CommandSource) Hotbar(org.spongepowered.api.item.inventory.entity.Hotbar) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) ItemTypes(org.spongepowered.api.item.ItemTypes) Sponge(org.spongepowered.api.Sponge) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) Plugin(org.spongepowered.api.plugin.Plugin) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DyeColors(org.spongepowered.api.data.type.DyeColors) QueryOperationTypes(org.spongepowered.api.item.inventory.query.QueryOperationTypes) Text(org.spongepowered.api.text.Text) Property(org.spongepowered.api.data.Property) Player(org.spongepowered.api.entity.living.player.Player) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) Listener(org.spongepowered.api.event.Listener) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) Listener(org.spongepowered.api.event.Listener)

Example 4 with CommandSpec

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

the class SpongeCommandFactory method createSpongePluginsCommand.

private static CommandSpec createSpongePluginsCommand() {
    return CommandSpec.builder().description(Text.of("List currently installed plugins")).permission("sponge.command.plugins").arguments(optionalWeak(literal(Text.of("reload"), "reload")), optional(plugin(Text.of("plugin")))).executor((src, args) -> {
        if (args.hasAny("reload") && src.hasPermission("sponge.command.plugins.reload")) {
            src.sendMessage(Text.of("Sending reload event to all plugins. Please wait."));
            Sponge.getCauseStackManager().pushCause(src);
            SpongeImpl.postEvent(SpongeEventFactory.createGameReloadEvent(Sponge.getCauseStackManager().getCurrentCause()));
            Sponge.getCauseStackManager().popCause();
            src.sendMessage(Text.of("Reload complete!"));
        } else if (args.hasAny("plugin")) {
            sendContainerMeta(src, args, "plugin");
        } else {
            final Collection<PluginContainer> containers = SpongeImpl.getGame().getPluginManager().getPlugins();
            final List<PluginContainer> sortedContainers = new ArrayList<>();
            // Add static listings first
            CONTAINER_LIST_STATICS.forEach(containerId -> containers.stream().filter(container -> container.getId().equalsIgnoreCase(containerId)).findFirst().ifPresent(sortedContainers::add));
            containers.stream().filter(SpongeImplHooks.getPluginFilterPredicate()).sorted(Comparator.comparing(PluginContainer::getName)).forEachOrdered(sortedContainers::add);
            if (src instanceof Player) {
                final List<Text> containerList = new ArrayList<>();
                final PaginationList.Builder builder = PaginationList.builder();
                builder.title(Text.of(TextColors.YELLOW, "Plugins", TextColors.WHITE, " (", sortedContainers.size(), ")")).padding(Text.of(TextColors.DARK_GREEN, "="));
                for (PluginContainer container : sortedContainers) {
                    final Text.Builder containerBuilder = Text.builder().append(Text.of(TextColors.RESET, " - ", TextColors.GREEN, container.getName())).onClick(TextActions.runCommand("/sponge:sponge plugins " + container.getId())).onHover(TextActions.showText(Text.of(TextColors.RESET, "ID: ", container.getId(), Text.NEW_LINE, "Version: ", container.getVersion().orElse("Unknown"))));
                    containerList.add(containerBuilder.build());
                }
                builder.contents(containerList).build().sendTo(src);
            } else {
                final Text.Builder builder = Text.builder();
                builder.append(Text.of(TextColors.YELLOW, "Plugins", TextColors.WHITE, " (", sortedContainers.size(), "): "));
                boolean first = true;
                for (PluginContainer container : sortedContainers) {
                    if (!first) {
                        builder.append(SEPARATOR_TEXT);
                    }
                    first = false;
                    builder.append(Text.of(TextColors.GREEN, container.getName()));
                }
                src.sendMessage(builder.build());
            }
        }
        return CommandResult.success();
    }).build();
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) ClickAction(org.spongepowered.api.text.action.ClickAction) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) DimensionType(org.spongepowered.api.world.DimensionType) URL(java.net.URL) CommandCallable(org.spongepowered.api.command.CommandCallable) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) GenericArguments(org.spongepowered.api.command.args.GenericArguments) GenericArguments.choices(org.spongepowered.api.command.args.GenericArguments.choices) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinDimensionType(org.spongepowered.common.interfaces.world.IMixinDimensionType) GenericArguments.plugin(org.spongepowered.api.command.args.GenericArguments.plugin) WorldConfig(org.spongepowered.common.config.type.WorldConfig) SpongeTimingsFactory(co.aikar.timings.SpongeTimingsFactory) TextActions(org.spongepowered.api.text.action.TextActions) User(org.spongepowered.api.entity.living.player.User) GenericArguments.dimension(org.spongepowered.api.command.args.GenericArguments.dimension) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Collection(java.util.Collection) GenericArguments.optional(org.spongepowered.api.command.args.GenericArguments.optional) Sponge(org.spongepowered.api.Sponge) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) MixinEnvironment(org.spongepowered.asm.mixin.MixinEnvironment) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) EntityUtil(org.spongepowered.common.entity.EntityUtil) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) SpongeApiTranslationHelper(org.spongepowered.api.util.SpongeApiTranslationHelper) World(org.spongepowered.api.world.World) DimensionConfig(org.spongepowered.common.config.type.DimensionConfig) CommandManager(org.spongepowered.api.command.CommandManager) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) SpongeConfig(org.spongepowered.common.config.SpongeConfig) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) SpongeImpl(org.spongepowered.common.SpongeImpl) Timings(co.aikar.timings.Timings) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) SpongeImplHooks(org.spongepowered.common.SpongeImplHooks) LocalDateTime(java.time.LocalDateTime) CommandMapping(org.spongepowered.api.command.CommandMapping) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Function(java.util.function.Function) TreeSet(java.util.TreeSet) IMPLEMENTATION(org.spongepowered.api.Platform.Component.IMPLEMENTATION) ArrayList(java.util.ArrayList) PaginationList(org.spongepowered.api.service.pagination.PaginationList) RayTraceResult(net.minecraft.util.math.RayTraceResult) GenericArguments.seq(org.spongepowered.api.command.args.GenericArguments.seq) 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) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Chunk(net.minecraft.world.chunk.Chunk) WorldServer(net.minecraft.world.WorldServer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) GenericArguments.literal(org.spongepowered.api.command.args.GenericArguments.literal) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) GenericArguments.world(org.spongepowered.api.command.args.GenericArguments.world) Entity(net.minecraft.entity.Entity) CommandResult(org.spongepowered.api.command.CommandResult) MalformedURLException(java.net.MalformedURLException) SpongeHooks(org.spongepowered.common.util.SpongeHooks) WorldManager(org.spongepowered.common.world.WorldManager) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) GenericArguments.string(org.spongepowered.api.command.args.GenericArguments.string) DecimalFormat(java.text.DecimalFormat) BlockUtil(org.spongepowered.common.block.BlockUtil) GeneralConfigBase(org.spongepowered.common.config.type.GeneralConfigBase) File(java.io.File) CommandException(org.spongepowered.api.command.CommandException) IBlockState(net.minecraft.block.state.IBlockState) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) DateTimeFormatter(java.time.format.DateTimeFormatter) GenericArguments.flags(org.spongepowered.api.command.args.GenericArguments.flags) GenericArguments.optionalWeak(org.spongepowered.api.command.args.GenericArguments.optionalWeak) GlobalConfig(org.spongepowered.common.config.type.GlobalConfig) Comparator(java.util.Comparator) Player(org.spongepowered.api.entity.living.player.Player) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) PaginationList(org.spongepowered.api.service.pagination.PaginationList)

Example 5 with CommandSpec

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

the class SpongeCommandFactory method createSpongeChunksCommand.

// Flag children
private static CommandSpec createSpongeChunksCommand() {
    return CommandSpec.builder().description(Text.of("Print chunk information, optionally dump")).arguments(optional(seq(literal(Text.of("dump"), "dump"), optional(literal(Text.of("dump-all"), "all"))))).permission("sponge.command.chunks").executor(new ConfigUsingExecutor(true) {

        @Override
        public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
            CommandResult res = super.execute(src, args);
            if (args.hasAny("dump")) {
                File file = new File(new File(new File("."), "chunk-dumps"), "chunk-info-" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss").format(Instant.now()) + "-server.txt");
                src.sendMessage(Text.of("Writing chunk info to: ", file));
                ChunkSaveHelper.writeChunks(file, args.hasAny("dump-all"));
                src.sendMessage(Text.of("Chunk info complete"));
            }
            return res;
        }

        @Override
        protected Text processGlobal(SpongeConfig<GlobalConfig> config, CommandSource source, CommandContext args) throws CommandException {
            for (World world : SpongeImpl.getGame().getServer().getWorlds()) {
                source.sendMessage(Text.of("World ", Text.of(TextStyles.BOLD, world.getName()), getChunksInfo(((WorldServer) world))));
            }
            return Text.of("Printed chunk info for all worlds ");
        }

        @Override
        protected Text processDimension(SpongeConfig<DimensionConfig> config, DimensionType dim, CommandSource source, CommandContext args) throws CommandException {
            SpongeImpl.getGame().getServer().getWorlds().stream().filter(world -> world.getDimension().getType().equals(dim)).forEach(world -> source.sendMessage(Text.of("World ", Text.of(TextStyles.BOLD, world.getName()), getChunksInfo(((WorldServer) world)))));
            return Text.of("Printed chunk info for all worlds in dimension ", dim.getName());
        }

        @Override
        protected Text processWorld(SpongeConfig<WorldConfig> config, World world, CommandSource source, CommandContext args) throws CommandException {
            return getChunksInfo((WorldServer) world);
        }

        protected Text key(Object text) {
            return Text.of(TextColors.GOLD, text);
        }

        protected Text value(Object text) {
            return Text.of(TextColors.GRAY, text);
        }

        protected Text getChunksInfo(WorldServer worldserver) {
            return Text.of(NEWLINE_TEXT, key("DimensionId: "), value(WorldManager.getDimensionId(worldserver)), NEWLINE_TEXT, key("Loaded chunks: "), value(worldserver.getChunkProvider().getLoadedChunkCount()), NEWLINE_TEXT, key("Active chunks: "), value(worldserver.getChunkProvider().getLoadedChunks().size()), NEWLINE_TEXT, key("Entities: "), value(worldserver.loadedEntityList.size()), NEWLINE_TEXT, key("Tile Entities: "), value(worldserver.loadedTileEntityList.size()), NEWLINE_TEXT, key("Removed Entities:"), value(worldserver.unloadedEntityList.size()), NEWLINE_TEXT, key("Removed Tile Entities: "), value(worldserver.tileEntitiesToBeRemoved), NEWLINE_TEXT);
        }
    }).build();
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) ClickAction(org.spongepowered.api.text.action.ClickAction) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) DimensionType(org.spongepowered.api.world.DimensionType) URL(java.net.URL) CommandCallable(org.spongepowered.api.command.CommandCallable) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) GenericArguments(org.spongepowered.api.command.args.GenericArguments) GenericArguments.choices(org.spongepowered.api.command.args.GenericArguments.choices) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinDimensionType(org.spongepowered.common.interfaces.world.IMixinDimensionType) GenericArguments.plugin(org.spongepowered.api.command.args.GenericArguments.plugin) WorldConfig(org.spongepowered.common.config.type.WorldConfig) SpongeTimingsFactory(co.aikar.timings.SpongeTimingsFactory) TextActions(org.spongepowered.api.text.action.TextActions) User(org.spongepowered.api.entity.living.player.User) GenericArguments.dimension(org.spongepowered.api.command.args.GenericArguments.dimension) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Collection(java.util.Collection) GenericArguments.optional(org.spongepowered.api.command.args.GenericArguments.optional) Sponge(org.spongepowered.api.Sponge) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) MixinEnvironment(org.spongepowered.asm.mixin.MixinEnvironment) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) EntityUtil(org.spongepowered.common.entity.EntityUtil) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) SpongeApiTranslationHelper(org.spongepowered.api.util.SpongeApiTranslationHelper) World(org.spongepowered.api.world.World) DimensionConfig(org.spongepowered.common.config.type.DimensionConfig) CommandManager(org.spongepowered.api.command.CommandManager) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) SpongeConfig(org.spongepowered.common.config.SpongeConfig) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) SpongeImpl(org.spongepowered.common.SpongeImpl) Timings(co.aikar.timings.Timings) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) SpongeImplHooks(org.spongepowered.common.SpongeImplHooks) LocalDateTime(java.time.LocalDateTime) CommandMapping(org.spongepowered.api.command.CommandMapping) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Function(java.util.function.Function) TreeSet(java.util.TreeSet) IMPLEMENTATION(org.spongepowered.api.Platform.Component.IMPLEMENTATION) ArrayList(java.util.ArrayList) PaginationList(org.spongepowered.api.service.pagination.PaginationList) RayTraceResult(net.minecraft.util.math.RayTraceResult) GenericArguments.seq(org.spongepowered.api.command.args.GenericArguments.seq) 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) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Chunk(net.minecraft.world.chunk.Chunk) WorldServer(net.minecraft.world.WorldServer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) GenericArguments.literal(org.spongepowered.api.command.args.GenericArguments.literal) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) GenericArguments.world(org.spongepowered.api.command.args.GenericArguments.world) Entity(net.minecraft.entity.Entity) CommandResult(org.spongepowered.api.command.CommandResult) MalformedURLException(java.net.MalformedURLException) SpongeHooks(org.spongepowered.common.util.SpongeHooks) WorldManager(org.spongepowered.common.world.WorldManager) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) GenericArguments.string(org.spongepowered.api.command.args.GenericArguments.string) DecimalFormat(java.text.DecimalFormat) BlockUtil(org.spongepowered.common.block.BlockUtil) GeneralConfigBase(org.spongepowered.common.config.type.GeneralConfigBase) File(java.io.File) CommandException(org.spongepowered.api.command.CommandException) IBlockState(net.minecraft.block.state.IBlockState) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) DateTimeFormatter(java.time.format.DateTimeFormatter) GenericArguments.flags(org.spongepowered.api.command.args.GenericArguments.flags) GenericArguments.optionalWeak(org.spongepowered.api.command.args.GenericArguments.optionalWeak) GlobalConfig(org.spongepowered.common.config.type.GlobalConfig) Comparator(java.util.Comparator) DimensionType(org.spongepowered.api.world.DimensionType) IMixinDimensionType(org.spongepowered.common.interfaces.world.IMixinDimensionType) CommandContext(org.spongepowered.api.command.args.CommandContext) SpongeConfig(org.spongepowered.common.config.SpongeConfig) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) CommandSource(org.spongepowered.api.command.CommandSource) World(org.spongepowered.api.world.World) File(java.io.File) CommandResult(org.spongepowered.api.command.CommandResult)

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