This commit is contained in:
nossr50
2024-05-12 09:33:42 -07:00
parent cae2132d8d
commit 17052861d1
5 changed files with 67 additions and 18 deletions

View File

@ -2,7 +2,6 @@ package com.gmail.nossr50.config.skills.alchemy;
import com.gmail.nossr50.config.LegacyConfigLoader;
import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.PotionUtil;
@ -21,6 +20,7 @@ import org.jetbrains.annotations.VisibleForTesting;
import java.io.File;
import java.util.*;
import static com.gmail.nossr50.util.ItemUtils.setItemName;
import static com.gmail.nossr50.util.PotionUtil.*;
import static com.gmail.nossr50.util.text.StringUtils.convertKeyToName;
@ -257,19 +257,15 @@ public class PotionConfig extends LegacyConfigLoader {
}
private void setPotionDisplayName(ConfigurationSection section, PotionMeta potionMeta) {
String configuredName = section.getString("Name", null);
if (configuredName != null) {
potionMeta.setItemName(configuredName);
// If a potion doesn't have any custom effects, there is no reason to override the vanilla name
if (potionMeta.getCustomEffects().isEmpty()) {
return;
}
final String configuredName = section.getString("Name", null);
if (configuredName != null) {
setItemName(potionMeta, configuredName);
}
//
// // Potion is water, but has effects
// if (isPotionTypeWater(potionMeta)
// && (PotionUtil.hasBasePotionEffects(potionMeta) || !potionMeta.getCustomEffects().isEmpty())) {
// // If we don't set a name for these potions, they will simply be called "Water Potion"
// final String name = section.getName().toUpperCase().replace("_", " ");
// potionMeta.setDisplayName(name);
// System.out.println("DEBUG: Renaming potion to " + name);
// }
}
/**

View File

@ -20,6 +20,8 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
@ -32,6 +34,43 @@ public final class ItemUtils {
*/
private ItemUtils() {}
private static final Method setItemName;
static {
setItemName = getSetItemName();
}
private static Method getSetItemName() {
try {
return ItemMeta.class.getMethod("setItemName", String.class);
} catch (NoSuchMethodException e) {
return null;
}
}
/**
* Sets the item name using the new API if available
* or falls back to the old API.
* @param itemMeta The item meta to set the name on
* @param name The name to set
*/
public static void setItemName(ItemMeta itemMeta, String name) {
if (setItemName != null) {
setItemNameModern(itemMeta, name);
} else {
itemMeta.setDisplayName(ChatColor.RESET + name);
}
}
private static void setItemNameModern(ItemMeta itemMeta, String name) {
try {
setItemName.invoke(itemMeta, name);
} catch (IllegalAccessException | InvocationTargetException e) {
mcMMO.p.getLogger().severe("Failed to set item name: " + e.getMessage());
throw new RuntimeException(e);
}
}
/**
* Checks if the item is a bow.
*