ChunkProtoTickListFix .java

net.minecraft.util.datafix.fixes.ChunkProtoTickListFix (class, 234 lines): members: PoorMansPalettedContainer, ALWAYS_WATERLOGGED, palettedContainers, tag.

net.minecraft.util.datafix.fixesclass234 linescommonextends DataFix9.5 KB

API Surface (3)

TypeRewriteRule makeRule()
List<? extends Dynamic<?>> palette()
long[] data()

Fields & Constants (11)

SECTION_WIDTH
ALWAYS_WATERLOGGED
tag
palettedContainers
sectionMinY
sectionX
sectionZ
protoTickListTag
container
sectionY
pos

String literals (27)

"minecraft:bubble_column"
"minecraft:kelp"
"minecraft:kelp_plant"
"minecraft:seagrass"
"minecraft:tall_seagrass"
"Level"
"Sections"
"block_states"
"biomes"
"palette"
"TileTicks"
"LiquidTicks"
"fluid_ticks"
"yPos"
"xPos"
"zPos"
"LiquidsToBeTicked"
"ToBeTicked"
"minecraft:air"
"minecraft:empty"
"minecraft:water"
"Properties"
"level"
"minecraft:flowing_water"

Code Graph: Dependencies (1)

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

Full Source

ChunkProtoTickListFix.java234 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix.fixes;
2
3
import com.google.common.base.Suppliers;
4
import com.google.common.collect.ImmutableMap;
5
import com.google.common.collect.ImmutableSet;
6
import com.mojang.datafixers.DSL;
7
import com.mojang.datafixers.DataFix;
8
import com.mojang.datafixers.DataFixUtils;
9
import com.mojang.datafixers.OpticFinder;
10
import com.mojang.datafixers.TypeRewriteRule;
11
import com.mojang.datafixers.Typed;
12
import com.mojang.datafixers.schemas.Schema;
13
import com.mojang.datafixers.types.Type;
14
import com.mojang.datafixers.types.templates.List.ListType;
15
import com.mojang.datafixers.util.Pair;
16
import com.mojang.serialization.Dynamic;
17
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
18
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
19
import java.util.Collections;
20
import java.util.List;
21
import java.util.Optional;
22
import java.util.function.Function;
23
import java.util.function.Supplier;
24
import java.util.stream.Stream;
25
import org.apache.commons.lang3.mutable.MutableInt;
26
import org.jspecify.annotations.Nullable;
27
28
public class ChunkProtoTickListFix extends DataFix {
29
	private static final int SECTION_WIDTH = 16;
30
	private static final ImmutableSet<String> ALWAYS_WATERLOGGED = ImmutableSet.of(
31
		"minecraft:bubble_column", "minecraft:kelp", "minecraft:kelp_plant", "minecraft:seagrass", "minecraft:tall_seagrass"
32
	);
33
34
	public ChunkProtoTickListFix(final Schema outputSchema) {
35
		super(outputSchema, false);
36
	}
37
38
	@Override
39
	protected TypeRewriteRule makeRule() {
40
		Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
41
		OpticFinder<?> levelFinder = chunkType.findField("Level");
42
		OpticFinder<?> sectionsFinder = levelFinder.type().findField("Sections");
43
		OpticFinder<?> sectionFinder = ((ListType)sectionsFinder.type()).getElement().finder();
44
		OpticFinder<?> blockStateContainerFinder = sectionFinder.type().findField("block_states");
45
		OpticFinder<?> biomeContainerFinder = sectionFinder.type().findField("biomes");
46
		OpticFinder<?> blockStatePaletteFinder = blockStateContainerFinder.type().findField("palette");
47
		OpticFinder<?> tileTickFinder = levelFinder.type().findField("TileTicks");
48
		return this.fixTypeEverywhereTyped(
49
			"ChunkProtoTickListFix",
50
			chunkType,
51
			chunk -> chunk.updateTyped(
52
				levelFinder,
53
				level -> {
54
					level = level.update(
55
						DSL.remainderFinder(),
56
						tag -> DataFixUtils.orElse(tag.get("LiquidTicks").result().map(v -> tag.set("fluid_ticks", (Dynamic<?>)v).remove("LiquidTicks")), tag)
57
					);
58
					Dynamic<?> chunkTag = level.get(DSL.remainderFinder());
59
					MutableInt lowestY = new MutableInt();
60
					Int2ObjectMap<Supplier<ChunkProtoTickListFix.PoorMansPalettedContainer>> palettedContainers = new Int2ObjectArrayMap<>();
61
					level.getOptionalTyped(sectionsFinder)
62
						.ifPresent(
63
							sections -> sections.getAllTyped(sectionFinder)
64
								.forEach(
65
									section -> {
66
										Dynamic<?> sectionRemainder = section.get(DSL.remainderFinder());
67
										int sectionY = sectionRemainder.get("Y").asInt(Integer.MAX_VALUE);
68
										if (sectionY != Integer.MAX_VALUE) {
69
											if (section.getOptionalTyped(biomeContainerFinder).isPresent()) {
70
												lowestY.setValue(Math.min(sectionY, lowestY.intValue()));
71
											}
72
73
											section.getOptionalTyped(blockStateContainerFinder)
74
												.ifPresent(
75
													blockContainer -> palettedContainers.put(
76
														sectionY,
77
														Suppliers.memoize(
78
															() -> {
79
																List<? extends Dynamic<?>> palette = blockContainer.getOptionalTyped(blockStatePaletteFinder)
80
																	.map(x -> x.write().result().map(r -> r.asList(Function.identity())).orElse(Collections.emptyList()))
81
																	.orElse(Collections.emptyList());
82
																long[] data = blockContainer.get(DSL.remainderFinder()).get("data").asLongStream().toArray();
83
																return new ChunkProtoTickListFix.PoorMansPalettedContainer(palette, data);
84
															}
85
														)
86
													)
87
												);
88
										}
89
									}
90
								)
91
						);
92
					byte sectionMinY = lowestY.byteValue();
93
					level = level.update(DSL.remainderFinder(), remainder -> remainder.update("yPos", y -> y.createByte(sectionMinY)));
94
					if (!level.getOptionalTyped(tileTickFinder).isPresent() && !chunkTag.get("fluid_ticks").result().isPresent()) {
95
						int sectionX = chunkTag.get("xPos").asInt(0);
96
						int sectionZ = chunkTag.get("zPos").asInt(0);
97
						Dynamic<?> fluidTicks = this.makeTickList(
98
							chunkTag, palettedContainers, sectionMinY, sectionX, sectionZ, "LiquidsToBeTicked", ChunkProtoTickListFix::getLiquid
99
						);
100
						Dynamic<?> blockTicks = this.makeTickList(chunkTag, palettedContainers, sectionMinY, sectionX, sectionZ, "ToBeTicked", ChunkProtoTickListFix::getBlock);
101
						Optional<? extends Pair<? extends Typed<?>, ?>> parsedBlockTicks = tileTickFinder.type().readTyped(blockTicks).result();
102
						if (parsedBlockTicks.isPresent()) {
103
							level = level.set(tileTickFinder, (Typed<?>)parsedBlockTicks.get().getFirst());
104
						}
105
106
						return level.update(DSL.remainderFinder(), remainder -> remainder.remove("ToBeTicked").remove("LiquidsToBeTicked").set("fluid_ticks", fluidTicks));
107
					} else {
108
						return level;
109
					}
110
				}
111
			)
112
		);
113
	}
114
115
	private Dynamic<?> makeTickList(
116
		final Dynamic<?> tag,
117
		final Int2ObjectMap<Supplier<ChunkProtoTickListFix.PoorMansPalettedContainer>> palettedContainers,
118
		final byte sectionMinY,
119
		final int sectionX,
120
		final int sectionZ,
121
		final String protoTickListTag,
122
		final Function<Dynamic<?>, String> typeGetter
123
	) {
124
		Stream<Dynamic<?>> newTickList = Stream.empty();
125
		List<? extends Dynamic<?>> ticksPerSection = tag.get(protoTickListTag).asList(Function.identity());
126
127
		for (int sectionYIndex = 0; sectionYIndex < ticksPerSection.size(); sectionYIndex++) {
128
			int sectionY = sectionYIndex + sectionMinY;
129
			Supplier<ChunkProtoTickListFix.PoorMansPalettedContainer> container = palettedContainers.get(sectionY);
130
			Stream<? extends Dynamic<?>> newTickListForSection = ticksPerSection.get(sectionYIndex)
131
				.asStream()
132
				.mapToInt(pos -> pos.asShort((short)-1))
133
				.filter(pos -> pos > 0)
134
				.mapToObj(pos -> this.createTick(tag, container, sectionX, sectionY, sectionZ, pos, typeGetter));
135
			newTickList = Stream.concat(newTickList, newTickListForSection);
136
		}
137
138
		return tag.createList(newTickList);
139
	}
140
141
	private static String getBlock(final @Nullable Dynamic<?> blockState) {
142
		return blockState != null ? blockState.get("Name").asString("minecraft:air") : "minecraft:air";
143
	}
144
145
	private static String getLiquid(final @Nullable Dynamic<?> blockState) {
146
		if (blockState == null) {
147
			return "minecraft:empty";
148
		} else {
149
			String block = blockState.get("Name").asString("");
150
			if ("minecraft:water".equals(block)) {
151
				return blockState.get("Properties").get("level").asInt(0) == 0 ? "minecraft:water" : "minecraft:flowing_water";
152
			} else if ("minecraft:lava".equals(block)) {
153
				return blockState.get("Properties").get("level").asInt(0) == 0 ? "minecraft:lava" : "minecraft:flowing_lava";
154
			} else {
155
				return !ALWAYS_WATERLOGGED.contains(block) && !blockState.get("Properties").get("waterlogged").asBoolean(false) ? "minecraft:empty" : "minecraft:water";
156
			}
157
		}
158
	}
159
160
	private Dynamic<?> createTick(
161
		final Dynamic<?> tag,
162
		final @Nullable Supplier<ChunkProtoTickListFix.PoorMansPalettedContainer> container,
163
		final int sectionX,
164
		final int sectionY,
165
		final int sectionZ,
166
		final int pos,
167
		final Function<Dynamic<?>, String> typeGetter
168
	) {
169
		int relativeX = pos & 15;
170
		int relativeY = pos >>> 4 & 15;
171
		int relativeZ = pos >>> 8 & 15;
172
		String type = typeGetter.apply(container != null ? container.get().get(relativeX, relativeY, relativeZ) : null);
173
		return tag.createMap(
174
			ImmutableMap.builder()
175
				.put(tag.createString("i"), tag.createString(type))
176
				.put(tag.createString("x"), tag.createInt(sectionX * 16 + relativeX))
177
				.put(tag.createString("y"), tag.createInt(sectionY * 16 + relativeY))
178
				.put(tag.createString("z"), tag.createInt(sectionZ * 16 + relativeZ))
179
				.put(tag.createString("t"), tag.createInt(0))
180
				.put(tag.createString("p"), tag.createInt(0))
181
				.build()
182
		);
183
	}
184
185
	public static final class PoorMansPalettedContainer {
186
		private static final long SIZE_BITS = 4L;
187
		private final List<? extends Dynamic<?>> palette;
188
		private final long[] data;
189
		private final int bits;
190
		private final long mask;
191
		private final int valuesPerLong;
192
193
		public PoorMansPalettedContainer(final List<? extends Dynamic<?>> palette, final long[] data) {
194
			this.palette = palette;
195
			this.data = data;
196
			this.bits = Math.max(4, ChunkHeightAndBiomeFix.ceillog2(palette.size()));
197
			this.mask = (1L << this.bits) - 1L;
198
			this.valuesPerLong = (char)(64 / this.bits);
199
		}
200
201
		public @Nullable Dynamic<?> get(final int x, final int y, final int z) {
202
			int entryCount = this.palette.size();
203
			if (entryCount < 1) {
204
				return null;
205
			} else if (entryCount == 1) {
206
				return (Dynamic<?>)this.palette.getFirst();
207
			} else {
208
				int index = this.getIndex(x, y, z);
209
				int cellIndex = index / this.valuesPerLong;
210
				if (cellIndex >= 0 && cellIndex < this.data.length) {
211
					long cellValue = this.data[cellIndex];
212
					int bitIndex = (index - cellIndex * this.valuesPerLong) * this.bits;
213
					int paletteIndex = (int)(cellValue >> bitIndex & this.mask);
214
					return (Dynamic<?>)(paletteIndex >= 0 && paletteIndex < entryCount ? this.palette.get(paletteIndex) : null);
215
				} else {
216
					return null;
217
				}
218
			}
219
		}
220
221
		private int getIndex(final int x, final int y, final int z) {
222
			return (y << 4 | z) << 4 | x;
223
		}
224
225
		public List<? extends Dynamic<?>> palette() {
226
			return this.palette;
227
		}
228
229
		public long[] data() {
230
			return this.data;
231
		}
232
	}
233
}
234

Decompiled Minecraft 26.3 source of ChunkProtoTickListFix (net/minecraft/util/datafix/fixes/ChunkProtoTickListFix.java) with Mojang official mappings, stored verbatim on the site.