From ddb945401c1ae74ca5ce798b87b4b0c9523732c7 Mon Sep 17 00:00:00 2001 From: shansen Date: Mon, 7 Mar 2016 12:11:46 +0100 Subject: [PATCH] Nbt --- .../java/me/shansen/nbt/NbtReflection.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/me/shansen/nbt/NbtReflection.java diff --git a/src/main/java/me/shansen/nbt/NbtReflection.java b/src/main/java/me/shansen/nbt/NbtReflection.java new file mode 100644 index 0000000..44ef91e --- /dev/null +++ b/src/main/java/me/shansen/nbt/NbtReflection.java @@ -0,0 +1,33 @@ +package me.shansen.nbt; + +import org.bukkit.Bukkit; +import org.bukkit.inventory.ItemStack; + +public class NbtReflection { + + public static ItemStack setNewEntityTag(ItemStack itemStack, String entityType) { + try { + String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; + if(version.contains("1_7") || version.contains("1_8")) { + // Legacy support. Can be cleaned up later. + return itemStack; + } + Class craftItemStack = Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack"); + Object nmsItemStack = craftItemStack.getMethod("asNMSCopy", ItemStack.class).invoke(craftItemStack, itemStack); + Object rootTag = Class.forName("net.minecraft.server." + version + ".NBTTagCompound").newInstance(); + Object nbtEntityTag = Class.forName("net.minecraft.server." + version + ".NBTTagCompound").newInstance(); + + nbtEntityTag.getClass().getMethod("setString", String.class, String.class).invoke(nbtEntityTag, "id", entityType); + rootTag.getClass().getMethod("set", String.class, rootTag.getClass().getSuperclass()) + .invoke(rootTag, "EntityTag", nbtEntityTag); + + nmsItemStack.getClass().getMethod("setTag", rootTag.getClass()).invoke(nmsItemStack, rootTag); + return (ItemStack) craftItemStack.getMethod("asBukkitCopy", nmsItemStack.getClass()) + .invoke(craftItemStack, nmsItemStack); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } + +} \ No newline at end of file