From 48ff8d8bd1e03fc02a4357fd84f8b5ab0e5a18f3 Mon Sep 17 00:00:00 2001 From: Zax71 Date: Sat, 9 Sep 2023 12:16:48 +0100 Subject: [PATCH] Switch to ContentDisplay API --- .../commands/GamerulesCommand.java | 77 ++++++++++--------- .../display/handlers/PagedSendHandler.java | 2 +- .../MultiverseCore/utils/MVCorei18n.java | 3 +- .../resources/multiverse-core_en.properties | 3 +- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java index 782ddf8d..eb263bbe 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java @@ -6,9 +6,9 @@ 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.Default; import co.aikar.commands.annotation.Description; -import co.aikar.commands.annotation.Flags; -import co.aikar.commands.annotation.Single; +import co.aikar.commands.annotation.Optional; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.dumptruckman.minecraft.util.Logging; @@ -16,11 +16,14 @@ import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.handlers.PagedSendHandler; +import com.onarandombox.MultiverseCore.display.parsers.MapContentProvider; import com.onarandombox.MultiverseCore.utils.MVCorei18n; import jakarta.inject.Inject; +import org.bukkit.ChatColor; import org.bukkit.GameRule; import org.bukkit.World; -import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; @@ -43,35 +46,42 @@ public class GamerulesCommand extends MultiverseCommand { } @Subcommand("gamerules|rules") - @CommandPermission("multiverse.core.gamerules.list") - @CommandCompletion("@mvworlds") - @Syntax("[World]") + @CommandPermission("multiverse.core.gamerule.list") + @CommandCompletion("@mvworlds @range:1-6") + @Syntax("[world] [page]") @Description("{@@mv-core.gamerules.description}") public void onGamerulesCommand(@NotNull BukkitCommandIssuer issuer, - @Single + @Optional @Syntax("") @Description("{@@mv-core.gamerules.description.world}") - MVWorld world + MVWorld world, + + @Optional + @Default("1") + @Syntax("") + @Description("{@@mv-core.gamerules.description.page}") + int page + + ) { - if (!issuer.isPlayer() && world == null) { - issuer.sendInfo(MVCorei18n.GAMERULES_ERROR_SPECIFYWORLD); - return; - } - - // Get the players world if none is specified - if (world == null) { - Player player = issuer.getPlayer(); // Need to do it here so the command can be run from console - Logging.finer("Getting the player's current world to list gamerules for"); - world = worldManager.getMVWorld(player.getWorld()); - } - - // Finally, send the list - issuer.sendInfo(MVCorei18n.GAMERULES_TITLE, "{world}", world.getName()); - issuer.sendMessage("\n" + encodeMap(issuer, getGameRuleMap(world.getCBWorld()))); - + Logging.finer("Page is: " + page); + ContentDisplay.create() + .addContent( + new MapContentProvider<>(getGameRuleMap(world.getCBWorld())) + .withKeyColor(ChatColor.AQUA) + .withValueColor(ChatColor.WHITE) + ) + .withSendHandler( + new PagedSendHandler() + .withHeader(this.getTitle(issuer, world.getCBWorld())) + .doPagination(true) + .withLinesPerPage(8) + .withTargetPage(page) + ) + .send(issuer); } /** @@ -93,18 +103,11 @@ public class GamerulesCommand extends MultiverseCommand { return gameRuleMap; } - private String encodeMap(CommandIssuer issuer, Map inMap) { - StringBuilder stringBuilder = new StringBuilder(); - for (String key : inMap.keySet()) { - String value = inMap.get(key); - - stringBuilder.append(this.commandManager.formatMessage( - issuer, - MessageType.INFO, - MVCorei18n.GAMERULES_RULE, - "{gamerule}", key, "{value}", value)) - .append("\n"); - } - return stringBuilder.toString(); + 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/com/onarandombox/MultiverseCore/display/handlers/PagedSendHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedSendHandler.java index 301e7347..03458779 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedSendHandler.java +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedSendHandler.java @@ -99,7 +99,7 @@ public class PagedSendHandler extends BaseSendHandler { /** * Sets whether display output should be paginated if is for console output. - * This option will be useless of {@link PagedSendHandler#paginate} is set to false. + * This option will be useless if {@link PagedSendHandler#paginate} is set to false. * * @param paginateInConsole State of doing pagination in console. * @return Same {@link PagedSendHandler} for method chaining. diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java index f2b7ac8f..3ba4cd44 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVCorei18n.java @@ -44,10 +44,9 @@ public enum MVCorei18n implements MessageKeyProvider { // Gamerules command GAMERULES_DESCRIPTION, + GAMERULES_DESCRIPTION_PAGE, GAMERULES_DESCRIPTION_WORLD, - GAMERULES_ERROR_SPECIFYWORLD, GAMERULES_TITLE, - GAMERULES_RULE, // import command IMPORT_IMPORTING, diff --git a/src/main/resources/multiverse-core_en.properties b/src/main/resources/multiverse-core_en.properties index a72bfb44..225fca53 100644 --- a/src/main/resources/multiverse-core_en.properties +++ b/src/main/resources/multiverse-core_en.properties @@ -61,10 +61,9 @@ mv-core.gamerule.success.multiple=&aSuccessfully set {gamerule} to {value} in {c # /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.error.specifyworld=You must specify a world from the command line mv-core.gamerules.title= --- Gamerules for {world} --- -mv-core.gamerules.rule=&l{gamerule}&f : &6{value} # /mv import mv-core.import.description=Imports an existing world folder.