LadderBlock .java

net.minecraft.world.level.block.LadderBlock (class, 124 lines): members: SHAPES, WATERLOGGED, FACING, getFluidState, canSurvive, mirror, rotate, ticks.

net.minecraft.world.level.blockclass124 linescommonextends Block4.6 KB

Implements

API Surface (7)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
FluidState getFluidState(final BlockState state)

Fields & Constants (10)

FACING
WATERLOGGED
SHAPES
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

LadderBlock.java124 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.Map;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.util.RandomSource;
7
import net.minecraft.world.item.context.BlockPlaceContext;
8
import net.minecraft.world.level.BlockGetter;
9
import net.minecraft.world.level.LevelReader;
10
import net.minecraft.world.level.ScheduledTickAccess;
11
import net.minecraft.world.level.block.state.BlockBehaviour;
12
import net.minecraft.world.level.block.state.BlockState;
13
import net.minecraft.world.level.block.state.StateDefinition;
14
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
15
import net.minecraft.world.level.block.state.properties.BooleanProperty;
16
import net.minecraft.world.level.block.state.properties.EnumProperty;
17
import net.minecraft.world.level.material.FluidState;
18
import net.minecraft.world.level.material.Fluids;
19
import net.minecraft.world.phys.shapes.CollisionContext;
20
import net.minecraft.world.phys.shapes.Shapes;
21
import net.minecraft.world.phys.shapes.VoxelShape;
22
import org.jspecify.annotations.Nullable;
23
24
/**
25
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
26
 */
27
public class LadderBlock extends Block implements SimpleWaterloggedBlock {
28
	public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
29
	public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
30
	public static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateHorizontal(Block.boxZ(16.0, 13.0, 16.0));
31
32
	/**
33
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
34
	 */
35
	public LadderBlock(final BlockBehaviour.Properties properties) {
36
		super(properties);
37
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, false));
38
	}
39
40
	@Override
41
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
42
		return SHAPES.get(state.getValue(FACING));
43
	}
44
45
	private boolean canAttachTo(final BlockGetter level, final BlockPos pos, final Direction direction) {
46
		BlockState blockState = level.getBlockState(pos);
47
		return blockState.isFaceSturdy(level, pos, direction);
48
	}
49
50
	@Override
51
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
52
		Direction direction = state.getValue(FACING);
53
		return this.canAttachTo(level, pos.relative(direction.getOpposite()), direction);
54
	}
55
56
	@Override
57
	protected BlockState updateShape(
58
		final BlockState state,
59
		final LevelReader level,
60
		final ScheduledTickAccess ticks,
61
		final BlockPos pos,
62
		final Direction directionToNeighbour,
63
		final BlockPos neighbourPos,
64
		final BlockState neighbourState,
65
		final RandomSource random
66
	) {
67
		if (directionToNeighbour.getOpposite() == state.getValue(FACING) && !state.canSurvive(level, pos)) {
68
			return Blocks.AIR.defaultBlockState();
69
		}
70
71
		if (state.getValue(WATERLOGGED)) {
72
			ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
73
		}
74
75
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
76
	}
77
78
	@Override
79
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
80
		if (!context.replacingClickedOnBlock()) {
81
			BlockState state = context.getLevel().getBlockState(context.getClickedPos().relative(context.getClickedFace().getOpposite()));
82
			if (state.is(this) && state.getValue(FACING) == context.getClickedFace()) {
83
				return null;
84
			}
85
		}
86
87
		BlockState state = this.defaultBlockState();
88
		LevelReader level = context.getLevel();
89
		BlockPos pos = context.getClickedPos();
90
		FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
91
92
		for (Direction direction : context.getNearestLookingDirections()) {
93
			if (direction.getAxis().isHorizontal()) {
94
				state = state.setValue(FACING, direction.getOpposite());
95
				if (state.canSurvive(level, pos)) {
96
					return state.setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
97
				}
98
			}
99
		}
100
101
		return null;
102
	}
103
104
	@Override
105
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
106
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
107
	}
108
109
	@Override
110
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
111
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
112
	}
113
114
	@Override
115
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
116
		builder.add(FACING, WATERLOGGED);
117
	}
118
119
	@Override
120
	protected FluidState getFluidState(final BlockState state) {
121
		return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
122
	}
123
}
124

Decompiled Minecraft 26.3 source of LadderBlock (net/minecraft/world/level/block/LadderBlock.java) with Mojang official mappings, stored verbatim on the site.