Get items in chest and add to players inventory

Discussion in 'Plugin Development' started by Switchbladed, Nov 5, 2012.

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

    Switchbladed

    What I'm trying to do is to, instead of opening a chest, give the player the items inside the chest, but also keep the items inside the chest, then I want to deny the player who opened the chest to open it again. So they can get the items, but they can't get it a 2nd time and it will display them a message telling them so.
    Then another player can come along and do the same thing and get the same items.

    Here is what I have:

    Code:
        public void onChestOpen(PlayerInteractEvent event) {
            Player p = (Player) event.getPlayer();
            Block block = event.getClickedBlock();
            if(event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && block != null);
            {
                if(block.getState() instanceof Chest || block.getState() instanceof DoubleChest)
                {
                    event.setCancelled(true);
                    Chest chest = (Chest) block.getState();
                    Inventory inv = chest.getBlockInventory();
                    p.getInventory().addItem(inv); //atm I'm getting errors on this line, and not sure what I need to do.
                   
                }
    Thanks in advance.

    Anyone got any idea??

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

    CevinWa

    event.setCancelled(true);
    Chest chest = ((Chest) block.getState());
    Inventory inv = chest.getBlockInventory();
    p.getInventory().addItem(inv); //atm I'm getting errors on this line, and not sure what I need to do.

    }


    Just (() theese you had wrong. That one just cast the block but not gets it's state the double cast cast's everything inside of ()
     
  3. Offline

    Switchbladed

    I still get errors on addItem
     
  4. Offline

    LukeSFT

    You cannot add a hole inventory with addItem().
    I think you have to get all Itemstacks, that are in inv and add them one by one to the players inventory.
     
  5. Offline

    Switchbladed

    Could I make a List or HashMap to do it?
     
  6. Offline

    LukeSFT

    Yes.

    I would recommend a list.
     
  7. Offline

    Switchbladed

    Ok I will try that soon. Thanks

    Ok, I got it working, here's how I did it:

    Code:
    Chest chest = ((Chest) block.getState());
    ItemStack[] items = chest.getInventory().getContents();
    p.getInventory().addItem(items);
    p.updateInventory();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  8. Offline

    LukeSFT

    Then sign your thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page