Bukkit drops exploded blocks

I am using Bukkit API 1.8.3 with Java 7.

I have a code that explodes blocks when you break it. It launches an explosion that breaks blocks around the block you just broke. My problem is that the exploded blocks are not discarded, but only the one that the player has already broken. I tried to fix it by adding this event (also, my events are logged):

@EventHandler
public void onEntityDamage(EntityDamageByBlockEvent e)
{
    if(e.getCause().equals(DamageCause.BLOCK_EXPLOSION))
    {
        if (explosive)
        {
            e.setCancelled(true);
        }
    }
}

      

This prevents damage to the player, but does not block it. I thought that since the dropped block is an entity, this would work. However, it is not. So how do I get the exploded blocks to fall?

This is the code I'm using to explode the block in the first place:

loc.getWorld().createExplosion(loc, lvl1);

      

loc

is the location of the block. lvl1

is the explosion radius float.

+3


source to share


1 answer


The reason is pretty simple: explosions usually do not drop all blocks by default.

However, you can change this: listen BlockExplodeEvent

and then call setYield

with value 1. Add this event handler:

@EventHandler
public void onBlockExplosion(BlockExplodeEvent e) {
    e.setYield(1);
}

      

You might want to add some sort of control as to whether the hack was caused by your plugin.


If that doesn't work, you can use this code:

@EventHandler
public void onBlockExplosion(BlockExplodeEvent e) {
    for (Block block : e.blockList()) {
        block.breakNaturally();
    }
}

      



The above code works, but it will probably break any other plugin that works with blockList

, since all blocks will become airy. So I highly recommend using the first code if possible.


BlockExplodeEvent appears only in the newest versions; if you don't have it, you will need to update it. It is found in both 1.8, but only the most recent updates.

Here you will need:

<repositories>
    <repository>
        <id>spigot-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.bukkit</groupId>
        <artifactId>bukkit</artifactId>
        <version>1.8.3-R0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

      

You will also need to do Maven → Update and then make sure the Force Update of Shapshots / Releases is checked.

+1


source







All Articles