simulating block destruction by player

Discussion in 'Plugin Development' started by zaph34r, Jul 22, 2011.

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

    zaph34r

    I am trying to destroy blocks programmatically, but keeping it as a player-originated event, to properly work with worldguard and plugins that influence item drops.
    Why does the following do nothing?

    Code:
    BlockBreakEvent event = new BlockBreakEvent(block, player);
    myPlugin.pluginManager.callEvent(event);
    
    i was under the assumption that it should simulate a block break by a player, but nothing happens. when i just .setType() in the same place it works (but of course without triggering a BlockBreakEvent), so the error must be in those 2 lines

    Any insight is appreciated :)
     
  2. Offline

    nisovin

    Calling an event doesn't actually do anything besides send the event to plugins. You'll need to do both.
    Code:
    BlockBreakEvent event = new BlockBreakEvent(block, player);
    myPlugin.pluginManager.callEvent(event);
    if (!event.isCancelled()) {
    block.setType(Material.AIR);
    }
    
     
  3. Offline

    zaph34r

    ok, that makes kind of sense, i thought the break events would be handled internally.
    thanks a lot :)
    so i guess item drops have to be done manually too then via .dropItemNaturally (or whatever it was) ?
     
  4. Offline

    nisovin

    Correct.
     
  5. Offline

    zaph34r

    i guess theres no convenient shortcut for things like gravel that already includes the chance to drop flint, or converting grass to dirt?
     
  6. Offline

    Crash

    Fortunately there is :
    Code:
    ((CraftPlayer)player).getHandle().itemInWorldManager.c(block.getX(), block.getY(), block.getZ());
    This will get rid of the block, drop the item, and call the event.
     
  7. Offline

    vildaberper

    Code:
    public static ItemStack getDrop(Block block){
        return new ItemStack (net.minecraft.server.Block.byId[block.getTypeId()].a(0, new Random()) , net.minecraft.server.Block.byId[block.getTypeId()].a (new Random()), block.getData());
    }
     
  8. Offline

    zaph34r

    is there any drawback to building against craftbukkit?
     
  9. Offline

    Shamebot

    It may break your plugin when a new minecraft version is released.
     
  10. Offline

    s1mpl3x

  11. Offline

    zaph34r

    thanks a lot :)
     
Thread Status:
Not open for further replies.

Share This Page