LeavesFix .java

net.minecraft.util.datafix.fixes.LeavesFix (class): members: LeavesSection, NORTH_EAST_MASK, NORTH_WEST_MASK, SOUTH_EAST_MASK, SOUTH_WEST_MASK, NORTH_MASK.

net.minecraft.util.datafix.fixesclass368 linescommonextends DataFix12.6 KB

API Surface (12)

TypeRewriteRule makeRule()
int getIndex(final int x, final int y, final int z)
int getSideMask(final boolean west, final boolean east, final boolean north, final boolean south)
boolean skippable()
boolean isLog(final int block)
boolean isLeaf(final int block)
void readStorage(final Dynamic<?> tag)
Typed<?> write(final Typed<?> section)
boolean isSkippable()
int getBlock(final int pos)
int getStateId(final String blockName, final boolean persistent, final int distance)
int getIndex()

Fields & Constants (14)

NORTH_WEST_MASK
WEST_MASK
SOUTH_WEST_MASK
SOUTH_MASK
SOUTH_EAST_MASK
EAST_MASK
NORTH_EAST_MASK
NORTH_MASK
DIRECTIONS
DECAY_DISTANCE
SIZE_BITS
SIZE
LEAVES
LOGS

String literals (37)

"minecraft:acacia_leaves"
"minecraft:birch_leaves"
"minecraft:dark_oak_leaves"
"minecraft:jungle_leaves"
"minecraft:oak_leaves"
"minecraft:spruce_leaves"
"minecraft:acacia_bark"
"minecraft:birch_bark"
"minecraft:dark_oak_bark"
"minecraft:jungle_bark"
"minecraft:oak_bark"
"minecraft:spruce_bark"
"minecraft:acacia_log"
"minecraft:birch_log"
"minecraft:dark_oak_log"
"minecraft:jungle_log"
"minecraft:oak_log"
"minecraft:spruce_log"
"minecraft:stripped_acacia_log"
"minecraft:stripped_birch_log"
"minecraft:stripped_dark_oak_log"
"minecraft:stripped_jungle_log"
"minecraft:stripped_oak_log"
"minecraft:stripped_spruce_log"

Code Graph: Dependencies (3)

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

Full Source

