Browse Source

Fix potion names Fixes #5211 Fixes #5180

nossr50 1 week ago
parent
commit
24888d13c2

+ 8 - 2
src/main/java/com/gmail/nossr50/config/skills/alchemy/PotionConfig.java

@@ -1,6 +1,6 @@
 package com.gmail.nossr50.config.skills.alchemy;
 
-import static com.gmail.nossr50.util.ItemUtils.setItemName;
+import static com.gmail.nossr50.util.ItemUtils.customName;
 import static com.gmail.nossr50.util.PotionUtil.matchPotionType;
 import static com.gmail.nossr50.util.PotionUtil.setBasePotionType;
 import static com.gmail.nossr50.util.PotionUtil.setUpgradedAndExtendedProperties;
@@ -15,6 +15,10 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.TextComponent;
+import net.kyori.adventure.text.format.NamedTextColor;
+import net.kyori.adventure.text.format.TextDecoration;
 import org.bukkit.ChatColor;
 import org.bukkit.Color;
 import org.bukkit.Material;
@@ -369,7 +373,9 @@ public class PotionConfig extends LegacyConfigLoader {
 
         final String configuredName = section.getString("Name", null);
         if (configuredName != null) {
-            setItemName(potionMeta, configuredName);
+            final TextComponent textComponent = Component.text(configuredName)
+                    .decoration(TextDecoration.ITALIC, false);
+            customName(potionMeta, textComponent, configuredName);
         }
     }
 

+ 11 - 10
src/main/java/com/gmail/nossr50/util/ItemUtils.java

@@ -18,6 +18,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
 import java.util.function.Predicate;
+import net.kyori.adventure.text.Component;
 import org.bukkit.ChatColor;
 import org.bukkit.Location;
 import org.bukkit.Material;
@@ -35,20 +36,20 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 public final class ItemUtils {
-    // Reflection for setItemName only available in newer APIs
-    private static final Method setItemName;
+    // Use custom name if available
+    private static final Method customName;
 
     static {
-        setItemName = getSetItemName();
+        customName = getCustomNameMethod();
     }
 
     private ItemUtils() {
         // private constructor
     }
 
-    private static Method getSetItemName() {
+    private static Method getCustomNameMethod() {
         try {
-            return ItemMeta.class.getMethod("setItemName", String.class);
+            return ItemMeta.class.getMethod("customName", Component.class);
         } catch (NoSuchMethodException e) {
             return null;
         }
@@ -60,17 +61,17 @@ public final class ItemUtils {
      * @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) {
+    public static void customName(ItemMeta itemMeta, Component name, String fallbackName) {
+        if (customName != null) {
             setItemNameModern(itemMeta, name);
         } else {
-            itemMeta.setDisplayName(ChatColor.RESET + name);
+            itemMeta.setDisplayName(ChatColor.RESET + fallbackName);
         }
     }
 
-    private static void setItemNameModern(ItemMeta itemMeta, String name) {
+    private static void setItemNameModern(ItemMeta itemMeta, Component name) {
         try {
-            setItemName.invoke(itemMeta, name);
+            customName.invoke(itemMeta, name);
         } catch (IllegalAccessException | InvocationTargetException e) {
             mcMMO.p.getLogger().severe("Failed to set item name: " + e.getMessage());
             throw new RuntimeException(e);