Skip to content
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
<build.version>1.5.0</build.version>
<build.version>1.5.1</build.version>
<build.number>-LOCAL</build.number>
<!-- Sonar Cloud -->
<sonar.projectKey>BentoBoxWorld_Challenges</sonar.projectKey>
Expand Down
61 changes: 31 additions & 30 deletions src/main/java/world/bentobox/challenges/panel/CommonPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,69 +120,70 @@ public static void reopen(CommonPanel panel) {
* @return List of strings that will be used in challenges description.
*/
protected List<String> generateChallengeDescription(Challenge challenge, @Nullable User target) {
// Some values to avoid over checking.
final boolean isCompletedOnce = target != null
// Determine if the challenge has been completed at least once
boolean isCompletedOnce = target != null
&& this.manager.isChallengeComplete(target.getUniqueId(), this.world, challenge);

final long doneTimes = target != null && challenge.isRepeatable()
// Calculate how many times the challenge has been completed
long doneTimes = (target != null && challenge.isRepeatable())
? this.manager.getChallengeTimes(target, this.world, challenge)
: (isCompletedOnce ? 0 : 1);

// Determine if the challenge has been fully completed (non-repeatable or reached max times)
boolean isCompletedAll = isCompletedOnce
&& (!challenge.isRepeatable() || challenge.getMaxTimes() > 0 && doneTimes >= challenge.getMaxTimes());
&& (!challenge.isRepeatable() || (challenge.getMaxTimes() > 0 && doneTimes >= challenge.getMaxTimes()));

final String reference = Constants.DESCRIPTIONS + "challenge.";
// Build a reference key for translation lookups
final String referenceKey = Constants.DESCRIPTIONS + "challenge.";

// Get description from custom translations
// Fetch a custom description translation; if empty, fallback to challenge's own description
String description = this.user
.getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".description");

if (description.isEmpty()) {
// Get data from object in single string.
// Combine the challenge description list into a single string and translate color codes
description = Util.translateColorCodes(String.join("\n", challenge.getDescription()));
}
// Replace any [label] placeholder with the actual top label
description = description.replace("[label]", this.topLabel);

// Non-memory optimal code used for easier debugging and nicer code layout for
// my eye :)
// Get status in single string
// Generate dynamic sections of the challenge lore
String status = this.generateChallengeStatus(isCompletedOnce, isCompletedAll, doneTimes,
challenge.getMaxTimes());
// Get requirements in single string
String requirements = isCompletedAll ? "" : this.generateRequirements(challenge, target);
// Get rewards in single string
String rewards = isCompletedAll ? "" : this.generateRewards(challenge, isCompletedOnce);
// Get coolDown in single string
String coolDown = isCompletedAll || challenge.getTimeout() <= 0 ? "" : this.generateCoolDown(challenge, target);
String coolDown = (isCompletedAll || challenge.getTimeout() <= 0) ? ""
: this.generateCoolDown(challenge, target);

String returnString;
// Check if the description (after removing blank lines) is not empty
if (!description.replaceAll("(?m)^[ \\t]*\\r?\\n", "").isEmpty()) {
String returnString = this.user.getTranslationOrNothing(reference + "lore", "[requirements]", requirements,
// Retrieve the lore translation without the description placeholder
returnString = this.user.getTranslationOrNothing(referenceKey + "lore", "[requirements]", requirements,
"[rewards]", rewards, "[status]", status, "[cooldown]", coolDown);

// remove empty lines from the generated text.
List<String> collect = Arrays.stream(returnString.replaceAll("(?m)^[ \\t]*\\r?\\n", "").split("\n"))
.collect(Collectors.toList());
// Remove any empty lines from the translated text and split it into individual lines
final String finalDescription = description; // ensure it's effectively final

// find and replace description from collected blocks.

for (int i = 0; i < collect.size(); i++) {
if (collect.get(i).contains(Constants.PARAMETER_DESCRIPTION)) {
collect.set(i, collect.get(i).replace(Constants.PARAMETER_DESCRIPTION, description));
}
}
List<String> lines = Arrays.stream(returnString.replaceAll("(?m)^[ \\t]*\\r?\\n", "").split("\n"))
.map(line -> line.contains(Constants.PARAMETER_DESCRIPTION)
? line.replace(Constants.PARAMETER_DESCRIPTION, finalDescription)
: line)
.collect(Collectors.toList());

return collect;
return lines;
} else {
String returnString = this.user.getTranslationOrNothing(reference + "lore", Constants.PARAMETER_DESCRIPTION,
// If description is empty, pass it directly as a parameter to the translation
returnString = this.user.getTranslationOrNothing(referenceKey + "lore", Constants.PARAMETER_DESCRIPTION,
description, "[requirements]", requirements, "[rewards]", rewards, "[status]", status, "[cooldown]",
coolDown);

// Remove empty lines and returns as a list.

// Remove empty lines and return the resulting lines as a list
return Arrays.stream(returnString.replaceAll("(?m)^[ \\t]*\\r?\\n", "").split("\n"))
.collect(Collectors.toList());
}
}


/**
* Generate cool down string.
*
Expand Down
28 changes: 12 additions & 16 deletions src/main/java/world/bentobox/challenges/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -745,7 +744,7 @@ public static String prettifyObject(PotionType type, User user)
* @param user the user
* @return the string
*/
public static String prettifyObject(ItemStack item, @Nullable PotionMeta potionMeta, User user)
public static String prettifyObject(ItemStack item, @Nullable PotionMeta potionMeta, User user)
{
if (potionMeta == null)
{
Expand All @@ -757,39 +756,36 @@ public static String prettifyObject(ItemStack item, @Nullable PotionMeta potionM
final String itemReference = Constants.ITEM_STACKS + itemType.name().toLowerCase() + ".";
final String metaReference = Constants.ITEM_STACKS + "meta.";

PotionData potionData = potionMeta.getBasePotionData();
PotionType potionData = potionMeta.getBasePotionType();

// Check custom translation for potions.
String type = user.getTranslationOrNothing(itemReference + potionData.getType().name().toLowerCase());

String type = user.getTranslationOrNothing(itemReference + potionData.name().toLowerCase());
if (type.isEmpty())
{
// Check potion types translation.
type = prettifyObject(potionData.getType(), user);
type = prettifyObject(potionData, user);
}

String upgraded = user.getTranslationOrNothing(metaReference + "upgraded");
String extended = user.getTranslationOrNothing(metaReference + "extended");

// Get item specific translation.
String specific = user.getTranslationOrNothing(itemReference + "name",
"[type]", type,
"[upgraded]", (potionData.isUpgraded() ? upgraded : ""),
"[extended]", (potionData.isExtended() ? extended : ""));
"[upgraded]", "", "[extended]", "");

if (specific.isEmpty())
{
// Use generic translation.
String meta = user.getTranslationOrNothing(metaReference + "potion-meta",
"[type]", type,
"[upgraded]", (potionData.isUpgraded() ? upgraded : ""),
"[extended]", (potionData.isExtended() ? extended : ""));

"[upgraded]", "", "[extended]", "");
BentoBox.getInstance().logDebug("Generic ref: " + Constants.ITEM_STACKS + "generic");
specific = user.getTranslationOrNothing(Constants.ITEM_STACKS + "generic",
"[type]", prettifyObject(itemType, user),
"[meta]", meta);
}

if (specific.isEmpty()) {
// Last ditch
specific = prettifyObject(itemType, user) + ": " + type;
}
BentoBox.getInstance().logDebug(specific);
return specific;
}

Expand Down
Loading