Skip to content

Commit a0a69b6

Browse files
authored
Merge pull request #387 from BentoBoxWorld/phase_block_names_placeholder
Added panel placeholders #385
2 parents f4c344a + c951ca0 commit a0a69b6

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
lines changed

src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.Material;
99

1010
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
11+
import world.bentobox.aoneblock.panels.PhasesPanel;
1112
import world.bentobox.bentobox.api.localization.TextVariables;
1213
import world.bentobox.bentobox.api.user.User;
1314
import world.bentobox.bentobox.database.objects.Island;
@@ -79,10 +80,11 @@ private String getPhaseBlocksForIsland(User user, Island i) {
7980
}
8081

8182
String result = set.stream().map(m -> getMaterialName(user, m))
82-
.map(string -> user.getTranslation("aoneblock.placeholders.block-list-format", TextVariables.NAME,
83+
.map(string -> user.getTranslation(PhasesPanel.REFERENCE + "blocks", TextVariables.NAME,
8384
string))
8485
.collect(Collectors.joining());
8586
// Removing the last newline character or comma if it exists
87+
result = result.trim();
8688
if (result.endsWith("\n") || result.endsWith(",")) {
8789
result = result.substring(0, result.length() - 1);
8890
}

src/main/java/world/bentobox/aoneblock/panels/PhasesPanel.java

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Map;
1515
import java.util.stream.Collectors;
1616

17+
import org.bukkit.ChatColor;
1718
import org.bukkit.Material;
1819
import org.bukkit.World;
1920
import org.bukkit.event.inventory.ClickType;
@@ -26,6 +27,7 @@
2627
import world.bentobox.aoneblock.oneblocks.OneBlockPhase;
2728
import world.bentobox.aoneblock.oneblocks.Requirement;
2829
import world.bentobox.bank.Bank;
30+
import world.bentobox.bentobox.BentoBox;
2931
import world.bentobox.bentobox.api.addons.Addon;
3032
import world.bentobox.bentobox.api.localization.TextVariables;
3133
import world.bentobox.bentobox.api.panels.PanelItem;
@@ -36,6 +38,7 @@
3638
import world.bentobox.bentobox.api.user.User;
3739
import world.bentobox.bentobox.database.objects.Island;
3840
import world.bentobox.bentobox.hooks.LangUtilsHook;
41+
import world.bentobox.bentobox.util.Util;
3942
import world.bentobox.level.Level;
4043

4144

@@ -55,6 +58,8 @@ public class PhasesPanel
5558
private static final String LEVEL = "[level]";
5659
private static final String PHASE2 = "[phase]";
5760
private static final String INDEXING = "indexing";
61+
private static final String BLOCKS = "[blocks]";
62+
public static final String REFERENCE = "aoneblock.gui.buttons.phase.";
5863

5964
// ---------------------------------------------------------------------
6065
// Section: Constructor
@@ -349,7 +354,7 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
349354
[level]
350355
[permission]
351356
*/
352-
final String reference = "aoneblock.gui.buttons.phase.";
357+
353358

354359
// Get settings for island.
355360
PanelItemBuilder builder = new PanelItemBuilder();
@@ -375,7 +380,7 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
375380
}
376381
else
377382
{
378-
builder.name(this.user.getTranslation(reference + "name",
383+
builder.name(this.user.getTranslation(REFERENCE + "name",
379384
PHASE2, phase.getPhaseName()));
380385
}
381386

@@ -391,16 +396,16 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
391396
phase.getRequirements().forEach(requirement -> {
392397
switch (requirement.getType())
393398
{
394-
case ECO -> economyText.append(this.user.getTranslationOrNothing(reference + "economy",
399+
case ECO -> economyText.append(this.user.getTranslationOrNothing(REFERENCE + "economy",
395400
TextVariables.NUMBER, String.valueOf(requirement.getEco())));
396401

397-
case BANK -> bankText.append(this.user.getTranslationOrNothing(reference + "bank",
402+
case BANK -> bankText.append(this.user.getTranslationOrNothing(REFERENCE + "bank",
398403
TextVariables.NUMBER, String.valueOf(requirement.getBank())));
399404

400-
case LEVEL -> levelText.append(this.user.getTranslationOrNothing(reference + "level",
405+
case LEVEL -> levelText.append(this.user.getTranslationOrNothing(REFERENCE + "level",
401406
TextVariables.NUMBER, String.valueOf(requirement.getLevel())));
402407

403-
case PERMISSION -> permissionText.append(this.user.getTranslationOrNothing(reference + "permission",
408+
case PERMISSION -> permissionText.append(this.user.getTranslationOrNothing(REFERENCE + "permission",
404409
PERMISSION, requirement.getPermission()));
405410
case COOLDOWN -> {
406411
// do nothing
@@ -410,6 +415,29 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
410415
}
411416
});
412417

418+
// Blocks Text
419+
String blocksText = user.getTranslation(REFERENCE + "blocks-prefix") + phase.getBlocks().keySet().stream()
420+
.map(m -> getMaterialName(user, m))
421+
.map(string -> user.getTranslation(REFERENCE + "blocks", TextVariables.NAME, string))
422+
.collect(Collectors.joining());
423+
// Removing the last newline character or comma if it exists
424+
blocksText = blocksText.trim();
425+
if (blocksText.endsWith("\n") || blocksText.endsWith(",")) {
426+
blocksText = blocksText.substring(0, blocksText.length() - 1);
427+
}
428+
// Insert newlines every x characters
429+
int wrapAt = 50; // Set default value
430+
try {
431+
// Attempt to parse the value from getTranslation
432+
wrapAt = Integer.valueOf(user.getTranslation(REFERENCE + "wrap-at"));
433+
434+
} catch (NumberFormatException e) {
435+
// If parsing fails, keep default value of 40
436+
addon.logError("Warning: Unable to parse 'wrap-at' value, using default of 50.");
437+
}
438+
439+
String formattedText = insertNewlines(blocksText, wrapAt);
440+
413441
if (template.description() != null)
414442
{
415443
String biomeText = phase.getPhaseBiome() == null ? "" : LangUtilsHook.getBiomeName(phase.getPhaseBiome(), this.user);
@@ -420,23 +448,24 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
420448
BANK, bankText.toString(),
421449
ECONOMY, economyText.toString(),
422450
LEVEL, levelText.toString(),
423-
PERMISSION, permissionText.toString());
451+
PERMISSION, permissionText.toString(), BLOCKS, formattedText);
424452
}
425453
else
426454
{
427455
// Null description, so we make our own
428-
String blockText = this.user.getTranslationOrNothing(reference + "starting-block",
456+
String blockText = this.user.getTranslationOrNothing(REFERENCE + "starting-block",
429457
TextVariables.NUMBER, phase.getBlockNumber());
430-
String biomeText = phase.getPhaseBiome() == null ? "" : this.user.getTranslationOrNothing(reference + "biome",
431-
BIOME, LangUtilsHook.getBiomeName(phase.getPhaseBiome(), this.user));
458+
String biomeText = phase.getPhaseBiome() == null ? ""
459+
: this.user.getTranslationOrNothing(REFERENCE + "biome",
460+
BIOME, LangUtilsHook.getBiomeName(phase.getPhaseBiome(), this.user));
432461

433-
descriptionText = this.user.getTranslationOrNothing(reference + "description",
462+
descriptionText = this.user.getTranslationOrNothing(REFERENCE + "description",
434463
"[starting-block]", biomeText,
435464
BIOME, blockText,
436465
BANK, bankText.toString(),
437466
ECONOMY, economyText.toString(),
438467
LEVEL, levelText.toString(),
439-
PERMISSION, permissionText.toString());
468+
PERMISSION, permissionText.toString(), BLOCKS, formattedText);
440469
}
441470

442471
// Strip out or replace formating
@@ -519,6 +548,46 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
519548
return builder.build();
520549
}
521550

551+
private String getMaterialName(User user, Material m) {
552+
return addon.getPlugin().getHooks().getHook("LangUtils").map(hook -> LangUtilsHook.getMaterialName(m, user))
553+
.orElse(Util.prettifyText(m.name()));
554+
}
555+
556+
private static String insertNewlines(String input, int interval) {
557+
StringBuilder result = new StringBuilder(input.length());
558+
int index = 0;
559+
char activeColor = 'a';
560+
int lastAmpIndex = -2;
561+
562+
while (index < input.length()) {
563+
if (input.charAt(index) == ChatColor.COLOR_CHAR && index < (input.length() - 1)) {
564+
lastAmpIndex = index;
565+
activeColor = input.charAt(index + 1);
566+
}
567+
if (input.length() < index + interval) {
568+
result.append(input.substring(index));
569+
break;
570+
}
571+
572+
// Find the space near the interval to break the line without cutting a word
573+
int breakPoint = input.lastIndexOf(' ', index + interval);
574+
if (breakPoint <= index) {
575+
breakPoint = index + interval; // In case there are no spaces, break at exact interval
576+
}
577+
578+
result.append(input.substring(index, breakPoint)).append('\n');
579+
if (lastAmpIndex >= 0) {
580+
// Append color code
581+
result.append(ChatColor.COLOR_CHAR);
582+
result.append(activeColor);
583+
result.append(" ");
584+
}
585+
index = breakPoint + 1; // Move past the last space
586+
}
587+
588+
return result.toString();
589+
}
590+
522591

523592
/**
524593
* This method checks if phase requirements fails.

src/main/resources/locales/en-US.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ aoneblock:
6464
cooldown: "&c Next phase will be available in [number] seconds!"
6565
placeholders:
6666
infinite: Infinite
67-
block-list-format: |
68-
&7- &e [name]
6967
gui:
7068
titles:
7169
phases: '&0&l OneBlock Phases'
@@ -91,6 +89,7 @@ aoneblock:
9189
[economy]
9290
[level]
9391
[permission]
92+
[blocks]
9493
# Replaces text with [starting-block]
9594
starting-block: "&7 Starts after breaking &e [number] blocks."
9695
# Replaces text with [biome]
@@ -103,6 +102,10 @@ aoneblock:
103102
level: "&7 Requires &e [number] &7 island level."
104103
# Replaces text with [permission]
105104
permission: "&7 Requires `&e[permission]&7` permission."
105+
# Replaces text with [blocks]
106+
blocks-prefix: '&7 Blocks in phase - '
107+
blocks: '&e [name], '
108+
wrap-at: '50'
106109
tips:
107110
click-to-previous: "&e Click &7 to view previous page."
108111
click-to-next: "&e Click &7 to view next page."

0 commit comments

Comments
 (0)