Solved Editing Every Item In A Chest

Discussion in 'Plugin Development' started by Quidam, Jun 14, 2013.

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

    Quidam

    Okay, so I'm trying to edit the ItemMeta of every item in a virtual chest, to include Lore. I've created a for loop for decreasing an integer, and then getting the item in that slot of the chest using the integer. However, my code isn't working.
    There is no error in the console or in Eclipse.

    Here is my code:
    Code:java
    1. @EventHandler
    2. public void onPlayerInventoryClose(InventoryCloseEvent e){
    3. Inventory inv = e.getInventory();
    4. Player p = (Player) e.getPlayer();
    5. if(inv.getTitle().equalsIgnoreCase("VirtualStorage")){
    6. int invlength = inv.getContents().length;
    7. for (int iterate = invlength; iterate == 0; iterate --){
    8. ItemMeta meta = inv.getItem(iterate).getItemMeta();
    9. ArrayList<String> lore = new ArrayList<String>();
    10. lore.add("VirtualStorage");
    11. inv.getItem(iterate).setItemMeta(meta);
    12. }
    13. save.put(p, inv);
    14. }
    15. }

    Note: "save.put(p, inv);" is simply saving the inventory in a HashMap.

    Any help would be greatly appreciated! Thank you.
     
  2. Offline

    geNAZt

    Well i would not iterate with an Integer

    Code:java
    1. @EventHandler
    2. public void onPlayerInventoryClose(InventoryCloseEvent e){
    3. Inventory inv = e.getInventory();
    4. Player p = (Player) e.getPlayer();
    5.  
    6. if(inv.getTitle().equalsIgnoreCase("VirtualStorage")){
    7. ItemStack[] invContent = inv.getContents();
    8.  
    9. //Nothing in this Chest
    10. if(invContent == null) return;
    11.  
    12. //Get every piece in this inventory
    13. for(ItemStack itemStack : invContent) {
    14. ItemMeta meta = itemStack.getItemMeta();
    15. ArrayList<String> lore = new ArrayList<String>();
    16. lore.add("VirtualStorage");
    17. itemStack.setItemMeta(meta);
    18. }
    19.  
    20. save.put(p, invContent.length);
    21. }
    22. }
     
  3. Offline

    Quidam

    For some reason, that deletes every item in the chest.

    EDIT: I am saving the inventory, not its length, if that makes any difference.
    EDIT: inv.setContents() was missing from the code, added it and still deletes every item
     
  4. Offline

    geNAZt

    If getContents() gets the content out of a chest this is the expected behaviour.

    You can place the items in the inventory after changing them.

    Code:java
    1. //Get every piece in this inventory
    2. for(ItemStack itemStack : invContent) {
    3. ItemMeta meta = itemStack.getItemMeta();
    4. ArrayList<String> lore = new ArrayList<String>();
    5. lore.add("VirtualStorage");
    6. itemStack.setItemMeta(meta);
    7. inv.addItem(itemStack);
    8. }
     
  5. Offline

    Quidam

    geNAZt
    Figured I'd make this independent, not an edit, since it is significant.
    I checked for errors, and found that the ItemMeta is null.

    EDIT: No, that is false. ItemMeta is not null, the actual ItemStack is null.
     
  6. Offline

    geNAZt

    And if ItemMeta is set to null the itemstack disappears ? Interessing, sounds like a bug :D
     
  7. Offline

    Quidam

    Update:
    Converted to List and iterated through list. Still nothing.
     
  8. Offline

    geNAZt

    So if a ItemStack is null you also can check the whole inventory for items.

    I rewrote the thing a bit and i think this should finally work:

    Code:java
    1. @EventHandler
    2. public void onPlayerInventoryClose(InventoryCloseEvent e){
    3. Inventory inv = e.getInventory();
    4. Player p = (Player) e.getPlayer();
    5.  
    6. if(inv.getTitle().equalsIgnoreCase("VirtualStorage")){
    7. ItemStack[] invContent = inv.getContents();
    8.  
    9. //Nothing in this Chest
    10. if(invContent == null) return;
    11.  
    12. //Get every piece in this inventory
    13. for(ItemStack itemStack : invContent) {
    14. if(itemStack == null) continue;
    15.  
    16. ItemMeta meta = itemStack.getItemMeta();
    17. if(meta == null) continue;
    18.  
    19. ArrayList<String> lore = new ArrayList<String>();
    20. lore.add("VirtualStorage");
    21. meta.setLore(lore);
    22. itemStack.setItemMeta(meta);
    23. }
    24.  
    25. save.put(p, invContent.length);
    26. }
    27. }
    28.  
     
  9. Offline

    Quidam

    Thank you so much! It worked like a charm!
     
Thread Status:
Not open for further replies.

Share This Page