package team.unstudio.udpl.item;

import org.bukkit.inventory.ItemStack;

import team.unstudio.udpl.core.UDPLib;
import team.unstudio.udpl.nms.ReflectionUtils;
import team.unstudio.udpl.nms.ReflectionUtils.PackageType;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class ItemHelper {

	private ItemHelper() {}
	
	private static final boolean DEBUG = UDPLib.isDebug();
	
	public static Object getNMSItemStack(ItemStack item) {
		try {
			Method asNMSCopy = ReflectionUtils.getMethod(PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftItemStack"),
					"asNMSCopy", ItemStack.class);
			return asNMSCopy.invoke(PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftItemStack"), item);
		} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | IllegalArgumentException
				| InvocationTargetException e) {
			if (DEBUG)
				e.printStackTrace();
		}
		return null;
	}
	   
    /**
     * 转换为JSON格式
     * @param itemStack 物品
     * @return 
     */
    public static String toJson(ItemStack itemStack){
		try {
			Class<?> ccitemstack = ReflectionUtils.PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftItemStack");
			Class<?> citemstack = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("ItemStack");
			Class<?> cmap = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("NBTTagCompound");
			Object nbt = cmap.newInstance();
			Method asnmscopy = ccitemstack.getDeclaredMethod("asNMSCopy", ItemStack.class);
			asnmscopy.setAccessible(true);
			Method save = citemstack.getDeclaredMethod("save", cmap);
			save.setAccessible(true);
			return save.invoke(asnmscopy.invoke(null, itemStack), nbt).toString();
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
			if(DEBUG)
				e.printStackTrace();
		}
		return "";
    }
}

@@ -307,16 +307,80 @@
 Method;%0D
+%0Aimport java.util.Optional;%0D%0A%0D%0Aimport javax.annotation.Nonnull;%0D
 %0A%0D%0Apubli
@@ -513,22 +513,32 @@
  static 
+Optional%3C
 Object
+%3E
  getNMSI
@@ -736,92 +736,57 @@
 urn 
-asNMSCopy.invoke(PackageType.CRAFTBUKKIT_INVENTORY.getClass(%22CraftItemStack%22)
+Optional.ofNullable(asNMSCopy.invoke(null
 , item)
+)
 ;%0D%0A%09
@@ -988,20 +988,32 @@
 %09return 
-null
+Optional.empty()
 ;%0D%0A%09%7D%0D%0A%09
@@ -1089,16 +1089,16 @@
 eturn %0D%0A
-
      */%0D
@@ -1098,16 +1098,27 @@
     */%0D%0A
+%09@Nonnull%0D%0A
     publ

Update ItemHelper