Canceling BLOCK_BREAK - block still disappears for a while

Discussion in 'Plugin Development' started by Lazlo, Sep 5, 2011.

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

    Lazlo

    I want to cancel breaking a block under certain conditions.

    Code:
    @Override
    public void onBlockBreak(BlockBreakEvent event) {
    	super.onBlockBreak(event);
    
    	if (event.getBlock().getTypeId() == whatever) {
    		event.setCancelled(true);
    		return;
    	}
    }
    This works, as in, the block doesn't get destroyed on server side (I can't go through), but it disappears on client side. Then, after a couple of seconds, I suppose until a chunk update or whatnot, it reappears on the client.

    I mostly want to make that block "unbreakable", and the sometimes up to 5 seconds delay until it reappears is unacceptable for my mod.

    Any idea?
     
  2. Offline

    Chrisward

    Its probably lag
     
  3. Offline

    matter123

    its most likely the clients packet preprocesser it assumes that the server will accept all packets the client sends so it removes the block then when the packet comes back as it got rejected it shows the bock back
     
  4. Offline

    Lazlo

    Figured as much, but the delay is surprisingly big. Other packets get received meanwhile. And for some other blocks, regular ones, canceling is much faster.
     
  5. Say, why are you calling "super.onBlockBreak(event);"? What's that supposed to do?
     
  6. You could just cancel it yourself if it bothers you that much. Just get the id of the block broken and set the block to that type so it is restored.
     
  7. Offline

    nisovin

    You could try something like this:
    Code:
        @Override
        public void onBlockBreak(BlockBreakEvent event) {
            if (event.getBlock().getTypeId() == whatever) {
                event.setCancelled(true);
                Block b = event.getBlock();
                event.getPlayer().sendBlockChange(b.getLocation(), b.getTypeId(), b.getData());
            }
        }
    
    Though, when I test it on a local server, the block pops back up almost immediately, even without this extra code.
     
  8. Offline

    Haias

    I'm assuming the block you're talking about here is a tree, flower, wild grass, mushroom, right?

    I had this same problem. I simply hooked into the onPlayerInteract function and cancelled it when the player left clicked a block.
     
  9. Offline

    Lazlo

    Exactly this. Will try it soon.
    I think it's linked to the fact that it's instant break.

    @nisovin: I'll try your technique after Haias' if it doesn't work.
     
Thread Status:
Not open for further replies.

Share This Page