RailState .java

net.minecraft.world.level.block.RailState (class): members: countPotentialConnections, getConnections, isStraight, connections, getState, block, place.

net.minecraft.world.level.blockclass353 linescommon8.8 KB

API Surface (4)

List<BlockPos> getConnections()
int countPotentialConnections()
RailState place(final boolean hasSignal, final boolean first, final RailShape defaultShape)
BlockState getState()

Fields & Constants (6)

level
pos
block
state
isStraight
connections

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

RailState.java353 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import com.google.common.collect.Lists;
4
import java.util.List;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.world.level.Level;
8
import net.minecraft.world.level.block.state.BlockState;
9
import net.minecraft.world.level.block.state.properties.RailShape;
10
import org.jspecify.annotations.Nullable;
11
12
public class RailState {
13
	private final Level level;
14
	private final BlockPos pos;
15
	private final BaseRailBlock block;
16
	private BlockState state;
17
	private final boolean isStraight;
18
	private final List<BlockPos> connections = Lists.newArrayList();
19
20
	public RailState(final Level level, final BlockPos pos, final BlockState state) {
21
		this.level = level;
22
		this.pos = pos;
23
		this.state = state;
24
		this.block = (BaseRailBlock)state.getBlock();
25
		RailShape direction = state.getValue(this.block.getShapeProperty());
26
		this.isStraight = this.block.isStraight();
27
		this.updateConnections(direction);
28
	}
29
30
	public List<BlockPos> getConnections() {
31
		return this.connections;
32
	}
33
34
	private void updateConnections(final RailShape direction) {
35
		this.connections.clear();
36
		switch (direction) {
37
			case NORTH_SOUTH:
38
				this.connections.add(this.pos.north());
39
				this.connections.add(this.pos.south());
40
				break;
41
			case EAST_WEST:
42
				this.connections.add(this.pos.west());
43
				this.connections.add(this.pos.east());
44
				break;
45
			case ASCENDING_EAST:
46
				this.connections.add(this.pos.west());
47
				this.connections.add(this.pos.east().above());
48
				break;
49
			case ASCENDING_WEST:
50
				this.connections.add(this.pos.west().above());
51
				this.connections.add(this.pos.east());
52
				break;
53
			case ASCENDING_NORTH:
54
				this.connections.add(this.pos.north().above());
55
				this.connections.add(this.pos.south());
56
				break;
57
			case ASCENDING_SOUTH:
58
				this.connections.add(this.pos.north());
59
				this.connections.add(this.pos.south().above());
60
				break;
61
			case SOUTH_EAST:
62
				this.connections.add(this.pos.east());
63
				this.connections.add(this.pos.south());
64
				break;
65
			case SOUTH_WEST:
66
				this.connections.add(this.pos.west());
67
				this.connections.add(this.pos.south());
68
				break;
69
			case NORTH_WEST:
70
				this.connections.add(this.pos.west());
71
				this.connections.add(this.pos.north());
72
				break;
73
			case NORTH_EAST:
74
				this.connections.add(this.pos.east());
75
				this.connections.add(this.pos.north());
76
		}
77
	}
78
79
	private void removeSoftConnections() {
80
		for (int i = 0; i < this.connections.size(); i++) {
81
			RailState rail = this.getRail(this.connections.get(i));
82
			if (rail != null && rail.connectsTo(this)) {
83
				this.connections.set(i, rail.pos);
84
			} else {
85
				this.connections.remove(i--);
86
			}
87
		}
88
	}
89
90
	private boolean hasRail(final BlockPos pos) {
91
		return BaseRailBlock.isRail(this.level, pos) || BaseRailBlock.isRail(this.level, pos.above()) || BaseRailBlock.isRail(this.level, pos.below());
92
	}
93
94
	private @Nullable RailState getRail(final BlockPos pos) {
95
		BlockPos testPos = pos;
96
		BlockState testState = this.level.getBlockState(testPos);
97
		if (BaseRailBlock.isRail(testState)) {
98
			return new RailState(this.level, testPos, testState);
99
		}
100
101
		testPos = pos.above();
102
		testState = this.level.getBlockState(testPos);
103
		if (BaseRailBlock.isRail(testState)) {
104
			return new RailState(this.level, testPos, testState);
105
		}
106
107
		testPos = pos.below();
108
		testState = this.level.getBlockState(testPos);
109
		return BaseRailBlock.isRail(testState) ? new RailState(this.level, testPos, testState) : null;
110
	}
111
112
	private boolean connectsTo(final RailState rail) {
113
		return this.hasConnection(rail.pos);
114
	}
115
116
	private boolean hasConnection(final BlockPos railPos) {
117
		for (int i = 0; i < this.connections.size(); i++) {
118
			BlockPos pos = this.connections.get(i);
119
			if (pos.getX() == railPos.getX() && pos.getZ() == railPos.getZ()) {
120
				return true;
121
			}
122
		}
123
124
		return false;
125
	}
126
127
	protected int countPotentialConnections() {
128
		int count = 0;
129
130
		for (Direction direction : Direction.Plane.HORIZONTAL) {
131
			if (this.hasRail(this.pos.relative(direction))) {
132
				count++;
133
			}
134
		}
135
136
		return count;
137
	}
138
139
	private boolean canConnectTo(final RailState rail) {
140
		return this.connectsTo(rail) || this.connections.size() != 2;
141
	}
142
143
	private void connectTo(final RailState rail) {
144
		this.connections.add(rail.pos);
145
		BlockPos north = this.pos.north();
146
		BlockPos south = this.pos.south();
147
		BlockPos west = this.pos.west();
148
		BlockPos east = this.pos.east();
149
		boolean n = this.hasConnection(north);
150
		boolean s = this.hasConnection(south);
151
		boolean w = this.hasConnection(west);
152
		boolean e = this.hasConnection(east);
153
		RailShape shape = null;
154
		if (n || s) {
155
			shape = RailShape.NORTH_SOUTH;
156
		}
157
158
		if (w || e) {
159
			shape = RailShape.EAST_WEST;
160
		}
161
162
		if (!this.isStraight) {
163
			if (s && e && !n && !w) {
164
				shape = RailShape.SOUTH_EAST;
165
			}
166
167
			if (s && w && !n && !e) {
168
				shape = RailShape.SOUTH_WEST;
169
			}
170
171
			if (n && w && !s && !e) {
172
				shape = RailShape.NORTH_WEST;
173
			}
174
175
			if (n && e && !s && !w) {
176
				shape = RailShape.NORTH_EAST;
177
			}
178
		}
179
180
		if (shape == RailShape.NORTH_SOUTH) {
181
			if (BaseRailBlock.isRail(this.level, north.above())) {
182
				shape = RailShape.ASCENDING_NORTH;
183
			}
184
185
			if (BaseRailBlock.isRail(this.level, south.above())) {
186
				shape = RailShape.ASCENDING_SOUTH;
187
			}
188
		}
189
190
		if (shape == RailShape.EAST_WEST) {
191
			if (BaseRailBlock.isRail(this.level, east.above())) {
192
				shape = RailShape.ASCENDING_EAST;
193
			}
194
195
			if (BaseRailBlock.isRail(this.level, west.above())) {
196
				shape = RailShape.ASCENDING_WEST;
197
			}
198
		}
199
200
		if (shape == null) {
201
			shape = RailShape.NORTH_SOUTH;
202
		}
203
204
		this.state = this.state.setValue(this.block.getShapeProperty(), shape);
205
		this.level.setBlockAndUpdate(this.pos, this.state);
206
	}
207
208
	private boolean hasNeighborRail(final BlockPos railPos) {
209
		RailState neighbor = this.getRail(railPos);
210
		if (neighbor == null) {
211
			return false;
212
		}
213
214
		neighbor.removeSoftConnections();
215
		return neighbor.canConnectTo(this);
216
	}
217
218
	public RailState place(final boolean hasSignal, final boolean first, final RailShape defaultShape) {
219
		BlockPos north = this.pos.north();
220
		BlockPos south = this.pos.south();
221
		BlockPos west = this.pos.west();
222
		BlockPos east = this.pos.east();
223
		boolean n = this.hasNeighborRail(north);
224
		boolean s = this.hasNeighborRail(south);
225
		boolean w = this.hasNeighborRail(west);
226
		boolean e = this.hasNeighborRail(east);
227
		RailShape shape = null;
228
		boolean northOrSouth = n || s;
229
		boolean westOrEast = w || e;
230
		if (northOrSouth && !westOrEast) {
231
			shape = RailShape.NORTH_SOUTH;
232
		}
233
234
		if (westOrEast && !northOrSouth) {
235
			shape = RailShape.EAST_WEST;
236
		}
237
238
		boolean southAndEast = s && e;
239
		boolean southAndWest = s && w;
240
		boolean northAndEast = n && e;
241
		boolean northAndWest = n && w;
242
		if (!this.isStraight) {
243
			if (southAndEast && !n && !w) {
244
				shape = RailShape.SOUTH_EAST;
245
			}
246
247
			if (southAndWest && !n && !e) {
248
				shape = RailShape.SOUTH_WEST;
249
			}
250
251
			if (northAndWest && !s && !e) {
252
				shape = RailShape.NORTH_WEST;
253
			}
254
255
			if (northAndEast && !s && !w) {
256
				shape = RailShape.NORTH_EAST;
257
			}
258
		}
259
260
		if (shape == null) {
261
			if (northOrSouth && westOrEast) {
262
				shape = defaultShape;
263
			} else if (northOrSouth) {
264
				shape = RailShape.NORTH_SOUTH;
265
			} else if (westOrEast) {
266
				shape = RailShape.EAST_WEST;
267
			}
268
269
			if (!this.isStraight) {
270
				if (hasSignal) {
271
					if (southAndEast) {
272
						shape = RailShape.SOUTH_EAST;
273
					}
274
275
					if (southAndWest) {
276
						shape = RailShape.SOUTH_WEST;
277
					}
278
279
					if (northAndEast) {
280
						shape = RailShape.NORTH_EAST;
281
					}
282
283
					if (northAndWest) {
284
						shape = RailShape.NORTH_WEST;
285
					}
286
				} else {
287
					if (northAndWest) {
288
						shape = RailShape.NORTH_WEST;
289
					}
290
291
					if (northAndEast) {
292
						shape = RailShape.NORTH_EAST;
293
					}
294
295
					if (southAndWest) {
296
						shape = RailShape.SOUTH_WEST;
297
					}
298
299
					if (southAndEast) {
300
						shape = RailShape.SOUTH_EAST;
301
					}
302
				}
303
			}
304
		}
305
306
		if (shape == RailShape.NORTH_SOUTH) {
307
			if (BaseRailBlock.isRail(this.level, north.above())) {
308
				shape = RailShape.ASCENDING_NORTH;
309
			}
310
311
			if (BaseRailBlock.isRail(this.level, south.above())) {
312
				shape = RailShape.ASCENDING_SOUTH;
313
			}
314
		}
315
316
		if (shape == RailShape.EAST_WEST) {
317
			if (BaseRailBlock.isRail(this.level, east.above())) {
318
				shape = RailShape.ASCENDING_EAST;
319
			}
320
321
			if (BaseRailBlock.isRail(this.level, west.above())) {
322
				shape = RailShape.ASCENDING_WEST;
323
			}
324
		}
325
326
		if (shape == null) {
327
			shape = defaultShape;
328
		}
329
330
		this.updateConnections(shape);
331
		this.state = this.state.setValue(this.block.getShapeProperty(), shape);
332
		if (first || this.level.getBlockState(this.pos) != this.state) {
333
			this.level.setBlockAndUpdate(this.pos, this.state);
334
335
			for (int i = 0; i < this.connections.size(); i++) {
336
				RailState neighbor = this.getRail(this.connections.get(i));
337
				if (neighbor != null) {
338
					neighbor.removeSoftConnections();
339
					if (neighbor.canConnectTo(this)) {
340
						neighbor.connectTo(this);
341
					}
342
				}
343
			}
344
		}
345
346
		return this;
347
	}
348
349
	public BlockState getState() {
350
		return this.state;
351
	}
352
}
353

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