BitStorageAlignFix .java

net.minecraft.util.datafix.fixes.BitStorageAlignFix (class): members: HEIGHTMAP_SIZE, sectionsFinder, paletteFinder, sectionFinder, addPadding, makeRule.

net.minecraft.util.datafix.fixesclass134 linescommonextends DataFix4.4 KB

API Surface (2)

TypeRewriteRule makeRule()
long[] addPadding(final int size, final int bits, final long[] data)

Fields & Constants (6)

BIT_TO_LONG_SHIFT
HEIGHTMAP_BITS
HEIGHTMAP_SIZE
sectionsFinder
sectionFinder
paletteFinder

String literals (5)

"Level"
"Sections"
"Palette"
"Heightmaps"
"BlockStates"

Code Graph: Dependencies (1)

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

Full Source

BitStorageAlignFix.java134 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix.fixes;
2
3
import com.mojang.datafixers.DSL;
4
import com.mojang.datafixers.DataFix;
5
import com.mojang.datafixers.DataFixUtils;
6
import com.mojang.datafixers.OpticFinder;
7
import com.mojang.datafixers.TypeRewriteRule;
8
import com.mojang.datafixers.Typed;
9
import com.mojang.datafixers.schemas.Schema;
10
import com.mojang.datafixers.types.Type;
11
import com.mojang.datafixers.types.templates.List.ListType;
12
import com.mojang.datafixers.util.Pair;
13
import com.mojang.serialization.Dynamic;
14
import java.util.List;
15
import java.util.stream.LongStream;
16
import net.minecraft.util.Mth;
17
18
public class BitStorageAlignFix extends DataFix {
19
	private static final int BIT_TO_LONG_SHIFT = 6;
20
	private static final int HEIGHTMAP_BITS = 9;
21
	private static final int HEIGHTMAP_SIZE = 256;
22
23
	public BitStorageAlignFix(final Schema schema) {
24
		super(schema, false);
25
	}
26
27
	@Override
28
	protected TypeRewriteRule makeRule() {
29
		Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
30
		Type<?> levelType = chunkType.findFieldType("Level");
31
		OpticFinder<?> levelFinder = DSL.fieldFinder("Level", levelType);
32
		OpticFinder<?> sectionsFinder = levelFinder.type().findField("Sections");
33
		Type<?> sectionType = ((ListType)sectionsFinder.type()).getElement();
34
		OpticFinder<?> sectionFinder = DSL.typeFinder(sectionType);
35
		Type<Pair<String, Dynamic<?>>> blockStateType = DSL.named(References.BLOCK_STATE.typeName(), DSL.remainderType());
36
		OpticFinder<List<Pair<String, Dynamic<?>>>> paletteFinder = DSL.fieldFinder("Palette", DSL.list(blockStateType));
37
		return this.fixTypeEverywhereTyped(
38
			"BitStorageAlignFix",
39
			chunkType,
40
			this.getOutputSchema().getType(References.CHUNK),
41
			chunk -> chunk.updateTyped(levelFinder, level -> this.updateHeightmaps(updateSections(sectionsFinder, sectionFinder, paletteFinder, level)))
42
		);
43
	}
44
45
	private Typed<?> updateHeightmaps(final Typed<?> level) {
46
		return level.update(
47
			DSL.remainderFinder(),
48
			tag -> tag.update(
49
				"Heightmaps", heightmaps -> heightmaps.updateMapValues(e -> e.mapSecond(heightmap -> updateBitStorage(tag, (Dynamic<?>)heightmap, 256, 9)))
50
			)
51
		);
52
	}
53
54
	private static Typed<?> updateSections(
55
		final OpticFinder<?> sectionsFinder,
56
		final OpticFinder<?> sectionFinder,
57
		final OpticFinder<List<Pair<String, Dynamic<?>>>> paletteFinder,
58
		final Typed<?> level
59
	) {
60
		return level.updateTyped(
61
			sectionsFinder,
62
			sections -> sections.updateTyped(
63
				sectionFinder,
64
				section -> {
65
					int bits = section.getOptional(paletteFinder).map(palette -> Math.max(4, DataFixUtils.ceillog2(palette.size()))).orElse(0);
66
					return bits != 0 && !Mth.isPowerOfTwo(bits)
67
						? section.update(DSL.remainderFinder(), tag -> tag.update("BlockStates", states -> updateBitStorage(tag, states, 4096, bits)))
68
						: section;
69
				}
70
			)
71
		);
72
	}
73
74
	private static Dynamic<?> updateBitStorage(final Dynamic<?> tag, final Dynamic<?> storage, final int size, final int bits) {
75
		long[] input = storage.asLongStream().toArray();
76
		long[] output = addPadding(size, bits, input);
77
		return tag.createLongList(LongStream.of(output));
78
	}
79
80
	public static long[] addPadding(final int size, final int bits, final long[] data) {
81
		int dataLength = data.length;
82
		if (dataLength == 0) {
83
			return data;
84
		}
85
86
		long mask = (1L << bits) - 1L;
87
		int valuesPerLong = 64 / bits;
88
		int requiredLength = (size + valuesPerLong - 1) / valuesPerLong;
89
		long[] result = new long[requiredLength];
90
		int outputDataIndex = 0;
91
		int outputStart = 0;
92
		long outputData = 0L;
93
		int currentIndex = 0;
94
		long current = data[0];
95
		long next = dataLength > 1 ? data[1] : 0L;
96
97
		for (int index = 0; index < size; index++) {
98
			int position = index * bits;
99
			int startData = position >> 6;
100
			int endData = (index + 1) * bits - 1 >> 6;
101
			int startBit = position ^ startData << 6;
102
			if (startData != currentIndex) {
103
				current = next;
104
				next = startData + 1 < dataLength ? data[startData + 1] : 0L;
105
				currentIndex = startData;
106
			}
107
108
			long valueToInsert;
109
			if (startData == endData) {
110
				valueToInsert = current >>> startBit & mask;
111
			} else {
112
				int shiftBits = 64 - startBit;
113
				valueToInsert = (current >>> startBit | next << shiftBits) & mask;
114
			}
115
116
			int outputEnd = outputStart + bits;
117
			if (outputEnd >= 64) {
118
				result[outputDataIndex++] = outputData;
119
				outputData = valueToInsert;
120
				outputStart = bits;
121
			} else {
122
				outputData |= valueToInsert << outputStart;
123
				outputStart = outputEnd;
124
			}
125
		}
126
127
		if (outputData != 0L) {
128
			result[outputDataIndex] = outputData;
129
		}
130
131
		return result;
132
	}
133
}
134

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