1818import java .util .List ;
1919import java .util .Locale ;
2020import java .util .function .Predicate ;
21+ import net .kyori .adventure .text .Component ;
2122import org .bukkit .ChatColor ;
2223import org .bukkit .Location ;
2324import org .bukkit .Material ;
3536import org .jetbrains .annotations .Nullable ;
3637
3738public 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