Skip to content

Commit 24888d1

Browse files
committed
Fix potion names Fixes #5211 Fixes #5180
1 parent 2f624e3 commit 24888d1

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/main/java/com/gmail/nossr50/config/skills/alchemy/PotionConfig.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.gmail.nossr50.config.skills.alchemy;
22

3-
import static com.gmail.nossr50.util.ItemUtils.setItemName;
3+
import static com.gmail.nossr50.util.ItemUtils.customName;
44
import static com.gmail.nossr50.util.PotionUtil.matchPotionType;
55
import static com.gmail.nossr50.util.PotionUtil.setBasePotionType;
66
import static com.gmail.nossr50.util.PotionUtil.setUpgradedAndExtendedProperties;
@@ -15,6 +15,10 @@
1515
import java.util.HashMap;
1616
import java.util.List;
1717
import java.util.Map;
18+
import net.kyori.adventure.text.Component;
19+
import net.kyori.adventure.text.TextComponent;
20+
import net.kyori.adventure.text.format.NamedTextColor;
21+
import net.kyori.adventure.text.format.TextDecoration;
1822
import org.bukkit.ChatColor;
1923
import org.bukkit.Color;
2024
import org.bukkit.Material;
@@ -369,7 +373,9 @@ private void setPotionDisplayName(ConfigurationSection section, PotionMeta potio
369373

370374
final String configuredName = section.getString("Name", null);
371375
if (configuredName != null) {
372-
setItemName(potionMeta, configuredName);
376+
final TextComponent textComponent = Component.text(configuredName)
377+
.decoration(TextDecoration.ITALIC, false);
378+
customName(potionMeta, textComponent, configuredName);
373379
}
374380
}
375381

src/main/java/com/gmail/nossr50/util/ItemUtils.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.Locale;
2020
import java.util.function.Predicate;
21+
import net.kyori.adventure.text.Component;
2122
import org.bukkit.ChatColor;
2223
import org.bukkit.Location;
2324
import org.bukkit.Material;
@@ -35,20 +36,20 @@
3536
import org.jetbrains.annotations.Nullable;
3637

3738
public final class ItemUtils {
38-
// Reflection for setItemName only available in newer APIs
39-
private static final Method setItemName;
39+
// Use custom name if available
40+
private static final Method customName;
4041

4142
static {
42-
setItemName = getSetItemName();
43+
customName = getCustomNameMethod();
4344
}
4445

4546
private ItemUtils() {
4647
// private constructor
4748
}
4849

49-
private static Method getSetItemName() {
50+
private static Method getCustomNameMethod() {
5051
try {
51-
return ItemMeta.class.getMethod("setItemName", String.class);
52+
return ItemMeta.class.getMethod("customName", Component.class);
5253
} catch (NoSuchMethodException e) {
5354
return null;
5455
}
@@ -60,17 +61,17 @@ private static Method getSetItemName() {
6061
* @param itemMeta The item meta to set the name on
6162
* @param name The name to set
6263
*/
63-
public static void setItemName(ItemMeta itemMeta, String name) {
64-
if (setItemName != null) {
64+
public static void customName(ItemMeta itemMeta, Component name, String fallbackName) {
65+
if (customName != null) {
6566
setItemNameModern(itemMeta, name);
6667
} else {
67-
itemMeta.setDisplayName(ChatColor.RESET + name);
68+
itemMeta.setDisplayName(ChatColor.RESET + fallbackName);
6869
}
6970
}
7071

71-
private static void setItemNameModern(ItemMeta itemMeta, String name) {
72+
private static void setItemNameModern(ItemMeta itemMeta, Component name) {
7273
try {
73-
setItemName.invoke(itemMeta, name);
74+
customName.invoke(itemMeta, name);
7475
} catch (IllegalAccessException | InvocationTargetException e) {
7576
mcMMO.p.getLogger().severe("Failed to set item name: " + e.getMessage());
7677
throw new RuntimeException(e);

0 commit comments

Comments
 (0)