CollectingNeighborUpdater .java

net.minecraft.world.level.redstone.CollectingNeighborUpdater (class): members: SimpleNeighborUpdate, MultiNeighborUpdate, FullNeighborUpdate, ShapeUpdate.

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

Implements

API Surface (13)

void setDebugListener(final @Nullable Consumer<BlockPos> debugListener)
void shapeUpdate(final Direction direction, final BlockState neighborState, final BlockPos pos, final BlockPos neighborPos, final @Block.UpdateFlags int updateFlags, final int updateLimit)
void neighborChanged(final BlockPos pos, final Block block, final @Nullable Orientation orientation)
void neighborChanged(final BlockState state, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
void updateNeighborsAtExceptFromFacing(final BlockPos pos, final Block block, final @Nullable Direction skipDirection, final @Nullable Orientation orientation)
boolean runNext(final Level level)
void forEachUpdatedPos(final Consumer<BlockPos> output)
boolean runNext(final Level level)
void forEachUpdatedPos(final Consumer<BlockPos> output)
boolean runNext(final Level level)
void forEachUpdatedPos(final Consumer<BlockPos> output)
boolean runNext(final Level level)
void forEachUpdatedPos(final Consumer<BlockPos> output)

Fields & Constants (14)

LOGGER
level
maxChainedNeighborUpdates
stack
addedThisLayer
count
debugListener
direction
neighborState
pos
neighborPos
updateFlags
orientation
skipDirection

Full Source

CollectingNeighborUpdater.java213 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.redstone;
2
3
import com.mojang.logging.LogUtils;
4
import java.util.ArrayDeque;
5
import java.util.ArrayList;
6
import java.util.List;
7
import java.util.function.Consumer;
8
import net.minecraft.core.BlockPos;
9
import net.minecraft.core.Direction;
10
import net.minecraft.world.flag.FeatureFlags;
11
import net.minecraft.world.level.Level;
12
import net.minecraft.world.level.block.Block;
13
import net.minecraft.world.level.block.state.BlockState;
14
import org.jspecify.annotations.Nullable;
15
import org.slf4j.Logger;
16
17
public class CollectingNeighborUpdater implements NeighborUpdater {
18
	private static final Logger LOGGER = LogUtils.getLogger();
19
	private final Level level;
20
	private final int maxChainedNeighborUpdates;
21
	private final ArrayDeque<CollectingNeighborUpdater.NeighborUpdates> stack = new ArrayDeque<>();
22
	private final List<CollectingNeighborUpdater.NeighborUpdates> addedThisLayer = new ArrayList<>();
23
	private int count = 0;
24
	private @Nullable Consumer<BlockPos> debugListener;
25
26
	public CollectingNeighborUpdater(final Level level, final int maxChainedNeighborUpdates) {
27
		this.level = level;
28
		this.maxChainedNeighborUpdates = maxChainedNeighborUpdates;
29
	}
30
31
	public void setDebugListener(final @Nullable Consumer<BlockPos> debugListener) {
32
		this.debugListener = debugListener;
33
	}
34
35
	@Override
36
	public void shapeUpdate(
37
		final Direction direction,
38
		final BlockState neighborState,
39
		final BlockPos pos,
40
		final BlockPos neighborPos,
41
		final @Block.UpdateFlags int updateFlags,
42
		final int updateLimit
43
	) {
44
		this.addAndRun(pos, new CollectingNeighborUpdater.ShapeUpdate(direction, neighborState, pos.immutable(), neighborPos.immutable(), updateFlags, updateLimit));
45
	}
46
47
	@Override
48
	public void neighborChanged(final BlockPos pos, final Block block, final @Nullable Orientation orientation) {
49
		this.addAndRun(pos, new CollectingNeighborUpdater.SimpleNeighborUpdate(pos, block, orientation));
50
	}
51
52
	@Override
53
	public void neighborChanged(
54
		final BlockState state, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
55
	) {
56
		this.addAndRun(pos, new CollectingNeighborUpdater.FullNeighborUpdate(state, pos.immutable(), block, orientation, movedByPiston));
57
	}
58
59
	@Override
60
	public void updateNeighborsAtExceptFromFacing(
61
		final BlockPos pos, final Block block, final @Nullable Direction skipDirection, final @Nullable Orientation orientation
62
	) {
63
		this.addAndRun(pos, new CollectingNeighborUpdater.MultiNeighborUpdate(pos.immutable(), block, orientation, skipDirection));
64
	}
65
66
	private void addAndRun(final BlockPos pos, final CollectingNeighborUpdater.NeighborUpdates update) {
67
		boolean runningAlready = this.count > 0;
68
		boolean tooManyUpdates = this.maxChainedNeighborUpdates >= 0 && this.count >= this.maxChainedNeighborUpdates;
69
		this.count++;
70
		if (!tooManyUpdates) {
71
			if (runningAlready) {
72
				this.addedThisLayer.add(update);
73
			} else {
74
				this.stack.push(update);
75
			}
76
		} else if (this.count - 1 == this.maxChainedNeighborUpdates) {
77
			LOGGER.error("Too many chained neighbor updates. Skipping the rest. First skipped position: {}", pos.toShortString());
78
		}
79
80
		if (!runningAlready) {
81
			this.runUpdates();
82
		}
83
	}
84
85
	private void runUpdates() {
86
		try {
87
			while (!this.stack.isEmpty() || !this.addedThisLayer.isEmpty()) {
88
				for (int i = this.addedThisLayer.size() - 1; i >= 0; i--) {
89
					this.stack.push(this.addedThisLayer.get(i));
90
				}
91
92
				this.addedThisLayer.clear();
93
				CollectingNeighborUpdater.NeighborUpdates nextUpdates = this.stack.peek();
94
				if (this.debugListener != null) {
95
					nextUpdates.forEachUpdatedPos(this.debugListener);
96
				}
97
98
				while (this.addedThisLayer.isEmpty()) {
99
					if (!nextUpdates.runNext(this.level)) {
100
						this.stack.pop();
101
						break;
102
					}
103
				}
104
			}
105
		} finally {
106
			this.stack.clear();
107
			this.addedThisLayer.clear();
108
			this.count = 0;
109
		}
110
	}
111
112
	private record FullNeighborUpdate(BlockState state, BlockPos pos, Block block, @Nullable Orientation orientation, boolean movedByPiston)
113
		implements CollectingNeighborUpdater.NeighborUpdates {
114
		@Override
115
		public boolean runNext(final Level level) {
116
			NeighborUpdater.executeUpdate(level, this.state, this.pos, this.block, this.orientation, this.movedByPiston);
117
			return false;
118
		}
119
120
		@Override
121
		public void forEachUpdatedPos(final Consumer<BlockPos> output) {
122
			output.accept(this.pos);
123
		}
124
	}
125
126
	private static final class MultiNeighborUpdate implements CollectingNeighborUpdater.NeighborUpdates {
127
		private final BlockPos sourcePos;
128
		private final Block sourceBlock;
129
		private @Nullable Orientation orientation;
130
		private final @Nullable Direction skipDirection;
131
		private int idx = 0;
132
133
		public MultiNeighborUpdate(
134
			final BlockPos sourcePos, final Block sourceBlock, final @Nullable Orientation orientation, final @Nullable Direction skipDirection
135
		) {
136
			this.sourcePos = sourcePos;
137
			this.sourceBlock = sourceBlock;
138
			this.orientation = orientation;
139
			this.skipDirection = skipDirection;
140
			if (NeighborUpdater.UPDATE_ORDER[this.idx] == skipDirection) {
141
				this.idx++;
142
			}
143
		}
144
145
		@Override
146
		public boolean runNext(final Level level) {
147
			Direction direction = NeighborUpdater.UPDATE_ORDER[this.idx++];
148
			BlockPos neighborPos = this.sourcePos.relative(direction);
149
			BlockState state = level.getBlockState(neighborPos);
150
			Orientation orientation = null;
151
			if (level.enabledFeatures().contains(FeatureFlags.REDSTONE_EXPERIMENTS)) {
152
				if (this.orientation == null) {
153
					this.orientation = ExperimentalRedstoneUtils.initialOrientation(level, this.skipDirection == null ? null : this.skipDirection.getOpposite(), null);
154
				}
155
156
				orientation = this.orientation.withFront(direction);
157
			}
158
159
			NeighborUpdater.executeUpdate(level, state, neighborPos, this.sourceBlock, orientation, false);
160
			if (this.idx < NeighborUpdater.UPDATE_ORDER.length && NeighborUpdater.UPDATE_ORDER[this.idx] == this.skipDirection) {
161
				this.idx++;
162
			}
163
164
			return this.idx < NeighborUpdater.UPDATE_ORDER.length;
165
		}
166
167
		@Override
168
		public void forEachUpdatedPos(final Consumer<BlockPos> output) {
169
			for (Direction direction : NeighborUpdater.UPDATE_ORDER) {
170
				if (direction != this.skipDirection) {
171
					BlockPos neighborPos = this.sourcePos.relative(direction);
172
					output.accept(neighborPos);
173
				}
174
			}
175
		}
176
	}
177
178
	private interface NeighborUpdates {
179
		boolean runNext(Level level);
180
181
		void forEachUpdatedPos(Consumer<BlockPos> output);
182
	}
183
184
	private record ShapeUpdate(
185
		Direction direction, BlockState neighborState, BlockPos pos, BlockPos neighborPos, @Block.UpdateFlags int updateFlags, int updateLimit
186
	) implements CollectingNeighborUpdater.NeighborUpdates {
187
		@Override
188
		public boolean runNext(final Level level) {
189
			NeighborUpdater.executeShapeUpdate(level, this.direction, this.pos, this.neighborPos, this.neighborState, this.updateFlags, this.updateLimit);
190
			return false;
191
		}
192
193
		@Override
194
		public void forEachUpdatedPos(final Consumer<BlockPos> output) {
195
			output.accept(this.pos);
196
		}
197
	}
198
199
	private record SimpleNeighborUpdate(BlockPos pos, Block block, @Nullable Orientation orientation) implements CollectingNeighborUpdater.NeighborUpdates {
200
		@Override
201
		public boolean runNext(final Level level) {
202
			BlockState state = level.getBlockState(this.pos);
203
			NeighborUpdater.executeUpdate(level, state, this.pos, this.block, this.orientation, false);
204
			return false;
205
		}
206
207
		@Override
208
		public void forEachUpdatedPos(final Consumer<BlockPos> output) {
209
			output.accept(this.pos);
210
		}
211
	}
212
}
213

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