| 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.util.Either;
|
| 11 | import com.mojang.datafixers.util.Pair;
|
| 12 | import com.mojang.datafixers.util.Unit;
|
| 13 | import com.mojang.serialization.Dynamic;
|
| 14 | import java.util.List;
|
| 15 | import java.util.function.Function;
|
| 16 | import java.util.function.Predicate;
|
| 17 |
|
| 18 | public class EquipmentFormatFix extends DataFix {
|
| 19 | public EquipmentFormatFix(final Schema outputSchema) {
|
| 20 | super(outputSchema, true);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | protected TypeRewriteRule makeRule() {
|
| 25 | Type<?> oldItemStackType = this.getInputSchema().getTypeRaw(References.ITEM_STACK);
|
| 26 | Type<?> newItemStackType = this.getOutputSchema().getTypeRaw(References.ITEM_STACK);
|
| 27 | OpticFinder<?> idFinder = oldItemStackType.findField("id");
|
| 28 | return this.fix(oldItemStackType, newItemStackType, idFinder);
|
| 29 | }
|
| 30 |
|
| 31 | private <ItemStackOld, ItemStackNew> TypeRewriteRule fix(
|
| 32 | final Type<ItemStackOld> oldItemStackType, final Type<ItemStackNew> newItemStackType, final OpticFinder<?> idFinder
|
| 33 | ) {
|
| 34 | Type<Pair<String, Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<ItemStackOld, Unit>, Either<ItemStackOld, Unit>>>>>> oldEquipmentType = DSL.named(
|
| 35 | References.ENTITY_EQUIPMENT.typeName(),
|
| 36 | DSL.and(
|
| 37 | DSL.optional(DSL.field("ArmorItems", DSL.list(oldItemStackType))),
|
| 38 | DSL.optional(DSL.field("HandItems", DSL.list(oldItemStackType))),
|
| 39 | DSL.optional(DSL.field("body_armor_item", oldItemStackType)),
|
| 40 | DSL.optional(DSL.field("saddle", oldItemStackType))
|
| 41 | )
|
| 42 | );
|
| 43 | Type<Pair<String, Either<Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Dynamic<?>>>>>>>>>, Unit>>> newEquipmentType = DSL.named(
|
| 44 | References.ENTITY_EQUIPMENT.typeName(),
|
| 45 | DSL.optional(
|
| 46 | DSL.field(
|
| 47 | "equipment",
|
| 48 | DSL.and(
|
| 49 | DSL.optional(DSL.field("mainhand", newItemStackType)),
|
| 50 | DSL.optional(DSL.field("offhand", newItemStackType)),
|
| 51 | DSL.optional(DSL.field("feet", newItemStackType)),
|
| 52 | DSL.and(
|
| 53 | DSL.optional(DSL.field("legs", newItemStackType)),
|
| 54 | DSL.optional(DSL.field("chest", newItemStackType)),
|
| 55 | DSL.optional(DSL.field("head", newItemStackType)),
|
| 56 | DSL.and(DSL.optional(DSL.field("body", newItemStackType)), DSL.optional(DSL.field("saddle", newItemStackType)), DSL.remainderType())
|
| 57 | )
|
| 58 | )
|
| 59 | )
|
| 60 | )
|
| 61 | );
|
| 62 | if (!oldEquipmentType.equals(this.getInputSchema().getType(References.ENTITY_EQUIPMENT))) {
|
| 63 | throw new IllegalStateException("Input entity_equipment type does not match expected");
|
| 64 | } else if (!newEquipmentType.equals(this.getOutputSchema().getType(References.ENTITY_EQUIPMENT))) {
|
| 65 | throw new IllegalStateException("Output entity_equipment type does not match expected");
|
| 66 | } else {
|
| 67 | return this.fixTypeEverywhere(
|
| 68 | "EquipmentFormatFix",
|
| 69 | oldEquipmentType,
|
| 70 | newEquipmentType,
|
| 71 | ops -> {
|
| 72 | Predicate<ItemStackOld> isPlaceholder = itemStack -> {
|
| 73 | Typed<ItemStackOld> typed = new Typed<>(oldItemStackType, ops, itemStack);
|
| 74 | return typed.getOptional(idFinder).isEmpty();
|
| 75 | };
|
| 76 | return namedOldEquipment -> {
|
| 77 | String typeName = namedOldEquipment.getFirst();
|
| 78 | Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<ItemStackOld, Unit>, Either<ItemStackOld, Unit>>>> oldEquipment = namedOldEquipment.getSecond();
|
| 79 | List<ItemStackOld> armorItems = oldEquipment.getFirst().map(Function.identity(), ignored -> List.of());
|
| 80 | List<ItemStackOld> handItems = oldEquipment.getSecond().getFirst().map(Function.identity(), ignored -> List.of());
|
| 81 | Either<ItemStackOld, Unit> body = oldEquipment.getSecond().getSecond().getFirst();
|
| 82 | Either<ItemStackOld, Unit> saddle = oldEquipment.getSecond().getSecond().getSecond();
|
| 83 | Either<ItemStackOld, Unit> feet = getItemFromList(0, armorItems, isPlaceholder);
|
| 84 | Either<ItemStackOld, Unit> legs = getItemFromList(1, armorItems, isPlaceholder);
|
| 85 | Either<ItemStackOld, Unit> chest = getItemFromList(2, armorItems, isPlaceholder);
|
| 86 | Either<ItemStackOld, Unit> head = getItemFromList(3, armorItems, isPlaceholder);
|
| 87 | Either<ItemStackOld, Unit> mainhand = getItemFromList(0, handItems, isPlaceholder);
|
| 88 | Either<ItemStackOld, Unit> offhand = getItemFromList(1, handItems, isPlaceholder);
|
| 89 | return areAllEmpty(body, saddle, feet, legs, chest, head, mainhand, offhand)
|
| 90 | ? Pair.of(typeName, Either.right(Unit.INSTANCE))
|
| 91 | : Pair.of(
|
| 92 | typeName,
|
| 93 | Either.left(
|
| 94 | Pair.of(
|
| 95 | (Either<ItemStackNew, Unit>)mainhand,
|
| 96 | Pair.of(
|
| 97 | (Either<ItemStackNew, Unit>)offhand,
|
| 98 | Pair.of(
|
| 99 | (Either<ItemStackNew, Unit>)feet,
|
| 100 | Pair.of(
|
| 101 | (Either<ItemStackNew, Unit>)legs,
|
| 102 | Pair.of(
|
| 103 | (Either<ItemStackNew, Unit>)chest,
|
| 104 | Pair.of(
|
| 105 | (Either<ItemStackNew, Unit>)head, Pair.of((Either<ItemStackNew, Unit>)body, Pair.of((Either<ItemStackNew, Unit>)saddle, new Dynamic(ops)))
|
| 106 | )
|
| 107 | )
|
| 108 | )
|
| 109 | )
|
| 110 | )
|
| 111 | )
|
| 112 | )
|
| 113 | );
|
| 114 | };
|
| 115 | }
|
| 116 | );
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @SafeVarargs
|
| 121 | private static boolean areAllEmpty(final Either<?, Unit>... fields) {
|
| 122 | for (Either<?, Unit> field : fields) {
|
| 123 | if (field.right().isEmpty()) {
|
| 124 | return false;
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | return true;
|
| 129 | }
|
| 130 |
|
| 131 | private static <ItemStack> Either<ItemStack, Unit> getItemFromList(final int index, final List<ItemStack> items, final Predicate<ItemStack> isPlaceholder) {
|
| 132 | if (index >= items.size()) {
|
| 133 | return Either.right(Unit.INSTANCE);
|
| 134 | }
|
| 135 |
|
| 136 | ItemStack item = items.get(index);
|
| 137 | return isPlaceholder.test(item) ? Either.right(Unit.INSTANCE) : Either.left(item);
|
| 138 | }
|
| 139 | }
|
| 140 |
|