3

I am using the Bukkit 1.8.3 API with Java 7.

I have code that explodes blocks upon 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 dropping, just the one that the player has broke already. I have tried to fix it by adding this event (also, my events are registered):

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

This stops the player from being damaged, but not the blocks from being dropped. I thought that since the dropped block is an entity this would work. However it is not. So how would I get the exploded blocks to drop?

This is the code I use to explode the block in the first place:

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

loc is the block location. lvl1 is the float for the radius of the explosion.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
JarFile
  • 318
  • 2
  • 8
  • 30

1 Answers1

1

This cause of this is fairly simple: explosions generally don't drop all of the blocks by default.

You can, however, change this: Listen to the BlockExplodeEvent, and then call setYield with a value of 1. Add this event handler:

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

You might want to add some kind of check as to whether the explosion was caused by your plugin, though.


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

@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 does stuff with the blockList, as all of the blocks will become air. As such, I strongly recommend using the first code if you can.


BlockExplodeEvent seems to be only in the newest versions; if you don't have it, you'll need to update. It's found in both 1.8 releases, but only the most recent updates.

Here's the pom you'll 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 that "Force Update of Shapshots/Releases" is checked.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
  • I know if the explosion is caused by my plugin because I have the explosion boolean set to true when it explodes. From my understanding, there is no BlockExplodeEvent. There is no import for it, thus giving me an error. – JarFile May 16 '15 at 05:35
  • @JarFile BlockExplodeEvent is new to bukkit 1.8.3 -- if you're using 1.7.10 in your plugin (for compilation), it won't be there. I'd make sure your using 1.8.3 in your POM. (the full package is `org.bukkit.event.block.BlockExplodeEvent`). – Pokechu22 May 16 '15 at 05:37
  • Updated to it and now the event works, but the setYield is not there and my main class has an error about referencing. I don't know if I am using a correct version of the api. I thought I was. – JarFile May 16 '15 at 06:03
  • @JarFile Hm... See my edit for the POM you should have; also note that you'll need to make sure "Force Update of Shapshots/Releases" is checked when updating the project with maven. – Pokechu22 May 16 '15 at 16:59
  • I don't understand what the pom is used for. Also there isn't any .setYield but now there are not errors except for no setYield. – JarFile May 16 '15 at 22:51
  • @JarFile To be honest, I'm not 100% sure what Maven / the Pom file does, but I am pretty sure that it's something that automatically downloads all the required libraries. As for why .setYield isn't visible, I'm not sure; it works for me. Does getYield exist for you? (that's only to test; calling getYield won't do anything useful). – Pokechu22 May 17 '15 at 01:00
  • `getYield` nor `setYield` exists for me... Very weird how BlockDamageEvent works. – JarFile May 17 '15 at 01:03
  • @JarFile I've added an alternative method that doesn't use `setYeild`, although it's probably not a very good option and might break other plugins. – Pokechu22 May 17 '15 at 18:01
  • How would it break other plugins – JarFile May 17 '15 at 18:36
  • @JarFile It's probably a lot less likely that it would break ones than I thought, but I can see it causing issues if another plugin uses the explosion event to modify drops. But now that I think about it, it wouldn't be as bad since the only time this would occur is when your own plugin is the creator of the explosion. In your specific case I think you'd be fine using it; I'm just including a warning since it's (officially) the worse way of doing it. – Pokechu22 May 17 '15 at 23:15