Created
November 20, 2020 22:12
-
-
Save Braayy/7ee6fea2c0d949354da9ffa348230be5 to your computer and use it in GitHub Desktop.
Simple NBTTagCompound Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package braayy.lib.util; | |
import org.bukkit.inventory.ItemStack; | |
import java.lang.reflect.Method; | |
@SuppressWarnings("ConstantConditions") | |
public class NBTTag { | |
private static Class<?> NBT_TAG_COMPOUND_CLASS; | |
private static Method HAS_KEY_METHOD; | |
private static Method SET_STRING_METHOD; | |
private static Method GET_STRING_METHOD; | |
private static Method SET_INT_METHOD; | |
private static Method GET_INT_METHOD; | |
private static Method SET_METHOD; | |
private static Method GET_COMPOUND_METHOD; | |
private static Method AS_NMS_COPY_METHOD; | |
private static Method AS_BUKKIT_MIRROR_METHOD; | |
private static Method GET_TAG_METHOD; | |
private static Method SET_TAG_METHOD; | |
static { | |
try { | |
NBT_TAG_COMPOUND_CLASS = NMSUtil.getNMSClass("NBTTagCompound"); | |
HAS_KEY_METHOD = NBT_TAG_COMPOUND_CLASS.getDeclaredMethod("hasKey", String.class); | |
SET_STRING_METHOD = NBT_TAG_COMPOUND_CLASS.getDeclaredMethod("setString", String.class, String.class); | |
GET_STRING_METHOD = NBT_TAG_COMPOUND_CLASS.getDeclaredMethod("getString", String.class); | |
SET_INT_METHOD = NBT_TAG_COMPOUND_CLASS.getDeclaredMethod("setInt", String.class, int.class); | |
GET_INT_METHOD = NBT_TAG_COMPOUND_CLASS.getDeclaredMethod("getInt", String.class); | |
Class<?> nbtBaseClass = NMSUtil.getNMSClass("NBTBase"); | |
SET_METHOD = NBT_TAG_COMPOUND_CLASS.getDeclaredMethod("set", String.class, nbtBaseClass); | |
GET_COMPOUND_METHOD = NBT_TAG_COMPOUND_CLASS.getDeclaredMethod("getCompound", String.class); | |
Class<?> craftItemStackClass = NMSUtil.getOBCClass("inventory.CraftItemStack"); | |
AS_NMS_COPY_METHOD = craftItemStackClass.getDeclaredMethod("asNMSCopy", ItemStack.class); | |
Class<?> itemStackClass = NMSUtil.getNMSClass("ItemStack"); | |
AS_BUKKIT_MIRROR_METHOD = craftItemStackClass.getDeclaredMethod("asCraftMirror", itemStackClass); | |
GET_TAG_METHOD = itemStackClass.getDeclaredMethod("getTag"); | |
SET_TAG_METHOD = itemStackClass.getDeclaredMethod("setTag", NBT_TAG_COMPOUND_CLASS); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
public static NBTTag newTag() { | |
try { | |
return new NBTTag(NBT_TAG_COMPOUND_CLASS.newInstance()); | |
} catch (Exception exception) { | |
throw new IllegalStateException(exception); | |
} | |
} | |
public static NBTTag getItemNBT(ItemStack stack) { | |
try { | |
Object nmsStack = AS_NMS_COPY_METHOD.invoke(null, stack); | |
Object tag = GET_TAG_METHOD.invoke(nmsStack); | |
return tag != null ? new NBTTag(tag) : newTag(); | |
} catch (Exception exception) { | |
throw new IllegalStateException(exception); | |
} | |
} | |
private final Object handle; | |
private NBTTag(Object tag) { | |
this.handle = tag; | |
} | |
public boolean has(String key) { | |
try { | |
return (boolean) HAS_KEY_METHOD.invoke(this.handle, key); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
return false; | |
} | |
} | |
public void setString(String key, String value) { | |
try { | |
SET_STRING_METHOD.invoke(this.handle, key, value); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
public void setInt(String key, int value) { | |
try { | |
SET_INT_METHOD.invoke(this.handle, key, value); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
public void setTag(String key, NBTTag tag) { | |
try { | |
SET_METHOD.invoke(this.handle, key, tag.handle); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
public String getString(String key) { | |
try { | |
return (String) GET_STRING_METHOD.invoke(this.handle, key); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
return null; | |
} | |
} | |
public int getInt(String key) { | |
try { | |
return (int) GET_INT_METHOD.invoke(this.handle, key); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
return 0; | |
} | |
} | |
public NBTTag getTag(String key) { | |
try { | |
return new NBTTag(GET_COMPOUND_METHOD.invoke(this.handle, key)); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
return null; | |
} | |
} | |
public Object getHandle() { | |
return this.handle; | |
} | |
public ItemStack apply(ItemStack stack) { | |
try { | |
Object nmsStack = AS_NMS_COPY_METHOD.invoke(null, stack); | |
SET_TAG_METHOD.invoke(nmsStack, this.handle); | |
return (ItemStack) AS_BUKKIT_MIRROR_METHOD.invoke(null, nmsStack); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment