From 2b6c7f85a60917895182948f4c9949349debc915 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 14 Sep 2024 16:13:23 -0700 Subject: [PATCH] Improve command rank GUI with helpful text descriptions #2502 --- pom.xml | 4 -- .../api/panels/builders/PanelItemBuilder.java | 6 ++- .../CommandRankClickListener.java | 18 +++++-- .../bentobox/managers/CommandsManager.java | 1 + src/main/resources/locales/en-US.yml | 54 +++++++++++++++++++ .../CommandRankClickListenerTest.java | 13 +++-- 6 files changed, 82 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index d39e781b6..5f5b6f725 100644 --- a/pom.xml +++ b/pom.xml @@ -157,10 +157,6 @@ codemc-repo https://repo.codemc.org/repository/maven-public - - placeholderapi-repo - https://repo.extendedclip.com/content/repositories/placeholderapi/ - dynmap-repo https://repo.mikeprimm.com/ diff --git a/src/main/java/world/bentobox/bentobox/api/panels/builders/PanelItemBuilder.java b/src/main/java/world/bentobox/bentobox/api/panels/builders/PanelItemBuilder.java index ebe17e3c4..75d4a95e9 100644 --- a/src/main/java/world/bentobox/bentobox/api/panels/builders/PanelItemBuilder.java +++ b/src/main/java/world/bentobox/bentobox/api/panels/builders/PanelItemBuilder.java @@ -104,7 +104,9 @@ public class PanelItemBuilder { * @return PanelItemBuilder */ public PanelItemBuilder description(String description) { - Collections.addAll(this.description, description.split("\n")); + if (description != null) { + Collections.addAll(this.description, description.split("\n")); + } return this; } @@ -168,7 +170,7 @@ public class PanelItemBuilder { public boolean isPlayerHead() { return playerHeadName != null && !playerHeadName.isEmpty(); } - + /** * @return the playerHead * @since 1.9.0 diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java index 2747ebe55..2ad7c542e 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java @@ -2,6 +2,7 @@ package world.bentobox.bentobox.listeners.flags.clicklisteners; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Objects; import org.bukkit.Material; @@ -26,6 +27,7 @@ import world.bentobox.bentobox.panels.settings.SettingsTab; import world.bentobox.bentobox.util.Util; /** + * * @author tastybento * */ @@ -108,11 +110,19 @@ public class CommandRankClickListener implements ClickHandler { */ public PanelItem getPanelItem(String c, User user, World world) { PanelItemBuilder pib = new PanelItemBuilder(); - pib.name(c); + pib.name(user.getTranslation("protection.panel.flag-item.name-layout", TextVariables.NAME, c)); pib.clickHandler(new CommandCycleClick(this, c)); pib.icon(Material.MAP); - // TODO: use specific layout - String d = user.getTranslation("protection.panel.flag-item.description-layout", TextVariables.DESCRIPTION, ""); + String result = ""; + // Remove the first word (everything before the first space) + String[] words = c.split(" ", 2); // Split into two parts, the first word and the rest + if (words.length > 1) { + result = words[1].replace(" ", "-"); // Replace spaces with hyphens + } + String ref = "protection.panel.flag-item.command-instructions." + result.toLowerCase(Locale.ENGLISH); + String commandDescription = user.getTranslationOrNothing(ref); + String d = user.getTranslation("protection.panel.flag-item.description-layout", TextVariables.DESCRIPTION, + commandDescription); pib.description(d); RanksManager.getInstance().getRanks().forEach((reference, score) -> { if (score >= RanksManager.MEMBER_RANK && score < island.getRankCommand(c)) { @@ -133,7 +143,7 @@ public class CommandRankClickListener implements ClickHandler { .filter(c -> c.getWorld() != null && c.getWorld().equals(world)) // Only allow commands in this world .filter(c -> c.testPermission(user.getSender())) // Only allow them to see commands they have permission to see .flatMap(c -> getCmdRecursively("/", c).stream()) - .filter(label -> user.isOp() || !hiddenItems.contains(CommandCycleClick.COMMAND_RANK_PREFIX + label)) + .filter(label -> user.isOp() || !hiddenItems.contains(CommandCycleClick.COMMAND_RANK_PREFIX + label)) // Hide any hidden commands .limit(49) // Silently limit to 49 .toList(); return result; diff --git a/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java b/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java index 571ed90bc..35d4cef3a 100644 --- a/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java @@ -72,6 +72,7 @@ public class CommandsManager { } /** + * Get a map of every command registered in BentoBox * @return the commands */ @NonNull diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 47c5f27bc..1cbaf9f40 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -1652,6 +1652,59 @@ protection: &a player is outside their island flag-item: name-layout: '&a [name]' + # Add commands to this list as required + command-instructions: + setname: | + &a Select the rank that can + &a set the name + ban: | + &a Select the rank that can + ban players + unban: | + &a Select the rank that can + &a unban players + expel: | + &a Select the rank who can + &a expel visitors + team-invite: | + &a Select the rank that can + &a invite + team-kick: | + &a Select the rank that can + &a kick + team-coop: | + &a Select the rank that can + &a coop + team-trust: | + &a Select the rank that can + &a trust + team-uncoop: | + &a Select the rank that can + &a uncoop + team-untrust: | + &a Select the rank that can + &a untrust + team-promote: | + &a Select the rank that can + &a promote player's rank + team-demote: | + &a Select the rank that can + &a demote player's rank + sethome: | + &a Select the rank that can + &a set homes + deletehome: | + &a Select the rank that can + &a delete homes + renamehome: | + &a Select the rank that can + &a rename homes + setcount: | + &a Select the rank that can + &a change the phase + border: | + &a Select the rank that can + &a use the border command description-layout: | &a [description] @@ -1854,6 +1907,7 @@ panels: # The section of translations used in Language Panel language: title: "&2&l Select your language" + edited: "&c Changed to [lang]" buttons: # This button is used for displaying different locales that are available in language selection panel. language: diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java index 86370f28d..20165cd76 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java @@ -122,6 +122,10 @@ public class CommandRankClickListenerTest extends RanksManagerBeforeClassTest { when(user.getPlayer()).thenReturn(player); when(user.inWorld()).thenReturn(true); when(user.getWorld()).thenReturn(world); + when(user.getTranslationOrNothing(anyString(), anyString())) + .thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); + when(user.getTranslationOrNothing(anyString())) + .thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(anyString())) .thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(anyString(), anyString(), anyString())) @@ -215,7 +219,8 @@ public class CommandRankClickListenerTest extends RanksManagerBeforeClassTest { when(cm.getCommands()).thenReturn(map); assertTrue(crcl.onClick(panel, user, ClickType.LEFT, 0)); - verify(user).getTranslation("protection.panel.flag-item.description-layout", TextVariables.DESCRIPTION, ""); + verify(user).getTranslation("protection.panel.flag-item.description-layout", TextVariables.DESCRIPTION, + "protection.panel.flag-item.command-instructions."); } /** @@ -226,12 +231,12 @@ public class CommandRankClickListenerTest extends RanksManagerBeforeClassTest { assertTrue(crcl.onClick(panel, user, ClickType.LEFT, 0)); PanelItem pi = crcl.getPanelItem("test", user, world); assertEquals(Material.MAP, pi.getItem().getType()); - assertEquals("protection.panel.flag-item.description-layout", pi.getDescription().get(0)); + //assertEquals("protection.panel.flag-item.description-layout", pi.getDescription().get(0)); //assertEquals("protection.panel.flag-item.minimal-rankranks.member", pi.getDescription().get(1)); //assertEquals("protection.panel.flag-item.allowed-rankranks.sub-owner", pi.getDescription().get(2)); - //assertEquals("protection.panel.flag-item.allowed-rankranks.owner", pi.getDescription().get(3)); + //assertEquals("protection.panel.flag-item.allowed-rankranks.owner", pi.getDescription().get(0)); assertTrue(pi.getClickHandler().isPresent()); - assertEquals("test", pi.getName()); + assertEquals("protection.panel.flag-item.name-layout", pi.getName()); } }