Inventory Manipulation

Discussion in 'Plugin Development' started by russ9929, Jun 26, 2012.

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

    russ9929

    So I want to do a dungeon plugin where on the command /joindungeon the players inventory is save in a hashmap that I made then I used

    Code:
        public void storeInventories(Player[] players){
            for(Player p : players){
                inventories.put(p, p.getInventory());
            }
        }
    to save the inventories. Now I want to give them there inventory back and a reward of diamonds. So how would I replace the inventory and give the new items.
     
  2. Offline

    EnvisionRed

    I don't think your current hashmap will work. You should probably store "p.getInventory().getContents()" which returns an ItemStack array as opposed to the player's inventory object.
    Change the type of the inventories hashmap to <Player, ItemStack[]> and then have it save their items like so:
    Code:
    for(player p : players) {
      inventories.put(p, p.getInventory().getContents());
    }

    Then to return their items maybe something like:
    Code:
    public void restoreInventories(Player[] players){
        for(Player p : players){
            if (inventories.containsKey(p)) {
            ItemStack[] items = inventory.get(p);
            for(ItemStack s : items) {
                p.getInventory().addItem(s);
                }
            }
        }
    }
    Idk if that would work but I think it's worth a shot.
     
Thread Status:
Not open for further replies.

Share This Page