| 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.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.datafixers.util.Either;
|
| 12 | import com.mojang.datafixers.util.Pair;
|
| 13 | import com.mojang.datafixers.util.Unit;
|
| 14 | import com.mojang.serialization.Dynamic;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Optional;
|
| 17 |
|
| 18 | public class FurnaceRecipeFix extends DataFix {
|
| 19 | public FurnaceRecipeFix(final Schema schema, final boolean changesType) {
|
| 20 | super(schema, changesType);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | protected TypeRewriteRule makeRule() {
|
| 25 | return this.cap(this.getOutputSchema().getTypeRaw(References.RECIPE));
|
| 26 | }
|
| 27 |
|
| 28 | private <R> TypeRewriteRule cap(final Type<R> recipeType) {
|
| 29 | Type<Pair<Either<Pair<List<Pair<R, Integer>>, Dynamic<?>>, Unit>, Dynamic<?>>> replacedType = DSL.and(
|
| 30 | DSL.optional(DSL.field("RecipesUsed", DSL.and(DSL.compoundList(recipeType, DSL.intType()), DSL.remainderType()))), DSL.remainderType()
|
| 31 | );
|
| 32 | OpticFinder<?> oldFurnaceFinder = DSL.namedChoice("minecraft:furnace", this.getInputSchema().getChoiceType(References.BLOCK_ENTITY, "minecraft:furnace"));
|
| 33 | OpticFinder<?> oldBlastFurnaceFinder = DSL.namedChoice(
|
| 34 | "minecraft:blast_furnace", this.getInputSchema().getChoiceType(References.BLOCK_ENTITY, "minecraft:blast_furnace")
|
| 35 | );
|
| 36 | OpticFinder<?> oldSmokerFinder = DSL.namedChoice("minecraft:smoker", this.getInputSchema().getChoiceType(References.BLOCK_ENTITY, "minecraft:smoker"));
|
| 37 | Type<?> newFurnaceType = this.getOutputSchema().getChoiceType(References.BLOCK_ENTITY, "minecraft:furnace");
|
| 38 | Type<?> newBlastFurnaceFinder = this.getOutputSchema().getChoiceType(References.BLOCK_ENTITY, "minecraft:blast_furnace");
|
| 39 | Type<?> newSmokerFinder = this.getOutputSchema().getChoiceType(References.BLOCK_ENTITY, "minecraft:smoker");
|
| 40 | Type<?> oldEntityType = this.getInputSchema().getType(References.BLOCK_ENTITY);
|
| 41 | Type<?> newEntityType = this.getOutputSchema().getType(References.BLOCK_ENTITY);
|
| 42 | return this.fixTypeEverywhereTyped(
|
| 43 | "FurnaceRecipesFix",
|
| 44 | oldEntityType,
|
| 45 | newEntityType,
|
| 46 | input -> input.updateTyped(oldFurnaceFinder, newFurnaceType, furnace -> this.updateFurnaceContents(recipeType, replacedType, furnace))
|
| 47 | .updateTyped(oldBlastFurnaceFinder, newBlastFurnaceFinder, blastFurnace -> this.updateFurnaceContents(recipeType, replacedType, blastFurnace))
|
| 48 | .updateTyped(oldSmokerFinder, newSmokerFinder, smoker -> this.updateFurnaceContents(recipeType, replacedType, smoker))
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | private <R> Typed<?> updateFurnaceContents(
|
| 53 | final Type<R> recipeType, final Type<Pair<Either<Pair<List<Pair<R, Integer>>, Dynamic<?>>, Unit>, Dynamic<?>>> replacedType, final Typed<?> input
|
| 54 | ) {
|
| 55 | Dynamic<?> tag = input.getOrCreate(DSL.remainderFinder());
|
| 56 | int recipesUsedSize = tag.get("RecipesUsedSize").asInt(0);
|
| 57 | tag = tag.remove("RecipesUsedSize");
|
| 58 | List<Pair<R, Integer>> results = Lists.newArrayList();
|
| 59 |
|
| 60 | for (int i = 0; i < recipesUsedSize; i++) {
|
| 61 | String locationKey = "RecipeLocation" + i;
|
| 62 | String amountKey = "RecipeAmount" + i;
|
| 63 | Optional<? extends Dynamic<?>> maybeLocation = tag.get(locationKey).result();
|
| 64 | int amount = tag.get(amountKey).asInt(0);
|
| 65 | if (amount > 0) {
|
| 66 | maybeLocation.ifPresent(location -> {
|
| 67 | Optional<? extends Pair<R, ? extends Dynamic<?>>> parseResult = recipeType.read((Dynamic<?>)location).result();
|
| 68 | parseResult.ifPresent(r -> results.add(Pair.of(r.getFirst(), amount)));
|
| 69 | });
|
| 70 | }
|
| 71 |
|
| 72 | tag = tag.remove(locationKey).remove(amountKey);
|
| 73 | }
|
| 74 |
|
| 75 | return input.set(DSL.remainderFinder(), replacedType, Pair.of(Either.left(Pair.of(results, tag.emptyMap())), tag));
|
| 76 | }
|
| 77 | }
|
| 78 |
|