| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.mojang.datafixers.DSL;
|
| 5 | import com.mojang.datafixers.DataFix;
|
| 6 | import com.mojang.datafixers.OpticFinder;
|
| 7 | import com.mojang.datafixers.TypeRewriteRule;
|
| 8 | import com.mojang.datafixers.Typed;
|
| 9 | import com.mojang.datafixers.schemas.Schema;
|
| 10 | import com.mojang.datafixers.types.Type;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import java.util.function.Function;
|
| 13 | import net.minecraft.util.Util;
|
| 14 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 15 |
|
| 16 | public class FixProjectileStoredItem extends DataFix {
|
| 17 | private static final String EMPTY_POTION = "minecraft:empty";
|
| 18 |
|
| 19 | public FixProjectileStoredItem(final Schema outputSchema) {
|
| 20 | super(outputSchema, true);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | protected TypeRewriteRule makeRule() {
|
| 25 | Type<?> inputEntityType = this.getInputSchema().getType(References.ENTITY);
|
| 26 | Type<?> outputEntityType = this.getOutputSchema().getType(References.ENTITY);
|
| 27 | return this.fixTypeEverywhereTyped(
|
| 28 | "Fix AbstractArrow item type",
|
| 29 | inputEntityType,
|
| 30 | outputEntityType,
|
| 31 | ExtraDataFixUtils.chainAllFilters(
|
| 32 | this.fixChoice("minecraft:trident", FixProjectileStoredItem::castUnchecked),
|
| 33 | this.fixChoice("minecraft:arrow", FixProjectileStoredItem::fixArrow),
|
| 34 | this.fixChoice("minecraft:spectral_arrow", FixProjectileStoredItem::fixSpectralArrow)
|
| 35 | )
|
| 36 | );
|
| 37 | }
|
| 38 |
|
| 39 | private Function<Typed<?>, Typed<?>> fixChoice(final String entityName, final FixProjectileStoredItem.SubFixer<?> fixer) {
|
| 40 | Type<?> inputEntityChoiceType = this.getInputSchema().getChoiceType(References.ENTITY, entityName);
|
| 41 | Type<?> outputEntityChoiceType = this.getOutputSchema().getChoiceType(References.ENTITY, entityName);
|
| 42 | return fixChoiceCap(entityName, fixer, inputEntityChoiceType, outputEntityChoiceType);
|
| 43 | }
|
| 44 |
|
| 45 | private static <T> Function<Typed<?>, Typed<?>> fixChoiceCap(
|
| 46 | final String entityName, final FixProjectileStoredItem.SubFixer<?> fixer, final Type<?> inputEntityChoiceType, final Type<T> outputEntityChoiceType
|
| 47 | ) {
|
| 48 | OpticFinder<?> entityF = DSL.namedChoice(entityName, inputEntityChoiceType);
|
| 49 | FixProjectileStoredItem.SubFixer<T> typedFixer = (FixProjectileStoredItem.SubFixer<T>)fixer;
|
| 50 | return input -> input.updateTyped(entityF, outputEntityChoiceType, typed -> typedFixer.fix(typed, outputEntityChoiceType));
|
| 51 | }
|
| 52 |
|
| 53 | private static <T> Typed<T> fixArrow(final Typed<?> typed, final Type<T> outputType) {
|
| 54 | return Util.writeAndReadTypedOrThrow(typed, outputType, input -> input.set("item", createItemStack(input, getArrowType(input))));
|
| 55 | }
|
| 56 |
|
| 57 | private static String getArrowType(final Dynamic<?> input) {
|
| 58 | return input.get("Potion").asString("minecraft:empty").equals("minecraft:empty") ? "minecraft:arrow" : "minecraft:tipped_arrow";
|
| 59 | }
|
| 60 |
|
| 61 | private static <T> Typed<T> fixSpectralArrow(final Typed<?> typed, final Type<T> outputType) {
|
| 62 | return Util.writeAndReadTypedOrThrow(typed, outputType, input -> input.set("item", createItemStack(input, "minecraft:spectral_arrow")));
|
| 63 | }
|
| 64 |
|
| 65 | private static Dynamic<?> createItemStack(final Dynamic<?> input, final String itemName) {
|
| 66 | return input.createMap(ImmutableMap.of(input.createString("id"), input.createString(itemName), input.createString("Count"), input.createInt(1)));
|
| 67 | }
|
| 68 |
|
| 69 | private static <T> Typed<T> castUnchecked(final Typed<?> input, final Type<T> outputType) {
|
| 70 | return new Typed<>(outputType, input.getOps(), (T)input.getValue());
|
| 71 | }
|
| 72 |
|
| 73 | private interface SubFixer<F> {
|
| 74 | Typed<F> fix(final Typed<?> input, final Type<F> outputType);
|
| 75 | }
|
| 76 | }
|
| 77 |
|