Adds some code for displaying an item's cost
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
2025-08-04 16:55:38 +02:00
parent ad5066af4b
commit 1d17239247
4 changed files with 85 additions and 76 deletions

View File

@@ -2,6 +2,8 @@ package net.knarcraft.blacksmith.container;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
@@ -68,20 +70,46 @@ public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack i
return new ActionCost(this.monetaryCost, this.expCost, this.itemCost, requiredPermissions);
}
/**
* Displays this action cost as a string
*
* @param player <p>The player to calculate the cost for</p>
* @return <p>The string representation of this action cost</p>
*/
@NotNull
public String displayCost() {
public String displayCost(@NotNull Player player) {
StringBuilder builder = new StringBuilder();
if (monetaryCost > 0) {
builder.append(EconomyManager.format(monetaryCost)).append(", ");
builder.append(EconomyManager.format(monetaryCost)).append(", ").append("\n");
}
if (expCost > 0) {
builder.append(expCost).append("exp, ");
builder.append(expCost).append("exp, ").append("\n");
}
if (itemCost != null) {
// TODO: Present name, amount and name + lore
builder.append(itemCost);
NamespacedKey itemName = itemCost.getType().getKeyOrNull();
if (itemName != null) {
builder.append(itemCost.getAmount()).append(" x ").append(itemName);
ItemMeta itemMeta = itemCost.getItemMeta();
if (itemMeta != null && itemMeta.hasDisplayName()) {
builder.append("(").append(itemMeta.getDisplayName()).append(")");
}
if (itemMeta != null && itemMeta.hasLore() && itemMeta.getLore() != null) {
for (String lore : itemMeta.getLore()) {
builder.append("\n").append(lore);
}
}
builder.append("\n");
}
}
if (!requiredPermissions().isEmpty()) {
for (String permission : requiredPermissions()) {
if (player.hasPermission(permission)) {
builder.append(ChatColor.DARK_GREEN).append("O ").append(permission).append("\n");
} else {
builder.append(ChatColor.DARK_RED).append("X ").append(permission).append("\n");
}
}
}
// TODO: Display required permissions if the player doesn't have them?
return builder.toString();
}