ItemStack arrays/lists

Discussion in 'Plugin Development' started by EvilKanoa, Jan 27, 2013.

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

    EvilKanoa

    Hey guys, I need a bit of help with some code I'm working on.
    Basically, I want to add someones armor and the item in there hand to a HashMap<Player, ItemStack[]>

    Here is how I thought I would do it:
    inv is the hashmap...
    Code:
    ItemStack[] newinv;               
    newinv.add(player.getInventory().getArmorContents());               
    newinv.add(player.getItemInHand());
    inv.put(player, newinv);
    How should I go about doing this?
     
  2. Offline

    raGan.

    Do NOT use player as a key in hashmap unless you really know what you are doing.
     
  3. Offline

    william9518

    One, use player.getname. And itemstack is a configurationserializable, so just use a list and add to list
     
  4. Offline

    EvilKanoa

    Alright, so player.getName?
    and why?
     
  5. Offline

    raGan.

  6. Offline

    william9518

    Something about hashmap cannot release player object when they leave, so ram cannot release it, causing memory leaks like whole player inventory disappear
     
  7. Offline

    EvilKanoa

    Ahh, scary stuff!

    Anyways, I'm pretty sure I'm doing something stupid here, but how do I initialize the List?
    Code:
    List<ItemStack> newinv;
    newinv.add(player.getInventory().getHelmet());
    newinv.add(player.getInventory().getChestplate());
    newinv.add(player.getInventory().getLeggings());
    newinv.add(player.getInventory().getBoots());
    newinv.add(player.getItemInHand());
    EDIT: Lmao I'm dumb :)
    Code:
    List<ItemStack> newinv = new ArrayList<ItemStack>();
    But now I can't add it to the HashMap cause its not a ItemStack[], its a List<ItemStack>,
    How do I convert? Can I just cast newinv to ItemStack[]?
     
  8. Offline

    william9518

    = new ArrayList<ItemStack>();
    Be sure to use java.util.list not the other one

    No. U cannot. Use a for loop like for int i = 0; i < list.size; i++
    And add them each into the itemstack like itemstack = list.get i

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  9. Offline

    EvilKanoa

    Thanks for the help :)
    I got it working :D
    Turns out you don't need a for loop...just a bit 'o' google
    Code:
                   
    List<ItemStack> newinv = new ArrayList<ItemStack>();
    newinv.add(player.getInventory().getHelmet());
    newinv.add(player.getInventory().getChestplate());
    newinv.add(player.getInventory().getLeggings());
    newinv.add(player.getInventory().getBoots());
    newinv.add(player.getItemInHand());
                   
    ItemStack[] newStack = newinv.toArray(new ItemStack[newinv.size()]);
    inv.put(player.getName(), newStack);
    That works perfectly :)
     
Thread Status:
Not open for further replies.

Share This Page