Search code examples
javaminecraftbukkit

Bukkit event thrown on creeper destruction or enderman block-taking?


What is the name of the event that gets triggered when a creeper destroys blocks, and how do I use it?

Similarly, when an Enderman steals a block from the world, another event is called, what is it and how is it used?


Solution

  • When a Creeper destroys blocks via explosion, the event EntityExplodeEvent is called. You can check an example on how to use it below:

    @EventHandler
    public void onCreeperExplode(EntityExplodeEvent e) {
        Entity entity = e.getEntity();
        if (entity.getType().equals(EntityType.CREEPER)) {
        //It's a creeper
        //You can cancel it
        e.setCancelled(true);    //This prevents damage
    
        //Or cancel the block destruction
        e.blockList().clear();
        }
    }
    

    For the Enderman block-taking, you can use an EntityBlockChangeEvent, called whenever an entity changes a block (excluding players).

    @EventHandler
    public void onEndermanBlockTake(EntityChangeBlockEvent e) {
        Entity entity = e.getEntity();
        if (entity.getType().equals(EntityType.Enderman)) {
            //It's an enderman
            Block b = e.getBlock();    //Getting the block
            e.setCancelled(true);    //Cancelling the event
        }
    }