From 7c6d7d58830184f5682d641ddb58f7e73091ea0e Mon Sep 17 00:00:00 2001 From: Zax71 Date: Sun, 24 Sep 2023 10:34:11 +0100 Subject: [PATCH] Merge /mv gamerule and /mv gamerules --- .../core/commands/GameruleCommand.java | 120 ++++++++++++++-- .../core/commands/GamerulesCommand.java | 132 ------------------ .../multiverse/core/utils/MVCorei18n.java | 14 +- .../resources/multiverse-core_en.properties | 22 +-- 4 files changed, 129 insertions(+), 159 deletions(-) delete mode 100644 src/main/java/org/mvplugins/multiverse/core/commands/GamerulesCommand.java diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java index 86d5f1c2..10566296 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java +++ b/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java @@ -1,61 +1,104 @@ package org.mvplugins.multiverse.core.commands; import co.aikar.commands.BukkitCommandIssuer; +import co.aikar.commands.CommandIssuer; +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.Flags; +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.bukkit.GameRule; import org.bukkit.World; 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.context.GameRuleValue; +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.MapContentProvider; import org.mvplugins.multiverse.core.utils.MVCorei18n; import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; +import java.util.HashMap; +import java.util.Map; + @Service @CommandAlias("mv") +@Subcommand("gamerule|rule|gamerules|rules") class GameruleCommand extends MultiverseCommand { + 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 GameruleCommand(@NotNull MVCommandManager commandManager) { super(commandManager); } - @Subcommand("gamerule") + @Subcommand("set") @CommandPermission("multiverse.core.gamerule.set") @CommandCompletion("@gamerules true|false|@range:1-10 @mvworlds:multiple|*") @Syntax(" [World or *]") - @Description("{@@mv-core.gamerule.description}") + @Description("{@@mv-core.gamerule.set.description}") void onGameruleCommand( BukkitCommandIssuer issuer, @Syntax("") - @Description("{@@mv-core.gamerule.gamerule.description}") + @Description("{@@mv-core.gamerule.set.gamerule.description}") GameRule gamerule, @Syntax("") - @Description("{@@mv-core.gamerule.value.description}") + @Description("{@@mv-core.gamerule.set.value.description}") GameRuleValue gameRuleValue, @Flags("resolve=issuerAware") @Syntax("[World or *]") - @Description("{@@mv-core.gamerule.world.description}") - LoadedMultiverseWorld[] worlds) { + @Description("{@@mv-core.gamerule.set.world.description}") + LoadedMultiverseWorld[] worlds) + { Object value = gameRuleValue.getValue(); boolean success = true; for (LoadedMultiverseWorld world : worlds) { // Set gamerules and add false to list if it fails World bukkitWorld = world.getBukkitWorld().getOrNull(); if (bukkitWorld == null || !bukkitWorld.setGameRule(gamerule, value)) { - issuer.sendError(MVCorei18n.GAMERULE_FAILED, + issuer.sendError(MVCorei18n.GAMERULE_SET_FAILED, "{gamerule}", gamerule.getName(), "{value}", value.toString(), "{world}", world.getName(), @@ -66,16 +109,75 @@ class GameruleCommand extends MultiverseCommand { // Tell user if it was successful if (success) { if (worlds.length == 1) { - issuer.sendInfo(MVCorei18n.GAMERULE_SUCCESS_SINGLE, + issuer.sendInfo(MVCorei18n.GAMERULE_SET_SUCCESS_SINGLE, "{gamerule}", gamerule.getName(), "{value}", value.toString(), "{world}", worlds[0].getName()); } else if (worlds.length > 1) { - issuer.sendInfo(MVCorei18n.GAMERULE_SUCCESS_MULTIPLE, + issuer.sendInfo(MVCorei18n.GAMERULE_SET_SUCCESS_MULTIPLE, "{gamerule}", gamerule.getName(), "{value}", value.toString(), "{count}", String.valueOf(worlds.length)); } } } + + @Subcommand("list") + @CommandPermission("multiverse.core.gamerule.list") + @CommandCompletion("@mvworlds|@flags:groupName=mvgamerulescommand @flags:groupName=mvgamerulescommand") + @Syntax("[world] [--page ] [--filter ]") + @Description("{@@mv-core.gamerule.list.description}") + void onGamerulesCommand( + @NotNull MVCommandIssuer issuer, + + @Flags("resolve=issuerAware") + @Syntax("") + @Description("{@@mv-core.gamerule.list.description.world}") + LoadedMultiverseWorld world, + + @Optional + @Syntax("[--page ] [--filter ]") + @Description("{@@mv-core.gamerule.list.description.page}") + String[] flags) { + ParsedCommandFlags parsedFlags = parseFlags(flags); + + ContentDisplay.create() + .addContent(MapContentProvider.forContent(getGameRuleMap(world.getBukkitWorld().getOrNull())) // TODO: Handle null + .withKeyColor(ChatColor.AQUA) + .withValueColor(ChatColor.WHITE)) + .withSendHandler(PagedSendHandler.create() + .withHeader(this.getListTitle(issuer, world.getBukkitWorld().getOrNull())) + .doPagination(true) + .withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1)) + .withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get()))) + .send(issuer); + } + + /** + * Gets all the gamerules and their values for a given world. + * + * @param world The world to find gamerules for. + * @return A map of the gamerules and their values + */ + private Map getGameRuleMap(World world) { + Map gameRuleMap = new HashMap<>(); + + for (GameRule gamerule : GameRule.values()) { + Object gameruleValue = world.getGameRuleValue(gamerule); + if (gameruleValue == null) { + gameRuleMap.put(gamerule.getName(), "null"); + continue; + } + gameRuleMap.put(gamerule.getName(), gameruleValue.toString()); + } + return gameRuleMap; + } + + private String getListTitle(CommandIssuer issuer, World world) { + return this.commandManager.formatMessage( + issuer, + MessageType.INFO, + MVCorei18n.GAMERULE_LIST_TITLE, + "{world}", world.getName()); + } } diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/GamerulesCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/GamerulesCommand.java deleted file mode 100644 index d733a8b3..00000000 --- a/src/main/java/org/mvplugins/multiverse/core/commands/GamerulesCommand.java +++ /dev/null @@ -1,132 +0,0 @@ -package org.mvplugins.multiverse.core.commands; - -import java.util.HashMap; -import java.util.Map; - -import co.aikar.commands.CommandIssuer; -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.Flags; -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.bukkit.GameRule; -import org.bukkit.World; -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.MapContentProvider; -import org.mvplugins.multiverse.core.utils.MVCorei18n; -import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; - -/** - * List all gamerules in your current or specified world. - */ -@Service -@CommandAlias("mv") -class GamerulesCommand extends MultiverseCommand { - - 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 - GamerulesCommand(@NotNull MVCommandManager commandManager) { - super(commandManager); - } - - @Subcommand("gamerules|rules") - @CommandPermission("multiverse.core.gamerule.list") - @CommandCompletion("@mvworlds|@flags:groupName=mvgamerulescommand @flags:groupName=mvgamerulescommand") - @Syntax("[world] [--page ] [--filter ]") - @Description("{@@mv-core.gamerules.description}") - void onGamerulesCommand( - @NotNull MVCommandIssuer issuer, - - @Flags("resolve=issuerAware") - @Syntax("") - @Description("{@@mv-core.gamerules.description.world}") - LoadedMultiverseWorld world, - - @Optional - @Syntax("[--page ] [--filter ]") - @Description("{@@mv-core.gamerules.description.page}") - String[] flags) { - ParsedCommandFlags parsedFlags = parseFlags(flags); - - ContentDisplay.create() - .addContent(MapContentProvider.forContent(getGameRuleMap(world.getBukkitWorld().getOrNull())) // TODO: Handle null - .withKeyColor(ChatColor.AQUA) - .withValueColor(ChatColor.WHITE)) - .withSendHandler(PagedSendHandler.create() - .withHeader(this.getTitle(issuer, world.getBukkitWorld().getOrNull())) - .doPagination(true) - .withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1)) - .withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get()))) - .send(issuer); - } - - /** - * Gets all the gamerules and their values for a given world. - * - * @param world The world to find gamerules for. - * @return A map of the gamerules and their values - */ - private Map getGameRuleMap(World world) { - Map gameRuleMap = new HashMap<>(); - - for (GameRule gamerule : GameRule.values()) { - Object gameruleValue = world.getGameRuleValue(gamerule); - if (gameruleValue == null) { - gameRuleMap.put(gamerule.getName(), "null"); - continue; - } - gameRuleMap.put(gamerule.getName(), gameruleValue.toString()); - } - return gameRuleMap; - } - - private String getTitle(CommandIssuer issuer, World world) { - return this.commandManager.formatMessage( - issuer, - MessageType.INFO, - MVCorei18n.GAMERULES_TITLE, - "{world}", world.getName()); - } -} 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..7006b9a5 100644 --- a/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java +++ b/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java @@ -50,15 +50,15 @@ public enum MVCorei18n implements MessageKeyProvider { DUMPS_URL_LIST, // gamerule command - GAMERULE_FAILED, - GAMERULE_SUCCESS_SINGLE, - GAMERULE_SUCCESS_MULTIPLE, + GAMERULE_SET_FAILED, + GAMERULE_SET_SUCCESS_SINGLE, + GAMERULE_SET_SUCCESS_MULTIPLE, // Gamerules command - GAMERULES_DESCRIPTION, - GAMERULES_DESCRIPTION_PAGE, - GAMERULES_DESCRIPTION_WORLD, - GAMERULES_TITLE, + GAMERULE_LIST_DESCRIPTION, + GAMERULE_LIST_DESCRIPTION_PAGE, + GAMERULE_LIST_DESCRIPTION_WORLD, + GAMERULE_LIST_TITLE, // import command IMPORT_IMPORTING, diff --git a/src/main/resources/multiverse-core_en.properties b/src/main/resources/multiverse-core_en.properties index 178a6efd..c78be4b4 100644 --- a/src/main/resources/multiverse-core_en.properties +++ b/src/main/resources/multiverse-core_en.properties @@ -61,19 +61,19 @@ mv-core.dumps.description=Dumps version info to the console or paste services mv-core.dumps.url.list={service} : {link} # /mv gamerule -mv-core.gamerule.description=Changes a gamerule in one or more worlds -mv-core.gamerule.gamerule.description=Gamerule to set -mv-core.gamerule.value.description=Value of gamerule -mv-core.gamerule.world.description=World to apply gamerule to, current world by default -mv-core.gamerule.failed=Failed to set gamerule {gamerule} to {value} in {world}. &fIt should be a {type}. -mv-core.gamerule.success.single=&aSuccessfully set {gamerule} to {value} in {world}. -mv-core.gamerule.success.multiple=&aSuccessfully set {gamerule} to {value} in {count} worlds. +mv-core.gamerule.set.description=Changes a gamerule in one or more worlds +mv-core.gamerule.set.gamerule.description=Gamerule to set +mv-core.gamerule.set.value.description=Value of gamerule +mv-core.gamerule.set.world.description=World to apply gamerule to, current world by default +mv-core.gamerule.set.failed=Failed to set gamerule {gamerule} to {value} in {world}. &fIt should be a {type}. +mv-core.gamerule.set.success.single=&aSuccessfully set {gamerule} to {value} in {world}. +mv-core.gamerule.set.success.multiple=&aSuccessfully set {gamerule} to {value} in {count} worlds. # /mv gamerules -mv-core.gamerules.description=Lists gamerules for the specified world -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-core.gamerule.list.description=Lists gamerules for the specified world +mv-core.gamerule.list.description.page=The page to view +mv-core.gamerule.list.description.world=The world to list gamerules in +mv-core.gamerule.list.title= --- Gamerules for {world} --- # /mv import mv-core.import.description=Imports an existing world folder.