SideChainPartBlock .java

net.minecraft.world.level.block.SideChainPartBlock (interface): members: updateSelfAndNeighborsOnPoweringUp, updateNeighborsAfterPoweringDown, canConnect.

net.minecraft.world.level.blockinterface214 linescommon7.3 KB

API Surface (14)

boolean isConnectable()
boolean isUnconnectableOrChainEnd()
boolean connectsTowards(final SideChainPart endPart)
SideChainPartBlock.Neighbor left(final int steps)
SideChainPartBlock.Neighbor right(final int steps)
SideChainPartBlock.Neighbor left()
SideChainPartBlock.Neighbor right()
boolean isConnectable()
boolean isUnconnectableOrChainEnd()
boolean connectsTowards(final SideChainPart endPart)
void connectToTheRight()
void connectToTheLeft()
void disconnectFromRight()
void disconnectFromLeft()

Fields & Constants (1)

endPart

Code Graph: Dependencies (5)

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

Full Source

SideChainPartBlock.java214 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.HashMap;
4
import java.util.LinkedList;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.function.Consumer;
8
import java.util.function.IntFunction;
9
import net.minecraft.core.BlockPos;
10
import net.minecraft.core.Direction;
11
import net.minecraft.world.level.LevelAccessor;
12
import net.minecraft.world.level.block.state.BlockState;
13
import net.minecraft.world.level.block.state.properties.SideChainPart;
14
15
public interface SideChainPartBlock {
16
	SideChainPart getSideChainPart(final BlockState state);
17
18
	BlockState setSideChainPart(final BlockState state, final SideChainPart newPart);
19
20
	Direction getFacing(final BlockState state);
21
22
	boolean isConnectable(final BlockState state);
23
24
	int getMaxChainLength();
25
26
	default List<BlockPos> getAllBlocksConnectedTo(final LevelAccessor level, final BlockPos pos) {
27
		BlockState state = level.getBlockState(pos);
28
		if (!this.isConnectable(state)) {
29
			return List.of();
30
		}
31
32
		SideChainPartBlock.Neighbors neighbors = this.getNeighbors(level, pos, this.getFacing(state));
33
		List<BlockPos> results = new LinkedList<>();
34
		results.add(pos);
35
		this.addBlocksConnectingTowards(neighbors::left, SideChainPart.LEFT, results::addFirst);
36
		this.addBlocksConnectingTowards(neighbors::right, SideChainPart.RIGHT, results::addLast);
37
		return results;
38
	}
39
40
	private void addBlocksConnectingTowards(
41
		final IntFunction<SideChainPartBlock.Neighbor> getNeighbor, final SideChainPart endPart, final Consumer<BlockPos> accumulator
42
	) {
43
		for (int steps = 1; steps < this.getMaxChainLength(); steps++) {
44
			SideChainPartBlock.Neighbor neighbor = getNeighbor.apply(steps);
45
			if (neighbor.connectsTowards(endPart)) {
46
				accumulator.accept(neighbor.pos());
47
			}
48
49
			if (neighbor.isUnconnectableOrChainEnd()) {
50
				break;
51
			}
52
		}
53
	}
54
55
	default void updateNeighborsAfterPoweringDown(final LevelAccessor level, final BlockPos pos, final BlockState state) {
56
		SideChainPartBlock.Neighbors neighbors = this.getNeighbors(level, pos, this.getFacing(state));
57
		neighbors.left().disconnectFromRight();
58
		neighbors.right().disconnectFromLeft();
59
	}
60
61
	default void updateSelfAndNeighborsOnPoweringUp(final LevelAccessor level, final BlockPos pos, final BlockState state, final BlockState oldState) {
62
		if (this.isConnectable(state)) {
63
			if (!this.isBeingUpdatedByNeighbor(state, oldState)) {
64
				SideChainPartBlock.Neighbors neighbors = this.getNeighbors(level, pos, this.getFacing(state));
65
				SideChainPart newPartForSelf = SideChainPart.UNCONNECTED;
66
				int existingChainOnTheLeft = neighbors.left().isConnectable() ? this.getAllBlocksConnectedTo(level, neighbors.left().pos()).size() : 0;
67
				int existingChainOnTheRight = neighbors.right().isConnectable() ? this.getAllBlocksConnectedTo(level, neighbors.right().pos()).size() : 0;
68
				int currentChainLength = 1;
69
				if (this.canConnect(existingChainOnTheLeft, currentChainLength)) {
70
					newPartForSelf = newPartForSelf.whenConnectedToTheLeft();
71
					neighbors.left().connectToTheRight();
72
					currentChainLength += existingChainOnTheLeft;
73
				}
74
75
				if (this.canConnect(existingChainOnTheRight, currentChainLength)) {
76
					newPartForSelf = newPartForSelf.whenConnectedToTheRight();
77
					neighbors.right().connectToTheLeft();
78
				}
79
80
				this.setPart(level, pos, newPartForSelf);
81
			}
82
		}
83
	}
84
85
	private boolean canConnect(final int newBlocksToConnectTo, final int currentChainLength) {
86
		return newBlocksToConnectTo > 0 && currentChainLength + newBlocksToConnectTo <= this.getMaxChainLength();
87
	}
88
89
	private boolean isBeingUpdatedByNeighbor(final BlockState state, final BlockState oldState) {
90
		boolean isGettingConnected = this.getSideChainPart(state).isConnected();
91
		boolean hasBeenConnectedBefore = this.isConnectable(oldState) && this.getSideChainPart(oldState).isConnected();
92
		return isGettingConnected || hasBeenConnectedBefore;
93
	}
94
95
	private SideChainPartBlock.Neighbors getNeighbors(final LevelAccessor level, final BlockPos center, final Direction facing) {
96
		return new SideChainPartBlock.Neighbors(this, level, facing, center, new HashMap<>());
97
	}
98
99
	private void setPart(final LevelAccessor level, final BlockPos pos, final SideChainPart newPart) {
100
		BlockState state = level.getBlockState(pos);
101
		if (this.getSideChainPart(state) != newPart) {
102
			level.setBlockAndUpdate(pos, this.setSideChainPart(state, newPart));
103
		}
104
	}
105
106
	record EmptyNeighbor(BlockPos pos) implements SideChainPartBlock.Neighbor {
107
		@Override
108
		public boolean isConnectable() {
109
			return false;
110
		}
111
112
		@Override
113
		public boolean isUnconnectableOrChainEnd() {
114
			return true;
115
		}
116
117
		@Override
118
		public boolean connectsTowards(final SideChainPart endPart) {
119
			return false;
120
		}
121
	}
122
123
	sealed interface Neighbor permits SideChainPartBlock.EmptyNeighbor, SideChainPartBlock.SideChainNeighbor {
124
		BlockPos pos();
125
126
		boolean isConnectable();
127
128
		boolean isUnconnectableOrChainEnd();
129
130
		boolean connectsTowards(final SideChainPart endPart);
131
132
		default void connectToTheRight() {
133
		}
134
135
		default void connectToTheLeft() {
136
		}
137
138
		default void disconnectFromRight() {
139
		}
140
141
		default void disconnectFromLeft() {
142
		}
143
	}
144
145
	record Neighbors(SideChainPartBlock block, LevelAccessor level, Direction facing, BlockPos center, Map<BlockPos, SideChainPartBlock.Neighbor> cache) {
146
		private boolean isConnectableToThisBlock(final BlockState neighbor) {
147
			return this.block.isConnectable(neighbor) && this.block.getFacing(neighbor) == this.facing;
148
		}
149
150
		private SideChainPartBlock.Neighbor createNewNeighbor(final BlockPos pos) {
151
			BlockState neighbor = this.level.getBlockState(pos);
152
			SideChainPart part = this.isConnectableToThisBlock(neighbor) ? this.block.getSideChainPart(neighbor) : null;
153
			return part == null ? new SideChainPartBlock.EmptyNeighbor(pos) : new SideChainPartBlock.SideChainNeighbor(this.level, this.block, pos, part);
154
		}
155
156
		private SideChainPartBlock.Neighbor getOrCreateNeighbor(final Direction dir, final Integer steps) {
157
			return this.cache.computeIfAbsent(this.center.relative(dir, steps), this::createNewNeighbor);
158
		}
159
160
		public SideChainPartBlock.Neighbor left(final int steps) {
161
			return this.getOrCreateNeighbor(this.facing.getClockWise(), steps);
162
		}
163
164
		public SideChainPartBlock.Neighbor right(final int steps) {
165
			return this.getOrCreateNeighbor(this.facing.getCounterClockWise(), steps);
166
		}
167
168
		public SideChainPartBlock.Neighbor left() {
169
			return this.left(1);
170
		}
171
172
		public SideChainPartBlock.Neighbor right() {
173
			return this.right(1);
174
		}
175
	}
176
177
	record SideChainNeighbor(LevelAccessor level, SideChainPartBlock block, BlockPos pos, SideChainPart part) implements SideChainPartBlock.Neighbor {
178
		@Override
179
		public boolean isConnectable() {
180
			return true;
181
		}
182
183
		@Override
184
		public boolean isUnconnectableOrChainEnd() {
185
			return this.part.isChainEnd();
186
		}
187
188
		@Override
189
		public boolean connectsTowards(final SideChainPart endPart) {
190
			return this.part.isConnectionTowards(endPart);
191
		}
192
193
		@Override
194
		public void connectToTheRight() {
195
			this.block.setPart(this.level, this.pos, this.part.whenConnectedToTheRight());
196
		}
197
198
		@Override
199
		public void connectToTheLeft() {
200
			this.block.setPart(this.level, this.pos, this.part.whenConnectedToTheLeft());
201
		}
202
203
		@Override
204
		public void disconnectFromRight() {
205
			this.block.setPart(this.level, this.pos, this.part.whenDisconnectedFromTheRight());
206
		}
207
208
		@Override
209
		public void disconnectFromLeft() {
210
			this.block.setPart(this.level, this.pos, this.part.whenDisconnectedFromTheLeft());
211
		}
212
	}
213
}
214

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