Replace block onBlockDamage BREAK

Discussion in 'Plugin Development' started by lokki23, Feb 8, 2011.

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

    lokki23

    Hello,
    I need to place a new block when a block is broke, but I have a problem with this code:
    Code:
    public void onBlockDamage(BlockDamageEvent event) {
            Block block = event.getBlock();
    
            if((event.getDamageLevel() == BlockDamageLevel.BROKEN) && (block.getType() == Material.WALL_SIGN)){
                Block newBlock = block.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ());
                newBlock.setType(Material.DIRT);
    }
    In fact, the block is well transformed but drop on the floor, like if you break it normaly
    Sorry if my english is not perfect.
     
  2. Offline

    jimrhoskins

    So, my understanding of your code is that when you break a wall sign, you want the block it was occupying to transform into dirt.

    The first thing is block and newBlock are the same in this case. When a block breaks the block still exists, and just becomes air (and an item may be dropped in it's place). So you can just get rid of the newBlock line and use block
    .

    Then your setType is in fact setting the type to Dirt, but since it is the same block that is currently being broken, the dirt is broken and the item drops. So, all you need to do is call event.setCancelled(true) to signal Minecraft that you do not want this block to actually break.

    I tested this code and it works to my understanding of what you are trying to do.

    Code:
    public void onBlockDamage(BlockDamageEvent event) {
            Block block = event.getBlock();
    
            if((event.getDamageLevel() == BlockDamageLevel.BROKEN) && (block.getType() == Material.WALL_SIGN)){
                block.setType(Material.DIRT);
                event.setCancelled(true);
            }
        }
     
  3. Offline

    lokki23

    Thank you very well, it is exactly that,
    but if I do reappear a WALL_SIGN or a DOOR, can I choose orientation
    Code:
    if((event.getDamageLevel() == BlockDamageLevel.BROKEN) && (block.getType() == Material.WALL_SIGN)){
                BlockState blockState = event.getBlock().getState();
    
                event.setCancelled(true);
    
                Sign sign = (Sign) blockState;
                sign.setLine(0, "test");
                sign.update();
    }
    and Is it possible to make a block unbreakable ?
     
Thread Status:
Not open for further replies.

Share This Page