Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added panel placeholders #385 #387

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
93 changes: 81 additions & 12 deletions src/main/java/world/bentobox/aoneblock/panels/PhasesPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;


Expand All @@ -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
Expand Down Expand Up @@ -349,7 +354,7 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
[level]
[permission]
*/
final String reference = "aoneblock.gui.buttons.phase.";


// Get settings for island.
PanelItemBuilder builder = new PanelItemBuilder();
Expand All @@ -375,7 +380,7 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
}
else
{
builder.name(this.user.getTranslation(reference + "name",
builder.name(this.user.getTranslation(REFERENCE + "name",
PHASE2, phase.getPhaseName()));
}

Expand All @@ -391,16 +396,16 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
phase.getRequirements().forEach(requirement -> {
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
Expand All @@ -410,6 +415,29 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
}
});

// Blocks Text
String blocksText = user.getTranslation(REFERENCE + "blocks-prefix") + phase.getBlocks().keySet().stream()
.map(m -> 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);
Expand All @@ -420,23 +448,24 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
BANK, bankText.toString(),
ECONOMY, economyText.toString(),
LEVEL, levelText.toString(),
PERMISSION, permissionText.toString());
PERMISSION, permissionText.toString(), BLOCKS, formattedText);
}
else
{
// Null description, so we make our own
String blockText = this.user.getTranslationOrNothing(reference + "starting-block",
String blockText = this.user.getTranslationOrNothing(REFERENCE + "starting-block",
TextVariables.NUMBER, phase.getBlockNumber());
String biomeText = phase.getPhaseBiome() == null ? "" : this.user.getTranslationOrNothing(reference + "biome",
BIOME, LangUtilsHook.getBiomeName(phase.getPhaseBiome(), this.user));
String biomeText = phase.getPhaseBiome() == null ? ""
: this.user.getTranslationOrNothing(REFERENCE + "biome",
BIOME, LangUtilsHook.getBiomeName(phase.getPhaseBiome(), this.user));

descriptionText = this.user.getTranslationOrNothing(reference + "description",
descriptionText = this.user.getTranslationOrNothing(REFERENCE + "description",
"[starting-block]", biomeText,
BIOME, blockText,
BANK, bankText.toString(),
ECONOMY, economyText.toString(),
LEVEL, levelText.toString(),
PERMISSION, permissionText.toString());
PERMISSION, permissionText.toString(), BLOCKS, formattedText);
}

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

private String getMaterialName(User user, Material m) {
return addon.getPlugin().getHooks().getHook("LangUtils").map(hook -> 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.
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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]
Expand All @@ -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."
Expand Down
Loading