BaseRailBlock .java

net.minecraft.world.level.block.BaseRailBlock (class): members: SHAPE_SLOPE, SHAPE_FLAT, updateDir, isRail, isStraight, getShapeProperty, updateState.

net.minecraft.world.level.blockclass297 linescommonextends Block9.5 KB

Implements

API Surface (16)

boolean isRail(final Level level, final BlockPos pos)
boolean isRail(final BlockState state)
boolean isStraight()
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)
void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston)
BlockState updateState(BlockState state, final Level level, final BlockPos pos, final boolean movedByPiston)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
void updateState(final BlockState state, final Level level, final BlockPos pos, final Block block)
BlockState updateDir(final Level level, final BlockPos pos, final BlockState state, final boolean first)
void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston)
BlockState getStateForPlacement(final BlockPlaceContext context)
RailShape rotate(final RailShape shape, final Rotation rotation)
RailShape mirror(final RailShape shape, final Mirror mirror)
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)
FluidState getFluidState(final BlockState state)

Fields & Constants (12)

WATERLOGGED
SHAPE_FLAT
SHAPE_SLOPE
isStraight
orientation
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

BaseRailBlock.java297 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.tags.BlockTags;
7
import net.minecraft.util.RandomSource;
8
import net.minecraft.world.item.context.BlockPlaceContext;
9
import net.minecraft.world.level.BlockGetter;
10
import net.minecraft.world.level.Level;
11
import net.minecraft.world.level.LevelReader;
12
import net.minecraft.world.level.ScheduledTickAccess;
13
import net.minecraft.world.level.block.state.BlockBehaviour;
14
import net.minecraft.world.level.block.state.BlockState;
15
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
16
import net.minecraft.world.level.block.state.properties.BooleanProperty;
17
import net.minecraft.world.level.block.state.properties.Property;
18
import net.minecraft.world.level.block.state.properties.RailShape;
19
import net.minecraft.world.level.material.FluidState;
20
import net.minecraft.world.level.material.Fluids;
21
import net.minecraft.world.level.redstone.Orientation;
22
import net.minecraft.world.phys.shapes.CollisionContext;
23
import net.minecraft.world.phys.shapes.VoxelShape;
24
import org.jspecify.annotations.Nullable;
25
26
public abstract class BaseRailBlock extends Block implements SimpleWaterloggedBlock {
27
	public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
28
	private static final VoxelShape SHAPE_FLAT = Block.column(16.0, 0.0, 2.0);
29
	private static final VoxelShape SHAPE_SLOPE = Block.column(16.0, 0.0, 8.0);
30
	private final boolean isStraight;
31
32
	public static boolean isRail(final Level level, final BlockPos pos) {
33
		return isRail(level.getBlockState(pos));
34
	}
35
36
	public static boolean isRail(final BlockState state) {
37
		return state.is(BlockTags.RAILS) && state.getBlock() instanceof BaseRailBlock;
38
	}
39
40
	protected BaseRailBlock(final boolean isStraight, final BlockBehaviour.Properties properties) {
41
		super(properties);
42
		this.isStraight = isStraight;
43
	}
44
45
	public boolean isStraight() {
46
		return this.isStraight;
47
	}
48
49
	@Override
50
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
51
		return state.getValue(this.getShapeProperty()).isSlope() ? SHAPE_SLOPE : SHAPE_FLAT;
52
	}
53
54
	@Override
55
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
56
		return canSupportRigidBlock(level, pos.below());
57
	}
58
59
	@Override
60
	protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
61
		if (!oldState.is(state.getBlock())) {
62
			this.updateState(state, level, pos, movedByPiston);
63
		}
64
	}
65
66
	protected BlockState updateState(BlockState state, final Level level, final BlockPos pos, final boolean movedByPiston) {
67
		state = this.updateDir(level, pos, state, true);
68
		if (this.isStraight) {
69
			level.neighborChanged(state, pos, this, null, movedByPiston);
70
		}
71
72
		return state;
73
	}
74
75
	@Override
