From c951ca06888514dc0b664155fd718945559c7dd3 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 17 Apr 2024 21:07:15 -0700 Subject: [PATCH] Added panel placeholders #385 --- .../aoneblock/AOneBlockPlaceholders.java | 4 +- .../aoneblock/panels/PhasesPanel.java | 93 ++++++++++++++++--- src/main/resources/locales/en-US.yml | 7 +- 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java b/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java index 0632ade..9c24f69 100644 --- a/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java +++ b/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java @@ -8,6 +8,7 @@ import org.bukkit.Material; import world.bentobox.aoneblock.dataobjects.OneBlockIslands; +import world.bentobox.aoneblock.panels.PhasesPanel; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; @@ -79,10 +80,11 @@ private String getPhaseBlocksForIsland(User user, Island i) { } String result = set.stream().map(m -> getMaterialName(user, m)) - .map(string -> user.getTranslation("aoneblock.placeholders.block-list-format", TextVariables.NAME, + .map(string -> user.getTranslation(PhasesPanel.REFERENCE + "blocks", TextVariables.NAME, string)) .collect(Collectors.joining()); // Removing the last newline character or comma if it exists + result = result.trim(); if (result.endsWith("\n") || result.endsWith(",")) { result = result.substring(0, result.length() - 1); } diff --git a/src/main/java/world/bentobox/aoneblock/panels/PhasesPanel.java b/src/main/java/world/bentobox/aoneblock/panels/PhasesPanel.java index c2643f9..d2acc5d 100644 --- a/src/main/java/world/bentobox/aoneblock/panels/PhasesPanel.java +++ b/src/main/java/world/bentobox/aoneblock/panels/PhasesPanel.java @@ -14,6 +14,7 @@ import java.util.Map; import java.util.stream.Collectors; +import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.event.inventory.ClickType; @@ -26,6 +27,7 @@ import world.bentobox.aoneblock.oneblocks.OneBlockPhase; import world.bentobox.aoneblock.oneblocks.Requirement; import world.bentobox.bank.Bank; +import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.panels.PanelItem; @@ -36,6 +38,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.hooks.LangUtilsHook; +import world.bentobox.bentobox.util.Util; import world.bentobox.level.Level; @@ -55,6 +58,8 @@ public class PhasesPanel private static final String LEVEL = "[level]"; private static final String PHASE2 = "[phase]"; private static final String INDEXING = "indexing"; + private static final String BLOCKS = "[blocks]"; + public static final String REFERENCE = "aoneblock.gui.buttons.phase."; // --------------------------------------------------------------------- // Section: Constructor @@ -349,7 +354,7 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry { switch (requirement.getType()) { - case ECO -> economyText.append(this.user.getTranslationOrNothing(reference + "economy", + case ECO -> economyText.append(this.user.getTranslationOrNothing(REFERENCE + "economy", TextVariables.NUMBER, String.valueOf(requirement.getEco()))); - case BANK -> bankText.append(this.user.getTranslationOrNothing(reference + "bank", + case BANK -> bankText.append(this.user.getTranslationOrNothing(REFERENCE + "bank", TextVariables.NUMBER, String.valueOf(requirement.getBank()))); - case LEVEL -> levelText.append(this.user.getTranslationOrNothing(reference + "level", + case LEVEL -> levelText.append(this.user.getTranslationOrNothing(REFERENCE + "level", TextVariables.NUMBER, String.valueOf(requirement.getLevel()))); - case PERMISSION -> permissionText.append(this.user.getTranslationOrNothing(reference + "permission", + case PERMISSION -> permissionText.append(this.user.getTranslationOrNothing(REFERENCE + "permission", PERMISSION, requirement.getPermission())); case COOLDOWN -> { // do nothing @@ -410,6 +415,29 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry getMaterialName(user, m)) + .map(string -> user.getTranslation(REFERENCE + "blocks", TextVariables.NAME, string)) + .collect(Collectors.joining()); + // Removing the last newline character or comma if it exists + blocksText = blocksText.trim(); + if (blocksText.endsWith("\n") || blocksText.endsWith(",")) { + blocksText = blocksText.substring(0, blocksText.length() - 1); + } + // Insert newlines every x characters + int wrapAt = 50; // Set default value + try { + // Attempt to parse the value from getTranslation + wrapAt = Integer.valueOf(user.getTranslation(REFERENCE + "wrap-at")); + + } catch (NumberFormatException e) { + // If parsing fails, keep default value of 40 + addon.logError("Warning: Unable to parse 'wrap-at' value, using default of 50."); + } + + String formattedText = insertNewlines(blocksText, wrapAt); + if (template.description() != null) { String biomeText = phase.getPhaseBiome() == null ? "" : LangUtilsHook.getBiomeName(phase.getPhaseBiome(), this.user); @@ -420,23 +448,24 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry LangUtilsHook.getMaterialName(m, user)) + .orElse(Util.prettifyText(m.name())); + } + + private static String insertNewlines(String input, int interval) { + StringBuilder result = new StringBuilder(input.length()); + int index = 0; + char activeColor = 'a'; + int lastAmpIndex = -2; + + while (index < input.length()) { + if (input.charAt(index) == ChatColor.COLOR_CHAR && index < (input.length() - 1)) { + lastAmpIndex = index; + activeColor = input.charAt(index + 1); + } + if (input.length() < index + interval) { + result.append(input.substring(index)); + break; + } + + // Find the space near the interval to break the line without cutting a word + int breakPoint = input.lastIndexOf(' ', index + interval); + if (breakPoint <= index) { + breakPoint = index + interval; // In case there are no spaces, break at exact interval + } + + result.append(input.substring(index, breakPoint)).append('\n'); + if (lastAmpIndex >= 0) { + // Append color code + result.append(ChatColor.COLOR_CHAR); + result.append(activeColor); + result.append(" "); + } + index = breakPoint + 1; // Move past the last space + } + + return result.toString(); + } + /** * This method checks if phase requirements fails. diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index fdcf508..4627dd7 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -64,8 +64,6 @@ aoneblock: cooldown: "&c Next phase will be available in [number] seconds!" placeholders: infinite: Infinite - block-list-format: | - &7- &e [name] gui: titles: phases: '&0&l OneBlock Phases' @@ -91,6 +89,7 @@ aoneblock: [economy] [level] [permission] + [blocks] # Replaces text with [starting-block] starting-block: "&7 Starts after breaking &e [number] blocks." # Replaces text with [biome] @@ -103,6 +102,10 @@ aoneblock: level: "&7 Requires &e [number] &7 island level." # Replaces text with [permission] permission: "&7 Requires `&e[permission]&7` permission." + # Replaces text with [blocks] + blocks-prefix: '&7 Blocks in phase - ' + blocks: '&e [name], ' + wrap-at: '50' tips: click-to-previous: "&e Click &7 to view previous page." click-to-next: "&e Click &7 to view next page."