| 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.List.ListType;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import java.util.Optional;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 |
|
| 15 | public class VillagerRebuildLevelAndXpFix extends DataFix {
|
| 16 | private static final int TRADES_PER_LEVEL = 2;
|
| 17 | private static final int[] LEVEL_XP_THRESHOLDS = new int[]{0, 10, 50, 100, 150};
|
| 18 |
|
| 19 | public static int getMinXpPerLevel(final int level) {
|
| 20 | return LEVEL_XP_THRESHOLDS[Mth.clamp(level - 1, 0, LEVEL_XP_THRESHOLDS.length - 1)];
|
| 21 | }
|
| 22 |
|
| 23 | public VillagerRebuildLevelAndXpFix(final Schema outputSchema, final boolean changesType) {
|
| 24 | super(outputSchema, changesType);
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public TypeRewriteRule makeRule() {
|
| 29 | Type<?> villagerType = this.getInputSchema().getChoiceType(References.ENTITY, "minecraft:villager");
|
| 30 | OpticFinder<?> entityF = DSL.namedChoice("minecraft:villager", villagerType);
|
| 31 | OpticFinder<?> offersF = villagerType.findField("Offers");
|
| 32 | Type<?> offersType = offersF.type();
|
| 33 | OpticFinder<?> recipeListF = offersType.findField("Recipes");
|
| 34 | ListType<?> recipeListType = (ListType<?>)recipeListF.type();
|
| 35 | OpticFinder<?> recipeF = recipeListType.getElement().finder();
|
| 36 | return this.fixTypeEverywhereTyped(
|
| 37 | "Villager level and xp rebuild",
|
| 38 | this.getInputSchema().getType(References.ENTITY),
|
| 39 | input -> input.updateTyped(
|
| 40 | entityF,
|
| 41 | villagerType,
|
| 42 | villager -> {
|
| 43 | Dynamic<?> remainder = villager.get(DSL.remainderFinder());
|
| 44 | int level = remainder.get("VillagerData").get("level").asInt(0);
|
| 45 | Typed<?> modifiedVillager = villager;
|
| 46 | if (level == 0 || level == 1) {
|
| 47 | int offerCount = villager.getOptionalTyped(offersF)
|
| 48 | .flatMap(o -> o.getOptionalTyped(recipeListF))
|
| 49 | .map(recipeList -> recipeList.getAllTyped(recipeF).size())
|
| 50 | .orElse(0);
|
| 51 | level = Mth.clamp(offerCount / 2, 1, 5);
|
| 52 | if (level > 1) {
|
| 53 | modifiedVillager = addLevel(modifiedVillager, level);
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | Optional<Number> xp = remainder.get("Xp").asNumber().result();
|
| 58 | if (xp.isEmpty()) {
|
| 59 | modifiedVillager = addXpFromLevel(modifiedVillager, level);
|
| 60 | }
|
| 61 |
|
| 62 | return modifiedVillager;
|
| 63 | }
|
| 64 | )
|
| 65 | );
|
| 66 | }
|
| 67 |
|
| 68 | private static Typed<?> addLevel(final Typed<?> villager, final int level) {
|
| 69 | return villager.update(
|
| 70 | DSL.remainderFinder(), remainder -> remainder.update("VillagerData", villagerData -> villagerData.set("level", villagerData.createInt(level)))
|
| 71 | );
|
| 72 | }
|
| 73 |
|
| 74 | private static Typed<?> addXpFromLevel(final Typed<?> villager, final int level) {
|
| 75 | int xp = getMinXpPerLevel(level);
|
| 76 | return villager.update(DSL.remainderFinder(), remainder -> remainder.set("Xp", remainder.createInt(xp)));
|
| 77 | }
|
| 78 | }
|
| 79 |
|