TrappedChestBlockEntityFix .java

net.minecraft.util.datafix.fixes.TrappedChestBlockEntityFix (class): members: TrappedChestSection, SIZE_BITS, SIZE, makeRule, LOGGER. strings: "xPos".

net.minecraft.util.datafix.fixesclass121 linescommonextends DataFix4.8 KB

API Surface (3)

TypeRewriteRule makeRule()
boolean skippable()
boolean isTrappedChest(final int block)

Fields & Constants (3)

LOGGER
SIZE
SIZE_BITS

String literals (12)

"Level"
"TileEntities"
"Tile entity type is not a list type."
"Sections"
"Expecting sections to be a list."
"AddTrappedChestFix"
"Trapped Chest fix"
"xPos"
"zPos"
"minecraft:chest"
"Block Entity was expected to be a chest"
"minecraft:trapped_chest"

Code Graph: Dependencies (3)

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

Full Source

TrappedChestBlockEntityFix.java121 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.OpticFinder;
6
import com.mojang.datafixers.TypeRewriteRule;
7
import com.mojang.datafixers.Typed;
8
import com.mojang.datafixers.schemas.Schema;
9
import com.mojang.datafixers.types.Type;
10
import com.mojang.datafixers.types.templates.List.ListType;
11
import com.mojang.datafixers.types.templates.TaggedChoice.TaggedChoiceType;
12
import com.mojang.logging.LogUtils;
13
import com.mojang.serialization.Dynamic;
14
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
15
import it.unimi.dsi.fastutil.ints.IntSet;
16
import java.util.List;
17
import java.util.Objects;
18
import java.util.Optional;
19
import org.jspecify.annotations.Nullable;
20
import org.slf4j.Logger;
21
22
public class TrappedChestBlockEntityFix extends DataFix {
23
	private static final Logger LOGGER = LogUtils.getLogger();
24
	private static final int SIZE = 4096;
25
	private static final short SIZE_BITS = 12;
26
27
	public TrappedChestBlockEntityFix(final Schema outputSchema, final boolean changesType) {
28
		super(outputSchema, changesType);
29
	}
30
31
	@Override
32
	public TypeRewriteRule makeRule() {
33
		Type<?> chunkType = this.getOutputSchema().getType(References.CHUNK);
34
		Type<?> levelType = chunkType.findFieldType("Level");
35
		if (!(levelType.findFieldType("TileEntities") instanceof ListType<?> tileEntityListType)) {
36
			throw new IllegalStateException("Tile entity type is not a list type.");
37
		} else {
38
			OpticFinder<? extends List<?>> tileEntitiesF = DSL.fieldFinder("TileEntities", (Type<? extends List<?>>)tileEntityListType);
39
			Type<?> chunkType1 = this.getInputSchema().getType(References.CHUNK);
40
			OpticFinder<?> levelFinder = chunkType1.findField("Level");
41
			OpticFinder<?> sectionsFinder = levelFinder.type().findField("Sections");
42
			Type<?> sectionsType = sectionsFinder.type();
43
			if (!(sectionsType instanceof ListType)) {
44
				throw new IllegalStateException("Expecting sections to be a list.");
45
			}
46
47
			Type<?> sectionType = ((ListType)sectionsType).getElement();
48
			OpticFinder<?> sectionFinder = DSL.typeFinder(sectionType);
49
			return TypeRewriteRule.seq(
50
				new AddNewChoices(this.getOutputSchema(), "AddTrappedChestFix", References.BLOCK_ENTITY).makeRule(),
51
				this.fixTypeEverywhereTyped("Trapped Chest fix", chunkType1, chunk -> chunk.updateTyped(levelFinder, level -> {
52
					Optional<? extends Typed<?>> sections = level.getOptionalTyped(sectionsFinder);
53
					if (sections.isEmpty()) {
54
						return level;
55
					}
56
57
					List<? extends Typed<?>> sectionList = sections.get().getAllTyped(sectionFinder);
58
					IntSet chestLocations = new IntOpenHashSet();
59
60
					for (Typed<?> section : sectionList) {
61
						TrappedChestBlockEntityFix.TrappedChestSection trappedChestSection = new TrappedChestBlockEntityFix.TrappedChestSection(section, this.getInputSchema());
62
						if (!trappedChestSection.isSkippable()) {
63
							for (int i = 0; i < 4096; i++) {
64
								int block = trappedChestSection.getBlock(i);
65
								if (trappedChestSection.isTrappedChest(block)) {
66
									chestLocations.add(trappedChestSection.getIndex() << 12 | i);
67
								}
68
							}
69
						}
70
					}
71
72
					Dynamic<?> levelTag = level.get(DSL.remainderFinder());
73
					int chunkX = levelTag.get("xPos").asInt(0);
74
					int chunkZ = levelTag.get("zPos").asInt(0);
75
					TaggedChoiceType<String> tileEntityChoiceType = (TaggedChoiceType<String>)this.getInputSchema().findChoiceType(References.BLOCK_ENTITY);
76
					return level.updateTyped(tileEntitiesF, tileEntities -> tileEntities.updateTyped(tileEntityChoiceType.finder(), tileEntity -> {
77
						Dynamic<?> tag = tileEntity.getOrCreate(DSL.remainderFinder());
78
						int x = tag.get("x").asInt(0) - (chunkX << 4);
79
						int y = tag.get("y").asInt(0);
80
						int z = tag.get("z").asInt(0) - (chunkZ << 4);
81
						return chestLocations.contains(LeavesFix.getIndex(x, y, z)) ? tileEntity.update(tileEntityChoiceType.finder(), stringPair -> stringPair.mapFirst(s -> {
82
							if (!Objects.equals(s, "minecraft:chest")) {
83
								LOGGER.warn("Block Entity was expected to be a chest");
84
							}
85
86
							return "minecraft:trapped_chest";
87
						})) : tileEntity;
88
					}));
89
				}))
90
			);
91
		}
92
	}
93
94
	public static final class TrappedChestSection extends LeavesFix.Section {
95
		private @Nullable IntSet chestIds;
96
97
		public TrappedChestSection(final Typed<?> section, final Schema inputSchema) {
98
			super(section, inputSchema);
99
		}
100
101
		@Override
102
		protected boolean skippable() {
103
			this.chestIds = new IntOpenHashSet();
104
105
			for (int i = 0; i < this.palette.size(); i++) {
106
				Dynamic<?> paletteTag = this.palette.get(i);
107
				String blockName = paletteTag.get("Name").asString("");
108
				if (Objects.equals(blockName, "minecraft:trapped_chest")) {
109
					this.chestIds.add(i);
110
				}
111
			}
112
113
			return this.chestIds.isEmpty();
114
		}
115
116
		public boolean isTrappedChest(final int block) {
117
			return this.chestIds.contains(block);
118
		}
119
	}
120
}
121

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