How can I fill a chest of this type?

Discussion in 'Spigot Plugin Development' started by Vrsd12, May 3, 2017.

  1. How can I fill a chest of this type?
    Code (Text):
    FallingBlock block = map.spawnFallingBlock(objspawn, Material.CHEST, (byte) 0);
     
  2. I would create an inventory and put whatever you want in there and attach it to a chest and put that as Material.CHEST
     
  3. WAS

    WAS

    That, is technically, not a chest, it is a falling entity, that looks like a chest. So, you need to determine where the falling block lands, spawn a chest of your type with your inventory of custom items, or fill the chest normally.
     
  4. It's no good because I'm doing it with kits ;)
     
  5. And something like that
    Code (Text):
    public void ChestItem() {        
             ItemStack item = new ItemStack(Material.CHEST, 1);
                ItemMeta meta = item.getItemMeta();
                meta.setDisplayName("┬žaSupply Drop");
                item.setItemMeta(meta);
               
             
         }
     
  6. It has nothing to do with what you're doing it with... Just listen to:
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityChangeBlockEvent.html
    and check if the entity is one of the falling sand objects from your plugin. Then get the resulting block's state and cast to chest (making sure it is actually a chest as good code would dictate), and add your items to its inventory (retrieved through #getBlockInventory()).
     
    • Agree Agree x 1
  7. Code (Text):
         @EventHandler
        public void fallingChest(EntityChangeBlockEvent event){
             if (event.getEntityType() == EntityType.FALLING_BLOCK) {
            if (event.getEntity() instanceof FallingBlock){
                FallingBlock fb = (FallingBlock) event.getEntity();
                if (fb.getMaterial() == Material.CHEST){
                    event.setCancelled(true);
                    event.getBlock().setType(Material.CHEST);
                    Chest chest = (Chest) event.getBlock().getState();
                    Inventory inv = chest.getInventory();
                    inv.addItem(new ItemStack(Material.WOOD,1));
                }
            }
        }
      }
     
  8. or
    Code (Text):
        public void fallingChest(EntityChangeBlockEvent event){
              FallingBlock fallingBlock = (FallingBlock) event.getEntity();
              if (fallingBlock.getMaterial() == Material.CHEST) {
                     Chest chest = (Chest) event.getBlock().getState();
                    Inventory inv = chest.getInventory();
                    inv.addItem(new ItemStack(Material.WOOD,1));
              }
        }