Skip to content

Commit 938b681

Browse files
committed
added option to remove a set key
1 parent f53b1a6 commit 938b681

File tree

1 file changed

+25
-1
lines changed
  • Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/compound

1 file changed

+25
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.lang.reflect.Constructor;
1111
import java.lang.reflect.InvocationTargetException;
1212
import java.lang.reflect.Method;
13+
import java.util.Arrays;
1314
import java.util.logging.Level;
1415

1516
/**
@@ -250,23 +251,30 @@ private static String getCraftBukkitPath() {
250251
*/
251252
public static class CompoundSession {
252253
private static final Method hasKey;
254+
255+
private static final Method remove ;
253256
private static final Method setBoolean;
254257
private static final Method getBoolean;
255258

256259
private final Object handle;
257260

258261
static {
259262
Method hasTagKey = null;
260-
Method setBooleanM = null;
263+
Method removeM =null;
264+
Method setBooleanM = null;
261265
Method getBooleanM = null;
262266
try {
263267
final Class<?> nbtTag = Class.forName(getNbtTagPath());
268+
264269
hasTagKey = nbtTag.getMethod("hasKey", String.class);
270+
removeM = nbtTag.getMethod("remove", String.class);
271+
265272
setBooleanM = nbtTag.getMethod("setBoolean", String.class, boolean.class);
266273
getBooleanM = nbtTag.getMethod("getBoolean", String.class);
267274
} catch (ClassNotFoundException | NoSuchMethodException e) {
268275
logger.logError(e, () -> "Failed to bind NBT methods");
269276
}
277+
remove = removeM ;
270278
hasKey = hasTagKey;
271279
setBoolean = setBooleanM;
272280
getBoolean = getBooleanM;
@@ -314,6 +322,22 @@ public boolean hasKey(@Nonnull String key) {
314322
return false;
315323
}
316324

325+
326+
/**
327+
* Remove this {@link CompoundTag} value and the given key.
328+
*
329+
* @param key the NBT key to remove.
330+
*/
331+
public void remove(@Nonnull String key) {
332+
if (remove == null) return;
333+
334+
try {
335+
remove.invoke(handle, key);
336+
} catch (IllegalAccessException | InvocationTargetException e) {
337+
logger.logError(e, () -> "Failed to check if the compound have the key.");
338+
}
339+
}
340+
317341
/**
318342
* Sets a boolean value in the underlying NBTTagCompound.
319343
*

0 commit comments

Comments
 (0)