Use CraftItemStack where possible. The meta implementations are fuller. Especially noticeable with Shield banner pattern serialization and deserialization.

This commit is contained in:
Olof Larsson 2016-04-24 10:32:41 +02:00
parent b491cd72f8
commit a6a97d1962
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
4 changed files with 129 additions and 5 deletions

View File

@ -1,8 +1,9 @@
package com.massivecraft.massivecore.item; package com.massivecraft.massivecore.item;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.nms.NmsItemStack;
public abstract class WriterAbstractItemStack<FA, FB> extends WriterAbstract<DataItemStack, ItemStack, DataItemStack, ItemStack, FA, FB> public abstract class WriterAbstractItemStack<FA, FB> extends WriterAbstract<DataItemStack, ItemStack, DataItemStack, ItemStack, FA, FB>
{ {
@ -19,7 +20,7 @@ public abstract class WriterAbstractItemStack<FA, FB> extends WriterAbstract<Dat
@Override @Override
public ItemStack createB() public ItemStack createB()
{ {
return new ItemStack(Material.AIR); return NmsItemStack.get().createItemStack();
} }
} }

View File

@ -3,6 +3,8 @@ package com.massivecraft.massivecore.item;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.nms.NmsItemStack;
public abstract class WriterAbstractMeta<OB, CB, FA, FB> extends WriterAbstract<DataItemStack, OB, DataItemStack, CB, FA, FB> public abstract class WriterAbstractMeta<OB, CB, FA, FB> extends WriterAbstract<DataItemStack, OB, DataItemStack, CB, FA, FB>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -33,7 +35,9 @@ public abstract class WriterAbstractMeta<OB, CB, FA, FB> extends WriterAbstract<
@Override @Override
public CB createB() public CB createB()
{ {
return (CB) new ItemStack(this.getMaterial()).getItemMeta(); ItemStack itemStack = NmsItemStack.get().createItemStack();
itemStack.setType(this.getMaterial());
return (CB) itemStack.getItemMeta();
} }
} }

View File

@ -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;
}
}

View File

@ -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 // 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) public static Method getMethod(Class<?> clazz, String name)
{ {
return getMethod(clazz, name, EMPTY_ARRAY_OF_CLASS); 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") @SuppressWarnings("unchecked")
public static <T> T invokeMethod(Method method, Object target) public static <T> T invokeMethod(Method method, Object target)
{ {
return (T) invokeMethod(method, target, EMPTY_ARRAY_OF_OBJECT); return (T) invokeMethod(method, target, EMPTY_ARRAY_OF_OBJECT);
} }
// -------------------------------------------- //
// CONSTRUCTOR
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public static <T> Constructor<T> getConstructor(Class<?> clazz, Class<?>... parameterTypes)
{
try
{
Constructor<T> ret = (Constructor<T>) clazz.getDeclaredConstructor(parameterTypes);
makeAccessible(ret);
return ret;
}
catch (Exception e)
{
throw asRuntimeException(e);
}
}
public static <T> Constructor<T> getConstructor(Class<?> clazz)
{
return getConstructor(clazz, EMPTY_ARRAY_OF_CLASS);
}
@SuppressWarnings("unchecked")
public static <T> T invokeConstructor(Constructor<?> constructor, Object... arguments)
{
try
{
return (T) constructor.newInstance(arguments);
}
catch (Exception e)
{
throw asRuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public static <T> T invokeConstructor(Constructor<?> constructor)
{
return (T) invokeConstructor(constructor, EMPTY_ARRAY_OF_OBJECT);
}
// -------------------------------------------- // // -------------------------------------------- //
// SINGLETON INSTANCE // SINGLETON INSTANCE
// -------------------------------------------- // // -------------------------------------------- //