tutorial:waterloggable

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:waterloggable [2022/12/16 02:12] solidblocktutorial:waterloggable [2024/10/27 14:25] (current) – update solidblock
Line 19: Line 19:
         setDefaultState(getDefaultState()         setDefaultState(getDefaultState()
             .with(Properties.HORIZONTAL_FACING, Direction.NORTH)             .with(Properties.HORIZONTAL_FACING, Direction.NORTH)
-            .with(WATERLOGGED, false);+            .with(WATERLOGGED, false));
     }     }
          
Line 33: Line 33:
     @Override     @Override
     public BlockState getPlacementState(ItemPlacementContext ctx) {     public BlockState getPlacementState(ItemPlacementContext ctx) {
-        return (BlockState)this.getDefaultState() +        return this.getDefaultState() 
-            .with(Properties.HORIZONTAL_FACING, ctx.getPlayerFacing().getOpposite()) +            .with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()) 
-            .with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER);+            .with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).isOf(Fluids.WATER));
     }     }
 </code> </code>
Line 48: Line 48:
  
 Override ''getStateForNeighborUpdate'' so that it correctly handles the flowing of water. Override ''getStateForNeighborUpdate'' so that it correctly handles the flowing of water.
 +
 +//For versions 1.21.1 and below, write like this://
 <yarncode java> <yarncode java>
     @Override     @Override
     public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {     public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
         if (state.get(WATERLOGGED)) {         if (state.get(WATERLOGGED)) {
-            // This is for 1.17 and below: world.getFluidTickScheduler().schedule(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));+            // For 1.17 and below:  
 +            // world.getFluidTickScheduler().schedule(pos, Fluids.WATER, Fluids.WATER.getTickRate(world)); 
 +            // For versions since 1.18 below 1.21.2:
             world.method_39281(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));             world.method_39281(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
         }         }
  
         return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);         return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);
 +    }
 +</yarncode>
 +
 +//For versions 1.21.2 and above, write like this://
 +<yarncode java>
 +    @Override
 +    public BlockState getStateForNeighborUpdate(BlockState state, WorldView world, ScheduledTickView tickView, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, Random random) {
 +        if (state.get(WATERLOGGED)) {
 +            // For versions since 1.21.2:
 +            tickView.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
 +        }
 +
 +        return super.getStateForNeighborUpdate(state, world, tickView, pos, direction, neighborPos, neighborState, random);
     }     }
 </yarncode> </yarncode>
  
 Now the block becomes waterloggable, and works correctly with water. Now the block becomes waterloggable, and works correctly with water.
tutorial/waterloggable.1671156769.txt.gz · Last modified: 2022/12/16 02:12 by solidblock