Skip to content

Commit

Permalink
Merge pull request #122 from HelpChat/tweak/damage-api
Browse files Browse the repository at this point in the history
  • Loading branch information
BlitzOffline authored Aug 4, 2024
2 parents 0a3dd5c + ec2c1cb commit b7b1cc4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -801,30 +803,7 @@ private Map<Integer, TreeMap<Integer, MenuItem>> 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-<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));
Expand Down Expand Up @@ -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:",
" -> <number>",
" -> placeholder-<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]);
}
}
24 changes: 13 additions & 11 deletions src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +32,6 @@ public class MenuItemOptions {
private final List<String> lore;
private final DyeColor baseColor;
private HeadType headType;
private final String placeholderData;
private final String rgb;

private final String trimMaterial;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -128,8 +126,8 @@ private MenuItemOptions(final @NotNull MenuItemOptionsBuilder builder) {
return material;
}

public short data() {
return data;
public @NotNull Optional<String> damage() {
return Optional.ofNullable(damage);
}

public int amount() {
Expand Down Expand Up @@ -168,10 +166,6 @@ public void headType(final @Nullable HeadType headType) {
return Optional.ofNullable(headType);
}

public @NotNull Optional<String> placeholderData() {
return Optional.ofNullable(placeholderData);
}

public @NotNull Optional<String> rgb() {
return Optional.ofNullable(rgb);
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -357,7 +350,6 @@ public static class MenuItemOptionsBuilder {
private List<String> lore = Collections.emptyList();
private DyeColor baseColor;
private HeadType headType;
private String placeholderData;
private String rgb;

private String trimMaterial;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit b7b1cc4

Please sign in to comment.