76
	protected void neighborChanged(
77
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
78
	) {
79
		if (!level.isClientSide() && level.getBlockState(pos).is(this)) {
80
			RailShape shape = state.getValue(this.getShapeProperty());
81
			if (shouldBeRemoved(pos, level, shape)) {
82
				dropResources(state, level, pos);
83
				level.removeBlock(pos, movedByPiston);
84
			} else {
85
				this.updateState(state, level, pos, block);
86
			}
87
		}
88
	}
89
90
	private static boolean shouldBeRemoved(final BlockPos pos, final Level level, final RailShape shape) {
91
		if (!canSupportRigidBlock(level, pos.below())) {
92
			return true;
93
		}
94
95
		return switch (shape) {
96
			case ASCENDING_EAST -> !canSupportRigidBlock(level, pos.east());
97
			case ASCENDING_WEST -> !canSupportRigidBlock(level, pos.west());
98
			case ASCENDING_NORTH -> !canSupportRigidBlock(level, pos.north());
99
			case ASCENDING_SOUTH -> !canSupportRigidBlock(level, pos.south());
100
			default -> false;
101
		};
102
	}
103
104
	protected void updateState(final BlockState state, final Level level, final BlockPos pos, final Block block) {
105
	}
106
107
	protected BlockState updateDir(final Level level, final BlockPos pos, final BlockState state, final boolean first) {
108
		if (level.isClientSide()) {
109
			return state;
110
		}
111
112
		RailShape current = state.getValue(this.getShapeProperty());
113
		return new RailState(level, pos, state).place(level.hasNeighborSignal(pos), first, current).getState();
114
	}
115
116
	@Override
117
	protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
118
		if (!movedByPiston) {
119
			if (state.getValue(this.getShapeProperty()).isSlope()) {
120
				level.updateNeighborsAt(pos.above(), this);
121
			}
122
123
			if (this.isStraight) {
124
				level.updateNeighborsAt(pos, this);
125
				level.updateNeighborsAt(pos.below(), this);
126
			}
127
		}
128
	}
129
130
	@Override
131
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
132
		FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
133
		boolean isWaterSource = replacedFluidState.is(Fluids.WATER);
134
		BlockState state = super.defaultBlockState();
135
		Direction direction = context.getHorizontalDirection();
136
		boolean isEastWest = direction == Direction.EAST || direction == Direction.WEST;
137
		return state.setValue(this.getShapeProperty(), isEastWest ? RailShape.EAST_WEST : RailShape.NORTH_SOUTH).setValue(WATERLOGGED, isWaterSource);
138
	}
139
140
	public abstract Property<RailShape> getShapeProperty();
