11package org .broken .arrow .library .itemcreator .utility .compound ;
22
3- import org . broken . arrow . library . logging . Logging ;
3+
44import org .broken .arrow .library .logging .Validate ;
55
66import javax .annotation .Nonnull ;
7- import java .lang .reflect .InvocationTargetException ;
8- import java .lang .reflect .Method ;
7+
98
109/**
1110 * Wraps an underlying NBTTagCompound belonging to an NMS ItemStack.
2423 *
2524 */
2625public final class CompoundTag {
27- private static final Logging logger = new Logging (CompoundTag .class );
28- private static final Method hasKey ;
29- private static final Method setBoolean ;
30- private static final Method getBoolean ;
31- private final Object handle ;
32-
33- static {
34- Method hasTagKey = null ;
35- Method setBooleanM = null ;
36- Method getBooleanM = null ;
37- try {
38- final Class <?> nbtTag = Class .forName (NbtData .getNbtTagPath ());
39- hasTagKey = nbtTag .getMethod ("hasKey" , String .class );
40- setBooleanM = nbtTag .getMethod ("setBoolean" , String .class , boolean .class );
41- getBooleanM = nbtTag .getMethod ("getBoolean" , String .class );
42- } catch (ClassNotFoundException | NoSuchMethodException e ) {
43- logger .logError (e , () -> "Failed to bind NBT methods" );
44- }
45- hasKey = hasTagKey ;
46- setBoolean = setBooleanM ;
47- getBoolean = getBooleanM ;
48-
49- }
26+ private final LegacyNBT .CompoundSession compoundSession ;
5027
5128 CompoundTag (@ Nonnull final Object handle ) {
5229 Validate .checkNotNull (handle , "CompoundTag handle cannot be null" );
53- this .handle = handle ;
30+ compoundSession = LegacyNBT .compoundSession (handle );
31+ Validate .checkNotNull (compoundSession , "The compound session could not be loaded." );
5432 }
5533
5634 /**
@@ -60,7 +38,7 @@ public final class CompoundTag {
6038 */
6139 @ Nonnull
6240 Object getHandle () {
63- return handle ;
41+ return compoundSession . getHandle () ;
6442 }
6543
6644 /**
@@ -70,14 +48,7 @@ Object getHandle() {
7048 * @return {@code true} if the key exists, otherwise {@code false}
7149 */
7250 public boolean hasKey (@ Nonnull String key ) {
73- if (hasKey == null ) return false ;
74-
75- try {
76- return (boolean ) hasKey .invoke (handle , key );
77- } catch (IllegalAccessException | InvocationTargetException e ) {
78- logger .logError (e , () -> "Failed to check if the compound have the key." );
79- }
80- return false ;
51+ return this .compoundSession .hasKey (key );
8152 }
8253
8354 /**
@@ -87,13 +58,7 @@ public boolean hasKey(@Nonnull String key) {
8758 * @param value the boolean value to assign
8859 */
8960 public void setBoolean (@ Nonnull String key , boolean value ) {
90- if (setBoolean == null ) return ;
91-
92- try {
93- setBoolean .invoke (handle , key , value );
94- } catch (IllegalAccessException | InvocationTargetException e ) {
95- logger .logError (e , () -> "Failed to set boolean value from reflection" );
96- }
61+ this .compoundSession .setBoolean (key ,value );
9762 }
9863
9964 /**
@@ -103,13 +68,6 @@ public void setBoolean(@Nonnull String key, boolean value) {
10368 * @return the stored boolean value, or {@code false} if unavailable
10469 */
10570 public boolean getBoolean (@ Nonnull String key ) {
106- if (getBoolean == null ) return false ;
107-
108- try {
109- return (boolean ) getBoolean .invoke (handle , key );
110- } catch (IllegalAccessException | InvocationTargetException e ) {
111- logger .logError (e , () -> "Failed to retrieve boolean value from reflection" );
112- }
113- return false ;
71+ return this .compoundSession .getBoolean (key );
11472 }
11573}
0 commit comments