1010import java .util .UUID ;
1111import java .util .function .BiConsumer ;
1212
13+ import org .bukkit .Bukkit ;
1314import org .bukkit .Material ;
14- import org .bukkit .craftbukkit .v1_21_R7 .inventory .CraftItemStack ;
1515import org .bukkit .inventory .ItemStack ;
1616import org .bukkit .inventory .meta .SkullMeta ;
1717
@@ -57,6 +57,9 @@ public class ZipNmsManager implements NmsManager {
5757
5858 private static final BiConsumer <SkullMeta , GameProfile > SET_PROFILE ;
5959
60+ private static final Method CRAFT_ITEM_STACK_AS_NMS_COPY ;
61+ private static final Method CRAFT_ITEM_STACK_AS_CRAFT_MIRROR ;
62+
6063 static {
6164 BiConsumer <SkullMeta , GameProfile > setProfile = (meta , profile ) -> {
6265 throw new NullPointerException ("Unable to find 'setProfile' method!" );
@@ -89,6 +92,49 @@ public class ZipNmsManager implements NmsManager {
8992 }
9093
9194 SET_PROFILE = setProfile ;
95+
96+ String craftItemStackClass = Bukkit .getServer ().getClass ().getPackageName () + ".inventory.CraftItemStack" ;
97+ // CraftItemStack.asNMSCopy(item)
98+ try {
99+ Class <?> craftItemStack = Class .forName (craftItemStackClass );
100+ Method method = ReflectionUtil .searchMethod (
101+ craftItemStack ,
102+ net .minecraft .world .item .ItemStack .class ,
103+ ItemStack .class );
104+ method .setAccessible (true );
105+ CRAFT_ITEM_STACK_AS_NMS_COPY = method ;
106+ } catch (ClassNotFoundException e ) {
107+ throw new IllegalStateException (e );
108+ }
109+
110+ // CraftItemStack.asCraftMirror(item)
111+ try {
112+ Class <?> craftItemStack = Class .forName (craftItemStackClass );
113+ Method method = ReflectionUtil .searchMethod (
114+ craftItemStack ,
115+ craftItemStack ,
116+ net .minecraft .world .item .ItemStack .class );
117+ method .setAccessible (true );
118+ CRAFT_ITEM_STACK_AS_CRAFT_MIRROR = method ;
119+ } catch (ClassNotFoundException e ) {
120+ throw new IllegalStateException (e );
121+ }
122+ }
123+
124+ private static net .minecraft .world .item .ItemStack asNmsCopy (ItemStack item ) {
125+ try {
126+ return (net .minecraft .world .item .ItemStack ) CRAFT_ITEM_STACK_AS_NMS_COPY .invoke (null , item );
127+ } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) {
128+ throw new IllegalStateException (e );
129+ }
130+ }
131+
132+ private static ItemStack asCraftMirror (net .minecraft .world .item .ItemStack item ) {
133+ try {
134+ return (ItemStack ) CRAFT_ITEM_STACK_AS_CRAFT_MIRROR .invoke (null , item );
135+ } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) {
136+ throw new IllegalStateException (e );
137+ }
92138 }
93139
94140 @ Override
@@ -99,7 +145,7 @@ public JsonObject itemstackToJsonElement(ItemStack[] items) {
99145 if (item == null || item .getType () == Material .AIR ) {
100146 continue ;
101147 }
102- net .minecraft .world .item .ItemStack minecraftItem = CraftItemStack . asNMSCopy (item );
148+ net .minecraft .world .item .ItemStack minecraftItem = asNmsCopy (item );
103149
104150 DataResult <JsonElement > result = net .minecraft .world .item .ItemStack .CODEC .encodeStart (DYNAMIC_OPS_JSON , minecraftItem );
105151 JsonObject resultJson = result .getOrThrow ().getAsJsonObject ();
@@ -138,7 +184,7 @@ public ItemStackContainerResult jsonElementToItemStack(JsonObject json) {
138184 .parse (DYNAMIC_OPS_JSON , dynamicItemFixed .getValue ())
139185 .getOrThrow ();
140186
141- ItemStack bukkitItem = CraftItemStack . asCraftMirror (minecraftItem );
187+ ItemStack bukkitItem = asCraftMirror (minecraftItem );
142188 int slot = item .getAsJsonObject ().get (BPConstants .KEY_INVENTORY_SLOT ).getAsInt ();
143189
144190 items .add (new ItemStackWithSlot (slot , bukkitItem ));
0 commit comments