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
34 changes: 2 additions & 32 deletions src/main/java/world/bentobox/level/panels/DetailsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import org.bukkit.inventory.ItemStack;

import com.google.common.base.Enums;
import com.nexomc.nexo.api.NexoItems;

import lv.id.bonne.panelutils.PanelUtils;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.TemplatedPanel;
Expand All @@ -24,8 +22,6 @@
import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.hooks.ItemsAdderHook;
import world.bentobox.bentobox.hooks.OraxenHook;
import world.bentobox.level.Level;
import world.bentobox.level.objects.IslandLevels;
import world.bentobox.level.util.Utils;
Expand Down Expand Up @@ -742,12 +738,9 @@ private BlockDataRec getBlockData(Object key) {
Utils.prettifyObject(key, this.user),
this.user.getTranslation(this.world, "level.gui.buttons.spawner.block-name"));
} else if (key instanceof String s) {
Optional<ItemStack> optItem = getCustomBlockItemStack(s);
Optional<ItemStack> optItem = Utils.getCustomBlockItemStack(addon, s);
ItemStack icon = optItem.orElse(new ItemStack(Material.PAPER));
String disp = optItem
.filter(is -> is.getItemMeta() != null && is.getItemMeta().hasDisplayName())
.map(is -> is.getItemMeta().getDisplayName())
.orElse(Utils.prettifyObject(s, this.user));
String disp = Utils.getCustomBlockDisplayName(optItem, s, this.user);

return new BlockDataRec(icon,
this.user.getTranslationOrNothing(ref + "id", "[id]", s),
Expand All @@ -758,29 +751,6 @@ private BlockDataRec getBlockData(Object key) {
return new BlockDataRec(new ItemStack(Material.PAPER), "", 0, 0, Utils.prettifyObject(key, this.user), "");
}

/**
* Returns the best available ItemStack for a custom-block string ID.
* Checks Oraxen, Nexo, and ItemsAdder in order; returns empty when none matches.
*
* @param id the custom block ID (e.g. "oraxen:my_block", "nexo:my_block", or an ItemsAdder ID)
* @return an Optional containing the representative ItemStack, or empty
*/
private Optional<ItemStack> getCustomBlockItemStack(String id) {
if (id.startsWith("oraxen:") && BentoBox.getInstance().getHooks().getHook("Oraxen").isPresent()) {
return OraxenHook.getOptionalItemById(id.substring(7))
.map(itemBuilder -> itemBuilder.build());
}
if (id.startsWith("nexo:") && addon.isNexo()) {
com.nexomc.nexo.items.ItemBuilder nexoBuilder = NexoItems.itemFromId(id.substring(5));
return nexoBuilder != null ? Optional.of(nexoBuilder.build()) : Optional.empty();
}
if (addon.isItemsAdder() && ItemsAdderHook.isInRegistry(id)) {
return ItemsAdderHook.getItemStack(id);
}
return Optional.empty();
}


// ---------------------------------------------------------------------
// Section: Other Methods
// ---------------------------------------------------------------------
Expand Down
38 changes: 26 additions & 12 deletions src/main/java/world/bentobox/level/panels/ValuePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -668,6 +669,16 @@ private PanelItem createMaterialButton(ItemTemplateRecord template, TemplatedPan
return this.createMaterialButton(template, this.elementList.get(index));
}

private static String stripCustomPrefix(String key) {
if (key.startsWith("oraxen:")) {
return key.substring(7);
}
if (key.startsWith("nexo:")) {
return key.substring(5);
}
return key;
}

private Material getIcon(String key) {
// Filter out some names
key = key.replaceAll("wall_", "");
Expand Down Expand Up @@ -773,18 +784,21 @@ private PanelItem createMaterialButton(ItemTemplateRecord template, BlockRecord
? this.user.getTranslationOrNothing(baseKey + "limit", TextVariables.NUMBER, String.valueOf(blockLimit))
: "";

// Determine icon and display material text
Material icon = getIcon(key);
builder.icon((icon == null || icon == Material.AIR) ? Material.PAPER : icon);
if (key.startsWith("oraxen:")) {
key = key.substring(7);
} else if (key.startsWith("nexo:")) {
key = key.substring(5);
}
String displayMaterial = (icon == null) ? Util.prettifyText(key) : Utils.prettifyObject(key, user);
// Special handling for spawn eggs
if (icon != null && icon.name().endsWith("_SPAWN_EGG")) {
displayMaterial = Util.prettifyText(key);
// Prefer the actual custom-block ItemStack (real texture + display name) over a fallback Material icon.
Optional<ItemStack> customStack = Utils.getCustomBlockItemStack(addon, key);
String displayMaterial;
if (customStack.isPresent()) {
builder.icon(customStack.get().clone());
displayMaterial = Utils.getCustomBlockDisplayName(customStack, stripCustomPrefix(key), user);
} else {
Material icon = getIcon(key);
builder.icon((icon == null || icon == Material.AIR) ? Material.PAPER : icon);
String stripped = stripCustomPrefix(key);
displayMaterial = (icon == null) ? Util.prettifyText(stripped) : Utils.prettifyObject(stripped, user);
// Special handling for spawn eggs
if (icon != null && icon.name().endsWith("_SPAWN_EGG")) {
displayMaterial = Util.prettifyText(stripped);
}
}

// Set button title if available
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/world/bentobox/level/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@

import java.text.NumberFormat;
import java.util.List;
import java.util.Optional;

import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachmentInfo;

import com.nexomc.nexo.api.NexoItems;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.hooks.ItemsAdderHook;
import world.bentobox.bentobox.hooks.LangUtilsHook;
import world.bentobox.bentobox.hooks.OraxenHook;
import world.bentobox.level.Level;


public class Utils
Expand Down Expand Up @@ -216,6 +224,45 @@
return "";
}

/**
* Returns the best available ItemStack for a custom-block string ID.
* Checks Oraxen, Nexo, and ItemsAdder in order; returns empty when none matches.
*
* @param addon the Level addon
* @param id the custom block ID (e.g. "oraxen:my_block", "nexo:my_block", or an ItemsAdder ID)
* @return an Optional containing the representative ItemStack, or empty
*/
public static Optional<ItemStack> getCustomBlockItemStack(Level addon, String id) {
if (id == null) {
return Optional.empty();
}
if (id.startsWith("oraxen:") && BentoBox.getInstance().getHooks().getHook("Oraxen").isPresent()) {
return OraxenHook.getOptionalItemById(id.substring(7)).map(itemBuilder -> itemBuilder.build());

Check warning on line 240 in src/main/java/world/bentobox/level/util/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this lambda with method reference 'ItemBuilder::build'.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Level&issues=AZ4Js1QYqmm_xGZ5eFnW&open=AZ4Js1QYqmm_xGZ5eFnW&pullRequest=427
}
if (id.startsWith("nexo:") && addon.isNexo()) {
com.nexomc.nexo.items.ItemBuilder nexoBuilder = NexoItems.itemFromId(id.substring(5));
return nexoBuilder != null ? Optional.of(nexoBuilder.build()) : Optional.empty();
}
if (addon.isItemsAdder() && ItemsAdderHook.isInRegistry(id)) {
return ItemsAdderHook.getItemStack(id);
}
return Optional.empty();
}

/**
* Returns the display name from an ItemStack's meta when present, otherwise falls back to
* {@link #prettifyObject(Object, User)} on the original key.
*
* @param itemStack the optional ItemStack (typically from a custom-block plugin)
* @param key the raw key used as a fallback for prettification
* @param user the user for translation lookups
* @return the human-readable display name
*/
public static String getCustomBlockDisplayName(Optional<ItemStack> itemStack, String key, User user) {
return itemStack.filter(is -> is.getItemMeta() != null && is.getItemMeta().hasDisplayName())
.map(is -> is.getItemMeta().getDisplayName()).orElse(prettifyObject(key, user));

Check warning on line 263 in src/main/java/world/bentobox/level/util/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "getDisplayName"; it is deprecated.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Level&issues=AZ4Js1QYqmm_xGZ5eFnX&open=AZ4Js1QYqmm_xGZ5eFnX&pullRequest=427
}

public static String prettifyDescription(Object object, User user) {
if (object instanceof String key) {
String translation = user.getTranslationOrNothing(LEVEL_MATERIALS + key + DESCRIPTION);
Expand Down
Loading