Solved Check a chest, get contents and clone to the player inventory

Discussion in 'Plugin Development' started by user_91277742, Aug 6, 2018.

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

    user_91277742

    So, I want to get a chest under a NPC , clone thats items and add to the player inventory.
    I have more or less the code done, But im a little stuck when i want to do the loop :C

    Code:
        public void libros(LivingEntity en,Player p) {
    
     //here Im getting the location from the npc , 3 blocks under him
            Location loc = en.getLocation();
            loc.setY(loc.getY() -3);
    //And here checking if the block is a chest
            Block block = loc.getWorld().getBlockAt(loc);
            if(block.getType() == Material.CHEST) {
    //here is where im stuck. I know i need to do a loop to get all the itemstacks from chest. But i dont know what i //need to put here "adwoijdwaoijdwoij"
                Chest cofre = (Chest) adwoijdwaoijdwoij
                for (ItemStack libros : cofre) {
                    if (libros != null) {
                        ItemStack clone = libros.clone();
                        p.getInventory().setItem(p.getInventory().firstEmpty(), clone);
                    }
                        break;
                }           
            }
       
    
     
  2. Offline

    KarimAKL

    @Ahrigumi
    Do you want to set the player's inventory to exactly how the chest inventory was or do you want to add the items?
    If the first one then i would probably try getting the chest contents and set the player contents to that.
    If the second one then maybe make a loop like this "for (int i = 0; i < chest.getContents().length; i++) {"

    EDIT: I haven't tried the first one but the second one works for me.
     
  3. Offline

    user_91277742

    @KarimAKL Yes, i didnt know that :p , Using
    Code:
                Chest asd= (Chest) block.getState();
                p.getInventory().setContents(asd.getInventory().getContents());
    
    is so easy C:
     
  4. Offline

    KarimAKL

    @Ahrigumi Did it work? If that's the case then i'm glad i could help. :7
     
    user_91277742 likes this.
  5. Offline

    user_91277742

Thread Status:
Not open for further replies.

Share This Page