141
142
	protected RailShape rotate(final RailShape shape, final Rotation rotation) {
143
		return switch (rotation) {
144
			case CLOCKWISE_180 -> {
145
				switch (shape) {
146
					case ASCENDING_EAST:
147
						yield RailShape.ASCENDING_WEST;
148
					case ASCENDING_WEST:
149
						yield RailShape.ASCENDING_EAST;
150
					case ASCENDING_NORTH:
151
						yield RailShape.ASCENDING_SOUTH;
152
					case ASCENDING_SOUTH:
153
						yield RailShape.ASCENDING_NORTH;
154
					case NORTH_SOUTH:
155
						yield RailShape.NORTH_SOUTH;
156
					case EAST_WEST:
157
						yield RailShape.EAST_WEST;
158
					case SOUTH_EAST:
159
						yield RailShape.NORTH_WEST;
160
					case SOUTH_WEST:
161
						yield RailShape.NORTH_EAST;
162
					case NORTH_WEST:
163
						yield RailShape.SOUTH_EAST;
164
					case NORTH_EAST:
165
						yield RailShape.SOUTH_WEST;
166
					default:
167
						throw new MatchException(null, null);
168
				}
169
			}
170
			case COUNTERCLOCKWISE_90 -> {
171
				switch (shape) {
172
					case ASCENDING_EAST:
173
						yield RailShape.ASCENDING_NORTH;
174
					case ASCENDING_WEST:
175
						yield RailShape.ASCENDING_SOUTH;
176
					case ASCENDING_NORTH:
177
						yield RailShape.ASCENDING_WEST;
178
					case ASCENDING_SOUTH:
179
						yield RailShape.ASCENDING_EAST;
180
					case NORTH_SOUTH:
181
						yield RailShape.EAST_WEST;
182
					case EAST_WEST:
183
						yield RailShape.NORTH_SOUTH;
184
					case SOUTH_EAST:
185
						yield RailShape.NORTH_EAST;
186
					case SOUTH_WEST:
187
						yield RailShape.SOUTH_EAST;
188
					case NORTH_WEST:
189
						yield RailShape.SOUTH_WEST;
190
					case NORTH_EAST:
191
						yield RailShape.NORTH_WEST;
192
					default:
193
						throw new MatchException(null, null);
194
				}
195
			}
196
			case CLOCKWISE_90 -> {
197
				switch (shape) {
198
					case ASCENDING_EAST:
199
						yield RailShape.ASCENDING_SOUTH;
200
					case ASCENDING_WEST:
201
						yield RailShape.ASCENDING_NORTH;
202
					case ASCENDING_NORTH:
203
						yield RailShape.ASCENDING_EAST;
204
					case ASCENDING_SOUTH:
205
						yield RailShape.ASCENDING_WEST;
206
					case NORTH_SOUTH:
207
						yield RailShape.EAST_WEST;
208
					case EAST_WEST:
209
						yield RailShape.NORTH_SOUTH;
210
					case SOUTH_EAST:
211
						yield RailShape.SOUTH_WEST;
212
					case SOUTH_WEST:
213
						yield RailShape.NORTH_WEST;
214
					case NORTH_WEST:
215
						yield RailShape.NORTH_EAST;
216
					case NORTH_EAST:
217
						yield RailShape.SOUTH_EAST;
218
					default:
219
						throw new MatchException(null, null);
220
				}
221
			}
222
			default -> shape;
223
		};
224
	}
225
226
	protected RailShape mirror(final RailShape shape, final Mirror mirror) {
227
		return switch (mirror) {
228
			case LEFT_RIGHT -> {
229
				switch (shape) {
230
					case ASCENDING_NORTH:
231
						yield RailShape.ASCENDING_SOUTH;
232
					case ASCENDING_SOUTH:
233
						yield RailShape.ASCENDING_NORTH;
234
					case NORTH_SOUTH:
235
					case EAST_WEST:
236
					default:
237
						yield shape;
238
					case SOUTH_EAST:
239
						yield RailShape.NORTH_EAST;
240
					case SOUTH_WEST:
241
						yield RailShape.NORTH_WEST;
242
					case NORTH_WEST:
243
						yield RailShape.SOUTH_WEST;
244
					case NORTH_EAST:
245
						yield RailShape.SOUTH_EAST;
246
				}
247
			}
248
			case FRONT_BACK -> {
249
				switch (shape) {
250
					case ASCENDING_EAST:
251
						yield RailShape.ASCENDING_WEST;
252
					case ASCENDING_WEST:
253
						yield RailShape.ASCENDING_EAST;
254
					case ASCENDING_NORTH:
255
					case ASCENDING_SOUTH:
256
					case NORTH_SOUTH:
257
					case EAST_WEST:
258
					default:
259
						yield shape;
260
					case SOUTH_EAST:
261
						yield RailShape.SOUTH_WEST;
262
					case SOUTH_WEST:
263
						yield RailShape.SOUTH_EAST;
264
					case NORTH_WEST:
265
						yield RailShape.NORTH_EAST;
266
					case NORTH_EAST:
267
						yield RailShape.NORTH_WEST;
268
				}
269
			}
270
			default -> shape;
271
		};
272
	}
273
274
	@Override
275
	protected BlockState updateShape(
276
		final BlockState state,
277
		final LevelReader level,
278
		final ScheduledTickAccess ticks,
279
		final BlockPos pos,
280
		final Direction directionToNeighbour,
281
		final BlockPos neighbourPos,
282
		final BlockState neighbourState,
283
		final RandomSource random
284
	) {
285
		if (state.getValue(WATERLOGGED)) {
286
			ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
287
		}
288
289
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
290
	}
291
292
	@Override
293
	protected FluidState getFluidState(final BlockState state) {
294
		return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
295
	}
296
}
297

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