chest drops

Discussion in 'Plugin Development' started by FlugRost, Jul 11, 2015.

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

    FlugRost

    Hey,

    Have anyone an idea to delete all drops of a chest from an explosion?
     
  2. Offline

    Creeperzombi3

    @FlugRost
    Well, what do you have so far?
     
  3. Offline

    FlugRost

    When TNT destroy a chest, the items from the chest are dropping in the world. How can i cancel this?
     
  4. Offline

    teej107

    @FlugRost Going from the top of my head, I think you can get a collection of blocks that are destroyed from a EntityExplosionEvent (name might be off). You can iterate through the blocks and remove the appropriate ones from the collection. After that, just set the removed blocks to air.
     
  5. Offline

    FlugRost

    Code:
    public class OnExplode implements Listener {
    
        private StadtPlugin plugin;
    
        public OnExplode(StadtPlugin plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void onExplode(EntityExplodeEvent e){
            if(!e.isCancelled()){
                for(Block b : e.blockList()){
                    if(b.getType() == Material.CHEST){
                        ((Chest)b).getInventory().clear();
                        b.setType(Material.AIR);
                    }
                }
            }
        }
    }
    I had try this, but it doesn't work. All items were dropping.
     
  6. Offline

    Nic2555

    I would have loop throug the item and add them to à list. Once the chest explode, loop throug the entity of the world and if they appear in the first list, just remove them.
     
  7. Offline

    teej107

    Did you register the event?
     
  8. Offline

    FlugRost

    Code:
    public class OnExplode implements Listener {
        private StadtPlugin plugin;
        public OnExplode(StadtPlugin plugin) {
            this.plugin = plugin;
        }
        @EventHandler
        public void onExplode(EntityExplodeEvent e){
            if(!e.isCancelled()){
                for(Block b : e.blockList()){
                    if(b.getType() == Material.CHEST){
                        ((Chest)b).getInventory().clear();
                        b.setType(Material.AIR);
                        plugin.getServer().getLogger().info("test");
                    }
                }
            }
        }
    }
    The server print "test".

    This is a good idea, but wouldn'd like to look for every entity.
     
  9. Offline

    Nic2555

    @FlugRost yea, but it takes half à millionth of à second to check.
     
  10. Offline

    FlugRost

    mh OK.
    What do you think about this?

    Code:
       
    contens = new ArrayList<Item>();
    
    World w = Bukkit.getWorld("world");
                    removeDrops(w.getChunkAt(b.getLocation()));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(0,0,16)));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(16,0,0)));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(16,0,16)));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(-16,0,0)));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(-16,0,-16)));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(0,0,-16)));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(16,0,-16)));
                    removeDrops(w.getChunkAt(b.getLocation().clone().add(-16,0,16)));
    
    
    private void removeDrops(Chunk chunk){
            for(Entity ent : chunk.getEntities()){
                if(ent.getType() == EntityType.DROPPED_ITEM){
                    ItemStack item = (ItemStack)ent;
                    if(contens.contains(item)){
                        contens.remove(item);
                        ent.remove();
                    }
                }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page