Solved Chest Contents

Discussion in 'Plugin Development' started by Coopah, Apr 15, 2015.

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

    Coopah

    How would I get all the contents in a chest and then calling them to Item. I want to be able to shoot the items out of a chest into the sky.
     
  2. Cast the block to Chest and use chest.getBlockInventory()
     
    Coopah likes this.
  3. Offline

    undeaD_D

    Code:
        /*
         * import org.bukkit.block.Block;
         * import org.bukkit.block.Chest;
         * import org.bukkit.entity.Item;
         * import org.bukkit.inventory.ItemStack;
         * import org.bukkit.util.Vector;
         */
      
        public void explodeChest(Block b) {
           // check if block is a chest
            if(b.getState() instanceof Chest) {
    
                // get the content of the chest and clone it
                ItemStack[] items = ((Chest) b.getState()).getInventory().getContents().clone();
    
                // clear the content of the chest (prevent duplication)
                ((Chest) b.getState()).getInventory().clear();
    
                // iterate through all items
                for(ItemStack item : items) {
                    // prevent nullpointer exception from empty slots
                    if(item != null) {
                        // drop item one block above the chest
                        Item i = b.getWorld().dropItem(b.getLocation().add(0, 1, 0), item);
                        // add upwards velocity to the item
                        i.setVelocity(new Vector(0,.5,0));
                    }
                }
          
            }  
        }
     
    Last edited: Apr 15, 2015
    Coopah likes this.
  4. Offline

    nverdier

    Dude. Really?!?!
     
    FisheyLP likes this.
  5. ! Spoonfeed alert !
     
  6. Offline

    Signatured

    He just wanted to help...

    @undeaD_D I suggest you comment your code to explain what it does. That way it's not 100% spoonfeeding. Thank you for helping :)
     
    undeaD_D likes this.
  7. Offline

    nverdier

    @Signatured It's still spoonfeeding even if you comment your code.
     
  8. Offline

    Signatured

    At least he will know what he's doing rather than just copy/pasting.
     
  9. Offline

    nverdier

    @Signatured But he could still just copy and paste and not read the comments. The best thing to do is just give him 'pseudocode' instructions.
     
  10. Offline

    Coopah

    @FisheyLP Thanks, I tried basically that but I forgot the BlockInventory part.

    @undeaD_D
    How would I go about shooting the items slower, like instead of shooting them all at once?
     
    Last edited by a moderator: Apr 15, 2015
  11. @Coopah
    Make a repeating task (and cancel it if the chest is empty)
     
  12. Offline

    Coopah

    @FisheyLP
    Yes, but how would I shoot the items out individually?
     
  13. Offline

    bennie3211

    @Coopah get the first item from a list of all items, shoot it, remove it from list and repeat
     
  14. Inside the repeating task
    Loop through the items -> if it is not air -> shoot it -> remove the item from inventory -> break;
     
Thread Status:
Not open for further replies.

Share This Page