From 87eb24b38d272fdde3e8de344ff6bc0f42798552 Mon Sep 17 00:00:00 2001 From: phage Date: Sun, 3 May 2020 15:25:41 -0500 Subject: [PATCH] Factored out NBT API Factored out the local install of the NBT API and added a repository for it --- build.gradle | 4 + .../crate/events/PlayerEvents.java | 2 +- .../monthlycrates/nbt/ClassWrapper.java | 39 -- .../monthlycrates/nbt/NBTCompound.java | 200 ---------- .../monthlycrates/nbt/NBTCompoundList.java | 101 ----- .../monthlycrates/nbt/NBTContainer.java | 35 -- .../monthlycrates/nbt/NBTEntity.java | 22 - .../splodgebox/monthlycrates/nbt/NBTFile.java | 46 --- .../monthlycrates/nbt/NBTIntegerList.java | 42 -- .../splodgebox/monthlycrates/nbt/NBTItem.java | 53 --- .../splodgebox/monthlycrates/nbt/NBTList.java | 114 ------ .../monthlycrates/nbt/NBTListCompound.java | 102 ----- .../monthlycrates/nbt/NBTReflectionUtil.java | 348 ---------------- .../monthlycrates/nbt/NBTStringList.java | 39 -- .../monthlycrates/nbt/NBTTileEntity.java | 22 - .../splodgebox/monthlycrates/nbt/NBTType.java | 34 -- .../monthlycrates/nbt/NbtApiException.java | 56 --- .../monthlycrates/nbt/ObjectCreator.java | 28 -- .../monthlycrates/nbt/ReflectionMethod.java | 121 ------ .../nbt/utils/ApiMetricsLite.java | 377 ------------------ .../monthlycrates/nbt/utils/GsonWrapper.java | 27 -- .../nbt/utils/MinecraftVersion.java | 62 --- .../nbt/utils/nmsmappings/ClassWrapper.java | 79 ---- .../nbt/utils/nmsmappings/ObjectCreator.java | 55 --- .../nbt/utils/nmsmappings/PackageWrapper.java | 28 -- .../utils/nmsmappings/ReflectionMethod.java | 183 --------- .../monthlycrates/utils/ItemStackBuilder.java | 3 +- 27 files changed, 6 insertions(+), 2216 deletions(-) delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/ClassWrapper.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompound.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompoundList.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTContainer.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTEntity.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTFile.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/NBTIntegerList.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTItem.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTList.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTListCompound.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTReflectionUtil.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/NBTStringList.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTTileEntity.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/NBTType.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/NbtApiException.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/ObjectCreator.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/ReflectionMethod.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/utils/ApiMetricsLite.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/utils/GsonWrapper.java delete mode 100755 src/main/java/net/splodgebox/monthlycrates/nbt/utils/MinecraftVersion.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ClassWrapper.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ObjectCreator.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/PackageWrapper.java delete mode 100644 src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ReflectionMethod.java diff --git a/build.gradle b/build.gradle index 6763b19..dfb6a12 100644 --- a/build.gradle +++ b/build.gradle @@ -47,6 +47,9 @@ repositories { maven { url 'https://jitpack.io' } + maven { + url 'https://repo.codemc.org/repository/maven-public/' + } } compileJava { @@ -62,6 +65,7 @@ dependencies { apt "org.projectlombok:lombok:1.18.0" compile "co.aikar:acf-core:0.5.0-SNAPSHOT" compile "co.aikar:acf-paper:0.5.0-SNAPSHOT" + compileOnly 'de.tr7zw:item-nbt-api:2.3.1' } shadowJar { diff --git a/src/main/java/net/splodgebox/monthlycrates/crate/events/PlayerEvents.java b/src/main/java/net/splodgebox/monthlycrates/crate/events/PlayerEvents.java index c828290..65c8541 100644 --- a/src/main/java/net/splodgebox/monthlycrates/crate/events/PlayerEvents.java +++ b/src/main/java/net/splodgebox/monthlycrates/crate/events/PlayerEvents.java @@ -1,8 +1,8 @@ package net.splodgebox.monthlycrates.crate.events; +import de.tr7zw.changeme.nbtapi.NBTItem; import net.splodgebox.monthlycrates.MonthlyCrates; import net.splodgebox.monthlycrates.crate.AnimationManager; -import net.splodgebox.monthlycrates.nbt.NBTItem; import net.splodgebox.monthlycrates.utils.Message; import org.bukkit.Material; import org.bukkit.entity.Player; diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/ClassWrapper.java b/src/main/java/net/splodgebox/monthlycrates/nbt/ClassWrapper.java deleted file mode 100755 index ae07f4e..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/ClassWrapper.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import org.bukkit.Bukkit; - -public enum ClassWrapper { -CRAFT_ITEMSTACK("org.bukkit.craftbukkit.", ".inventory.CraftItemStack"), -CRAFT_ENTITY("org.bukkit.craftbukkit.", ".entity.CraftEntity"), -CRAFT_WORLD("org.bukkit.craftbukkit.", ".CraftWorld"), -NMS_NBTBASE("net.minecraft.server.", ".NBTBase"), -NMS_NBTTAGSTRING("net.minecraft.server.", ".NBTTagString"), -NMS_ITEMSTACK("net.minecraft.server.", ".ItemStack"), -NMS_NBTTAGCOMPOUND("net.minecraft.server.", ".NBTTagCompound"), -NMS_NBTTAGLIST("net.minecraft.server.", ".NBTTagList"), -NMS_NBTCOMPRESSEDSTREAMTOOLS("net.minecraft.server.", ".NBTCompressedStreamTools"), -NMS_MOJANGSONPARSER("net.minecraft.server.", ".MojangsonParser"), -NMS_TILEENTITY("net.minecraft.server.", ".TileEntity"), -NMS_BLOCKPOSITION("net.minecraft.server.", ".BlockPosition"), -NMS_WORLD("net.minecraft.server.", ".WorldServer"), -NMS_ENTITY("net.minecraft.server.", ".Entity"), -; - - - - private Class clazz; - - ClassWrapper(String pre, String suffix){ - try{ - String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; - clazz = Class.forName(pre + version + suffix); - }catch(Exception ex){ - ex.printStackTrace(); - } - } - - public Class getClazz(){ - return clazz; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompound.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompound.java deleted file mode 100755 index 75e7021..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompound.java +++ /dev/null @@ -1,200 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import java.util.Set; - -import net.splodgebox.monthlycrates.nbt.utils.MinecraftVersion; - - -public class NBTCompound { - - private String compundName; - private NBTCompound parent; - - protected NBTCompound(NBTCompound owner, String name) { - this.compundName = name; - this.parent = owner; - } - - public String getName() { - return compundName; - } - - protected Object getCompound() { - return parent.getCompound(); - } - - protected void setCompound(Object compound) { - parent.setCompound(compound); - } - - public NBTCompound getParent() { - return parent; - } - - public void mergeCompound(NBTCompound comp){ - NBTReflectionUtil.addOtherNBTCompound(this, comp); - } - - public void setString(String key, String value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_STRING, key, value); - } - - public String getString(String key) { - return (String) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_STRING, key); - } - - protected String getContent(String key) { - return NBTReflectionUtil.getContent(this, key); - } - - public void setInteger(String key, Integer value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_INT, key, value); - } - - public Integer getInteger(String key) { - return (Integer) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_INT, key); - } - - public void setDouble(String key, Double value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_DOUBLE, key, value); - } - - public Double getDouble(String key) { - return (Double) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_DOUBLE, key); - } - - public void setByte(String key, Byte value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_BYTE, key, value); - } - - public Byte getByte(String key) { - return (Byte) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_BYTE, key); - } - - public void setShort(String key, Short value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_SHORT, key, value); - } - - public Short getShort(String key) { - return (Short) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_SHORT, key); - } - - public void setLong(String key, Long value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_LONG, key, value); - } - - public Long getLong(String key) { - return (Long) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_LONG, key); - } - - public void setFloat(String key, Float value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_FLOAT, key, value); - } - - public Float getFloat(String key) { - return (Float) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_FLOAT, key); - } - - public void setByteArray(String key, byte[] value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_BYTEARRAY, key, value); - } - - public byte[] getByteArray(String key) { - return (byte[]) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_BYTEARRAY, key); - } - - public void setIntArray(String key, int[] value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_INTARRAY, key, value); - } - - public int[] getIntArray(String key) { - return (int[]) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_INTARRAY, key); - } - - public void setBoolean(String key, Boolean value) { - NBTReflectionUtil.setData(this, ReflectionMethod.COMPOUND_SET_BOOLEAN, key, value); - } - - protected void set(String key, Object val) { - NBTReflectionUtil.set(this, key, val); - } - - public Boolean getBoolean(String key) { - return (Boolean) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_BOOLEAN, key); - } - - public void setObject(String key, Object value) { - NBTReflectionUtil.setObject(this, key, value); - } - - public T getObject(String key, Class type) { - return NBTReflectionUtil.getObject(this, key, type); - } - - public Boolean hasKey(String key) { - Boolean b = (Boolean) NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_HAS_KEY, key); - if(b == null)return false; - return b; - } - - public void removeKey(String key) { - NBTReflectionUtil.remove(this, key); - } - - public Set getKeys() { - return NBTReflectionUtil.getKeys(this); - } - - public NBTCompound addCompound(String name) { - if(getType(name) == NBTType.NBTTagCompound)return getCompound(name); - NBTReflectionUtil.addNBTTagCompound(this, name); - return getCompound(name); - } - - public NBTCompound getCompound(String name) { - NBTCompound next = new NBTCompound(this, name); - if (NBTReflectionUtil.valideCompound(next)) return next; - return null; - } - - public NBTList getList(String name, NBTType type) { - return NBTReflectionUtil.getList(this, name, type); - } - - public NBTType getType(String name) { - if (MinecraftVersion.getVersion() == MinecraftVersion.MC1_7_R4) return null; - Object o = NBTReflectionUtil.getData(this, ReflectionMethod.COMPOUND_GET_TYPE, name); - if(o == null)return null; - return NBTType.valueOf((byte) o); - } - - @Override - public String toString() { - StringBuilder result = new StringBuilder(); - for (String key : getKeys()) { - result.append(toString(key)); - } - return result.toString(); - } - - public String toString(String key) { - StringBuilder result = new StringBuilder(); - NBTCompound compound = this; - while (compound.getParent() != null) { - result.append(" "); - compound = compound.getParent(); - } - if (this.getType(key) == NBTType.NBTTagCompound) { - return this.getCompound(key).toString(); - } else { - return result + "-" + key + ": " + getContent(key) + System.lineSeparator(); - } - } - - public String asNBTString(){ - Object comp = NBTReflectionUtil.gettoCompount(getCompound(), this); - if(comp == null)return "{}"; - return comp.toString(); - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompoundList.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompoundList.java deleted file mode 100644 index 768373e..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTCompoundList.java +++ /dev/null @@ -1,101 +0,0 @@ -package de.tr7zw.changeme.nbtapi; - -import org.apache.commons.lang.NotImplementedException; - -import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; -import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper; -import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod; - -/** - * {@link NBTListCompound} implementation for NBTLists - * - * @author tr7zw - * - */ -public class NBTCompoundList extends NBTList { - - protected NBTCompoundList(NBTCompound owner, String name, NBTType type, Object list) { - super(owner, name, type, list); - } - - /** - * Adds a new Compound to the end of the List and returns it. - * - * @return The added {@link NBTListCompound} - */ - public NBTListCompound addCompound() { - try { - Object compound = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); - if (MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_14_R1.getVersionId()) { - ReflectionMethod.LIST_ADD.run(listObject, size(), compound); - } else { - ReflectionMethod.LEGACY_LIST_ADD.run(listObject, compound); - } - return new NBTListCompound(this, compound); - } catch (Exception ex) { - throw new NbtApiException(ex); - } - } - - /** - * Adds a new Compound to the end of the List. - * - * @param empty This has to be null! - * @return True, if compound was added - */ - @Override - public boolean add(NBTListCompound empty) { - if (empty != null) { - throw new NotImplementedException("You need to pass null! ListCompounds from other lists won't work."); - } - try { - Object compound = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); - if (MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_14_R1.getVersionId()) { - ReflectionMethod.LIST_ADD.run(listObject, 0, compound); - } else { - ReflectionMethod.LEGACY_LIST_ADD.run(listObject, compound); - } - return true; - } catch (Exception ex) { - throw new NbtApiException(ex); - } - } - - @Override - public void add(int index, NBTListCompound element) { - if (element != null) { - throw new NotImplementedException("You need to pass null! ListCompounds from other lists won't work."); - } - try { - Object compound = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); - if (MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_14_R1.getVersionId()) { - ReflectionMethod.LIST_ADD.run(listObject, index, compound); - } else { - ReflectionMethod.LEGACY_LIST_ADD.run(listObject, compound); - } - } catch (Exception ex) { - throw new NbtApiException(ex); - } - } - - @Override - public NBTListCompound get(int index) { - try { - Object compound = ReflectionMethod.LIST_GET_COMPOUND.run(listObject, index); - return new NBTListCompound(this, compound); - } catch (Exception ex) { - throw new NbtApiException(ex); - } - } - - @Override - public NBTListCompound set(int index, NBTListCompound element) { - throw new NotImplementedException("This method doesn't work in the ListCompound context."); - } - - @Override - protected Object asTag(NBTListCompound object) { - return null; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTContainer.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTContainer.java deleted file mode 100755 index 96dffa9..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTContainer.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -public class NBTContainer extends NBTCompound{ - - private Object nbt; - - public NBTContainer() { - super(null, null); - nbt = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - - protected NBTContainer(Object nbt){ - super(null, null); - this.nbt = nbt; - } - - public NBTContainer(String nbtString) throws IllegalArgumentException { - super(null, null); - try{ - nbt = ReflectionMethod.PARSE_NBT.run(null, nbtString); - }catch(Exception ex){ - ex.printStackTrace(); - throw new IllegalArgumentException("Malformed Json: " + ex.getMessage()); - } - } - - protected Object getCompound() { - return nbt; - } - - protected void setCompound(Object tag) { - nbt = tag; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTEntity.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTEntity.java deleted file mode 100755 index a57c5a9..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTEntity.java +++ /dev/null @@ -1,22 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import org.bukkit.entity.Entity; - -public class NBTEntity extends NBTCompound { - - private final Entity ent; - - public NBTEntity(Entity entity) { - super(null, null); - ent = entity; - } - - protected Object getCompound() { - return NBTReflectionUtil.getEntityNBTTagCompound(NBTReflectionUtil.getNMSEntity(ent)); - } - - protected void setCompound(Object compound) { - NBTReflectionUtil.setEntityNBTTag(compound, NBTReflectionUtil.getNMSEntity(ent)); - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTFile.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTFile.java deleted file mode 100755 index 61a1f68..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTFile.java +++ /dev/null @@ -1,46 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; - -public class NBTFile extends NBTCompound { - - private final File file; - private Object nbt; - - public NBTFile(File file) throws IOException { - super(null, null); - this.file = file; - if (file.exists()) { - FileInputStream inputsteam = new FileInputStream(file); - nbt = NBTReflectionUtil.readNBTFile(inputsteam); - } else { - nbt = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - save(); - } - } - - public void save() throws IOException { - if (!file.exists()) { - file.getParentFile().mkdirs(); - file.createNewFile(); - } - FileOutputStream outStream = new FileOutputStream(file); - NBTReflectionUtil.saveNBTFile(nbt, outStream); - } - - public File getFile() { - return file; - } - - protected Object getCompound() { - return nbt; - } - - protected void setCompound(Object compound) { - nbt = compound; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTIntegerList.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTIntegerList.java deleted file mode 100644 index 566c3b0..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTIntegerList.java +++ /dev/null @@ -1,42 +0,0 @@ -package de.tr7zw.changeme.nbtapi; - -import java.lang.reflect.InvocationTargetException; - -import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper; -import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod; - -/** - * Integer implementation for NBTLists - * - * @author tr7zw - * - */ -public class NBTIntegerList extends NBTList { - - protected NBTIntegerList(NBTCompound owner, String name, NBTType type, Object list) { - super(owner, name, type, list); - } - - @Override - protected Object asTag(Integer object) { - try { - return ClassWrapper.NMS_NBTTAGINT.getClazz().getConstructor(int.class).newInstance(object); - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | SecurityException e) { - throw new NbtApiException("Error while wrapping the Oject " + object + " to it's NMS object!", e); - } - } - - @Override - public Integer get(int index) { - try { - Object obj = ReflectionMethod.LIST_GET.run(listObject, index); - return Integer.valueOf(obj.toString()); - } catch (NumberFormatException nf) { - return 0; - } catch (Exception ex) { - throw new NbtApiException(ex); - } - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTItem.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTItem.java deleted file mode 100755 index ddcec66..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTItem.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import org.bukkit.inventory.ItemStack; - -public class NBTItem extends NBTCompound { - - private ItemStack bukkitItem; - - public NBTItem(ItemStack item) { - super(null, null); - if(item == null){ - throw new NullPointerException("ItemStack can't be null!"); - } - bukkitItem = item.clone(); - } - - protected Object getCompound() { - return NBTReflectionUtil.getItemRootNBTTagCompound(ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, bukkitItem)); - } - - protected void setCompound(Object compound) { - Object stack = ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, bukkitItem); - ReflectionMethod.ITEMSTACK_SET_TAG.run(stack, compound); - bukkitItem = (ItemStack) ReflectionMethod.ITEMSTACK_BUKKITMIRROR.run(null, stack); - } - - public ItemStack getItem() { - return bukkitItem; - } - - protected void setItem(ItemStack item) { - bukkitItem = item; - } - - - /** - * This may return true even when the NBT is empty. - * - * @return Does the ItemStack have a NBTCompound. - */ - public boolean hasNBTData(){ - return getCompound() != null; - } - - public static NBTContainer convertItemtoNBT(ItemStack item){ - return NBTReflectionUtil.convertNMSItemtoNBTCompound(ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, item)); - } - - public static ItemStack convertNBTtoItem(NBTCompound comp){ - return (ItemStack) ReflectionMethod.ITEMSTACK_BUKKITMIRROR.run(null, NBTReflectionUtil.convertNBTCompoundtoNMSItem(comp)); - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTList.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTList.java deleted file mode 100755 index 7c5e918..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTList.java +++ /dev/null @@ -1,114 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -public class NBTList { - - private String listName; - private NBTCompound parent; - private NBTType type; - private Object listObject; - - protected NBTList(NBTCompound owner, String name, NBTType type, Object list) { - parent = owner; - listName = name; - this.type = type; - this.listObject = list; - if (!(type == NBTType.NBTTagString || type == NBTType.NBTTagCompound)) { - System.err.println("List types != String/Compound are currently not implemented!"); - } - } - - protected void save() { - parent.set(listName, listObject); - } - - public NBTListCompound addCompound() { - if (type != NBTType.NBTTagCompound) { - new Throwable("Using Compound method on a non Compound list!").printStackTrace(); - return null; - } - try { - Object compound = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); - ReflectionMethod.LIST_ADD.run(listObject, compound); - return new NBTListCompound(this, compound); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - public NBTListCompound getCompound(int id) { - if (type != NBTType.NBTTagCompound) { - new Throwable("Using Compound method on a non Compound list!").printStackTrace(); - return null; - } - try { - Object compound = ReflectionMethod.LIST_GET.run(listObject, id); - return new NBTListCompound(this, compound); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - public String getString(int i) { - if (type != NBTType.NBTTagString) { - new Throwable("Using String method on a non String list!").printStackTrace(); - return null; - } - try { - return (String) ReflectionMethod.LIST_GET_STRING.run(listObject, i); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - public void addString(String s) { - if (type != NBTType.NBTTagString) { - new Throwable("Using String method on a non String list!").printStackTrace(); - return; - } - try { - ReflectionMethod.LIST_ADD.run(listObject, ClassWrapper.NMS_NBTTAGSTRING.getClazz().getConstructor(String.class).newInstance(s)); - save(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public void setString(int i, String s) { - if (type != NBTType.NBTTagString) { - new Throwable("Using String method on a non String list!").printStackTrace(); - return; - } - try { - ReflectionMethod.LIST_SET.run(listObject, i, ClassWrapper.NMS_NBTTAGSTRING.getClazz().getConstructor(String.class).newInstance(s)); - save(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public void remove(int i) { - try { - ReflectionMethod.LIST_REMOVE_KEY.run(listObject, i); - save(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public int size() { - try { - return (int) ReflectionMethod.LIST_SIZE.run(listObject); - } catch (Exception ex) { - ex.printStackTrace(); - } - return -1; - } - - public NBTType getType() { - return type; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTListCompound.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTListCompound.java deleted file mode 100755 index 6e80c1b..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTListCompound.java +++ /dev/null @@ -1,102 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import java.util.HashSet; -import java.util.Set; - -public class NBTListCompound { - - private NBTList owner; - private Object compound; - - protected NBTListCompound(NBTList parent, Object obj) { - owner = parent; - compound = obj; - } - - public void setString(String key, String value) { - if (value == null) { - remove(key); - return; - } - try { - compound.getClass().getMethod("setString", String.class, String.class).invoke(compound, key, value); - owner.save(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public void setInteger(String key, int value) { - try { - compound.getClass().getMethod("setInt", String.class, int.class).invoke(compound, key, value); - owner.save(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public int getInteger(String value) { - try { - return (int) compound.getClass().getMethod("getInt", String.class).invoke(compound, value); - } catch (Exception ex) { - ex.printStackTrace(); - } - return 0; - } - - public void setDouble(String key, double value) { - try { - compound.getClass().getMethod("setDouble", String.class, double.class).invoke(compound, key, value); - owner.save(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public double getDouble(String key) { - try { - return (double) compound.getClass().getMethod("getDouble", String.class).invoke(compound, key); - } catch (Exception ex) { - ex.printStackTrace(); - } - return 0; - } - - - public String getString(String key) { - try { - return (String) compound.getClass().getMethod("getString", String.class).invoke(compound, key); - } catch (Exception ex) { - ex.printStackTrace(); - } - return ""; - } - - public boolean hasKey(String key) { - try { - return (boolean) compound.getClass().getMethod("hasKey", String.class).invoke(compound, key); - } catch (Exception ex) { - ex.printStackTrace(); - } - return false; - } - - @SuppressWarnings("unchecked") - public Set getKeys() { - try { - return (Set) ReflectionMethod.LISTCOMPOUND_GET_KEYS.run(compound); - } catch (Exception ex) { - ex.printStackTrace(); - } - return new HashSet<>(); - } - - public void remove(String key) { - try { - compound.getClass().getMethod("remove", String.class).invoke(compound, key); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTReflectionUtil.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTReflectionUtil.java deleted file mode 100755 index c8b593d..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTReflectionUtil.java +++ /dev/null @@ -1,348 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.Set; -import java.util.Stack; - -import org.bukkit.block.BlockState; -import org.bukkit.entity.Entity; - -import net.splodgebox.monthlycrates.nbt.utils.GsonWrapper; -import net.splodgebox.monthlycrates.nbt.utils.MinecraftVersion; - -public class NBTReflectionUtil { - - public static Object getNMSEntity(Entity entity) { - try { - return ReflectionMethod.CRAFT_ENTITY_GET_HANDLE.run(ClassWrapper.CRAFT_ENTITY.getClazz().cast(entity)); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public static Object readNBTFile(FileInputStream stream) { - try { - return ReflectionMethod.NBTFILE_READ.run(null, stream); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public static Object saveNBTFile(Object nbt, FileOutputStream stream) { - try { - return ReflectionMethod.NBTFILE_WRITE.run(null, nbt, stream); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - @SuppressWarnings({"unchecked"}) - public static Object getItemRootNBTTagCompound(Object nmsitem) { - @SuppressWarnings("rawtypes") - Class clazz = nmsitem.getClass(); - Method method; - try { - method = clazz.getMethod("getTag"); - Object answer = method.invoke(nmsitem); - return answer; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - @SuppressWarnings({"unchecked"}) - public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) { - @SuppressWarnings("rawtypes") - Class clazz = ClassWrapper.NMS_ITEMSTACK.getClazz(); - try { - if(MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_12_R1.getVersionId()){ - Constructor constructor = clazz.getConstructor(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()); - constructor.setAccessible(true); - return constructor.newInstance(nbtcompound.getCompound()); - }else{ - Method method = clazz.getMethod("createStack", ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()); - method.setAccessible(true); - return method.invoke(null, nbtcompound.getCompound()); - } - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - @SuppressWarnings({"unchecked"}) - public static NBTContainer convertNMSItemtoNBTCompound(Object nmsitem) { - @SuppressWarnings("rawtypes") - Class clazz = nmsitem.getClass(); - Method method; - try { - method = clazz.getMethod("save", ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()); - Object answer = method.invoke(nmsitem, ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance()); - return new NBTContainer(answer); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public static Object getEntityNBTTagCompound(Object NMSEntity) { - try { - Object nbt = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); - Object answer = ReflectionMethod.NMS_ENTITY_GET_NBT.run(NMSEntity, nbt); - if (answer == null) - answer = nbt; - return answer; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public static Object setEntityNBTTag(Object NBTTag, Object NMSEntity) { - try { - ReflectionMethod.NMS_ENTITY_SET_NBT.run(NMSEntity, NBTTag); - return NMSEntity; - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - public static Object getTileEntityNBTTagCompound(BlockState tile) { - try { - Object pos = ObjectCreator.NMS_BLOCKPOSITION.getInstance(tile.getX(), tile.getY(), tile.getZ()); - Object cworld = ClassWrapper.CRAFT_WORLD.getClazz().cast(tile.getWorld()); - Object nmsworld = ReflectionMethod.CRAFT_WORLD_GET_HANDLE.run(cworld); - Object o = ReflectionMethod.NMS_WORLD_GET_TILEENTITY.run(nmsworld, pos); - Object tag = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); - Object answer = ReflectionMethod.TILEENTITY_GET_NBT.run(o, tag); - if (answer == null) - answer = tag; - return answer; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public static void setTileEntityNBTTagCompound(BlockState tile, Object comp) { - try { - Object pos = ObjectCreator.NMS_BLOCKPOSITION.getInstance(tile.getX(), tile.getY(), tile.getZ()); - Object cworld = ClassWrapper.CRAFT_WORLD.getClazz().cast(tile.getWorld()); - Object nmsworld = ReflectionMethod.CRAFT_WORLD_GET_HANDLE.run(cworld); - Object o = ReflectionMethod.NMS_WORLD_GET_TILEENTITY.run(nmsworld, pos); - ReflectionMethod.TILEENTITY_SET_NBT.run(o, comp); - } catch (Exception e) { - e.printStackTrace(); - } - } - - - @SuppressWarnings("unchecked") - public static Object getSubNBTTagCompound(Object compound, String name) { - @SuppressWarnings("rawtypes") - Class c = compound.getClass(); - Method method; - try { - method = c.getMethod("getCompound", String.class); - Object answer = method.invoke(compound, name); - return answer; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public static void addNBTTagCompound(NBTCompound comp, String name) { - if (name == null) { - remove(comp, name); - return; - } - Object nbttag = comp.getCompound(); - if (nbttag == null) { - nbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) return; - Object workingtag = gettoCompount(nbttag, comp); - Method method; - try { - method = workingtag.getClass().getMethod("set", String.class, ClassWrapper.NMS_NBTBASE.getClazz()); - method.invoke(workingtag, name, ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance()); - comp.setCompound(nbttag); - return; - } catch (Exception ex) { - ex.printStackTrace(); - } - return; - } - - public static Boolean valideCompound(NBTCompound comp) { - Object root = comp.getCompound(); - if (root == null) { - root = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - return (gettoCompount(root, comp)) != null; - } - - static Object gettoCompount(Object nbttag, NBTCompound comp) { - Stack structure = new Stack<>(); - while (comp.getParent() != null) { - structure.add(comp.getName()); - comp = comp.getParent(); - } - while (!structure.isEmpty()) { - nbttag = getSubNBTTagCompound(nbttag, structure.pop()); - if (nbttag == null) { - return null; - } - } - return nbttag; - } - - public static void addOtherNBTCompound(NBTCompound comp, NBTCompound nbtcompound) { - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) return; - Object workingtag = gettoCompount(rootnbttag, comp); - try { - ReflectionMethod.COMPOUND_ADD.run(workingtag, nbtcompound.getCompound()); - comp.setCompound(rootnbttag); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public static String getContent(NBTCompound comp, String key) { - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) return null; - Object workingtag = gettoCompount(rootnbttag, comp); - Method method; - try { - method = workingtag.getClass().getMethod("get", String.class); - return method.invoke(workingtag, key).toString(); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - public static void set(NBTCompound comp, String key, Object val) { - if (val == null) { - remove(comp, key); - return; - } - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) { - new Throwable("InvalideCompound").printStackTrace(); - return; - } - Object workingtag = gettoCompount(rootnbttag, comp); - Method method; - try { - method = workingtag.getClass().getMethod("set", String.class, ClassWrapper.NMS_NBTBASE.getClazz()); - method.invoke(workingtag, key, val); - comp.setCompound(rootnbttag); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public static NBTList getList(NBTCompound comp, String key, NBTType type) { - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) return null; - Object workingtag = gettoCompount(rootnbttag, comp); - Method method; - try { - method = workingtag.getClass().getMethod("getList", String.class, int.class); - return new NBTList(comp, key, type, method.invoke(workingtag, key, type.getId())); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - public static void setObject(NBTCompound comp, String key, Object value) { - if (!MinecraftVersion.hasGsonSupport()) return; - try { - String json = GsonWrapper.getString(value); - setData(comp, ReflectionMethod.COMPOUND_SET_STRING, key, json); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public static T getObject(NBTCompound comp, String key, Class type) { - if (!MinecraftVersion.hasGsonSupport()) return null; - String json = (String) getData(comp, ReflectionMethod.COMPOUND_GET_STRING, key); - if (json == null) { - return null; - } - return GsonWrapper.deserializeJson(json, type); - } - - public static void remove(NBTCompound comp, String key) { - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) return; - Object workingtag = gettoCompount(rootnbttag, comp); - ReflectionMethod.COMPOUND_REMOVE_KEY.run(workingtag, key); - comp.setCompound(rootnbttag); - } - - @SuppressWarnings("unchecked") - public static Set getKeys(NBTCompound comp) { - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) return null; - Object workingtag = gettoCompount(rootnbttag, comp); - return (Set) ReflectionMethod.COMPOUND_GET_KEYS.run(workingtag); - } - - public static void setData(NBTCompound comp, ReflectionMethod type, String key, Object data) { - if (data == null) { - remove(comp, key); - return; - } - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); - } - if (!valideCompound(comp)) return; - Object workingtag = gettoCompount(rootnbttag, comp); - type.run(workingtag, key, data); - comp.setCompound(rootnbttag); - } - - public static Object getData(NBTCompound comp, ReflectionMethod type, String key) { - Object rootnbttag = comp.getCompound(); - if (rootnbttag == null) { - return null; - } - if (!valideCompound(comp)) return null; - Object workingtag = gettoCompount(rootnbttag, comp); - return type.run(workingtag, key); - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTStringList.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTStringList.java deleted file mode 100644 index 593b832..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTStringList.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.tr7zw.changeme.nbtapi; - -import java.lang.reflect.InvocationTargetException; - -import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper; -import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod; - -/** - * String implementation for NBTLists - * - * @author tr7zw - * - */ -public class NBTStringList extends NBTList { - - protected NBTStringList(NBTCompound owner, String name, NBTType type, Object list) { - super(owner, name, type, list); - } - - @Override - public String get(int index) { - try { - return (String) ReflectionMethod.LIST_GET_STRING.run(listObject, index); - } catch (Exception ex) { - throw new NbtApiException(ex); - } - } - - @Override - protected Object asTag(String object) { - try { - return ClassWrapper.NMS_NBTTAGSTRING.getClazz().getConstructor(String.class).newInstance(object); - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | SecurityException e) { - throw new NbtApiException("Error while wrapping the Oject " + object + " to it's NMS object!", e); - } - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTTileEntity.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTTileEntity.java deleted file mode 100755 index 9950e4a..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTTileEntity.java +++ /dev/null @@ -1,22 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import org.bukkit.block.BlockState; - -public class NBTTileEntity extends NBTCompound { - - private final BlockState tile; - - public NBTTileEntity(BlockState tile) { - super(null, null); - this.tile = tile; - } - - protected Object getCompound() { - return NBTReflectionUtil.getTileEntityNBTTagCompound(tile); - } - - protected void setCompound(Object compound) { - NBTReflectionUtil.setTileEntityNBTTagCompound(tile, compound); - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTType.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NBTType.java deleted file mode 100755 index 2865f6f..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NBTType.java +++ /dev/null @@ -1,34 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -public enum NBTType { - NBTTagEnd(0), - NBTTagByte(1), - NBTTagShort(2), - NBTTagInt(3), - NBTTagLong(4), - NBTTagFloat(5), - NBTTagDouble(6), - NBTTagByteArray(7), - NBTTagIntArray(11), - NBTTagString(8), - NBTTagList(9), - NBTTagCompound(10); - - NBTType(int i) { - id = i; - } - - private final int id; - - public int getId() { - return id; - } - - public static NBTType valueOf(int id) { - for (NBTType t : values()) - if (t.getId() == id) - return t; - return NBTType.NBTTagEnd; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/NbtApiException.java b/src/main/java/net/splodgebox/monthlycrates/nbt/NbtApiException.java deleted file mode 100644 index a205efe..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/NbtApiException.java +++ /dev/null @@ -1,56 +0,0 @@ -package de.tr7zw.changeme.nbtapi; - -/** - * A generic {@link RuntimeException} that can be thrown by most methods in the - * NBTAPI. - * - * @author tr7zw - * - */ -public class NbtApiException extends RuntimeException { - - /** - * - */ - private static final long serialVersionUID = -993309714559452334L; - - /** - * - */ - public NbtApiException() { - super(); - } - - /** - * @param message - * @param cause - * @param enableSuppression - * @param writableStackTrace - */ - public NbtApiException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - - /** - * @param message - * @param cause - */ - public NbtApiException(String message, Throwable cause) { - super(message, cause); - } - - /** - * @param message - */ - public NbtApiException(String message) { - super(message); - } - - /** - * @param cause - */ - public NbtApiException(Throwable cause) { - super(cause); - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/ObjectCreator.java b/src/main/java/net/splodgebox/monthlycrates/nbt/ObjectCreator.java deleted file mode 100755 index 1cca175..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/ObjectCreator.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import java.lang.reflect.Constructor; - -public enum ObjectCreator { - NMS_NBTTAGCOMPOUND(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()), - NMS_BLOCKPOSITION(ClassWrapper.NMS_BLOCKPOSITION.getClazz(), int.class, int.class, int.class); - - private Constructor construct; - - ObjectCreator(Class clazz, Class... args){ - try{ - construct = clazz.getConstructor(args); - }catch(Exception ex){ - ex.printStackTrace(); - } - } - - public Object getInstance(Object... args){ - try{ - return construct.newInstance(args); - }catch(Exception ex){ - ex.printStackTrace(); - } - return null; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/ReflectionMethod.java b/src/main/java/net/splodgebox/monthlycrates/nbt/ReflectionMethod.java deleted file mode 100755 index 54dd8c5..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/ReflectionMethod.java +++ /dev/null @@ -1,121 +0,0 @@ -package net.splodgebox.monthlycrates.nbt; - -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.reflect.Method; - -import org.bukkit.inventory.ItemStack; - -import net.splodgebox.monthlycrates.nbt.utils.MinecraftVersion; - -public enum ReflectionMethod { - - COMPOUND_SET_FLOAT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, float.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setFloat")), - COMPOUND_SET_STRING(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setString")), - COMPOUND_SET_INT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setInt")), - COMPOUND_SET_BYTEARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, byte[].class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setByteArray")), - COMPOUND_SET_INTARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, int[].class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setIntArray")), - COMPOUND_SET_LONG(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, long.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setLong")), - COMPOUND_SET_SHORT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, short.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setShort")), - COMPOUND_SET_BYTE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, byte.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setByte")), - COMPOUND_SET_DOUBLE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, double.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setDouble")), - COMPOUND_SET_BOOLEAN(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, boolean.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setBoolean")), - COMPOUND_ADD(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a")), //FIXME: No Spigot mapping! - - COMPOUND_GET_FLOAT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getFloat")), - COMPOUND_GET_STRING(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getString")), - COMPOUND_GET_INT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getInt")), - COMPOUND_GET_BYTEARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getByteArray")), - COMPOUND_GET_INTARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getIntArray")), - COMPOUND_GET_LONG(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getLong")), - COMPOUND_GET_SHORT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getShort")), - COMPOUND_GET_BYTE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getByte")), - COMPOUND_GET_DOUBLE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getDouble")), - COMPOUND_GET_BOOLEAN(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getBoolean")), - - COMPOUND_REMOVE_KEY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "remove")), - COMPOUND_HAS_KEY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "hasKey")), - COMPOUND_GET_TYPE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "b"), new Since(MinecraftVersion.MC1_9_R1, "d")), //FIXME: No Spigot mapping! - COMPOUND_GET_KEYS(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "c"), new Since(MinecraftVersion.MC1_13_R1, "getKeys")), - - LISTCOMPOUND_GET_KEYS(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "c"), new Since(MinecraftVersion.MC1_13_R1, "getKeys")), - LIST_REMOVE_KEY(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a"), new Since(MinecraftVersion.MC1_9_R1, "remove")), - LIST_SIZE(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "size")), - LIST_SET(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class, ClassWrapper.NMS_NBTBASE.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a"), new Since(MinecraftVersion.MC1_13_R1, "set")), - LIST_ADD(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{ClassWrapper.NMS_NBTBASE.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "add")), - LIST_GET_STRING(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getString")), - LIST_GET(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "get")), - - ITEMSTACK_SET_TAG(ClassWrapper.NMS_ITEMSTACK.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setTag")), - ITEMSTACK_NMSCOPY(ClassWrapper.CRAFT_ITEMSTACK.getClazz(), new Class[]{ItemStack.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "asNMSCopy")), - ITEMSTACK_BUKKITMIRROR(ClassWrapper.CRAFT_ITEMSTACK.getClazz(), new Class[]{ClassWrapper.NMS_ITEMSTACK.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "asCraftMirror")), - - CRAFT_WORLD_GET_HANDLE(ClassWrapper.CRAFT_WORLD.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getHandle")), - NMS_WORLD_GET_TILEENTITY(ClassWrapper.NMS_WORLD.getClazz(), new Class[]{ClassWrapper.NMS_BLOCKPOSITION.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getTileEntity")), - - TILEENTITY_GET_NBT(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "b"), new Since(MinecraftVersion.MC1_9_R1, "save")), - TILEENTITY_SET_NBT(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "a"), new Since(MinecraftVersion.MC1_12_R1, "load")), - - CRAFT_ENTITY_GET_HANDLE(ClassWrapper.CRAFT_ENTITY.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getHandle")), - NMS_ENTITY_SET_NBT(ClassWrapper.NMS_ENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "f")), //FIXME: No Spigot mapping! - NMS_ENTITY_GET_NBT(ClassWrapper.NMS_ENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "e"), new Since(MinecraftVersion.MC1_12_R1, "save")), - - NBTFILE_READ(ClassWrapper.NMS_NBTCOMPRESSEDSTREAMTOOLS.getClazz(), new Class[]{InputStream.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a")), //FIXME: No Spigot mapping! - NBTFILE_WRITE(ClassWrapper.NMS_NBTCOMPRESSEDSTREAMTOOLS.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), OutputStream.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a")), //FIXME: No Spigot mapping! - - - PARSE_NBT(ClassWrapper.NMS_MOJANGSONPARSER.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "parse")), - - ; - - private Since targetVersion; - private Method method; - private boolean loaded = false; - private boolean compatible = false; - - ReflectionMethod(Class targetClass, Class[] args, MinecraftVersion addedSince, Since... methodnames){ - MinecraftVersion server = MinecraftVersion.getVersion(); - if(server.compareTo(addedSince) < 0)return; - compatible = true; - Since target = methodnames[0]; - for(Since s : methodnames){ - if(s.version.getVersionId() <= server.getVersionId() && target.version.getVersionId() < s.version.getVersionId()) - target = s; - } - targetVersion = target; - try{ - method = targetClass.getMethod(targetVersion.name, args); - method.setAccessible(true); - loaded = true; - }catch(NullPointerException | NoSuchMethodException | SecurityException ex){ - ex.printStackTrace(); - } - } - - public Object run(Object target, Object... args){ - try{ - return method.invoke(target, args); - }catch(Exception ex){ - ex.printStackTrace(); - } - return null; - } - - public boolean isLoaded() { - return loaded; - } - - public boolean isCompatible() { - return compatible; - } - - public static class Since{ - public final MinecraftVersion version; - public final String name; - public Since(MinecraftVersion version, String name) { - this.version = version; - this.name = name; - } - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/ApiMetricsLite.java b/src/main/java/net/splodgebox/monthlycrates/nbt/utils/ApiMetricsLite.java deleted file mode 100644 index 557a0c7..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/ApiMetricsLite.java +++ /dev/null @@ -1,377 +0,0 @@ -package de.tr7zw.changeme.nbtapi.utils; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.RegisteredServiceProvider; -import org.bukkit.plugin.ServicePriority; - -import javax.net.ssl.HttpsURLConnection; -import java.io.*; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.Collection; -import java.util.Timer; -import java.util.TimerTask; -import java.util.UUID; -import java.util.logging.Level; -import java.util.zip.GZIPOutputStream; -import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.logger; - -/** - * bStats collects some data for plugin authors. - *

- * Check out https://bStats.org/ to learn more about bStats! - * - * This class is modified by tr7zw to work when the api is shaded into other peoples plugins. - */ -public class ApiMetricsLite { - - private static final String PLUGINNAME = "ItemNBTAPI"; // DO NOT CHANGE THE NAME! else it won't link the data on bStats - private static final String PLUGINVERSION = "2.0.0"; // In case you fork the nbt-api for internal use in your network, plugins and so on, you *may* add that to the version here. (2.x.x-Timolia or something like that?) - // Not sure how good of an idea that is, so maybe just leave it as is ¯\_(ツ)_/¯ - - // The version of this bStats class - public static final int B_STATS_VERSION = 1; - - // The version of the NBT-Api bStats - public static final int NBT_BSTATS_VERSION = 1; - - // The url to which the data is sent - private static final String URL = "https://bStats.org/submitData/bukkit"; - - // Is bStats enabled on this server? - private boolean enabled; - - // Should failed requests be logged? - private static boolean logFailedRequests; - - // Should the sent data be logged? - private static boolean logSentData; - - // Should the response text be logged? - private static boolean logResponseStatusText; - - // The uuid of the server - private static String serverUUID; - - // The plugin - private Plugin plugin; - - /** - * Class constructor. - * - */ - public ApiMetricsLite() { - - // The register method just uses any enabled plugin it can find to register. This *shouldn't* cause any problems, since the plugin isn't used any other way. - // Register our service - for(Plugin plug : Bukkit.getPluginManager().getPlugins()) { - plugin = plug; - if(plugin != null) - break; - } - if(plugin == null) { - return;// Didn't find any plugin that could work - } - - // Get the config file - File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); - File configFile = new File(bStatsFolder, "config.yml"); - YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); - - // Check if the config file exists - if (!config.isSet("serverUuid")) { - - // Add default values - config.addDefault("enabled", true); - // Every server gets it's unique random id. - config.addDefault("serverUuid", UUID.randomUUID().toString()); - // Should failed request be logged? - config.addDefault("logFailedRequests", false); - // Should the sent data be logged? - config.addDefault("logSentData", false); - // Should the response text be logged? - config.addDefault("logResponseStatusText", false); - - // Inform the server owners about bStats - config.options().header( - "bStats collects some data for plugin authors like how many servers are using their plugins.\n" + - "To honor their work, you should not disable it.\n" + - "This has nearly no effect on the server performance!\n" + - "Check out https://bStats.org/ to learn more :)" - ).copyDefaults(true); - try { - config.save(configFile); - } catch (IOException ignored) { } - } - - // Load the data - serverUUID = config.getString("serverUuid"); - logFailedRequests = config.getBoolean("logFailedRequests", false); - enabled = config.getBoolean("enabled", true); - logSentData = config.getBoolean("logSentData", false); - logResponseStatusText = config.getBoolean("logResponseStatusText", false); - if (enabled) { - boolean found = false; - // Search for all other bStats Metrics classes to see if we are the first one - for (Class service : Bukkit.getServicesManager().getKnownServices()) { - try { - service.getField("NBT_BSTATS_VERSION"); // Create only one instance of the nbt-api bstats. - return; - } catch (NoSuchFieldException ignored) { } - try { - service.getField("B_STATS_VERSION"); // Our identifier :) - found = true; // We aren't the first - break; - } catch (NoSuchFieldException ignored) { } - } - // Register our service - Bukkit.getServicesManager().register(ApiMetricsLite.class, this, plugin, ServicePriority.Normal); - if (!found) { - logger.info("[NBTAPI] Using the plugin '" + plugin.getName() + "' to create a bStats instance!"); - // We are the first! - startSubmitting(); - } - } - } - - /** - * Checks if bStats is enabled. - * - * @return Whether bStats is enabled or not. - */ - public boolean isEnabled() { - return enabled; - } - - /** - * Starts the Scheduler which submits our data every 30 minutes. - */ - private void startSubmitting() { - final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags - timer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - if (!plugin.isEnabled()) { // Plugin was disabled - timer.cancel(); - return; - } - // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler - // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) - Bukkit.getScheduler().runTask(plugin, () -> submitData()); - } - }, 1000l * 60l * 5l, 1000l * 60l * 30l); - // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start - // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted! - // WARNING: Just don't do it! - } - - /** - * Gets the plugin specific data. - * This method is called using Reflection. - * - * @return The plugin specific data. - */ - public JsonObject getPluginData() { - JsonObject data = new JsonObject(); - - data.addProperty("pluginName", PLUGINNAME); // Append the name of the plugin - data.addProperty("pluginVersion", PLUGINVERSION); // Append the version of the plugin - data.add("customCharts", new JsonArray()); - - return data; - } - - /** - * Gets the server specific data. - * - * @return The server specific data. - */ - private JsonObject getServerData() { - // Minecraft specific data - int playerAmount; - try { - // Around MC 1.8 the return type was changed to a collection from an array, - // This fixes java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection; - Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers"); - playerAmount = onlinePlayersMethod.getReturnType().equals(Collection.class) - ? ((Collection) onlinePlayersMethod.invoke(Bukkit.getServer())).size() - : ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length; - } catch (Exception e) { - playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed - } - int onlineMode = Bukkit.getOnlineMode() ? 1 : 0; - String bukkitVersion = Bukkit.getVersion(); - String bukkitName = Bukkit.getName(); - - // OS/Java specific data - String javaVersion = System.getProperty("java.version"); - String osName = System.getProperty("os.name"); - String osArch = System.getProperty("os.arch"); - String osVersion = System.getProperty("os.version"); - int coreCount = Runtime.getRuntime().availableProcessors(); - - JsonObject data = new JsonObject(); - - data.addProperty("serverUUID", serverUUID); - - data.addProperty("playerAmount", playerAmount); - data.addProperty("onlineMode", onlineMode); - data.addProperty("bukkitVersion", bukkitVersion); - data.addProperty("bukkitName", bukkitName); - - data.addProperty("javaVersion", javaVersion); - data.addProperty("osName", osName); - data.addProperty("osArch", osArch); - data.addProperty("osVersion", osVersion); - data.addProperty("coreCount", coreCount); - - return data; - } - - /** - * Collects the data and sends it afterwards. - */ - private void submitData() { - final JsonObject data = getServerData(); - - JsonArray pluginData = new JsonArray(); - // Search for all other bStats Metrics classes to get their plugin data - for (Class service : Bukkit.getServicesManager().getKnownServices()) { - try { - service.getField("B_STATS_VERSION"); // Our identifier :) - - for (RegisteredServiceProvider provider : Bukkit.getServicesManager().getRegistrations(service)) { - try { - Object plugin = provider.getService().getMethod("getPluginData").invoke(provider.getProvider()); - if (plugin instanceof JsonObject) { - pluginData.add((JsonObject) plugin); - } else { // old bstats version compatibility - try { - Class jsonObjectJsonSimple = Class.forName("org.json.simple.JSONObject"); - if (plugin.getClass().isAssignableFrom(jsonObjectJsonSimple)) { - Method jsonStringGetter = jsonObjectJsonSimple.getDeclaredMethod("toJSONString"); - jsonStringGetter.setAccessible(true); - String jsonString = (String) jsonStringGetter.invoke(plugin); - JsonObject object = new JsonParser().parse(jsonString).getAsJsonObject(); - pluginData.add(object); - } - } catch (ClassNotFoundException e) { - // minecraft version 1.14+ - if (logFailedRequests) { - logger.log(Level.WARNING, "[NBTAPI][BSTATS] Encountered exception while posting request!", e); - // Not using the plugins logger since the plugin isn't the plugin containing the NBT-Api most of the time - //this.plugin.getLogger().log(Level.SEVERE, "Encountered unexpected exception ", e); - } - continue; // continue looping since we cannot do any other thing. - } - } - } catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { - } - } - } catch (NoSuchFieldException ignored) { } - } - - data.add("plugins", pluginData); - - // Create a new thread for the connection to the bStats server - new Thread(new Runnable() { - @Override - public void run() { - try { - // Send the data - sendData(plugin, data); - } catch (Exception e) { - // Something went wrong! :( - if (logFailedRequests) { - logger.log(Level.WARNING, "[NBTAPI][BSTATS] Could not submit plugin stats of " + plugin.getName(), e); - // Not using the plugins logger since the plugin isn't the plugin containing the NBT-Api most of the time - //plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e); - } - } - } - }).start(); - } - - /** - * Sends the data to the bStats server. - * - * @param plugin Any plugin. It's just used to get a logger instance. - * @param data The data to send. - * @throws Exception If the request failed. - */ - private static void sendData(Plugin plugin, JsonObject data) throws Exception { - if (data == null) { - throw new IllegalArgumentException("Data cannot be null!"); - } - if (Bukkit.isPrimaryThread()) { - throw new IllegalAccessException("This method must not be called from the main thread!"); - } - if (logSentData) { - System.out.println("[NBTAPI][BSTATS] Sending data to bStats: " + data.toString()); - // Not using the plugins logger since the plugin isn't the plugin containing the NBT-Api most of the time - //plugin.getLogger().info("Sending data to bStats: " + data.toString()); - } - HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection(); - - // Compress the data to save bandwidth - byte[] compressedData = compress(data.toString()); - - // Add headers - connection.setRequestMethod("POST"); - connection.addRequestProperty("Accept", "application/json"); - connection.addRequestProperty("Connection", "close"); - connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request - connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length)); - connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format - connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION); - - // Send data - connection.setDoOutput(true); - DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); - outputStream.write(compressedData); - outputStream.flush(); - outputStream.close(); - - InputStream inputStream = connection.getInputStream(); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - - StringBuilder builder = new StringBuilder(); - String line; - while ((line = bufferedReader.readLine()) != null) { - builder.append(line); - } - bufferedReader.close(); - if (logResponseStatusText) { - logger.info("[NBTAPI][BSTATS] Sent data to bStats and received response: " + builder.toString()); - // Not using the plugins logger since the plugin isn't the plugin containing the NBT-Api most of the time - //plugin.getLogger().info("Sent data to bStats and received response: " + builder.toString()); - } - } - - /** - * Gzips the given String. - * - * @param str The string to gzip. - * @return The gzipped String. - * @throws IOException If the compression failed. - */ - private static byte[] compress(final String str) throws IOException { - if (str == null) { - return new byte[0]; - } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - GZIPOutputStream gzip = new GZIPOutputStream(outputStream); - gzip.write(str.getBytes(StandardCharsets.UTF_8)); - gzip.close(); - return outputStream.toByteArray(); - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/GsonWrapper.java b/src/main/java/net/splodgebox/monthlycrates/nbt/utils/GsonWrapper.java deleted file mode 100755 index 1415e51..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/GsonWrapper.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.splodgebox.monthlycrates.nbt.utils; - -import com.google.gson.Gson; - -public class GsonWrapper { - - private static final Gson gson = new Gson(); - - public static String getString(Object obj) { - return gson.toJson(obj); - } - - public static T deserializeJson(String json, Class type) { - try { - if (json == null) { - return null; - } - - T obj = gson.fromJson(json, type); - return type.cast(obj); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/MinecraftVersion.java b/src/main/java/net/splodgebox/monthlycrates/nbt/utils/MinecraftVersion.java deleted file mode 100755 index 23cc038..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/MinecraftVersion.java +++ /dev/null @@ -1,62 +0,0 @@ -package net.splodgebox.monthlycrates.nbt.utils; - -import org.bukkit.Bukkit; - -public enum MinecraftVersion { - Unknown(Integer.MAX_VALUE),//Use the newest known mappings - MC1_7_R4(174), - MC1_8_R3(183), - MC1_9_R1(191), - MC1_9_R2(192), - MC1_10_R1(1101), - MC1_11_R1(1111), - MC1_12_R1(1121), - MC1_13_R1(1131), - MC1_13_R2(1132); - - private static MinecraftVersion version; - private static Boolean hasGsonSupport; - - private final int versionId; - - MinecraftVersion(int versionId) { - this.versionId = versionId; - } - - public int getVersionId() { - return versionId; - } - - public static MinecraftVersion getVersion() { - if (version != null) { - return version; - } - final String ver = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; - System.out.println("[NBTAPI] Found Spigot: " + ver + "! Trying to find NMS support"); - try { - version = MinecraftVersion.valueOf(ver.replace("v", "MC")); - } catch (IllegalArgumentException ex) { - version = MinecraftVersion.Unknown; - } - if (version != Unknown) { - System.out.println("[NBTAPI] NMS support '" + version.name() + "' loaded!"); - } else { - System.out.println("[NBTAPI] Wasn't able to find NMS Support! Some functions may not work!"); - } - return version; - } - - public static boolean hasGsonSupport() { - if (hasGsonSupport != null) { - return hasGsonSupport; - } - try { - System.out.println("Found Gson: " + Class.forName("com.google.gson.Gson")); - hasGsonSupport = true; - } catch (Exception ex) { - hasGsonSupport = false; - } - return hasGsonSupport; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ClassWrapper.java b/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ClassWrapper.java deleted file mode 100644 index 3be8ec7..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ClassWrapper.java +++ /dev/null @@ -1,79 +0,0 @@ -package de.tr7zw.changeme.nbtapi.utils.nmsmappings; - -import java.util.logging.Level; -import org.bukkit.Bukkit; - -import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; -import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.logger; - -/** - * Wraps NMS and CRAFT classes - * - * @author tr7zw - * - */ -@SuppressWarnings("javadoc") -public enum ClassWrapper { -CRAFT_ITEMSTACK(PackageWrapper.CRAFTBUKKIT, "inventory.CraftItemStack"), -CRAFT_ENTITY(PackageWrapper.CRAFTBUKKIT, "entity.CraftEntity"), -CRAFT_WORLD(PackageWrapper.CRAFTBUKKIT, "CraftWorld"), -NMS_NBTBASE(PackageWrapper.NMS, "NBTBase"), -NMS_NBTTAGSTRING(PackageWrapper.NMS, "NBTTagString"), -NMS_NBTTAGINT(PackageWrapper.NMS, "NBTTagInt"), -NMS_ITEMSTACK(PackageWrapper.NMS, "ItemStack"), -NMS_NBTTAGCOMPOUND(PackageWrapper.NMS, "NBTTagCompound"), -NMS_NBTTAGLIST(PackageWrapper.NMS, "NBTTagList"), -NMS_NBTCOMPRESSEDSTREAMTOOLS(PackageWrapper.NMS, "NBTCompressedStreamTools"), -NMS_MOJANGSONPARSER(PackageWrapper.NMS, "MojangsonParser"), -NMS_TILEENTITY(PackageWrapper.NMS, "TileEntity"), -NMS_BLOCKPOSITION(PackageWrapper.NMS, "BlockPosition"), -NMS_WORLDSERVER(PackageWrapper.NMS, "WorldServer"), -NMS_MINECRAFTSERVER(PackageWrapper.NMS, "MinecraftServer"), -NMS_WORLD(PackageWrapper.NMS, "World"), -NMS_ENTITY(PackageWrapper.NMS, "Entity"), -NMS_ENTITYTYPES(PackageWrapper.NMS, "EntityTypes"), -NMS_REGISTRYSIMPLE(PackageWrapper.NMS, "RegistrySimple", MinecraftVersion.MC1_11_R1, MinecraftVersion.MC1_12_R1), -NMS_REGISTRYMATERIALS(PackageWrapper.NMS, "RegistryMaterials"), -NMS_IREGISTRY(PackageWrapper.NMS, "IRegistry"), -NMS_MINECRAFTKEY(PackageWrapper.NMS, "MinecraftKey"), - -; - - private Class clazz; - private boolean enabled = false; - - ClassWrapper(PackageWrapper packageId, String suffix){ - this(packageId, suffix, null, null); - } - - ClassWrapper(PackageWrapper packageId, String suffix, MinecraftVersion from, MinecraftVersion to){ - if(from != null && MinecraftVersion.getVersion().getVersionId() < from.getVersionId()) { - return; - } - if(to != null && MinecraftVersion.getVersion().getVersionId() > to.getVersionId()) { - return; - } - enabled = true; - try{ - String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; - clazz = Class.forName(packageId.getUri() + "." + version + "." + suffix); - }catch(Exception ex){ - logger.log(Level.WARNING, "[NBTAPI] Error while trying to resolve the class '" + suffix + "'!", ex); - } - } - - /** - * @return The wrapped class - */ - public Class getClazz(){ - return clazz; - } - - /** - * @return Is this class available in this Version - */ - public boolean isEnabled() { - return enabled; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ObjectCreator.java b/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ObjectCreator.java deleted file mode 100644 index 71b30a3..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ObjectCreator.java +++ /dev/null @@ -1,55 +0,0 @@ -package de.tr7zw.changeme.nbtapi.utils.nmsmappings; - -import java.lang.reflect.Constructor; -import java.util.logging.Level; - -import de.tr7zw.changeme.nbtapi.NbtApiException; -import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; - -import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.logger; - -/** - * This Enum wraps Constructors for NMS classes - * - * @author tr7zw - * - */ -@SuppressWarnings("javadoc") -public enum ObjectCreator { - NMS_NBTTAGCOMPOUND(null, null, ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()), - NMS_BLOCKPOSITION(null, null, ClassWrapper.NMS_BLOCKPOSITION.getClazz(), int.class, int.class, int.class), - NMS_COMPOUNDFROMITEM(MinecraftVersion.MC1_11_R1, null, ClassWrapper.NMS_ITEMSTACK.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()), - ; - - private Constructor construct; - private Class targetClass; - - ObjectCreator(MinecraftVersion from, MinecraftVersion to, Class clazz, Class... args){ - if(from != null && MinecraftVersion.getVersion().getVersionId() < from.getVersionId()) - return; - if(to != null && MinecraftVersion.getVersion().getVersionId() > to.getVersionId()) - return; - try{ - this.targetClass = clazz; - construct = clazz.getDeclaredConstructor(args); - construct.setAccessible(true); - }catch(Exception ex){ - logger.log(Level.SEVERE, "Unable to find the constructor for the class '" + clazz.getName() + "'", ex); - } - } - - /** - * Creates an Object instance with given args - * - * @param args - * @return Object created - */ - public Object getInstance(Object... args){ - try{ - return construct.newInstance(args); - }catch(Exception ex){ - throw new NbtApiException("Exception while creating a new instance of '" + targetClass + "'", ex); - } - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/PackageWrapper.java b/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/PackageWrapper.java deleted file mode 100644 index 5aa7c9c..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/PackageWrapper.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.tr7zw.changeme.nbtapi.utils.nmsmappings; - -/** - * Package enum - * - * @author tr7zw - * - */ -@SuppressWarnings("javadoc") -public enum PackageWrapper { - NMS("net.minecraft.server"), - CRAFTBUKKIT("org.bukkit.craftbukkit"), - ; - - private final String uri; - - private PackageWrapper(String uri) { - this.uri = uri; - } - - /** - * @return The Uri for that package - */ - public String getUri() { - return uri; - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ReflectionMethod.java b/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ReflectionMethod.java deleted file mode 100644 index 2899064..0000000 --- a/src/main/java/net/splodgebox/monthlycrates/nbt/utils/nmsmappings/ReflectionMethod.java +++ /dev/null @@ -1,183 +0,0 @@ -package de.tr7zw.changeme.nbtapi.utils.nmsmappings; - -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.reflect.Method; -import java.util.logging.Level; - -import org.bukkit.inventory.ItemStack; - -import de.tr7zw.changeme.nbtapi.NbtApiException; -import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; -import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.logger; - -/** - * This class caches method reflections, keeps track of method name changes between versions and allows early checking for problems - * - * @author tr7zw - * - */ -@SuppressWarnings("javadoc") -public enum ReflectionMethod { - - COMPOUND_SET_FLOAT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, float.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setFloat")), - COMPOUND_SET_STRING(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setString")), - COMPOUND_SET_INT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setInt")), - COMPOUND_SET_BYTEARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, byte[].class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setByteArray")), - COMPOUND_SET_INTARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, int[].class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setIntArray")), - COMPOUND_SET_LONG(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, long.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setLong")), - COMPOUND_SET_SHORT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, short.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setShort")), - COMPOUND_SET_BYTE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, byte.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setByte")), - COMPOUND_SET_DOUBLE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, double.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setDouble")), - COMPOUND_SET_BOOLEAN(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, boolean.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setBoolean")), - COMPOUND_MERGE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a")), //FIXME: No Spigot mapping! - COMPOUND_SET(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, ClassWrapper.NMS_NBTBASE.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "set")), - COMPOUND_GET(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "get")), - COMPOUND_GET_LIST(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class, int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getList")), - - COMPOUND_GET_FLOAT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getFloat")), - COMPOUND_GET_STRING(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getString")), - COMPOUND_GET_INT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getInt")), - COMPOUND_GET_BYTEARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getByteArray")), - COMPOUND_GET_INTARRAY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getIntArray")), - COMPOUND_GET_LONG(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getLong")), - COMPOUND_GET_SHORT(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getShort")), - COMPOUND_GET_BYTE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getByte")), - COMPOUND_GET_DOUBLE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getDouble")), - COMPOUND_GET_BOOLEAN(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getBoolean")), - COMPOUND_GET_COMPOUND(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getCompound")), - - NMSITEM_GETTAG(ClassWrapper.NMS_ITEMSTACK.getClazz(), new Class[] {}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getTag")), - NMSITEM_SAVE(ClassWrapper.NMS_ITEMSTACK.getClazz(), new Class[] {ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "save")), - NMSITEM_CREATESTACK(ClassWrapper.NMS_ITEMSTACK.getClazz(), new Class[] {ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_10_R1, new Since(MinecraftVersion.MC1_7_R4, "createStack")), - - COMPOUND_REMOVE_KEY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "remove")), - COMPOUND_HAS_KEY(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "hasKey")), - COMPOUND_GET_TYPE(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "b"), new Since(MinecraftVersion.MC1_9_R1, "d")), //FIXME: No Spigot mapping! - COMPOUND_GET_KEYS(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "c"), new Since(MinecraftVersion.MC1_13_R1, "getKeys")), - - LISTCOMPOUND_GET_KEYS(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "c"), new Since(MinecraftVersion.MC1_13_R1, "getKeys")), - LIST_REMOVE_KEY(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a"), new Since(MinecraftVersion.MC1_9_R1, "remove")), - LIST_SIZE(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "size")), - LIST_SET(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class, ClassWrapper.NMS_NBTBASE.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a"), new Since(MinecraftVersion.MC1_13_R1, "set")), - LEGACY_LIST_ADD(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{ClassWrapper.NMS_NBTBASE.getClazz()}, MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_13_R2, new Since(MinecraftVersion.MC1_7_R4, "add")), - LIST_ADD(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class, ClassWrapper.NMS_NBTBASE.getClazz()}, MinecraftVersion.MC1_14_R1, new Since(MinecraftVersion.MC1_14_R1, "add")), - LIST_GET_STRING(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getString")), - LIST_GET_COMPOUND(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "get")), - LIST_GET(ClassWrapper.NMS_NBTTAGLIST.getClazz(), new Class[]{int.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "g"), new Since(MinecraftVersion.MC1_9_R1, "h"), new Since(MinecraftVersion.MC1_12_R1, "i"), new Since(MinecraftVersion.MC1_13_R1, "get")), - - ITEMSTACK_SET_TAG(ClassWrapper.NMS_ITEMSTACK.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setTag")), - ITEMSTACK_NMSCOPY(ClassWrapper.CRAFT_ITEMSTACK.getClazz(), new Class[]{ItemStack.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "asNMSCopy")), - ITEMSTACK_BUKKITMIRROR(ClassWrapper.CRAFT_ITEMSTACK.getClazz(), new Class[]{ClassWrapper.NMS_ITEMSTACK.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "asCraftMirror")), - - CRAFT_WORLD_GET_HANDLE(ClassWrapper.CRAFT_WORLD.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getHandle")), - NMS_WORLD_GET_TILEENTITY(ClassWrapper.NMS_WORLDSERVER.getClazz(), new Class[]{ClassWrapper.NMS_BLOCKPOSITION.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getTileEntity")), - NMS_WORLD_SET_TILEENTITY(ClassWrapper.NMS_WORLDSERVER.getClazz(), new Class[]{ClassWrapper.NMS_BLOCKPOSITION.getClazz(), ClassWrapper.NMS_TILEENTITY.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setTileEntity")), - NMS_WORLD_REMOVE_TILEENTITY(ClassWrapper.NMS_WORLDSERVER.getClazz(), new Class[]{ClassWrapper.NMS_BLOCKPOSITION.getClazz()}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "t"), new Since(MinecraftVersion.MC1_9_R1, "s"), new Since(MinecraftVersion.MC1_13_R1, "n"), new Since(MinecraftVersion.MC1_14_R1, "removeTileEntity")), - - TILEENTITY_LOAD_LEGACY191(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_MINECRAFTSERVER.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_9_R1, MinecraftVersion.MC1_9_R1, new Since(MinecraftVersion.MC1_9_R1, "a")), //FIXME: No Spigot mapping! - TILEENTITY_LOAD_LEGACY183(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, MinecraftVersion.MC1_9_R2, new Since(MinecraftVersion.MC1_8_R3, "c"), new Since(MinecraftVersion.MC1_9_R1, "a"), new Since(MinecraftVersion.MC1_9_R2, "c")), //FIXME: No Spigot mapping! - TILEENTITY_LOAD_LEGACY1121(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_WORLD.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_10_R1, MinecraftVersion.MC1_12_R1, new Since(MinecraftVersion.MC1_10_R1, "a"), new Since(MinecraftVersion.MC1_12_R1, "create")), - TILEENTITY_LOAD(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_13_R1, new Since(MinecraftVersion.MC1_12_R1, "create")), - - TILEENTITY_GET_NBT(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "b"), new Since(MinecraftVersion.MC1_9_R1, "save")), - TILEENTITY_SET_NBT(ClassWrapper.NMS_TILEENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "a"), new Since(MinecraftVersion.MC1_12_R1, "load")), - - CRAFT_ENTITY_GET_HANDLE(ClassWrapper.CRAFT_ENTITY.getClazz(), new Class[]{}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "getHandle")), - NMS_ENTITY_SET_NBT(ClassWrapper.NMS_ENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "f")), //FIXME: No Spigot mapping! - NMS_ENTITY_GET_NBT(ClassWrapper.NMS_ENTITY.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()}, MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "e"), new Since(MinecraftVersion.MC1_12_R1, "save")), - NMS_ENTITY_GETSAVEID(ClassWrapper.NMS_ENTITY.getClazz(), new Class[]{}, MinecraftVersion.MC1_14_R1,new Since(MinecraftVersion.MC1_14_R1, "getSaveID")), - - NBTFILE_READ(ClassWrapper.NMS_NBTCOMPRESSEDSTREAMTOOLS.getClazz(), new Class[]{InputStream.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a")), //FIXME: No Spigot mapping! - NBTFILE_WRITE(ClassWrapper.NMS_NBTCOMPRESSEDSTREAMTOOLS.getClazz(), new Class[]{ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), OutputStream.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a")), //FIXME: No Spigot mapping! - - - PARSE_NBT(ClassWrapper.NMS_MOJANGSONPARSER.getClazz(), new Class[]{String.class}, MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "parse")), - REGISTRY_KEYSET (ClassWrapper.NMS_REGISTRYSIMPLE.getClazz(), new Class[]{}, MinecraftVersion.MC1_11_R1, MinecraftVersion.MC1_13_R1, new Since(MinecraftVersion.MC1_11_R1, "keySet")), - REGISTRY_GET (ClassWrapper.NMS_REGISTRYSIMPLE.getClazz(), new Class[]{Object.class}, MinecraftVersion.MC1_11_R1, MinecraftVersion.MC1_13_R1, new Since(MinecraftVersion.MC1_11_R1, "get")), - REGISTRY_SET (ClassWrapper.NMS_REGISTRYSIMPLE.getClazz(), new Class[]{Object.class, Object.class}, MinecraftVersion.MC1_11_R1, MinecraftVersion.MC1_13_R1, new Since(MinecraftVersion.MC1_11_R1, "a")), //FIXME: No Spigot mapping! - REGISTRY_GET_INVERSE (ClassWrapper.NMS_REGISTRYMATERIALS.getClazz(), new Class[]{Object.class}, MinecraftVersion.MC1_11_R1, MinecraftVersion.MC1_13_R1, new Since(MinecraftVersion.MC1_11_R1, "b")), //FIXME: No Spigot mapping! - REGISTRYMATERIALS_KEYSET (ClassWrapper.NMS_REGISTRYMATERIALS.getClazz(), new Class[]{}, MinecraftVersion.MC1_13_R1, new Since(MinecraftVersion.MC1_13_R1, "keySet")), - REGISTRYMATERIALS_GET (ClassWrapper.NMS_REGISTRYMATERIALS.getClazz(), new Class[]{ClassWrapper.NMS_MINECRAFTKEY.getClazz()}, MinecraftVersion.MC1_13_R1, new Since(MinecraftVersion.MC1_13_R1, "get")), - - - ; - - private MinecraftVersion removedAfter; - private Since targetVersion; - private Method method; - private boolean loaded = false; - private boolean compatible = false; - private String methodName = null; - - ReflectionMethod(Class targetClass, Class[] args, MinecraftVersion addedSince, MinecraftVersion removedAfter, Since... methodnames){ - this.removedAfter = removedAfter; - MinecraftVersion server = MinecraftVersion.getVersion(); - if(server.compareTo(addedSince) < 0 || (this.removedAfter != null && server.getVersionId() > this.removedAfter.getVersionId()))return; - compatible = true; - Since target = methodnames[0]; - for(Since s : methodnames){ - if(s.version.getVersionId() <= server.getVersionId() && target.version.getVersionId() < s.version.getVersionId()) - target = s; - } - targetVersion = target; - try{ - method = targetClass.getMethod(targetVersion.name, args); - method.setAccessible(true); - loaded = true; - methodName = targetVersion.name; - }catch(NullPointerException | NoSuchMethodException | SecurityException ex){ - logger.log(Level.SEVERE, "Unable to find the method '" + targetVersion.name + "' in '" + targetClass.getSimpleName() + "'", ex); - } - } - - ReflectionMethod(Class targetClass, Class[] args, MinecraftVersion addedSince, Since... methodnames){ - this(targetClass, args, addedSince, null, methodnames); - } - - /** - * Runs the method on a given target object using the given args. - * - * @param target - * @param args - * @return Value returned by the method - */ - public Object run(Object target, Object... args){ - try{ - return method.invoke(target, args); - }catch(Exception ex){ - throw new NbtApiException("Error while calling the method '" + methodName + "', loaded: " + loaded + ".", ex); - } - } - - /** - * @return The MethodName, used in this Minecraft Version - */ - public String getMethodName() { - return methodName; - } - - /** - * @return Has this method been linked - */ - public boolean isLoaded() { - return loaded; - } - - /** - * @return Is this method available in this Minecraft Version - */ - public boolean isCompatible() { - return compatible; - } - - protected static class Since{ - public final MinecraftVersion version; - public final String name; - public Since(MinecraftVersion version, String name) { - this.version = version; - this.name = name; - } - } - -} diff --git a/src/main/java/net/splodgebox/monthlycrates/utils/ItemStackBuilder.java b/src/main/java/net/splodgebox/monthlycrates/utils/ItemStackBuilder.java index e891f71..98fb52b 100644 --- a/src/main/java/net/splodgebox/monthlycrates/utils/ItemStackBuilder.java +++ b/src/main/java/net/splodgebox/monthlycrates/utils/ItemStackBuilder.java @@ -1,8 +1,7 @@ package net.splodgebox.monthlycrates.utils; import com.google.common.collect.Lists; -import net.splodgebox.monthlycrates.nbt.NBTItem; -import net.splodgebox.monthlycrates.utils.Chat; +import de.tr7zw.changeme.nbtapi.NBTItem; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag;