Solved Iterating through a player's inventory slots

Discussion in 'Plugin Development' started by AoH_Ruthless, Feb 9, 2014.

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

    AoH_Ruthless

    Hey, I am working on a private kit plugin and to create a kit, I want it to copy the player's inventory and save it to a config. To do this, I am attempting to iterate through a player's inventory slots (0-36) and set them in a config. My question is if I'm iterating through it properly, because I've not really worked with this sort of stuff. Here is the code:

    Code:java
    1. String path = kitName + ".";
    2. kits.createSection(kitName);
    3.  
    4. // Iterate through all item slots in the player's inventory.
    5. for (int i = 36; --i >= 0;) {
    6. ItemStack is = p.getInventory().getItem(i);
    7.  
    8. // If null, loop through the next item and return.
    9. if (is == null) {
    10. i++;
    11. return;
    12. }
    13. String slot = path + "items." + i;
    14. kits.set(slot +".type", is.getType());
    15.  
    16. if (!is.hasItemMeta()) {
    17. i++;
    18. return;
    19. }
    20.  
    21. if (is.getItemMeta().hasDisplayName())
    22. kits.set(slot + ".name", is.getItemMeta().getDisplayName());
    23. i++;
    24. }
    25. plugin.saveConfig();

    p is the player, plugin is the main class, kits is a configuration section ("kits")

    This is how I want the config to look:
    Code:
    kits:
      kitName:
        items:
          '0':
            type: DIAMOND
            name: 'blah'
    Something like that.
    You may be asking: Why aren't I testing it?? This is because the plugin is not even closed to finished yet, and because a lot of methods are right now not-complete the plugin probably won't even work. I want to get this sorted out of the way before I get too far into the kits and have to change a lot of code.

    Thank you!
     
  2. Offline

    Gater12

    AoH_Ruthless
    Why don't you just loop through p.getInventory().getContents() ?
     
  3. Offline

    AoH_Ruthless

    Gater12
    How do I get the slot of the item with that?
     
  4. Offline

    Gater12

    AoH_Ruthless
    Got a point there....

    I'm a bit confused on your for loop... i is 36, if one less i >= 0, increment i ?
     
  5. Offline

    AoH_Ruthless

    Gater12
    I think I meant to do:
    Code:java
    1. for (int i = 0; i < 36; i++) {}


    Gater12
    Is that code right then (with the modified for loop)? I would have edited the post but I don't think you would have saw it so sorry.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
    Gater12 likes this.
  6. Offline

    Gater12

    AoH_Ruthless likes this.
Thread Status:
Not open for further replies.

Share This Page