EntityEquipmentToArmorAndHandFix .java

net.minecraft.util.datafix.fixes.EntityEquipmentToArmorAndHandFix (class, 107 lines): members: makeRule. strings: "DropChances", "Equipment", "HandItems".

net.minecraft.util.datafix.fixesclass107 linescommonextends DataFix4.5 KB

API Surface (1)

TypeRewriteRule makeRule()

String literals (11)

"Equipment"
"ArmorItems"
"HandItems"
"body_armor_item"
"saddle"
"EntityEquipmentToArmorAndHandFix - drop chances"
"EntityEquipmentToArmorAndHandFix - equipment"
"Could not parse newly created empty itemstack."
"DropChances"
"HandDropChances"
"ArmorDropChances"

Code Graph: Dependencies (1)

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

Full Source

EntityEquipmentToArmorAndHandFix.java107 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix.fixes;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.datafixers.DSL;
5
import com.mojang.datafixers.DataFix;
6
import com.mojang.datafixers.TypeRewriteRule;
7
import com.mojang.datafixers.schemas.Schema;
8
import com.mojang.datafixers.types.Type;
9
import com.mojang.datafixers.util.Either;
10
import com.mojang.datafixers.util.Pair;
11
import com.mojang.datafixers.util.Unit;
12
import com.mojang.serialization.Dynamic;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Optional;
16
import java.util.function.Function;
17
import java.util.stream.Stream;
18
19
public class EntityEquipmentToArmorAndHandFix extends DataFix {
20
	public EntityEquipmentToArmorAndHandFix(final Schema outputSchema) {
21
		super(outputSchema, true);
22
	}
23
24
	@Override
25
	public TypeRewriteRule makeRule() {
26
		return this.cap(this.getInputSchema().getTypeRaw(References.ITEM_STACK), this.getOutputSchema().getTypeRaw(References.ITEM_STACK));
27
	}
28
29
	private <ItemStackOld, ItemStackNew> TypeRewriteRule cap(final Type<ItemStackOld> oldItemStackType, final Type<ItemStackNew> newItemStackType) {
30
		Type<Pair<String, Either<List<ItemStackOld>, Unit>>> oldEquipmentType = DSL.named(
31
			References.ENTITY_EQUIPMENT.typeName(), DSL.optional(DSL.field("Equipment", DSL.list(oldItemStackType)))
32
		);
33
		Type<Pair<String, Pair<Either<List<ItemStackNew>, Unit>, Pair<Either<List<ItemStackNew>, Unit>, Pair<Either<ItemStackNew, Unit>, Either<ItemStackNew, Unit>>>>>> newEquipmentType = DSL.named(
34
			References.ENTITY_EQUIPMENT.typeName(),
35
			DSL.and(
36
				DSL.optional(DSL.field("ArmorItems", DSL.list(newItemStackType))),
37
				DSL.optional(DSL.field("HandItems", DSL.list(newItemStackType))),
38
				DSL.optional(DSL.field("body_armor_item", newItemStackType)),
39
				DSL.optional(DSL.field("saddle", newItemStackType))
40
			)
41
		);
42
		if (!oldEquipmentType.equals(this.getInputSchema().getType(References.ENTITY_EQUIPMENT))) {
43
			throw new IllegalStateException("Input entity_equipment type does not match expected");
44
		} else if (!newEquipmentType.equals(this.getOutputSchema().getType(References.ENTITY_EQUIPMENT))) {
45
			throw new IllegalStateException("Output entity_equipment type does not match expected");
46
		} else {
47
			return TypeRewriteRule.seq(
48
				this.fixTypeEverywhereTyped(
49
					"EntityEquipmentToArmorAndHandFix - drop chances",
50
					this.getInputSchema().getType(References.ENTITY),
51
					typed -> typed.update(DSL.remainderFinder(), EntityEquipmentToArmorAndHandFix::fixDropChances)
52
				),
53
				this.fixTypeEverywhere(
54
					"EntityEquipmentToArmorAndHandFix - equipment",
55
					oldEquipmentType,
56
					newEquipmentType,
57
					ops -> {
58
						ItemStackNew emptyStack = newItemStackType.read(new Dynamic<>(ops).emptyMap())
59
							.result()
60
							.orElseThrow(() -> new IllegalStateException("Could not parse newly created empty itemstack."))
61
							.getFirst();
62
						Either<ItemStackNew, Unit> noItem = Either.right(DSL.unit());
63
						return named -> named.mapSecond(equipmentField -> {
64
							List<ItemStackOld> items = equipmentField.map(Function.identity(), ignored -> List.of());
65
							Either<List<ItemStackNew>, Unit> handItems = Either.right(DSL.unit());
66
							Either<List<ItemStackNew>, Unit> armorItems = Either.right(DSL.unit());
67
							if (!items.isEmpty()) {
68
								handItems = Either.left(Lists.newArrayList((ItemStackNew[])(new Object[]{items.getFirst(), emptyStack})));
69
							}
70
71
							if (items.size() > 1) {
72
								List<ItemStackNew> armor = Lists.newArrayList(emptyStack, emptyStack, emptyStack, emptyStack);
73
74
								for (int i = 1; i < Math.min(items.size(), 5); i++) {
75
									armor.set(i - 1, (ItemStackNew)items.get(i));
76
								}
77
78
								armorItems = Either.left(armor);
79
							}
80
81
							return Pair.of(armorItems, Pair.of(handItems, Pair.of(noItem, noItem)));
82
						});
83
					}
84
				)
85
			);
86
		}
87
	}
88
89
	private static Dynamic<?> fixDropChances(Dynamic<?> tag) {
90
		Optional<? extends Stream<? extends Dynamic<?>>> dropChances = tag.get("DropChances").asStreamOpt().result();
91
		tag = tag.remove("DropChances");
92
		if (dropChances.isPresent()) {
93
			Iterator<Float> chances = Stream.concat(dropChances.get().map(value -> value.asFloat(0.0F)), Stream.generate(() -> 0.0F)).iterator();
94
			float handChance = chances.next();
95
			if (tag.get("HandDropChances").result().isEmpty()) {
96
				tag = tag.set("HandDropChances", tag.createList(Stream.of(handChance, 0.0F).map(tag::createFloat)));
97
			}
98
99
			if (tag.get("ArmorDropChances").result().isEmpty()) {
100
				tag = tag.set("ArmorDropChances", tag.createList(Stream.of(chances.next(), chances.next(), chances.next(), chances.next()).map(tag::createFloat)));
101
			}
102
		}
103
104
		return tag;
105
	}
106
}
107

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