|
10 | 10 | import java.lang.reflect.Constructor; |
11 | 11 | import java.lang.reflect.InvocationTargetException; |
12 | 12 | import java.lang.reflect.Method; |
| 13 | +import java.util.Arrays; |
13 | 14 | import java.util.logging.Level; |
14 | 15 |
|
15 | 16 | /** |
@@ -250,23 +251,30 @@ private static String getCraftBukkitPath() { |
250 | 251 | */ |
251 | 252 | public static class CompoundSession { |
252 | 253 | private static final Method hasKey; |
| 254 | + |
| 255 | + private static final Method remove ; |
253 | 256 | private static final Method setBoolean; |
254 | 257 | private static final Method getBoolean; |
255 | 258 |
|
256 | 259 | private final Object handle; |
257 | 260 |
|
258 | 261 | static { |
259 | 262 | Method hasTagKey = null; |
260 | | - Method setBooleanM = null; |
| 263 | + Method removeM =null; |
| 264 | + Method setBooleanM = null; |
261 | 265 | Method getBooleanM = null; |
262 | 266 | try { |
263 | 267 | final Class<?> nbtTag = Class.forName(getNbtTagPath()); |
| 268 | + |
264 | 269 | hasTagKey = nbtTag.getMethod("hasKey", String.class); |
| 270 | + removeM = nbtTag.getMethod("remove", String.class); |
| 271 | + |
265 | 272 | setBooleanM = nbtTag.getMethod("setBoolean", String.class, boolean.class); |
266 | 273 | getBooleanM = nbtTag.getMethod("getBoolean", String.class); |
267 | 274 | } catch (ClassNotFoundException | NoSuchMethodException e) { |
268 | 275 | logger.logError(e, () -> "Failed to bind NBT methods"); |
269 | 276 | } |
| 277 | + remove = removeM ; |
270 | 278 | hasKey = hasTagKey; |
271 | 279 | setBoolean = setBooleanM; |
272 | 280 | getBoolean = getBooleanM; |
@@ -314,6 +322,22 @@ public boolean hasKey(@Nonnull String key) { |
314 | 322 | return false; |
315 | 323 | } |
316 | 324 |
|
| 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 | + |
317 | 341 | /** |
318 | 342 | * Sets a boolean value in the underlying NBTTagCompound. |
319 | 343 | * |
|
0 commit comments