| 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.TaggedChoice.TaggedChoiceType;
|
| 11 | import com.mojang.datafixers.util.Pair;
|
| 12 | import com.mojang.serialization.Dynamic;
|
| 13 | import java.util.Set;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 16 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 17 |
|
| 18 | public class SaddleEquipmentSlotFix extends DataFix {
|
| 19 | private static final Set<String> ENTITIES_WITH_SADDLE_ITEM = Set.of(
|
| 20 | "minecraft:horse",
|
| 21 | "minecraft:skeleton_horse",
|
| 22 | "minecraft:zombie_horse",
|
| 23 | "minecraft:donkey",
|
| 24 | "minecraft:mule",
|
| 25 | "minecraft:camel",
|
| 26 | "minecraft:llama",
|
| 27 | "minecraft:trader_llama"
|
| 28 | );
|
| 29 | private static final Set<String> ENTITIES_WITH_SADDLE_FLAG = Set.of("minecraft:pig", "minecraft:strider");
|
| 30 | private static final String SADDLE_FLAG = "Saddle";
|
| 31 | private static final String NEW_SADDLE = "saddle";
|
| 32 |
|
| 33 | public SaddleEquipmentSlotFix(final Schema outputSchema) {
|
| 34 | super(outputSchema, true);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected TypeRewriteRule makeRule() {
|
| 39 | TaggedChoiceType<String> entityIdType = (TaggedChoiceType<String>)this.getInputSchema().findChoiceType(References.ENTITY);
|
| 40 | OpticFinder<Pair<String, ?>> entityIdF = DSL.typeFinder(entityIdType);
|
| 41 | Type<?> inputType = this.getInputSchema().getType(References.ENTITY);
|
| 42 | Type<?> outputType = this.getOutputSchema().getType(References.ENTITY);
|
| 43 | Type<?> patchedInputType = ExtraDataFixUtils.patchSubType(inputType, inputType, outputType);
|
| 44 | return this.fixTypeEverywhereTyped(
|
| 45 | "SaddleEquipmentSlotFix",
|
| 46 | inputType,
|
| 47 | outputType,
|
| 48 | input -> {
|
| 49 | String entityId = input.getOptional(entityIdF).map(Pair::getFirst).map(NamespacedSchema::ensureNamespaced).orElse("");
|
| 50 | Typed<?> fixedInput = ExtraDataFixUtils.cast(patchedInputType, input);
|
| 51 | if (ENTITIES_WITH_SADDLE_ITEM.contains(entityId)) {
|
| 52 | return Util.writeAndReadTypedOrThrow(fixedInput, outputType, SaddleEquipmentSlotFix::fixEntityWithSaddleItem);
|
| 53 | } else {
|
| 54 | return ENTITIES_WITH_SADDLE_FLAG.contains(entityId)
|
| 55 | ? Util.writeAndReadTypedOrThrow(fixedInput, outputType, SaddleEquipmentSlotFix::fixEntityWithSaddleFlag)
|
| 56 | : ExtraDataFixUtils.cast(outputType, input);
|
| 57 | }
|
| 58 | }
|
| 59 | );
|
| 60 | }
|
| 61 |
|
| 62 | private static Dynamic<?> fixEntityWithSaddleItem(final Dynamic<?> input) {
|
| 63 | return input.get("SaddleItem").result().isEmpty() ? input : fixDropChances(input.renameField("SaddleItem", "saddle"));
|
| 64 | }
|
| 65 |
|
| 66 | private static Dynamic<?> fixEntityWithSaddleFlag(Dynamic<?> tag) {
|
| 67 | boolean hasSaddle = tag.get("Saddle").asBoolean(false);
|
| 68 | tag = tag.remove("Saddle");
|
| 69 | if (!hasSaddle) {
|
| 70 | return tag;
|
| 71 | }
|
| 72 |
|
| 73 | Dynamic<?> saddleItem = tag.emptyMap().set("id", tag.createString("minecraft:saddle")).set("count", tag.createInt(1));
|
| 74 | return fixDropChances(tag.set("saddle", saddleItem));
|
| 75 | }
|
| 76 |
|
| 77 | private static Dynamic<?> fixDropChances(final Dynamic<?> tag) {
|
| 78 | Dynamic<?> dropChances = tag.get("drop_chances").orElseEmptyMap().set("saddle", tag.createFloat(2.0F));
|
| 79 | return tag.set("drop_chances", dropChances);
|
| 80 | }
|
| 81 | }
|
| 82 |
|