1111import java .lang .invoke .MethodHandles ;
1212import java .lang .invoke .MethodType ;
1313import java .lang .reflect .Constructor ;
14- import java .lang .reflect .InvocationTargetException ;
15- import java .lang .reflect .Method ;
16- import java .util .Arrays ;
1714import java .util .logging .Level ;
1815
1916/**
@@ -87,50 +84,59 @@ public static CompoundSession compoundSession(@Nonnull final Object handle) {
8784 * methods will fail silently or throw exceptions as appropriate.</p>
8885 */
8986 public static class NmsItemSession {
90- private static final Method asNMSCopy ;
91- private static final Method asBukkitCopy ;
92- private static final Method hasTag ;
93- private static final Method getTag ;
94- private static final Method setTag ;
87+ private static final MethodHandle NMS_ITEM_COPY ;
88+ private static final MethodHandle AS_BUKKIT_ITEM_COPY ;
89+ private static final MethodHandle HAS_TAG ;
90+ private static final MethodHandle GET_TAG ;
91+ private static final MethodHandle SET_TAG ;
9592 private static final Constructor <?> nbtTagConstructor ;
9693 private static final boolean REFLECTION_READY ;
9794
9895 private final Object nmsItemCopy ;
9996
10097 static {
10198 Constructor <?> tagConstructor = null ;
102- Method setNBTTag = null ;
103- Method getNBTTag = null ;
104- Method hasNBTTag = null ;
105- Method bukkitCopy = null ;
106- Method asNMSCopyItem = null ;
99+ MethodHandle setNBTTag = null ;
100+ MethodHandle getNBTTag = null ;
101+ MethodHandle hasNBTTag = null ;
102+ MethodHandle bukkitCopy = null ;
103+ MethodHandle nMSCopyItem = null ;
107104 boolean reflectionDone = false ;
108- try {
109- String craftPath = getCraftBukkitPath ();
110- String nmsPath = getNmsPath ();
111-
112- Class <?> craftItemStack = Class .forName (craftPath + ".inventory.CraftItemStack" );
113- Class <?> nmsItemStack = Class .forName (nmsPath + ".ItemStack" );
114- Class <?> nbtTagCompound = Class .forName (getNbtTagPath ());
115-
116- asNMSCopyItem = craftItemStack .getMethod ("asNMSCopy" , ItemStack .class );
117- bukkitCopy = craftItemStack .getMethod ("asBukkitCopy" , nmsItemStack );
118105
119- hasNBTTag = nmsItemStack .getMethod ("hasTag" );
120- getNBTTag = nmsItemStack .getMethod ("getTag" );
121- setNBTTag = nmsItemStack .getMethod ("setTag" , nbtTagCompound );
106+ try {
107+ final String craftPath = getCraftBukkitPath ();
108+ final String nmsPath = getNmsPath ();
109+
110+ final Class <?> craftItemStack = Class .forName (craftPath + ".inventory.CraftItemStack" );
111+ final Class <?> nmsItemStack = Class .forName (nmsPath + ".ItemStack" );
112+ final Class <?> nbtTagCompound = Class .forName (getNbtTagPath ());
113+ final MethodHandles .Lookup lookup = MethodHandles .lookup ();
114+
115+ nMSCopyItem = lookup .findStatic (craftItemStack , "asNMSCopy" ,
116+ MethodType .methodType (nmsItemStack , ItemStack .class ));
117+ bukkitCopy = lookup .findStatic (craftItemStack , "asBukkitCopy" ,
118+ MethodType .methodType (ItemStack .class , nmsItemStack ));
119+
120+ hasNBTTag = lookup .findVirtual (nmsItemStack , "hasTag" ,
121+ MethodType .methodType (boolean .class ));
122+ getNBTTag = lookup .findVirtual (nmsItemStack , "getTag" ,
123+ MethodType .methodType (nbtTagCompound ));
124+ setNBTTag = lookup .findVirtual (nmsItemStack , "setTag" ,
125+ MethodType .methodType (void .class , nbtTagCompound ));
122126
123127 tagConstructor = nbtTagCompound .getConstructor ();
124128 reflectionDone = true ;
125129 } catch (ClassNotFoundException | NoSuchMethodException e ) {
126130 logger .logError (e , () -> "Failed to initialize all NMS methods needed." );
131+ } catch (IllegalAccessException e ) {
132+ throw new RuntimeException (e );
127133 }
128134 nbtTagConstructor = tagConstructor ;
129- setTag = setNBTTag ;
130- getTag = getNBTTag ;
131- hasTag = hasNBTTag ;
132- asBukkitCopy = bukkitCopy ;
133- asNMSCopy = asNMSCopyItem ;
135+ SET_TAG = setNBTTag ;
136+ GET_TAG = getNBTTag ;
137+ HAS_TAG = hasNBTTag ;
138+ AS_BUKKIT_ITEM_COPY = bukkitCopy ;
139+ NMS_ITEM_COPY = nMSCopyItem ;
134140 REFLECTION_READY = reflectionDone ;
135141 }
136142
@@ -158,8 +164,8 @@ public boolean isReady() {
158164 public boolean hasTag () {
159165 if (!REFLECTION_READY || nmsItemCopy == null ) return false ;
160166 try {
161- return (boolean ) hasTag .invoke (this .nmsItemCopy );
162- } catch (IllegalAccessException | InvocationTargetException e ) {
167+ return (boolean ) HAS_TAG .invoke (this .nmsItemCopy );
168+ } catch (Throwable e ) {
163169 logger .logError (e , () -> "Failed to initialize hasTag" );
164170 }
165171 return false ;
@@ -178,14 +184,14 @@ public CompoundTag getOrCreateCompound() {
178184 try {
179185 Object tag ;
180186 if (hasTag ()) {
181- tag = getTag .invoke (nmsItemCopy );
187+ tag = GET_TAG .invoke (nmsItemCopy );
182188 } else {
183189 tag = nbtTagConstructor .newInstance ();
184- setTag .invoke (nmsItemCopy , tag );
190+ SET_TAG .invoke (nmsItemCopy , tag );
185191 }
186192 return new CompoundTag (tag );
187193
188- } catch (IllegalAccessException | InstantiationException | InvocationTargetException e ) {
194+ } catch (Throwable e ) {
189195 logger .logError (e , () -> "Failed to initialize CompoundTag" );
190196 }
191197 return null ;
@@ -200,8 +206,8 @@ public CompoundTag getOrCreateCompound() {
200206 public CompoundTag getCompound () {
201207 try {
202208 if (!hasTag ()) return null ;
203- return new CompoundTag (getTag .invoke (this .nmsItemCopy ));
204- } catch (IllegalAccessException | InvocationTargetException e ) {
209+ return new CompoundTag (GET_TAG .invoke (this .nmsItemCopy ));
210+ } catch (Throwable e ) {
205211 logger .logError (e , () -> "Failed to initialize CompoundTag" );
206212 }
207213 return null ;
@@ -218,9 +224,9 @@ public CompoundTag getCompound() {
218224 public ItemStack apply (@ Nonnull final CompoundTag tag ) {
219225 if (!REFLECTION_READY || nmsItemCopy == null ) return null ;
220226 try {
221- setTag .invoke (nmsItemCopy , tag .getHandle ());
222- return (ItemStack ) asBukkitCopy .invoke (null , nmsItemCopy );
223- } catch (Exception e ) {
227+ SET_TAG .invoke (nmsItemCopy , tag .getHandle ());
228+ return (ItemStack ) AS_BUKKIT_ITEM_COPY .invoke (nmsItemCopy );
229+ } catch (Throwable e ) {
224230 logger .logError (e , () -> "Failed to apply back to itemStack" );
225231 }
226232 return null ;
@@ -234,8 +240,8 @@ public ItemStack apply(@Nonnull final CompoundTag tag) {
234240 */
235241 private Object toNmsItemStack (@ Nonnull final ItemStack item ) {
236242 try {
237- return asNMSCopy .invoke (null , item );
238- } catch (IllegalAccessException | InvocationTargetException e ) {
243+ return NMS_ITEM_COPY .invoke (item );
244+ } catch (Throwable e ) {
239245 logger .logError (e , () -> "Failed to copy the bukkit stack to nms stack" );
240246 }
241247 return null ;
@@ -271,14 +277,8 @@ public static class CompoundSession {
271277 MethodHandle getBooleanM = null ;
272278 try {
273279 final Class <?> nbtTag = Class .forName (getNbtTagPath ());
274- /* hasTagKey = nbtTag.getMethod("hasKey", String.class);
275- removeM = nbtTag.getMethod("remove", String.class);
276-
277- setStringM = nbtTag.getMethod("setString", String.class, String.class);
278- getStringM = nbtTag.getMethod("getString", String.class);
279- setBooleanM = nbtTag.getMethod("setBoolean", String.class, boolean.class);
280- getBooleanM = nbtTag.getMethod("getBoolean", String.class);*/
281- MethodHandles .Lookup lookup = MethodHandles .lookup ();
280+ final MethodHandles .Lookup lookup = MethodHandles .lookup ();
281+
282282 hasTagKey = lookup .findVirtual (nbtTag , "hasKey" ,
283283 MethodType .methodType (boolean .class , String .class ));
284284 removeM = lookup .findVirtual (nbtTag , "remove" ,
@@ -358,7 +358,7 @@ public void remove(@Nonnull final String key) {
358358 remove .invoke (handle , key );
359359 } catch (Throwable e ) {
360360 logger .logError (e , () -> "Failed to check if the compound have the key." );
361- }
361+ }
362362 }
363363
364364 /**
0 commit comments