diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index 89a5bb6..5b1a553 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -15,6 +15,7 @@ import com.extendedclip.deluxemenus.requirement.*; import com.extendedclip.deluxemenus.requirement.wrappers.ItemWrapper; import com.extendedclip.deluxemenus.utils.DebugLevel; +import com.extendedclip.deluxemenus.utils.ItemUtils; import com.extendedclip.deluxemenus.utils.LocationUtils; import com.extendedclip.deluxemenus.utils.VersionHelper; @@ -30,6 +31,7 @@ import java.util.stream.Collectors; import com.google.common.base.Enums; +import com.google.common.primitives.Ints; import org.bukkit.DyeColor; import org.bukkit.Location; import org.bukkit.Material; @@ -801,30 +803,7 @@ private Map> loadMenuItems(FileConfiguration builder.itemFlags(itemFlags); } - if (c.contains(currentPath + "data")) { - if (c.isInt(currentPath + "data")) { - builder.data((short) c.getInt(currentPath + "data")); - } else { - String dataString = c.getString(currentPath + "data", ""); - if (dataString.startsWith("placeholder-")) { - String[] parts = dataString.split("-", 2); - if (parts.length < 2) { - DeluxeMenus.debug( - DebugLevel.HIGHEST, - Level.WARNING, - "Placeholder for data in item: " + key + " in menu " + name + " is not the valid format!", - "Valid format: placeholder-", - "Skipping item: " + key - ); - continue; - } - - if (containsPlaceholders(parts[1])) { - builder.placeholderData(parts[1]); - } - } - } - } + addDamageOptionToBuilder(c, currentPath, key, name, builder); if (VersionHelper.HAS_ARMOR_TRIMS) { builder.trimMaterial(c.getString(currentPath + "trim_material", null)); @@ -1565,4 +1544,72 @@ public void debug(String... messages) { public File getMenuDirector() { return menuDirectory; } + + public void addDamageOptionToBuilder(FileConfiguration c, String currentPath, String itemKey, String menuName, + MenuItemOptions.MenuItemOptionsBuilder builder) { + boolean damageOptionIsPresent = false; + String damageValue = null; + + String key = "damage"; + if (c.contains(currentPath + key)) { + damageOptionIsPresent = true; + damageValue = c.getString(currentPath + key, ""); + } + + key = "data"; + if (c.contains(currentPath + key)) { + if (!damageOptionIsPresent) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Found 'data' option for item: " + itemKey + " in menu: " + menuName+ ". This option " + + "is deprecated and will be removed soon. Please use 'damage' instead." + ); + damageValue = c.getString(currentPath + key, ""); + } else { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Found 'data' and 'damage' options for item: " + itemKey + " in menu: " + menuName + + ". 'data' option is deprecated and will be ignored. Using 'damage' instead." + ); + } + } + + if (damageValue == null) { + return; + } + + if (damageOptionIsPresent) { + key = "damage"; + } + + if (!ItemUtils.isPlaceholderOption(damageValue) && Ints.tryParse(damageValue) == null) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Found invalid value for '" + key + "' option for item: " + itemKey + " in menu: " + + menuName + ".", + "The correct formats for '" + key + "' are:", + " -> ", + " -> placeholder-", + "Ignoring the invalid value." + ); + return; + } + + final String[] parts = damageValue.split("-", 2); + if (parts.length >= 2 && !containsPlaceholders(parts[1])) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Could not find placeholder for '" + key + "' option for item: " + itemKey + " in menu: " + + menuName + ".", + "Ignoring the invalid value." + ); + return; + } + + builder.damage(parts.length == 1 ? parts[0] : parts[1]); + } } diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java b/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java index 2c59931..be8224f 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java @@ -25,6 +25,7 @@ import org.bukkit.inventory.meta.BannerMeta; import org.bukkit.inventory.meta.BlockDataMeta; import org.bukkit.inventory.meta.BlockStateMeta; +import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.FireworkEffectMeta; import org.bukkit.inventory.meta.ItemMeta; @@ -66,7 +67,7 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { String stringMaterial = this.options.material(); String lowercaseStringMaterial = stringMaterial.toLowerCase(Locale.ROOT); - if (ItemUtils.isPlaceholderMaterial(lowercaseStringMaterial)) { + if (ItemUtils.isPlaceholderOption(lowercaseStringMaterial)) { stringMaterial = holder.setPlaceholdersAndArguments(stringMaterial.substring(PLACEHOLDER_PREFIX.length())); lowercaseStringMaterial = stringMaterial.toLowerCase(Locale.ENGLISH); } @@ -173,24 +174,25 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { return itemStack; } - short data = this.options.data(); - - if (this.options.placeholderData().isPresent()) { - final String parsedData = holder.setPlaceholdersAndArguments(this.options.placeholderData().get()); + if (this.options.damage().isPresent()) { + final String parsedDamage = holder.setPlaceholdersAndArguments(this.options.damage().get()); try { - data = Short.parseShort(parsedData); + int damage = Integer.parseInt(parsedDamage); + if (damage > 0) { + final ItemMeta meta = itemStack.getItemMeta(); + if (meta instanceof Damageable) { + ((Damageable) meta).setDamage(damage); + itemStack.setItemMeta(meta); + } + } } catch (final NumberFormatException exception) { DeluxeMenus.printStacktrace( - "Invalid placeholder data found: " + parsedData + ".", + "Invalid damage found: " + parsedDamage + ".", exception ); } } - if (data > 0) { - itemStack.setDurability(data); - } - if (this.options.amount() != -1) { amount = this.options.amount(); } diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java index e4707fb..3c0400e 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuItemOptions.java @@ -23,7 +23,7 @@ public class MenuItemOptions { private final String material; - private final short data; + private final String damage; private final int amount; private final String customModelData; private final String dynamicAmount; @@ -32,7 +32,6 @@ public class MenuItemOptions { private final List lore; private final DyeColor baseColor; private HeadType headType; - private final String placeholderData; private final String rgb; private final String trimMaterial; @@ -76,7 +75,7 @@ public class MenuItemOptions { private MenuItemOptions(final @NotNull MenuItemOptionsBuilder builder) { this.material = builder.material; - this.data = builder.data; + this.damage = builder.damage; this.amount = builder.amount; this.customModelData = builder.customModelData; this.dynamicAmount = builder.dynamicAmount; @@ -87,7 +86,6 @@ private MenuItemOptions(final @NotNull MenuItemOptionsBuilder builder) { this.loreAppendMode = builder.loreAppendMode; this.baseColor = builder.baseColor; this.headType = builder.headType; - this.placeholderData = builder.placeholderData; this.rgb = builder.rgb; this.trimMaterial = builder.trimMaterial; this.trimPattern = builder.trimPattern; @@ -128,8 +126,8 @@ private MenuItemOptions(final @NotNull MenuItemOptionsBuilder builder) { return material; } - public short data() { - return data; + public @NotNull Optional damage() { + return Optional.ofNullable(damage); } public int amount() { @@ -168,10 +166,6 @@ public void headType(final @Nullable HeadType headType) { return Optional.ofNullable(headType); } - public @NotNull Optional placeholderData() { - return Optional.ofNullable(placeholderData); - } - public @NotNull Optional rgb() { return Optional.ofNullable(rgb); } @@ -303,7 +297,7 @@ public boolean updatePlaceholders() { public @NotNull MenuItemOptionsBuilder asBuilder() { return MenuItemOptions.builder() .material(this.material) - .data(this.data) + .damage(this.damage) .amount(this.amount) .customModelData(this.customModelData) .dynamicAmount(this.dynamicAmount) @@ -314,7 +308,6 @@ public boolean updatePlaceholders() { .loreAppendMode(this.loreAppendMode) .baseColor(this.baseColor) .headType(this.headType) - .placeholderData(this.placeholderData) .rgb(this.rgb) .trimMaterial(this.trimMaterial) .trimPattern(this.trimPattern) @@ -348,7 +341,7 @@ public boolean updatePlaceholders() { public static class MenuItemOptionsBuilder { private String material; - private short data; + private String damage; private int amount; private String customModelData; private String dynamicAmount; @@ -357,7 +350,6 @@ public static class MenuItemOptionsBuilder { private List lore = Collections.emptyList(); private DyeColor baseColor; private HeadType headType; - private String placeholderData; private String rgb; private String trimMaterial; @@ -407,8 +399,8 @@ public MenuItemOptionsBuilder material(final @NotNull String configMaterial) { return this; } - public MenuItemOptionsBuilder data(final short configData) { - this.data = configData; + public MenuItemOptionsBuilder damage(final @Nullable String configDamage) { + this.damage = configDamage; return this; } @@ -456,11 +448,6 @@ public MenuItemOptionsBuilder headType(final @Nullable HeadType headType) { return this; } - public MenuItemOptionsBuilder placeholderData(final @Nullable String placeholderData) { - this.placeholderData = placeholderData; - return this; - } - public MenuItemOptionsBuilder rgb(final @Nullable String rgb) { this.rgb = rgb; return this; diff --git a/src/main/java/com/extendedclip/deluxemenus/utils/ItemUtils.java b/src/main/java/com/extendedclip/deluxemenus/utils/ItemUtils.java index dd5a8b5..5c40429 100644 --- a/src/main/java/com/extendedclip/deluxemenus/utils/ItemUtils.java +++ b/src/main/java/com/extendedclip/deluxemenus/utils/ItemUtils.java @@ -7,6 +7,8 @@ import org.bukkit.potion.PotionType; import org.jetbrains.annotations.NotNull; +import java.util.Locale; + import static com.extendedclip.deluxemenus.utils.Constants.INVENTORY_ITEM_ACCESSORS; import static com.extendedclip.deluxemenus.utils.Constants.PLACEHOLDER_PREFIX; import static com.extendedclip.deluxemenus.utils.Constants.WATER_BOTTLE; @@ -18,13 +20,13 @@ private ItemUtils() { } /** - * Checks if the string starts with the substring "placeholder-". The check is case-sensitive. + * Checks if the string starts with the substring "placeholder-". The check is case-insensitive. * * @param material The string to check * @return true if the string starts with "placeholder-", false otherwise */ - public static boolean isPlaceholderMaterial(@NotNull final String material) { - return material.startsWith(PLACEHOLDER_PREFIX); + public static boolean isPlaceholderOption(@NotNull final String material) { + return material.toLowerCase(Locale.ROOT).startsWith(PLACEHOLDER_PREFIX); } /**