Minecraft forge 1.18.1 - Remove blocks

Viewed 131

I have an item which when useOn-ed should remove the block/change it to AIR.

I am using the line below:

level.removeBlock(positionClicked, false);

Once I run the client, the block is briefly removed and then reappears. I've also tried removing the block with:

level.destroyBlock(positionClicked, false);
level.setBlock(positionClicked, Blocks.AIR.defaultBlockState(), 1);

My guess is that I am not registering something correctly/the removed blocks are not applied to the client.

Full code:

public class TeleportationItem extends Item {

    public TeleportationItem(Properties pProperties) {
        super(pProperties);
    }

    @Override
    public InteractionResult useOn(UseOnContext pContext) {
        Level level = pContext.getLevel();
        if (level.isClientSide){
            BlockPos blockpos = pContext.getClickedPos();
            BlockState blockstate = level.getBlockState(blockpos);
            level.removeBlock(positionClicked, false);
            //level.setBlock(positionClicked, Blocks.AIR.defaultBlockState(), 1);
            // level.destroyBlock(positionClicked, false);
            return super.useOn(pContext);
        }
        return super.useOn(pContext);
    }
}
1 Answers

You have to do it server-side, not client-side. Simply change your if condition to if (!level.isClientSide){

"Non-client" side doesn't mean you're on a server, any interaction with the world like this has to be done server-side. What happens is that you remove a block only from your view, then your view is updated to the server which doesn't have the block removed.

Related