From ac174dcf66a7897b7f654fc044c82459e68a5f05 Mon Sep 17 00:00:00 2001 From: Zax71 Date: Fri, 22 Sep 2023 19:03:19 +0100 Subject: [PATCH 1/2] Add /mv generators --- .../core/commands/GeneratorsCommand.java | 104 ++++++++++++++++++ .../multiverse/core/utils/MVCorei18n.java | 5 + .../resources/multiverse-core_en.properties | 5 + 3 files changed, 114 insertions(+) create mode 100644 src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java new file mode 100644 index 00000000..293a00ce --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java @@ -0,0 +1,104 @@ +package org.mvplugins.multiverse.core.commands; + +import co.aikar.commands.InvalidCommandArgument; +import co.aikar.commands.MessageType; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Optional; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import jakarta.inject.Inject; +import org.bukkit.ChatColor; +import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; +import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; +import org.mvplugins.multiverse.core.commandtools.MVCommandManager; +import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; +import org.mvplugins.multiverse.core.commandtools.flags.CommandValueFlag; +import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags; +import org.mvplugins.multiverse.core.display.ContentDisplay; +import org.mvplugins.multiverse.core.display.filters.ContentFilter; +import org.mvplugins.multiverse.core.display.filters.DefaultContentFilter; +import org.mvplugins.multiverse.core.display.filters.RegexContentFilter; +import org.mvplugins.multiverse.core.display.handlers.PagedSendHandler; +import org.mvplugins.multiverse.core.display.parsers.ListContentProvider; +import org.mvplugins.multiverse.core.utils.MVCorei18n; +import org.mvplugins.multiverse.core.world.generators.GeneratorProvider; + +import java.util.List; + +/** + * List all gamerules in your current or specified world. + */ +@Service +@CommandAlias("mv") +class GeneratorsCommand extends MultiverseCommand { + + private final GeneratorProvider generatorProvider; + + private final CommandValueFlag PAGE_FLAG = flag(CommandValueFlag + .builder("--page", Integer.class) + .addAlias("-p") + .context(value -> { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new InvalidCommandArgument("Invalid page number: " + value); + } + }) + .build()); + + private final CommandValueFlag FILTER_FLAG = flag(CommandValueFlag + .builder("--filter", ContentFilter.class) + .addAlias("-f") + .context(value -> { + try { + return RegexContentFilter.fromString(value); + } catch (IllegalArgumentException e) { + throw new InvalidCommandArgument("Invalid filter: " + value); + } + }) + .build()); + + @Inject + GeneratorsCommand(@NotNull MVCommandManager commandManager, @NotNull GeneratorProvider generatorProvider) { + super(commandManager); + this.generatorProvider = generatorProvider; + } + + @Subcommand("generators|gens") + @CommandPermission("multiverse.core.generator") + @CommandCompletion("@flags:groupName=mvgeneratorscommand @flags:groupName=mvgeneratorscommand") + @Syntax("") + @Description("{@@mv-core.generators.description}") + void onGamerulesCommand( + @NotNull MVCommandIssuer issuer, + + @Optional + @Syntax("[--page ] [--filter ]") + @Description("{@@mv-core.generators.description.flags}") + String[] flags + ) { + ParsedCommandFlags parsedFlags = parseFlags(flags); + + // Get the generators loaded using the command suggestions + List generators = (List) generatorProvider.suggestGeneratorString(""); + + // Tell the user if we cannot find any generator plugins, then abort + if (generators.isEmpty()) { + issuer.sendMessage(commandManager.formatMessage(issuer, MessageType.INFO, MVCorei18n.GENERATORS_EMPTY)); + return; + } + + ContentDisplay.create() + .addContent(ListContentProvider.forContent(generators)) + .withSendHandler(PagedSendHandler.create() + .withHeader("%s====[ Multiverse Generator List ]====", ChatColor.AQUA) + .doPagination(true) + .withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1)) + .withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get()))) + .send(issuer); + } +} diff --git a/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java b/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java index c8a4af49..b92999d3 100644 --- a/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java +++ b/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java @@ -60,6 +60,11 @@ public enum MVCorei18n implements MessageKeyProvider { GAMERULES_DESCRIPTION_WORLD, GAMERULES_TITLE, + // Generators command + GENERATORS_DESCRIPTION, + GENERATORS_DESCRIPTION_FLAGS, + GENERATORS_EMPTY, + // import command IMPORT_IMPORTING, IMPORT_SUCCESS, diff --git a/src/main/resources/multiverse-core_en.properties b/src/main/resources/multiverse-core_en.properties index 178a6efd..15227afd 100644 --- a/src/main/resources/multiverse-core_en.properties +++ b/src/main/resources/multiverse-core_en.properties @@ -75,6 +75,11 @@ mv-core.gamerules.description.page=The page to view mv-core.gamerules.description.world=The world to list gamerules in mv-core.gamerules.title= --- Gamerules for {world} --- +# /mv generators +mv-core.generators.description=Lists generators known to Multiverse +mv-core.generators.description.flags=Filter - only shows entries matching this. Page - the page to show +mv-core.generators.empty=&cNo Generator Plugins found. + # /mv import mv-core.import.description=Imports an existing world folder. mv-core.import.name.description=Name of the world folder. From d6ccbd9eeed1c53d311d3ca0a73a13b670954239 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sat, 23 Sep 2023 09:50:22 +0800 Subject: [PATCH 2/2] Fix some checkstyles for generator command --- .../multiverse/core/commands/GeneratorsCommand.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java index 293a00ce..372fb2c2 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java +++ b/src/main/java/org/mvplugins/multiverse/core/commands/GeneratorsCommand.java @@ -1,5 +1,7 @@ package org.mvplugins.multiverse.core.commands; +import java.util.List; + import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.MessageType; import co.aikar.commands.annotation.CommandAlias; @@ -13,6 +15,7 @@ import jakarta.inject.Inject; import org.bukkit.ChatColor; import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; + import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; import org.mvplugins.multiverse.core.commandtools.MVCommandManager; import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; @@ -27,8 +30,6 @@ import org.mvplugins.multiverse.core.display.parsers.ListContentProvider; import org.mvplugins.multiverse.core.utils.MVCorei18n; import org.mvplugins.multiverse.core.world.generators.GeneratorProvider; -import java.util.List; - /** * List all gamerules in your current or specified world. */ @@ -79,8 +80,7 @@ class GeneratorsCommand extends MultiverseCommand { @Optional @Syntax("[--page ] [--filter ]") @Description("{@@mv-core.generators.description.flags}") - String[] flags - ) { + String[] flags) { ParsedCommandFlags parsedFlags = parseFlags(flags); // Get the generators loaded using the command suggestions