Last active
November 2, 2022 10:33
-
-
Save RealAlphaUA/054e3cf7670760f8a8e51b2c0164b2f1 to your computer and use it in GitHub Desktop.
NamedBinaryTagItemStack 1.8
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
import org.bukkit.Bukkit; | |
import org.bukkit.inventory.ItemStack; | |
import java.lang.reflect.Method; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
class NamedBinaryTagItemStack { | |
private ItemStack itemStack; | |
private Object craftItemStack; | |
private Object nbtTagCompound; | |
private static Class<?> NBTTagCompoundClazz; | |
private static Class<?> CraftItemStackClazz; | |
private static Method hasTagMethod, getTagMethod, asNMSCopyMethod, asBukkitCopyMethod, setTagMethod; | |
private static Method hasKeyMethod, removeMethod; | |
private static Method setStringMethod, setIntMethod, setByteMethod, setByteArrayMethod, setIntArrayMethod, setShortMethod, setDoubleMethod; | |
private static Method getStringMethod, getIntMethod, getByteMethod, getByteArrayMethod, getIntArrayMethod, getShortMethod, getSetDoubleMethod; | |
private static Logger logger = Logger.getLogger(NamedBinaryTagItemStack.class.getName()); | |
static { | |
try { | |
NBTTagCompoundClazz = getNMSClass("NBTTagCompound"); | |
hasKeyMethod = NBTTagCompoundClazz.getMethod("hasKey", String.class); | |
removeMethod = NBTTagCompoundClazz.getMethod("remove", String.class); | |
setStringMethod = NBTTagCompoundClazz.getMethod("setString", String.class, String.class); | |
setIntMethod = NBTTagCompoundClazz.getMethod("setInt", String.class, int.class); | |
setByteMethod = NBTTagCompoundClazz.getMethod("setByte", String.class, byte.class); | |
setByteArrayMethod = NBTTagCompoundClazz.getMethod("setByteArray", String.class, byte[].class); | |
setIntArrayMethod = NBTTagCompoundClazz.getMethod("setIntArray", String.class, int[].class); | |
setShortMethod = NBTTagCompoundClazz.getMethod("setShort", String.class, short.class); | |
setDoubleMethod = NBTTagCompoundClazz.getMethod("setDouble", String.class, double.class); | |
getStringMethod = NBTTagCompoundClazz.getMethod("getString", String.class); | |
getIntMethod = NBTTagCompoundClazz.getMethod("getInt", String.class); | |
getByteMethod = NBTTagCompoundClazz.getMethod("getByte", String.class); | |
getByteArrayMethod = NBTTagCompoundClazz.getMethod("getByteArray", String.class); | |
getIntArrayMethod = NBTTagCompoundClazz.getMethod("getIntArray", String.class); | |
getShortMethod = NBTTagCompoundClazz.getMethod("getShort", String.class); | |
getSetDoubleMethod = NBTTagCompoundClazz.getMethod("getDouble", String.class); | |
Class<?> craftItemStack = getNMSClass("ItemStack"); | |
CraftItemStackClazz = getCraftBukkitClass("inventory.CraftItemStack"); | |
asNMSCopyMethod = CraftItemStackClazz.getMethod("asNMSCopy", ItemStack.class); | |
asBukkitCopyMethod = CraftItemStackClazz.getMethod("asBukkitCopy", craftItemStack); | |
setTagMethod = craftItemStack.getMethod("setTag", NBTTagCompoundClazz); | |
getTagMethod = craftItemStack.getMethod("getTag"); | |
hasTagMethod = craftItemStack.getMethod("hasTag"); | |
}catch (NoSuchMethodException | SecurityException e){ | |
logger.log(Level.SEVERE, "Error while initializing NamedBinaryTagItemStack", e); | |
} | |
} | |
public NamedBinaryTagItemStack(ItemStack itemStack) { | |
this.itemStack = itemStack; | |
this.craftItemStack = getCraftItemStack(); | |
this.nbtTagCompound = getNBTTagCompound(); | |
} | |
public String getString(String tag) { | |
return (String) invokeMethod(getStringMethod, nbtTagCompound, tag); | |
} | |
public NamedBinaryTagItemStack setString(String tag, String value) { | |
invokeMethod(setStringMethod, nbtTagCompound, tag, value); | |
return this; | |
} | |
public NamedBinaryTagItemStack setDouble(String tag, double value) { | |
invokeMethod(setDoubleMethod, nbtTagCompound, tag, value); | |
return this; | |
} | |
public double getDouble(String tag) { | |
return (double) invokeMethod(getSetDoubleMethod, nbtTagCompound, tag); | |
} | |
public NamedBinaryTagItemStack setByte(String tag, byte b) { | |
invokeMethod(setByteMethod, nbtTagCompound, tag, b); | |
return this; | |
} | |
public byte getByte(String tag) { | |
return (byte) invokeMethod(getByteMethod, nbtTagCompound, tag); | |
} | |
public NamedBinaryTagItemStack setShort(String tag, short b) { | |
invokeMethod(setShortMethod, nbtTagCompound, tag, b); | |
return this; | |
} | |
public short getShort(String tag) { | |
return (short) invokeMethod(getShortMethod, nbtTagCompound, tag); | |
} | |
public NamedBinaryTagItemStack setInt(String tag, int b) { | |
invokeMethod(setIntMethod, nbtTagCompound, tag, b); | |
return this; | |
} | |
public int getInt(String tag) { | |
return (int) invokeMethod(getIntMethod, nbtTagCompound, tag); | |
} | |
public NamedBinaryTagItemStack setByteArray(String tag, byte[] b) { | |
invokeMethod(setByteArrayMethod, nbtTagCompound, tag, b); | |
return this; | |
} | |
public byte[] getByteArray(String tag) { | |
return (byte[]) invokeMethod(getByteArrayMethod, nbtTagCompound, tag); | |
} | |
public NamedBinaryTagItemStack setIntArray(String tag, int[] b) { | |
invokeMethod(setIntArrayMethod, nbtTagCompound, tag, b); | |
return this; | |
} | |
public int[] getIntArray(String tag) { | |
return (int[]) invokeMethod(getIntArrayMethod, nbtTagCompound, tag); | |
} | |
public boolean containsTag(String tag) { | |
return (boolean) invokeMethod(hasKeyMethod, nbtTagCompound, tag); | |
} | |
public NamedBinaryTagItemStack removeTag(String s) { | |
invokeMethod(removeMethod, nbtTagCompound, s); | |
return this; | |
} | |
public ItemStack build() { | |
setTag(this.nbtTagCompound); | |
return (ItemStack) invokeMethod(asBukkitCopyMethod, null, this.craftItemStack); | |
} | |
public Object getNBTTagCompound() { | |
try { | |
Object nbtTagCompound; | |
if (!hasTag()) { | |
nbtTagCompound = NBTTagCompoundClazz.newInstance(); | |
} else { | |
nbtTagCompound = getTag(); | |
} | |
return nbtTagCompound; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private Object getCraftItemStack() { | |
return invokeMethod(asNMSCopyMethod, null, itemStack); | |
} | |
private void setTag(Object object) { | |
invokeMethod(setTagMethod, craftItemStack, object); | |
} | |
private Object getTag() { | |
return invokeMethod(getTagMethod, craftItemStack); | |
} | |
private boolean hasTag() { | |
return (boolean) invokeMethod(hasTagMethod, craftItemStack); | |
} | |
private Object invokeMethod(Method method, Object o, Object... objects) { | |
try { | |
return method.invoke(o, objects); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static Class<?> getNMSClass(String nmsClassString) { | |
try { | |
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + "."; | |
String name = "net.minecraft.server." + version + nmsClassString; | |
return Class.forName(name); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static Class<?> getCraftBukkitClass(String nmsClassString) { | |
try { | |
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + "."; | |
String name = "org.bukkit.craftbukkit." + version + nmsClassString; | |
return Class.forName(name); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment