BlockBreakEvent.getBlock() returns air?

Discussion in 'Plugin Development' started by pkmnfrk, Mar 9, 2011.

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

    pkmnfrk

    Code:
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        
        Block broken = event.getBlock();
        
        player.chat(broken.getType().toString());
    }
    This sends "AIR" to the client. I guess this might be expected, since the block is broken, but it's not very useful to determine what type it was. Thus, I suspect it's a bug. Is this the case, or am I using the wrong event to detect when a block is broken?

    (tested in the latest CraftBukkit from GitHub)
     
  2. Offline

    Nohup

    do you have other plugins running? I use this with my DropBonus plugin and it is still working fine.
     
  3. Offline

    pkmnfrk

    Normally yes, but while I was testing this, I got rid of all the other ones (moved them out of the plugins folder) except for Essentials (for the /rel command)

    Edit: Durrrrrr. I am dumb. Code that I omitted does the equivalent of this:

    Code:
    for(int i = 0; i < BREAK_DIST; i++) {
        Block b = broken.getFace(BlockFace.WEST, i);
        if(b.getType() != broken.getType()) break;
    
        b.setType(Material.AIR);
    }
    
    As it turns out, Block.getFace(whatever, 0) returns the same block, so I was setting the broken block to air, causing broken.getType().toString() to return "AIR".

    My solution is to make my loop look like this:

    Code:
    for(int i = 1; i <= BREAK_DIST; i++) {
         Block b = broken.getFace(BlockFace.WEST, i);
         if(b.getType() != broken.getType()) break;
     
         b.setType(Material.AIR);
     }
    
     
Thread Status:
Not open for further replies.

Share This Page