PoweredRailBlock .java

net.minecraft.world.level.block.PoweredRailBlock (class): members: findPoweredRailSignal, isSameRailWithPower, getShapeProperty, updateState, POWERED.

net.minecraft.world.level.blockclass165 linescommonextends BaseRailBlock4.8 KB

API Surface (7)

boolean findPoweredRailSignal(final Level level, final BlockPos pos, final BlockState state, final boolean forward, final int searchDepth)
boolean isSameRailWithPower(final Level level, final BlockPos pos, final boolean forward, final int searchDepth, final RailShape dir)
void updateState(final BlockState state, final Level level, final BlockPos pos, final Block block)
Property<RailShape> getShapeProperty()
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (2)

SHAPE
POWERED

Full Source

PoweredRailBlock.java165 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.world.level.Level;
5
import net.minecraft.world.level.block.state.BlockBehaviour;
6
import net.minecraft.world.level.block.state.BlockState;
7
import net.minecraft.world.level.block.state.StateDefinition;
8
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
9
import net.minecraft.world.level.block.state.properties.BooleanProperty;
10
import net.minecraft.world.level.block.state.properties.EnumProperty;
11
import net.minecraft.world.level.block.state.properties.Property;
12
import net.minecraft.world.level.block.state.properties.RailShape;
13
14
/**
15
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
16
 */
17
public class PoweredRailBlock extends BaseRailBlock {
18
	public static final EnumProperty<RailShape> SHAPE = BlockStateProperties.RAIL_SHAPE_STRAIGHT;
19
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
20
21
	/**
22
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
23
	 */
24
	public PoweredRailBlock(final BlockBehaviour.Properties properties) {
25
		super(true, properties);
26
		this.registerDefaultState(this.stateDefinition.any().setValue(SHAPE, RailShape.NORTH_SOUTH).setValue(POWERED, false).setValue(WATERLOGGED, false));
27
	}
28
29
	protected boolean findPoweredRailSignal(final Level level, final BlockPos pos, final BlockState state, final boolean forward, final int searchDepth) {
30
		if (searchDepth >= 8) {
31
			return false;
32
		}
33
34
		int x = pos.getX();
35
		int y = pos.getY();
36
		int z = pos.getZ();
37
		boolean checkBelow = true;
38
		RailShape shape = state.getValue(SHAPE);
39
		switch (shape) {
40
			case NORTH_SOUTH:
41
				if (forward) {
42
					z++;
43
				} else {
44
					z--;
45
				}
46
				break;
47
			case EAST_WEST:
48
				if (forward) {
49
					x--;
50
				} else {
51
					x++;
52
				}
53
				break;
54
			case ASCENDING_EAST:
55
				if (forward) {
56
					x--;
57
				} else {
58
					x++;
59
					y++;
60
					checkBelow = false;
61
				}
62
63
				shape = RailShape.EAST_WEST;
64
				break;
65
			case ASCENDING_WEST:
66
				if (forward) {
67
					x--;
68
					y++;
69
					checkBelow = false;
70
				} else {
71
					x++;
72
				}
73
74
				shape = RailShape.EAST_WEST;
75
				break;
76
			case ASCENDING_NORTH:
77
				if (forward) {
78
					z++;
79
				} else {
80
					z--;
81
					y++;
82
					checkBelow = false;
83
				}
84
85
				shape = RailShape.NORTH_SOUTH;
86
				break;
87
			case ASCENDING_SOUTH:
88
				if (forward) {
89
					z++;
90
					y++;
91
					checkBelow = false;
92
				} else {
93
					z--;
94
				}
95
96
				shape = RailShape.NORTH_SOUTH;
97
		}
98
99
		return this.isSameRailWithPower(level, new BlockPos(x, y, z), forward, searchDepth, shape)
100
			? true
101
			: checkBelow && this.isSameRailWithPower(level, new BlockPos(x, y - 1, z), forward, searchDepth, shape);
102
	}
103
104
	protected boolean isSameRailWithPower(final Level level, final BlockPos pos, final boolean forward, final int searchDepth, final RailShape dir) {
105
		BlockState state = level.getBlockState(pos);
106
		if (!state.is(this)) {
107
			return false;
108
		}
109
110
		RailShape myShape = state.getValue(SHAPE);
111
		if (dir != RailShape.EAST_WEST || myShape != RailShape.NORTH_SOUTH && myShape != RailShape.ASCENDING_NORTH && myShape != RailShape.ASCENDING_SOUTH) {
112
			if (dir != RailShape.NORTH_SOUTH || myShape != RailShape.EAST_WEST && myShape != RailShape.ASCENDING_EAST && myShape != RailShape.ASCENDING_WEST) {
113
				if (!state.getValue(POWERED)) {
114
					return false;
115
				} else {
116
					return level.hasNeighborSignal(pos) ? true : this.findPoweredRailSignal(level, pos, state, forward, searchDepth + 1);
117
				}
118
			} else {
119
				return false;
120
			}
121
		} else {
122
			return false;
123
		}
124
	}
125
126
	@Override
127
	protected void updateState(final BlockState state, final Level level, final BlockPos pos, final Block block) {
128
		boolean isPowered = state.getValue(POWERED);
129
		boolean shouldPower = level.hasNeighborSignal(pos)
130
			|| this.findPoweredRailSignal(level, pos, state, true, 0)
131
			|| this.findPoweredRailSignal(level, pos, state, false, 0);
132
		if (shouldPower != isPowered) {
133
			level.setBlockAndUpdate(pos, state.setValue(POWERED, shouldPower));
134
			level.updateNeighborsAt(pos.below(), this);
135
			if (state.getValue(SHAPE).isSlope()) {
136
				level.updateNeighborsAt(pos.above(), this);
137
			}
138
		}
139
	}
140
141
	@Override
142
	public Property<RailShape> getShapeProperty() {
143
		return SHAPE;
144
	}
145
146
	@Override
147
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
148
		RailShape currentShape = state.getValue(SHAPE);
149
		RailShape newShape = this.rotate(currentShape, rotation);
150
		return state.setValue(SHAPE, newShape);
151
	}
152
153
	@Override
154
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
155
		RailShape currentShape = state.getValue(SHAPE);
156
		RailShape newShape = this.mirror(currentShape, mirror);
157
		return state.setValue(SHAPE, newShape);
158
	}
159
160
	@Override
161
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
162
		builder.add(SHAPE, POWERED, WATERLOGGED);
163
	}
164
}
165

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