UpgradeData .java

net.minecraft.world.level.chunk.UpgradeData (class): members: neighborBlockTicks, neighborFluidTicks, CHUNKY_FIXERS, BlockFixers, TAG_INDICES, BlockFixer.

net.minecraft.world.level.chunkclass455 linescommon16.6 KB

API Surface (10)

void upgrade(final LevelChunk chunk)
boolean isEmpty()
CompoundTag write()
UpgradeData copy()
BlockState updateShape(final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos)
BlockState updateShape(final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos)
BlockState updateShape(final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos)
BlockState updateShape(final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos)
void processChunk(final LevelAccessor level)
BlockState updateShape(final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos)

Fields & Constants (13)

LOGGER
EMPTY
TAG_INDICES
DIRECTIONS
BLOCK_TICKS_CODEC
FLUID_TICKS_CODEC
MAP
CHUNKY_FIXERS
sides
neighborBlockTicks
neighborFluidTicks
index
pos

String literals (4)

"Indices"
"Sides"
"neighbor_block_ticks"
"neighbor_fluid_ticks"

Full Source

UpgradeData.java455 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.chunk;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Sets;
5
import com.mojang.logging.LogUtils;
6
import com.mojang.serialization.Codec;
7
import it.unimi.dsi.fastutil.ints.IntArrays;
8
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
9
import it.unimi.dsi.fastutil.objects.ObjectSet;
10
import java.util.EnumSet;
11
import java.util.IdentityHashMap;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Set;
15
import net.minecraft.core.BlockPos;
16
import net.minecraft.core.CompositeDirection;
17
import net.minecraft.core.Direction;
18
import net.minecraft.core.SectionPos;
19
import net.minecraft.core.registries.BuiltInRegistries;
20
import net.minecraft.nbt.CompoundTag;
21
import net.minecraft.world.level.ChunkPos;
22
import net.minecraft.world.level.EmptyBlockGetter;
23
import net.minecraft.world.level.Level;
24
import net.minecraft.world.level.LevelAccessor;
25
import net.minecraft.world.level.LevelHeightAccessor;
26
import net.minecraft.world.level.block.Block;
27
import net.minecraft.world.level.block.Blocks;
28
import net.minecraft.world.level.block.ChestBlock;
29
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
30
import net.minecraft.world.level.block.StemBlock;
31
import net.minecraft.world.level.block.entity.BlockEntity;
32
import net.minecraft.world.level.block.entity.ChestBlockEntity;
33
import net.minecraft.world.level.block.state.BlockState;
34
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
35
import net.minecraft.world.level.block.state.properties.ChestType;
36
import net.minecraft.world.level.material.Fluid;
37
import net.minecraft.world.level.material.Fluids;
38
import net.minecraft.world.ticks.SavedTick;
39
import org.slf4j.Logger;
40
41
public class UpgradeData {
42
	private static final Logger LOGGER = LogUtils.getLogger();
43
	public static final UpgradeData EMPTY = new UpgradeData(EmptyBlockGetter.INSTANCE);
44
	private static final String TAG_INDICES = "Indices";
45
	private static final CompositeDirection.Direction8[] DIRECTIONS = CompositeDirection.Direction8.values();
46
	private static final Codec<List<SavedTick<Block>>> BLOCK_TICKS_CODEC = SavedTick.codec(BuiltInRegistries.BLOCK.byNameCodec().orElse(Blocks.AIR)).listOf();
47
	private static final Codec<List<SavedTick<Fluid>>> FLUID_TICKS_CODEC = SavedTick.codec(BuiltInRegistries.FLUID.byNameCodec().orElse(Fluids.EMPTY)).listOf();
48
	private final EnumSet<CompositeDirection.Direction8> sides = EnumSet.noneOf(CompositeDirection.Direction8.class);
49
	private final List<SavedTick<Block>> neighborBlockTicks = Lists.newArrayList();
50
	private final List<SavedTick<Fluid>> neighborFluidTicks = Lists.newArrayList();
51
	private final int[][] index;
52
	private static final Map<Block, UpgradeData.BlockFixer> MAP = new IdentityHashMap<>();
53
	private static final Set<UpgradeData.BlockFixer> CHUNKY_FIXERS = Sets.newHashSet();
54
55
	private UpgradeData(final LevelHeightAccessor levelHeightAccessor) {
56
		this.index = new int[levelHeightAccessor.getSectionsCount()][];
57
	}
58
59
	public UpgradeData(final CompoundTag tag, final LevelHeightAccessor levelHeightAccessor) {
60
		this(levelHeightAccessor);
61
		tag.getCompound("Indices").ifPresent(indicesTag -> {
62
			for (int i = 0; i < this.index.length; i++) {
63
				this.index[i] = indicesTag.getIntArray(String.valueOf(i)).orElse(null);
64
			}
65
		});
66
		int sideInt = tag.getIntOr("Sides", 0);
67
68
		for (CompositeDirection.Direction8 direction8 : CompositeDirection.Direction8.values()) {
69
			if ((sideInt & 1 << direction8.ordinal()) != 0) {
70
				this.sides.add(direction8);
71
			}
72
		}
73
74
		tag.read("neighbor_block_ticks", BLOCK_TICKS_CODEC).ifPresent(this.neighborBlockTicks::addAll);
75
		tag.read("neighbor_fluid_ticks", FLUID_TICKS_CODEC).ifPresent(this.neighborFluidTicks::addAll);
76
	}
77
78
	private UpgradeData(final UpgradeData source) {
79
		this.sides.addAll(source.sides);
80
		this.neighborBlockTicks.addAll(source.neighborBlockTicks);
81
		this.neighborFluidTicks.addAll(source.neighborFluidTicks);
82
		this.index = new int[source.index.length][];
83
84
		for (int i = 0; i < source.index.length; i++) {
85
			int[] indices = source.index[i];
86
			this.index[i] = indices != null ? IntArrays.copy(indices) : null;
87
		}
88
	}
89
90
	public void upgrade(final LevelChunk chunk) {
91
		this.upgradeInside(chunk);
92
93
		for (CompositeDirection.Direction8 direction8 : DIRECTIONS) {
94
			upgradeSides(chunk, direction8);
95
		}
96
97
		Level level = chunk.getLevel();
98
		this.neighborBlockTicks.forEach(tick -> {
99
			Block type = tick.type() == Blocks.AIR ? level.getBlockState(tick.pos()).getBlock() : tick.type();
100
			level.scheduleTick(tick.pos(), type, tick.delay(), tick.priority());
101
		});
102
		this.neighborFluidTicks.forEach(tick -> {
103
			Fluid type = tick.type() == Fluids.EMPTY ? level.getFluidState(tick.pos()).getType() : tick.type();
104
			level.scheduleTick(tick.pos(), type, tick.delay(), tick.priority());
105
		});
106
		CHUNKY_FIXERS.forEach(fixer -> fixer.processChunk(level));
107
	}
108
109
	private static void upgradeSides(final LevelChunk chunk, final CompositeDirection.Direction8 direction8) {
110
		Level level = chunk.getLevel();
111
		if (chunk.getUpgradeData().sides.remove(direction8)) {
112
			Set<Direction> directions = direction8.getDirections();
113
			int min = 0;
114
			int max = 15;
115
			boolean east = directions.contains(Direction.EAST);
116
			boolean west = directions.contains(Direction.WEST);
117
			boolean south = directions.contains(Direction.SOUTH);
118
			boolean north = directions.contains(Direction.NORTH);
119
			boolean singular = directions.size() == 1;
120
			ChunkPos chunkPos = chunk.getPos();
121
			int minX = chunkPos.getMinBlockX() + (!singular || !north && !south ? (west ? 0 : 15) : 1);
122
			int maxX = chunkPos.getMinBlockX() + (!singular || !north && !south ? (west ? 0 : 15) : 14);
123
			int minZ = chunkPos.getMinBlockZ() + (!singular || !east && !west ? (north ? 0 : 15) : 1);
124
			int maxZ = chunkPos.getMinBlockZ() + (!singular || !east && !west ? (north ? 0 : 15) : 14);
125
			Direction[] updateDirections = Direction.values();
126
			BlockPos.MutableBlockPos neighbourPos = new BlockPos.MutableBlockPos();
127
128
			for (BlockPos pos : BlockPos.betweenClosed(minX, level.getMinY(), minZ, maxX, level.getMaxY(), maxZ)) {
129
				BlockState state = level.getBlockState(pos);
130
				BlockState newState = state;
131
132
				for (Direction direction : updateDirections) {
133
					neighbourPos.setWithOffset(pos, direction);
134
					newState = updateState(newState, direction, level, pos, neighbourPos);
135
				}
136
137
				Block.updateOrDestroy(state, newState, level, pos, 18);
138
			}
139
		}
140
	}
141
142
	private static BlockState updateState(
143
		final BlockState state, final Direction direction, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos
144
	) {
145
		return MAP.getOrDefault(state.getBlock(), UpgradeData.BlockFixers.DEFAULT)
146
			.updateShape(state, direction, level.getBlockState(neighbourPos), level, pos, neighbourPos);
147
	}
148
149
	private void upgradeInside(final LevelChunk chunk) {
150
		BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
151
		BlockPos.MutableBlockPos neighbourPos = new BlockPos.MutableBlockPos();
152
		ChunkPos chunkPos = chunk.getPos();
153
		LevelAccessor level = chunk.getLevel();
154
155
		for (int sectionIndex = 0; sectionIndex < this.index.length; sectionIndex++) {
156
			LevelChunkSection chunkSection = chunk.getSection(sectionIndex);
157
			int[] upgradeIndex = this.index[sectionIndex];
158
			this.index[sectionIndex] = null;
159
			if (upgradeIndex != null && upgradeIndex.length > 0) {
160
				Direction[] directions = Direction.values();
161
				PalettedContainer<BlockState> states = chunkSection.getStates();
162
				int sectionY = chunk.getSectionYFromSectionIndex(sectionIndex);
163
				int bottomYInSection = SectionPos.sectionToBlockCoord(sectionY);
164
165
				for (int coordinate : upgradeIndex) {
166
					int x = coordinate & 15;
167
					int y = coordinate >> 8 & 15;
168
					int z = coordinate >> 4 & 15;
169
					pos.set(chunkPos.getMinBlockX() + x, bottomYInSection + y, chunkPos.getMinBlockZ() + z);
170
					BlockState state = states.get(coordinate);
171
					BlockState newState = state;
172
173
					for (Direction direction : directions) {
174
						neighbourPos.setWithOffset(pos, direction);
175
						if (SectionPos.blockToSectionCoord(pos.getX()) == chunkPos.x() && SectionPos.blockToSectionCoord(pos.getZ()) == chunkPos.z()) {
176
							newState = updateState(newState, direction, level, pos, neighbourPos);
177
						}
178
					}
179
180
					Block.updateOrDestroy(state, newState, level, pos, 18);
181
				}
182
			}
183
		}
184
185
		for (int i = 0; i < this.index.length; i++) {
186
			if (this.index[i] != null) {
187
				LOGGER.warn("Discarding update data for section {} for chunk ({} {})", level.getSectionYFromSectionIndex(i), chunkPos.x(), chunkPos.z());
188
			}
189
190
			this.index[i] = null;
191
		}
192
	}
193
194
	public boolean isEmpty() {
195
		for (int[] ints : this.index) {
196
			if (ints != null) {
197
				return false;
198
			}
199
		}
200
201
		return this.sides.isEmpty();
202
	}
203
204
	public CompoundTag write() {
205
		CompoundTag tag = new CompoundTag();
206
		CompoundTag indicesTag = new CompoundTag();
207
208
		for (int i = 0; i < this.index.length; i++) {
209
			String key = String.valueOf(i);
210
			if (this.index[i] != null && this.index[i].length != 0) {
211
				indicesTag.putIntArray(key, this.index[i]);
212
			}
213
		}
214
215
		if (!indicesTag.isEmpty()) {
216
			tag.put("Indices", indicesTag);
217
		}
218
219
		int sides = 0;
220
221
		for (CompositeDirection.Direction8 side : this.sides) {
222
			sides |= 1 << side.ordinal();
223
		}
224
225
		tag.putByte("Sides", (byte)sides);
226
		if (!this.neighborBlockTicks.isEmpty()) {
227
			tag.store("neighbor_block_ticks", BLOCK_TICKS_CODEC, this.neighborBlockTicks);
228
		}
229
230
		if (!this.neighborFluidTicks.isEmpty()) {
231
			tag.store("neighbor_fluid_ticks", FLUID_TICKS_CODEC, this.neighborFluidTicks);
232
		}
233
234
		return tag;
235
	}
236
237
	public UpgradeData copy() {
238
		return this == EMPTY ? EMPTY : new UpgradeData(this);
239
	}
240
241
	public interface BlockFixer {
242
		BlockState updateShape(
243
			final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos
244
		);
245
246
		default void processChunk(final LevelAccessor level) {
247
		}
248
	}
249
250
	private enum BlockFixers implements UpgradeData.BlockFixer {
251
		BLACKLIST(
252
			Blocks.OBSERVER,
253
			Blocks.NETHER_PORTAL,
254
			Blocks.CONCRETE_POWDER.white(),
255
			Blocks.CONCRETE_POWDER.orange(),
256
			Blocks.CONCRETE_POWDER.magenta(),
257
			Blocks.CONCRETE_POWDER.lightBlue(),
258
			Blocks.CONCRETE_POWDER.yellow(),
259
			Blocks.CONCRETE_POWDER.lime(),
260
			Blocks.CONCRETE_POWDER.pink(),
261
			Blocks.CONCRETE_POWDER.gray(),
262
			Blocks.CONCRETE_POWDER.lightGray(),
263
			Blocks.CONCRETE_POWDER.cyan(),
264
			Blocks.CONCRETE_POWDER.purple(),
265
			Blocks.CONCRETE_POWDER.blue(),
266
			Blocks.CONCRETE_POWDER.brown(),
267
			Blocks.CONCRETE_POWDER.green(),
268
			Blocks.CONCRETE_POWDER.red(),
269
			Blocks.CONCRETE_POWDER.black(),
270
			Blocks.ANVIL,
271
			Blocks.CHIPPED_ANVIL,
272
			Blocks.DAMAGED_ANVIL,
273
			Blocks.DRAGON_EGG,
274
			Blocks.GRAVEL,
275
			Blocks.SAND,
276
			Blocks.RED_SAND,
277
			Blocks.OAK_SIGN,
278
			Blocks.SPRUCE_SIGN,
279
			Blocks.BIRCH_SIGN,
280
			Blocks.ACACIA_SIGN,
281
			Blocks.CHERRY_SIGN,
282
			Blocks.JUNGLE_SIGN,
283
			Blocks.DARK_OAK_SIGN,
284
			Blocks.PALE_OAK_SIGN,
285
			Blocks.POPLAR_SIGN,
286
			Blocks.OAK_WALL_SIGN,
287
			Blocks.SPRUCE_WALL_SIGN,
288
			Blocks.BIRCH_WALL_SIGN,
289
			Blocks.ACACIA_WALL_SIGN,
290
			Blocks.JUNGLE_WALL_SIGN,
291
			Blocks.DARK_OAK_WALL_SIGN,
292
			Blocks.PALE_OAK_WALL_SIGN,
293
			Blocks.POPLAR_WALL_SIGN,
294
			Blocks.OAK_HANGING_SIGN,
295
			Blocks.SPRUCE_HANGING_SIGN,
296
			Blocks.BIRCH_HANGING_SIGN,
297
			Blocks.ACACIA_HANGING_SIGN,
298
			Blocks.JUNGLE_HANGING_SIGN,
299
			Blocks.DARK_OAK_HANGING_SIGN,
300
			Blocks.PALE_OAK_HANGING_SIGN,
301
			Blocks.POPLAR_HANGING_SIGN,
302
			Blocks.OAK_WALL_HANGING_SIGN,
303
			Blocks.SPRUCE_WALL_HANGING_SIGN,
304
			Blocks.BIRCH_WALL_HANGING_SIGN,
305
			Blocks.ACACIA_WALL_HANGING_SIGN,
306
			Blocks.JUNGLE_WALL_HANGING_SIGN,
307
			Blocks.DARK_OAK_WALL_HANGING_SIGN,
308
			Blocks.PALE_OAK_WALL_HANGING_SIGN,
309
			Blocks.POPLAR_WALL_HANGING_SIGN
310
		) {
311
			@Override
312
			public BlockState updateShape(
313
				final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos
314
			) {
315
				return state;
316
			}
317
		},
318
		DEFAULT {
319
			@Override
320
			public BlockState updateShape(
321
				final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos
322
			) {
323
				return state.updateShape(level, level, pos, direction, neighbourPos, level.getBlockState(neighbourPos), level.getRandom());
324
			}
325
		},
326
		CHEST(Blocks.CHEST, Blocks.TRAPPED_CHEST) {
327
			@Override
328
			public BlockState updateShape(
329
				final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos
330
			) {
331
				if (neighbour.is(state.getBlock())
332
					&& direction.getAxis().isHorizontal()
333
					&& state.getValue(ChestBlock.TYPE) == ChestType.SINGLE
334
					&& neighbour.getValue(ChestBlock.TYPE) == ChestType.SINGLE) {
335
					Direction facing = state.getValue(ChestBlock.FACING);
336
					if (direction.getAxis() != facing.getAxis() && facing == neighbour.getValue(ChestBlock.FACING)) {
337
						ChestType newType = direction == facing.getClockWise() ? ChestType.LEFT : ChestType.RIGHT;
338
						level.setBlock(neighbourPos, neighbour.setValue(ChestBlock.TYPE, newType.getOpposite()), 18);
339
						if (facing == Direction.NORTH || facing == Direction.EAST) {
340
							BlockEntity one = level.getBlockEntity(pos);
341
							BlockEntity two = level.getBlockEntity(neighbourPos);
342
							if (one instanceof ChestBlockEntity firstChestBlockEntity && two instanceof ChestBlockEntity secondChestBlockEntity) {
343
								ChestBlockEntity.swapContents(firstChestBlockEntity, secondChestBlockEntity);
344
							}
345
						}
346
347
						return state.setValue(ChestBlock.TYPE, newType);
348
					}
349
				}
350
351
				return state;
352
			}
353
		},
354
		LEAVES(
355
			true,
356
			Blocks.ACACIA_LEAVES,
357
			Blocks.CHERRY_LEAVES,
358
			Blocks.BIRCH_LEAVES,
359
			Blocks.PALE_OAK_LEAVES,
360
			Blocks.RED_POPLAR_LEAVES,
361
			Blocks.ORANGE_POPLAR_LEAVES,
362
			Blocks.YELLOW_POPLAR_LEAVES,
363
			Blocks.DARK_OAK_LEAVES,
364
			Blocks.JUNGLE_LEAVES,
365
			Blocks.OAK_LEAVES,
366
			Blocks.SPRUCE_LEAVES
367
		) {
368
			private final ThreadLocal<List<ObjectSet<BlockPos>>> queue = ThreadLocal.withInitial(() -> Lists.newArrayListWithCapacity(7));
369
370
			@Override
371
			public BlockState updateShape(
372
				final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos
373
			) {
374
				BlockState newState = state.updateShape(level, level, pos, direction, neighbourPos, level.getBlockState(neighbourPos), level.getRandom());
375
				if (state != newState) {
376
					int distance = newState.getValue(BlockStateProperties.DISTANCE);
377
					List<ObjectSet<BlockPos>> queue = this.queue.get();
378
					if (queue.isEmpty()) {
379
						for (int i = 0; i < 7; i++) {
380
							queue.add(new ObjectOpenHashSet<>());
381
						}
382
					}
383
384
					queue.get(distance).add(pos.immutable());
385
				}
386
387
				return state;
388
			}
389
390
			@Override
391
			public void processChunk(final LevelAccessor level) {
392
				BlockPos.MutableBlockPos neighborPos = new BlockPos.MutableBlockPos();
393
				List<ObjectSet<BlockPos>> queue = this.queue.get();
394
395
				for (int neighborDistance = 2; neighborDistance < queue.size(); neighborDistance++) {
396
					int currentDistance = neighborDistance - 1;
397
					ObjectSet<BlockPos> set = queue.get(currentDistance);
398
					ObjectSet<BlockPos> newSet = queue.get(neighborDistance);
399
400
					for (BlockPos pos : set) {
401
						BlockState state = level.getBlockState(pos);
402
						if (state.getValue(BlockStateProperties.DISTANCE) >= currentDistance) {
403
							level.setBlock(pos, state.setValue(BlockStateProperties.DISTANCE, currentDistance), 18);
404
							if (neighborDistance != 7) {
405
								for (Direction direction : DIRECTIONS) {
406
									neighborPos.setWithOffset(pos, direction);
407
									BlockState neighbor = level.getBlockState(neighborPos);
408
									if (neighbor.hasProperty(BlockStateProperties.DISTANCE) && state.getValue(BlockStateProperties.DISTANCE) > neighborDistance) {
409
										newSet.add(neighborPos.immutable());
410
									}
411
								}
412
							}
413
						}
414
					}
415
				}
416
417
				queue.clear();
418
			}
419
		},
420
		STEM_BLOCK(Blocks.MELON_STEM, Blocks.PUMPKIN_STEM) {
421
			@Override
422
			public BlockState updateShape(
423
				final BlockState state, final Direction direction, final BlockState neighbour, final LevelAccessor level, final BlockPos pos, final BlockPos neighbourPos
424
			) {
425
				if (state.getValue(StemBlock.AGE) == 7) {
426
					Block fruit = state.is(Blocks.PUMPKIN_STEM) ? Blocks.PUMPKIN : Blocks.MELON;
427
					if (neighbour.is(fruit)) {
428
						return (state.is(Blocks.PUMPKIN_STEM) ? Blocks.ATTACHED_PUMPKIN_STEM : Blocks.ATTACHED_MELON_STEM)
429
							.defaultBlockState()
430
							.setValue(HorizontalDirectionalBlock.FACING, direction);
431
					}
432
				}
433
434
				return state;
435
			}
436
		};
437
438
		public static final Direction[] DIRECTIONS = Direction.values();
439
440
		BlockFixers(final Block... blocks) {
441
			this(false, blocks);
442
		}
443
444
		BlockFixers(final boolean chunky, final Block... blocks) {
445
			for (Block block : blocks) {
446
				UpgradeData.MAP.put(block, this);
447
			}
448
449
			if (chunky) {
450
				UpgradeData.CHUNKY_FIXERS.add(this);
451
			}
452
		}
453
	}
454
}
455

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