| 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 java.util.function.Function;
|
| 11 | import java.util.function.UnaryOperator;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 14 |
|
| 15 | public class ProjectileStoredWeaponFix extends DataFix {
|
| 16 | public ProjectileStoredWeaponFix(final Schema outputSchema) {
|
| 17 | super(outputSchema, true);
|
| 18 | }
|
| 19 |
|
| 20 | @Override
|
| 21 | protected TypeRewriteRule makeRule() {
|
| 22 | Type<?> inputEntityType = this.getInputSchema().getType(References.ENTITY);
|
| 23 | Type<?> outputEntityType = this.getOutputSchema().getType(References.ENTITY);
|
| 24 | return this.fixTypeEverywhereTyped(
|
| 25 | "Fix Arrow stored weapon",
|
| 26 | inputEntityType,
|
| 27 | outputEntityType,
|
| 28 | ExtraDataFixUtils.chainAllFilters(this.fixChoice("minecraft:arrow"), this.fixChoice("minecraft:spectral_arrow"))
|
| 29 | );
|
| 30 | }
|
| 31 |
|
| 32 | private Function<Typed<?>, Typed<?>> fixChoice(final String entityName) {
|
| 33 | Type<?> inputEntityChoiceType = this.getInputSchema().getChoiceType(References.ENTITY, entityName);
|
| 34 | Type<?> outputEntityChoiceType = this.getOutputSchema().getChoiceType(References.ENTITY, entityName);
|
| 35 | return fixChoiceCap(entityName, inputEntityChoiceType, outputEntityChoiceType);
|
| 36 | }
|
| 37 |
|
| 38 | private static <T> Function<Typed<?>, Typed<?>> fixChoiceCap(
|
| 39 | final String entityName, final Type<?> inputEntityChoiceType, final Type<T> outputEntityChoiceType
|
| 40 | ) {
|
| 41 | OpticFinder<?> entityF = DSL.namedChoice(entityName, inputEntityChoiceType);
|
| 42 | return input -> input.updateTyped(
|
| 43 | entityF, outputEntityChoiceType, typed -> Util.writeAndReadTypedOrThrow(typed, outputEntityChoiceType, UnaryOperator.identity())
|
| 44 | );
|
| 45 | }
|
| 46 | }
|
| 47 |
|