SpawnerDataFix .java

net.minecraft.util.datafix.fixes.SpawnerDataFix (class, 57 lines): members: makeRule. strings: "Fix mob spawner data structure", "Weight", "SpawnData".

net.minecraft.util.datafix.fixesclass57 linescommonextends DataFix2.5 KB

API Surface (1)

TypeRewriteRule makeRule()

String literals (6)

"SpawnData"
"SpawnPotentials"
"Fix mob spawner data structure"
"Weight"
"weight"
"Entity"

Full Source

SpawnerDataFix.java57 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix.fixes;
2
3
import com.mojang.datafixers.DataFix;
4
import com.mojang.datafixers.OpticFinder;
5
import com.mojang.datafixers.TypeRewriteRule;
6
import com.mojang.datafixers.Typed;
7
import com.mojang.datafixers.schemas.Schema;
8
import com.mojang.datafixers.types.Type;
9
import com.mojang.datafixers.util.Pair;
10
import com.mojang.serialization.Dynamic;
11
import com.mojang.serialization.DynamicOps;
12
import java.util.List;
13
14
public class SpawnerDataFix extends DataFix {
15
	public SpawnerDataFix(final Schema outputSchema) {
16
		super(outputSchema, true);
17
	}
18
19
	@Override
20
	protected TypeRewriteRule makeRule() {
21
		Type<?> oldType = this.getInputSchema().getType(References.UNTAGGED_SPAWNER);
22
		Type<?> newType = this.getOutputSchema().getType(References.UNTAGGED_SPAWNER);
23
		OpticFinder<?> spawnDataFinder = oldType.findField("SpawnData");
24
		Type<?> newSpawnDataType = newType.findField("SpawnData").type();
25
		OpticFinder<?> spawnPotentialsFinder = oldType.findField("SpawnPotentials");
26
		Type<?> newSpawnPotentialsType = newType.findField("SpawnPotentials").type();
27
		return this.fixTypeEverywhereTyped(
28
			"Fix mob spawner data structure",
29
			oldType,
30
			newType,
31
			spawner -> spawner.updateTyped(spawnDataFinder, newSpawnDataType, spawnData -> this.wrapEntityToSpawnData(newSpawnDataType, spawnData))
32
				.updateTyped(
33
					spawnPotentialsFinder, newSpawnPotentialsType, spawnPotentials -> this.wrapSpawnPotentialsToWeightedEntries(newSpawnPotentialsType, spawnPotentials)
34
				)
35
		);
36
	}
37
38
	private <T> Typed<T> wrapEntityToSpawnData(final Type<T> newType, final Typed<?> spawnData) {
39
		DynamicOps<?> ops = spawnData.getOps();
40
		return new Typed<>(newType, ops, (T)Pair.<Object, Dynamic<?>>of(spawnData.getValue(), new Dynamic<>(ops)));
41
	}
42
43
	private <T> Typed<T> wrapSpawnPotentialsToWeightedEntries(final Type<T> newType, final Typed<?> spawnPotentials) {
44
		DynamicOps<?> ops = spawnPotentials.getOps();
45
		List<?> entries = (List<?>)spawnPotentials.getValue();
46
		List<?> wrappedEntries = entries.stream().map(o -> {
47
			Pair<Object, Dynamic<?>> entry = (Pair<Object, Dynamic<?>>)o;
48
			int weight = entry.getSecond().get("Weight").asNumber().result().orElse(1).intValue();
49
			Dynamic<?> newEntryRemainder = new Dynamic<>(ops);
50
			newEntryRemainder = newEntryRemainder.set("weight", newEntryRemainder.createInt(weight));
51
			Dynamic<?> newInnerRemainder = entry.getSecond().remove("Weight").remove("Entity");
52
			return Pair.of(Pair.of(entry.getFirst(), newInnerRemainder), newEntryRemainder);
53
		}).toList();
54
		return new Typed<>(newType, ops, (T)wrappedEntries);
55
	}
56
}
57

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