LeavesFix.java368 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix.fixes;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.ImmutableSet;
5
import com.google.common.collect.Lists;
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.Int2IntMap;
18
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
19
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
20
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
21
import it.unimi.dsi.fastutil.ints.IntIterator;
22
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
23
import it.unimi.dsi.fastutil.ints.IntSet;
24
import it.unimi.dsi.fastutil.objects.Object2IntMap;
25
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
26
import java.util.Arrays;
27
import java.util.List;
28
import java.util.Objects;
29
import java.util.Optional;
30
import java.util.Set;
31
import java.util.stream.Collectors;
32
import net.minecraft.util.datafix.PackedBitStorage;
33
import org.jspecify.annotations.Nullable;
34
35
public class LeavesFix extends DataFix {
36
	private static final int NORTH_WEST_MASK = 128;
37
	private static final int WEST_MASK = 64;
38
	private static final int SOUTH_WEST_MASK = 32;
39
	private static final int SOUTH_MASK = 16;
40
	private static final int SOUTH_EAST_MASK = 8;
41
	private static final int EAST_MASK = 4;
42
	private static final int NORTH_EAST_MASK = 2;
43
	private static final int NORTH_MASK = 1;
44
	private static final int[][] DIRECTIONS = new int[][]{{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
45
	private static final int DECAY_DISTANCE = 7;
46
	private static final int SIZE_BITS = 12;
47
	private static final int SIZE = 4096;
48
	private static final Object2IntMap<String> LEAVES = DataFixUtils.make(new Object2IntOpenHashMap<>(), map -> {
49
		map.put("minecraft:acacia_leaves", 0);
50
		map.put("minecraft:birch_leaves", 1);
51
		map.put("minecraft:dark_oak_leaves", 2);
52
		map.put("minecraft:jungle_leaves", 3);
53
		map.put("minecraft:oak_leaves", 4);
54
		map.put("minecraft:spruce_leaves", 5);
55
	});
56
	private static final Set<String> LOGS = ImmutableSet.of(
57
		"minecraft:acacia_bark",
58
		"minecraft:birch_bark",
59
		"minecraft:dark_oak_bark",
60
		"minecraft:jungle_bark",
61
		"minecraft:oak_bark",
62
		"minecraft:spruce_bark",
63
		"minecraft:acacia_log",
64
		"minecraft:birch_log",
65
		"minecraft:dark_oak_log",
66
		"minecraft:jungle_log",
67
		"minecraft:oak_log",
68
		"minecraft:spruce_log",
69
		"minecraft:stripped_acacia_log",
70
		"minecraft:stripped_birch_log",
71
		"minecraft:stripped_dark_oak_log",
72
		"minecraft:stripped_jungle_log",
73
		"minecraft:stripped_oak_log",
74
		"minecraft:stripped_spruce_log"
75
	);
76
77
	public LeavesFix(final Schema outputSchema, final boolean changesType) {
78
		super(outputSchema, changesType);
79
	}
80
81
	@Override
82
	protected TypeRewriteRule makeRule() {
83
		Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
84
		OpticFinder<?> levelFinder = chunkType.findField("Level");
85
		OpticFinder<?> sectionsFinder = levelFinder.type().findField("Sections");
86
		Type<?> sectionsType = sectionsFinder.type();
87
		if (!(sectionsType instanceof ListType)) {
88
			throw new IllegalStateException("Expecting sections to be a list.");
89
		}
90
91
		Type<?> sectionType = ((ListType)sectionsType).getElement();
92
		OpticFinder<?> sectionFinder = DSL.typeFinder(sectionType);
93
		return this.fixTypeEverywhereTyped(
94
			"Leaves fix",
95
			chunkType,
96
			chunk -> chunk.updateTyped(
97
				levelFinder,
98
				level -> {
99
					int[] sides = new int[]{0};
100
					Typed<?> newLevel = level.updateTyped(
101
						sectionsFinder,
102
						sections -> {
103
							Int2ObjectMap<LeavesFix.LeavesSection> sectionMap = new Int2ObjectOpenHashMap<>(
104
								sections.getAllTyped(sectionFinder)
105
									.stream()
106
									.map(sectionx -> new LeavesFix.LeavesSection(sectionx, this.getInputSchema()))
107
									.collect(Collectors.toMap(LeavesFix.Section::getIndex, s -> (LeavesFix.LeavesSection)s))
108
							);
109
							if (sectionMap.values().stream().allMatch(LeavesFix.Section::isSkippable)) {
110
								return sections;
111
							}
112
113
							List<IntSet> queue = Lists.newArrayList();
114
115
							for (int i = 0; i < 7; i++) {
116
								queue.add(new IntOpenHashSet());
117
							}
118
119
							for (LeavesFix.LeavesSection section : sectionMap.values()) {
120
								if (!section.isSkippable()) {
121
									for (int i = 0; i < 4096; i++) {
122
										int block = section.getBlock(i);
123
										if (section.isLog(block)) {
124
											queue.get(0).add(section.getIndex() << 12 | i);
125
										} else if (section.isLeaf(block)) {
126
											int x = this.getX(i);
127
											int z = this.getZ(i);
128
											sides[0] |= getSideMask(x == 0, x == 15, z == 0, z == 15);
129
										}
130
									}
131
								}
132
							}
133
134
							for (int i = 1; i < 7; i++) {
135
								IntSet set = queue.get(i - 1);
136
								IntSet newSet = queue.get(i);
137
								IntIterator iterator = set.iterator();
138
139
								while (iterator.hasNext()) {
140
									int posChunk = iterator.nextInt();
141
									int x = this.getX(posChunk);
142
									int y = this.getY(posChunk);
143
									int z = this.getZ(posChunk);
144
145
									for (int[] direction : DIRECTIONS) {
146
										int nx = x + direction[0];
147
										int nyChunk = y + direction[1];
148
										int nz = z + direction[2];
149
										if (nx >= 0 && nx <= 15 && nz >= 0 && nz <= 15 && nyChunk >= 0 && nyChunk <= 255) {
150
											LeavesFix.LeavesSection section = sectionMap.get(nyChunk >> 4);
151
											if (section != null && !section.isSkippable()) {
152
												int posSection = getIndex(nx, nyChunk & 15, nz);
153
												int block = section.getBlock(posSection);
154
												if (section.isLeaf(block)) {
155
													int oldDistance = section.getDistance(block);
156
													if (oldDistance > i) {
157
														section.setDistance(posSection, block, i);
158
														newSet.add(getIndex(nx, nyChunk, nz));
159
													}
160
												}
161
											}
162
										}
163
									}
164
								}
165
							}
166
167
							return sections.updateTyped(sectionFinder, sectionx -> sectionMap.get(sectionx.get(DSL.remainderFinder()).get("Y").asInt(0)).write(sectionx));
168
						}
169
					);
170
					if (sides[0] != 0) {
171
						newLevel = newLevel.update(DSL.remainderFinder(), tag -> {
172
							Dynamic<?> upgradeData = DataFixUtils.orElse(tag.get("UpgradeData").result(), tag.emptyMap());
173
							return tag.set("UpgradeData", upgradeData.set("Sides", tag.createByte((byte)(upgradeData.get("Sides").asByte((byte)0) | sides[0]))));
174
						});
175
					}
176
177
					return newLevel;
178
				}
179
			)
180
		);
181
	}
182
183
	public static int getIndex(final int x, final int y, final int z) {
184
		return y << 8 | z << 4 | x;
185
	}
186
187
	private int getX(final int index) {
188
		return index & 15;
189
	}
190
191
	private int getY(final int index) {
192
		return index >> 8 & 0xFF;
193
	}
194
195
	private int getZ(final int index) {
196
		return index >> 4 & 15;
197
	}
198
199
	public static int getSideMask(final boolean west, final boolean east, final boolean north, final boolean south) {
200
		int s = 0;
201
		if (north) {
202
			if (east) {
203
				s |= 2;
204
			} else if (west) {
205
				s |= 128;
206
			} else {
207
				s |= 1;
208
			}
209
		} else if (south) {
210
			if (west) {
211
				s |= 32;
212
			} else if (east) {
213
				s |= 8;
214
			} else {
215
				s |= 16;
216
			}
217
		} else if (east) {
218
			s |= 4;
219
		} else if (west) {
220
			s |= 64;
221
		}
222
223
		return s;
224
	}
225
226
	public static final class LeavesSection extends LeavesFix.Section {
227
		private static final String PERSISTENT = "persistent";
228
		private static final String DECAYABLE = "decayable";
229
		private static final String DISTANCE = "distance";
230
		private @Nullable IntSet leaveIds;
231
		private @Nullable IntSet logIds;
232
		private @Nullable Int2IntMap stateToIdMap;
233
234
		public LeavesSection(final Typed<?> section, final Schema inputSchema) {
235
			super(section, inputSchema);
236
		}
237
238
		@Override
239
		protected boolean skippable() {
240
			this.leaveIds = new IntOpenHashSet();
241
			this.logIds = new IntOpenHashSet();
242
			this.stateToIdMap = new Int2IntOpenHashMap();
243
244
			for (int i = 0; i < this.palette.size(); i++) {
245
				Dynamic<?> paletteTag = this.palette.get(i);
246
				String blockName = paletteTag.get("Name").asString("");
247
				if (LeavesFix.LEAVES.containsKey(blockName)) {
248
					boolean persistent = Objects.equals(paletteTag.get("Properties").get("decayable").asString(""), "false");
249
					this.leaveIds.add(i);
250
					this.stateToIdMap.put(getStateId(blockName, persistent, 7), i);
251
					this.palette.set(i, this.makeLeafTag(paletteTag, blockName, persistent, 7));
252
				}
253
254
				if (LeavesFix.LOGS.contains(blockName)) {
255
					this.logIds.add(i);
256
				}
257
			}
258
259
			return this.leaveIds.isEmpty() && this.logIds.isEmpty();
260
		}
261
262
		private Dynamic<?> makeLeafTag(final Dynamic<?> input, final String blockName, final boolean persistent, final int distance) {
263
			Dynamic<?> properties = input.emptyMap();
264
			properties = properties.set("persistent", properties.createString(persistent ? "true" : "false"));
265
			properties = properties.set("distance", properties.createString(Integer.toString(distance)));
266
			Dynamic<?> tag = input.emptyMap();
267
			tag = tag.set("Properties", properties);
268
			return tag.set("Name", tag.createString(blockName));
269
		}
270
271
		public boolean isLog(final int block) {
272
			return this.logIds.contains(block);
273
		}
274
275
		public boolean isLeaf(final int block) {
276
			return this.leaveIds.contains(block);
277
		}
278
279
		private int getDistance(final int block) {
280
			return this.isLog(block) ? 0 : Integer.parseInt(this.palette.get(block).get("Properties").get("distance").asString(""));
281
		}
282
283
		private void setDistance(final int pos, final int block, final int distance) {
284
			Dynamic<?> baseTag = this.palette.get(block);
285
			String blockName = baseTag.get("Name").asString("");
286
			boolean persistent = Objects.equals(baseTag.get("Properties").get("persistent").asString(""), "true");
287
			int stateId = getStateId(blockName, persistent, distance);
288
			if (!this.stateToIdMap.containsKey(stateId)) {
289
				int id = this.palette.size();
290
				this.leaveIds.add(id);
291
				this.stateToIdMap.put(stateId, id);
292
				this.palette.add(this.makeLeafTag(baseTag, blockName, persistent, distance));
293
			}
294
295
			int id = this.stateToIdMap.get(stateId);
296
			if (1 << this.storage.getBits() <= id) {
297
				PackedBitStorage newStorage = new PackedBitStorage(this.storage.getBits() + 1, 4096);
298
299
				for (int i = 0; i < 4096; i++) {
300
					newStorage.set(i, this.storage.get(i));
301
				}
302
303
				this.storage = newStorage;
304
			}
305
306
			this.storage.set(pos, id);
307
		}
308
	}
309
310
	public abstract static class Section {
311
		protected static final String BLOCK_STATES_TAG = "BlockStates";
312
		protected static final String NAME_TAG = "Name";
313
		protected static final String PROPERTIES_TAG = "Properties";
314
		private final Type<Pair<String, Dynamic<?>>> blockStateType = DSL.named(References.BLOCK_STATE.typeName(), DSL.remainderType());
315
		protected final OpticFinder<List<Pair<String, Dynamic<?>>>> paletteFinder = DSL.fieldFinder("Palette", DSL.list(this.blockStateType));
316
		protected final List<Dynamic<?>> palette;
317
		protected final int index;
318
		protected @Nullable PackedBitStorage storage;
319
320
		public Section(final Typed<?> section, final Schema inputSchema) {
321
			if (!Objects.equals(inputSchema.getType(References.BLOCK_STATE), this.blockStateType)) {
322
				throw new IllegalStateException("Block state type is not what was expected.");
323
			}
324
325
			Optional<List<Pair<String, Dynamic<?>>>> typedPalette = section.getOptional(this.paletteFinder);
326
			this.palette = typedPalette.<List>map(p -> p.stream().map(Pair::getSecond).collect(Collectors.toList())).orElse(ImmutableList.of());
327
			Dynamic<?> tag = section.get(DSL.remainderFinder());
328
			this.index = tag.get("Y").asInt(0);
329
			this.readStorage(tag);
330
		}
331
332
		protected void readStorage(final Dynamic<?> tag) {
333
			if (this.skippable()) {
334
				this.storage = null;
335
			} else {
336
				long[] states = tag.get("BlockStates").asLongStream().toArray();
337
				int size = Math.max(4, DataFixUtils.ceillog2(this.palette.size()));
338
				this.storage = new PackedBitStorage(size, 4096, states);
339
			}
340
		}
341
342
		public Typed<?> write(final Typed<?> section) {
343
			return this.isSkippable()
344
				? section
345
				: section.update(DSL.remainderFinder(), tag -> tag.set("BlockStates", tag.createLongList(Arrays.stream(this.storage.getRaw()))))
346
					.set(this.paletteFinder, this.palette.stream().map(b -> Pair.of(References.BLOCK_STATE.typeName(), b)).collect(Collectors.toList()));
347
		}
348
349
		public boolean isSkippable() {
350
			return this.storage == null;
351
		}
352
353
		public int getBlock(final int pos) {
354
			return this.storage.get(pos);
355
		}
356
357
		protected static int getStateId(final String blockName, final boolean persistent, final int distance) {
358
			return LeavesFix.LEAVES.get(blockName) << 5 | (persistent ? 16 : 0) | distance;
359
		}
360
361
		public int getIndex() {
362
			return this.index;
363
		}
364
365
		protected abstract boolean skippable();
366
	}
367
}
368

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