Skip to content

Commit 343261d

Browse files
committed
Abstract out the reflection logic
Instead of split it into the classes using reflection.
1 parent 8f74bf2 commit 343261d

File tree

4 files changed

+392
-187
lines changed

4 files changed

+392
-187
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/UnbreakableUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.broken.arrow.library.itemcreator.utility.compound.CompoundTag;
44
import org.broken.arrow.library.itemcreator.utility.compound.NbtData;
55
import org.broken.arrow.library.logging.Logging;
6-
import org.bukkit.Bukkit;
76
import org.bukkit.Material;
87
import org.bukkit.inventory.ItemStack;
98
import org.bukkit.inventory.meta.*;
@@ -197,7 +196,7 @@ public boolean hasBooleanTag(@Nonnull final ItemStack item, @Nonnull String key)
197196
if (compound == null) {
198197
return false;
199198
}
200-
return compound.hasKey(key);
199+
return compound.hasKey(key) && compound.getBoolean(key);
201200
}
202201

203202
}
Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.broken.arrow.library.itemcreator.utility.compound;
22

3-
import org.broken.arrow.library.logging.Logging;
3+
44
import org.broken.arrow.library.logging.Validate;
55

66
import 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.
@@ -24,33 +23,12 @@
2423
*
2524
*/
2625
public 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

Comments
 (0)