Solved Problems with getInvetory().getContents()

Discussion in 'Plugin Development' started by KillerOfPie, Jul 2, 2017.

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

    KillerOfPie

    So basically I'm trying to get the inventory that a player has just closed and saving the contents to a file to be reloaded later. I have everything working fine unless there is an empty space in the inventory. When there is one the getContents() method seems to stop at the space and not get anything after that. Any help would be appreciated, and if you need any other snippets of code ask and i'll get them

    Code:java
    1.  
    2. @EventHandler
    3. public void onECClose(InventoryCloseEvent e)
    4. {
    5. //only checking for enderchest inventory types
    6. if(e.getInventory().getType() == InventoryType.ENDER_CHEST)
    7. {
    8. Player p = (Player) e.getViewers().get(0);
    9. Yaml pf = PDPmain.getOfflinePlayerYaml(p.getName());
    10. String p2 = pf.getString(shareSelect);
    11.  
    12. //checks the inventory name to make sure im looking at correct inventory
    13. if(e.getInventory().getName() == p2 + "'s Ender Chest")
    14. plugin.saveInv(e.getInventory().getContents(), "Enderchest", p2);
    15.  
    16. //For debugging, prints out contents of the item stack
    17. for(ItemStack itm : e.getInventory().getContents())
    18. p.sendMessage(itm.toString());
    19. }
    20. }


    [​IMG]
     
    Last edited: Jul 5, 2017
  2. Offline

    timtower Administrator Administrator Moderator

    @KillerOfPie It probably throws a NullPointerException in the console. Could you confirm or deny that please?
     
    KillerOfPie likes this.
  3. Offline

    FrostedSnowman

    save it by encoding it in base64(store the string) and decode it when you need it again
     
  4. Offline

    timtower Administrator Administrator Moderator

    And that is gonna help for printing values how?
     
    KillerOfPie and Zombie_Striker like this.
  5. Offline

    FrostedSnowman

    because you can read the encoded string, silly.
     
  6. Offline

    timtower Administrator Administrator Moderator

    Manually? Base64?
     
    KillerOfPie and Zombie_Striker like this.
  7. Offline

    FrostedSnowman

    yes.
     
  8. Offline

    KillerOfPie

    @timtower No npe's. nothing shows up in console at all and @FrostedSnowman I've already checked and the storing isnt an issue
     
  9. Offline

    KillerOfPie

    Alright, so after a day or two of not looking at it i came back with fresh eyes and found they problem. Looks like it was the loading of the inventory from file, whenever there was a space it would stop loading and be finished(got the code online and was too tired to realize it) Ill post the original and fixed code below so that anybody curious can see what changed.

    Original code from a 3 year old post ;)
    Code:java
    1. public ItemStack[] loadInv(String inventoryName) {
    2. //Creates a blank list
    3. List<ItemStack> itemstackList = new ArrayList<ItemStack>();
    4. //Preparing
    5. boolean done = false;
    6. //Starts the counting
    7. int i = -1;
    8. while (done == false) {
    9. i++;
    10. //Checks if the config contains that slot
    11. if (getConfig().contains("Inventory"+"."+inventoryName+"."+i)) {
    12. //Adds the itemstack to the list
    13. itemstackList.add(getConfig().getItemStack("Inventory"+"."+inventoryName+"."+i));
    14. } else {
    15. //No more items, the list is complete
    16. done = true;
    17. }
    18. }
    19. //Some converting and returning
    20. ItemStack[] toReturn = itemstackList.toArray(new ItemStack[itemstackList.size()]);
    21. return toReturn;
    22. }


    Fixed code
    Code:java
    1. public ItemStack[] loadInv(String inventoryName, String playerName)
    2. {
    3. //Opens players PlayerDataPlus config file
    4. Yaml p = PDPmain.getOfflinePlayerYaml(playerName);
    5. //Creates a blank list
    6. List<ItemStack> itemstackList = new ArrayList<ItemStack>();
    7.  
    8. //Preparing
    9. int i = 0, total = 0;
    10.  
    11. //Checks amount of items in inventory
    12. for(String s : p.getConfigurationSection("Inventory." + inventoryName).getKeys(false))
    13. {
    14. //checks if the section is an int(should be the item location in the inventory
    15. if(isInt(s))
    16. {
    17. int n = Integer.parseInt(s);
    18. //makes total the highest inventory location
    19. if(n > total)
    20. total = n;
    21. }
    22. }
    23.  
    24. //Begin loading
    25. while (i <= total)
    26. {
    27. //Checks if the config contains that slot
    28. if (p.contains("Inventory."+inventoryName+"."+i))
    29. //Adds the itemstack to the list
    30. itemstackList.add(p.getItemStack("Inventory." + inventoryName + "." + i));
    31. else
    32. //If it doesnt exist adds air to prevent EC crunching
    33. itemstackList.add(new ItemStack(Material.AIR));
    34.  
    35. i++;
    36. }
    37. //Some converting and returning
    38. ItemStack[] toReturn = itemstackList.toArray(new ItemStack[itemstackList.size()]);
    39.  
    40. return toReturn;
    41. }
     
    Last edited: Jul 5, 2017
Thread Status:
Not open for further replies.

Share This Page