From a6a97d19628c1b4de1f800f5a036b2f066f34c22 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 24 Apr 2016 10:32:41 +0200 Subject: [PATCH] Use CraftItemStack where possible. The meta implementations are fuller. Especially noticeable with Shield banner pattern serialization and deserialization. --- .../item/WriterAbstractItemStack.java | 5 +- .../massivecore/item/WriterAbstractMeta.java | 6 +- .../massivecore/nms/NmsItemStack.java | 75 +++++++++++++++++++ .../massivecore/util/ReflectionUtil.java | 48 +++++++++++- 4 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 src/com/massivecraft/massivecore/nms/NmsItemStack.java diff --git a/src/com/massivecraft/massivecore/item/WriterAbstractItemStack.java b/src/com/massivecraft/massivecore/item/WriterAbstractItemStack.java index b7b5f9c1..2589a229 100644 --- a/src/com/massivecraft/massivecore/item/WriterAbstractItemStack.java +++ b/src/com/massivecraft/massivecore/item/WriterAbstractItemStack.java @@ -1,8 +1,9 @@ package com.massivecraft.massivecore.item; -import org.bukkit.Material; import org.bukkit.inventory.ItemStack; +import com.massivecraft.massivecore.nms.NmsItemStack; + public abstract class WriterAbstractItemStack extends WriterAbstract { @@ -19,7 +20,7 @@ public abstract class WriterAbstractItemStack extends WriterAbstract extends WriterAbstract { // -------------------------------------------- // @@ -33,7 +35,9 @@ public abstract class WriterAbstractMeta extends WriterAbstract< @Override public CB createB() { - return (CB) new ItemStack(this.getMaterial()).getItemMeta(); + ItemStack itemStack = NmsItemStack.get().createItemStack(); + itemStack.setType(this.getMaterial()); + return (CB) itemStack.getItemMeta(); } } diff --git a/src/com/massivecraft/massivecore/nms/NmsItemStack.java b/src/com/massivecraft/massivecore/nms/NmsItemStack.java new file mode 100644 index 00000000..72aea6f6 --- /dev/null +++ b/src/com/massivecraft/massivecore/nms/NmsItemStack.java @@ -0,0 +1,75 @@ +package com.massivecraft.massivecore.nms; + +import java.lang.reflect.Constructor; +import org.bukkit.inventory.ItemStack; + +import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; +import com.massivecraft.massivecore.util.ReflectionUtil; + +public class NmsItemStack extends NmsAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static NmsItemStack i = new NmsItemStack(); + public static NmsItemStack get () { return i; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + // net.minecraft.server.ItemStack + public Class classNmsItemStack; + + // org.bukkit.craftbukkit.inventory.CraftItemStack + public Class classCraftItemStack; + + // org.bukkit.craftbukkit.inventory.CraftItemStack(net.minecraft.server.ItemStack) + public Constructor constructorCraftItemStack; + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + // NOTE: This has been properly researched. + // NOTE: The constructor have been the same for quite some time! + @Override + public int getRequiredVersion() + { + return 6; + } + + @Override + protected void setup() throws Throwable + { + this.classNmsItemStack = PackageType.MINECRAFT_SERVER.getClass("ItemStack"); + this.classCraftItemStack = PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftItemStack"); + this.constructorCraftItemStack = ReflectionUtil.getConstructor(this.classCraftItemStack, this.classNmsItemStack); + } + + // -------------------------------------------- // + // METHOD + // -------------------------------------------- // + + @SuppressWarnings("deprecation") + public ItemStack createItemStack() + { + // Create + ItemStack ret = null; + + // Fill + if (this.isAvailable()) + { + ret = ReflectionUtil.invokeConstructor(this.constructorCraftItemStack, (Object[])null); + } + else + { + ret = new ItemStack(0); + } + + // Return + return ret; + } + +} diff --git a/src/com/massivecraft/massivecore/util/ReflectionUtil.java b/src/com/massivecraft/massivecore/util/ReflectionUtil.java index 45adaf0e..32b264e9 100644 --- a/src/com/massivecraft/massivecore/util/ReflectionUtil.java +++ b/src/com/massivecraft/massivecore/util/ReflectionUtil.java @@ -31,6 +31,9 @@ public class ReflectionUtil } } + private static final Class[] EMPTY_ARRAY_OF_CLASS = {}; + private static final Object[] EMPTY_ARRAY_OF_OBJECT = {}; + // -------------------------------------------- // // MAKE ACCESSIBLE // -------------------------------------------- // @@ -96,7 +99,6 @@ public class ReflectionUtil } } - private static final Class[] EMPTY_ARRAY_OF_CLASS = {}; public static Method getMethod(Class clazz, String name) { return getMethod(clazz, name, EMPTY_ARRAY_OF_CLASS); @@ -115,13 +117,55 @@ public class ReflectionUtil } } - private static final Object[] EMPTY_ARRAY_OF_OBJECT = {}; @SuppressWarnings("unchecked") public static T invokeMethod(Method method, Object target) { return (T) invokeMethod(method, target, EMPTY_ARRAY_OF_OBJECT); } + // -------------------------------------------- // + // CONSTRUCTOR + // -------------------------------------------- // + + @SuppressWarnings("unchecked") + public static Constructor getConstructor(Class clazz, Class... parameterTypes) + { + try + { + Constructor ret = (Constructor) clazz.getDeclaredConstructor(parameterTypes); + makeAccessible(ret); + return ret; + } + catch (Exception e) + { + throw asRuntimeException(e); + } + } + + public static Constructor getConstructor(Class clazz) + { + return getConstructor(clazz, EMPTY_ARRAY_OF_CLASS); + } + + @SuppressWarnings("unchecked") + public static T invokeConstructor(Constructor constructor, Object... arguments) + { + try + { + return (T) constructor.newInstance(arguments); + } + catch (Exception e) + { + throw asRuntimeException(e); + } + } + + @SuppressWarnings("unchecked") + public static T invokeConstructor(Constructor constructor) + { + return (T) invokeConstructor(constructor, EMPTY_ARRAY_OF_OBJECT); + } + // -------------------------------------------- // // SINGLETON INSTANCE // -------------------------------------------- //