I have this bit of code to turn items into strings: Spoiler Code (Text): public static void main(String[] args) { ItemStack empty = null; ItemStack itemStack = new ItemStack(Material.APPLE); String item = convertItemToString(itemStack.clone()); System.out.print("" + item); empty = readItemString(item); System.out.print("" + empty.getAmount() + " , " + empty.getType()); } public static String convertItemToString(ItemStack i) { String name = "%" + i.getType() + "~" + i.getAmount() + "~"; if(i.hasItemMeta()){ if(i.getItemMeta().hasDisplayName()) { name += (i.getItemMeta().getDisplayName() + "~"); } else { name += "null~"; } if(i.getItemMeta().hasLore()) { name += (i.getItemMeta().getLore().get(0) + "~"); } else { name += "null~"; } } else { name += "null~null~"; } if(i.getEnchantments().size()>0) { for(Enchantment e : i.getEnchantments().keySet()) { int power = i.getEnchantments().get(e); name += (e.getName() + "*" + power); } } return name; } However, when I run it, this error is the result and I can't figure out why... Code (Text): Exception in thread "main" java.lang.NullPointerException at org.bukkit.Bukkit.getItemFactory(Bukkit.java:1113) at org.bukkit.inventory.ItemStack.hasItemMeta(ItemStack.java:540) at vanillaamplified.main.MainTesting.convertItemToString(MainTesting.java:25) at vanillaamplified.main.MainTesting.main(MainTesting.java:17) This is the line where it is saying the error is happening: Code (Text): if(i.hasItemMeta()){ Please help me before I go insane.
I would not recommend you to write your own serialization method for ItemStacks You can just use this (Or JsonConfiguration should also work) to serialize: Code (Java): public static String itemToStringBlob(ItemStack itemStack) { YamlConfiguration config = new YamlConfiguration(); config.set("i", itemStack); return config.saveToString(); } And this to deserialize: Code (Java): public static ItemStack stringBlobToItem(String stringBlob) { YamlConfiguration config = new YamlConfiguration(); try { config.loadFromString(stringBlob); } catch (Exception e) { e.printStackTrace(); return null; } return config.getItemStack("i", null); }
Oh, alright, thought it was possible to test things in the main method before going to the server but I guessed wrong. Thanks for the help.
Yeah, I was going to do this at first but I ran into some issues with using it so I was just making a simple version for myself. I'm mostly just writing this for something I'll use once so efficiency isn't a priority..... yet.