Need second pair of eyes. Item not adding to inventory.

Discussion in 'Plugin Development' started by hubeb, Sep 10, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    hubeb

    Hey, I'm using an updated inventory serializer modified to accommodate item names and lore.
    The serializer works when converting items to strings:
    27;0#n@Sharpiet@276:e@21@3:e@20@2:e@48@5;
    and it saves to my database and flatfile (whichever is enabled). But when I try to load the serialized string it will not work for named or lore items. So if anyone can lend their eyes to helping me see what Ive done wrong it will be greatly appreciated.

    Here is the code to deserialize the string and add items to the inventory:
    Code:java
    1. public static ItemStack[] StringToInventory(String invString) {
    2. String[] serializedBlocks = invString.split(";");
    3. String invInfo = serializedBlocks[0];
    4. Inventory deserializedInventory = Bukkit.getServer().createInventory(null, Integer.valueOf(invInfo));
    5.  
    6. for (int i = 1; i < serializedBlocks.length; i++) {
    7. String[] serializedBlock = serializedBlocks[i].split("#");
    8. int stackPosition = Integer.valueOf(serializedBlock[0]);
    9.  
    10. if (stackPosition >= deserializedInventory.getSize()) {
    11. continue;
    12. }
    13. ItemStack is = null;
    14. Boolean createdItemStack = false;
    15. String[] serializedItemStack = serializedBlock[1].split(":");
    16. ItemMeta im = null;
    17. for (String itemInfo : serializedItemStack) {
    18. String[] itemAttribute = itemInfo.split("@");
    19. if (itemAttribute[0].equals("t")) {
    20. is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
    21. im = is.getItemMeta();
    22. createdItemStack = true;
    23. } else if (itemAttribute[0].equals("d") && createdItemStack) {
    24. is.setDurability(Short.valueOf(itemAttribute[1]));
    25. } else if (itemAttribute[0].equals("a") && createdItemStack) {
    26. is.setAmount(Integer.valueOf(itemAttribute[1]));
    27. } else if (itemAttribute[0].equals("e") && createdItemStack) {
    28. is.addUnsafeEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]));
    29. } else if (itemAttribute[0].equals("n") && createdItemStack) {
    30. im.setDisplayName(itemAttribute[1]);
    31. } else if (itemAttribute[0].equals("l") && createdItemStack){
    32. im = is.getItemMeta();
    33. List<String> lr = new ArrayList<>();
    34. for(String s : itemAttribute[2].split("|")){
    35. lr.add(s);
    36. }
    37. im.setLore(lr);
    38. }
    39. }
    40. if(im != null) {
    41. is.setItemMeta(im);
    42. }
    43. deserializedInventory.setItem(stackPosition, is);
    44. }
    45. return deserializedInventory.getContents();
    46. }
    47.  
    48. //CODE USED TO LOAD INTO THE INV
    49.  
    50. Inventory inv = Bukkit.getServer().createInventory(e.getPlayer(), InventoryType.CHEST, ChatColor.GOLD+"Item Bank");
    51. inv.setContents(SerializeInv.StringToInventory(s)); // s = string path and it isn't null[/i]


    Also, im not getting any errors.
     
  2. Offline

    valon750

    hubeb

    I was under the impression that ItemStack serialising as it is will account for item names and lore :S
     
  3. Offline

    hubeb

  4. Offline

    valon750

    hubeb

    I'm fairly sure that ConfigurationSection's now have getItemStack and setItemStack.
     
  5. Offline

    hubeb

Thread Status:
Not open for further replies.

Share This Page