From c60fb613d4b52fe953e9c93dbcf8935b1ff35b32 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 5 Mar 2024 18:09:18 +0100 Subject: [PATCH] Add NMS access to raw display name of items --- .../lib/paper/reflect/NMSTypeWrapper.java | 2 +- .../paper/reflect/PandalibPaperReflect.java | 2 ++ .../wrapper/craftbukkit/CraftMetaItem.java | 28 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftMetaItem.java diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSTypeWrapper.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSTypeWrapper.java index 4148045..2928b92 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSTypeWrapper.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSTypeWrapper.java @@ -107,7 +107,7 @@ import fr.pandacube.lib.paper.reflect.NMSReflect.ClassMapping; String classToPrint = type; String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1); String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'"); - if (!htmlTitle.equals("")) { + if (!htmlTitle.isEmpty()) { typeHTML = "" + classSimpleName + ""; } else { diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java index 9d94c3f..4a27649 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java @@ -3,6 +3,7 @@ package fr.pandacube.lib.paper.reflect; import fr.pandacube.lib.paper.reflect.wrapper.brigadier.CommandNode; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftItemStack; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftMapView; +import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftMetaItem; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftNamespacedKey; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftPlayer; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer; @@ -111,6 +112,7 @@ public class PandalibPaperReflect { // craftbukkit thAcc.catchThrowable(() -> initWrapper(CraftItemStack.class, CraftItemStack.REFLECT.get())); thAcc.catchThrowable(() -> initWrapper(CraftMapView.class, CraftMapView.REFLECT.get())); + thAcc.catchThrowable(() -> initWrapper(CraftMetaItem.class, CraftMetaItem.REFLECT.get())); thAcc.catchThrowable(() -> initWrapper(CraftNamespacedKey.class, CraftNamespacedKey.REFLECT.get())); thAcc.catchThrowable(() -> initWrapper(CraftPlayer.class, CraftPlayer.REFLECT.get())); thAcc.catchThrowable(() -> initWrapper(CraftServer.class, CraftServer.REFLECT.get())); diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftMetaItem.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftMetaItem.java new file mode 100644 index 0000000..5d0fe0f --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftMetaItem.java @@ -0,0 +1,28 @@ +package fr.pandacube.lib.paper.reflect.wrapper.craftbukkit; + +import fr.pandacube.lib.paper.reflect.OBCReflect; +import fr.pandacube.lib.reflect.ReflectClass; +import fr.pandacube.lib.reflect.ReflectField; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped; +import org.bukkit.inventory.meta.ItemMeta; + +import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; +import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; + +public class CraftMetaItem extends ReflectWrapperTyped { + public static final ReflectClass REFLECT = wrapEx(() -> OBCReflect.ofClass("inventory.CraftMetaItem")); + public static final ReflectField displayName = wrapEx(() -> REFLECT.field("displayName")); + + public String getRawDisplayName() { + return (String) wrapReflectEx(() -> displayName.getValue(__getRuntimeInstance())); + } + + public void setRawDisplayName(String rawDisplayName) { + wrapReflectEx(() -> displayName.setValue(__getRuntimeInstance(), rawDisplayName)); + } + + + protected CraftMetaItem(Object obj) { + super(obj); + } +}