| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DSL;
|
| 4 | import com.mojang.datafixers.DataFixUtils;
|
| 5 | import com.mojang.datafixers.Typed;
|
| 6 | import com.mojang.datafixers.DSL.TypeReference;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.serialization.Dynamic;
|
| 9 | import com.mojang.serialization.DynamicOps;
|
| 10 | import java.util.function.Function;
|
| 11 | import java.util.function.IntFunction;
|
| 12 |
|
| 13 | public class EntityVariantFix extends NamedEntityFix {
|
| 14 | private final String fieldName;
|
| 15 | private final IntFunction<String> idConversions;
|
| 16 |
|
| 17 | public EntityVariantFix(
|
| 18 | final Schema outputSchema,
|
| 19 | final String name,
|
| 20 | final TypeReference type,
|
| 21 | final String entityName,
|
| 22 | final String fieldName,
|
| 23 | final IntFunction<String> idConversions
|
| 24 | ) {
|
| 25 | super(outputSchema, false, name, type, entityName);
|
| 26 | this.fieldName = fieldName;
|
| 27 | this.idConversions = idConversions;
|
| 28 | }
|
| 29 |
|
| 30 | private static <T> Dynamic<T> updateAndRename(
|
| 31 | final Dynamic<T> input, final String oldKey, final String newKey, final Function<Dynamic<T>, Dynamic<T>> function
|
| 32 | ) {
|
| 33 | return input.map(v -> {
|
| 34 | DynamicOps<T> ops = input.getOps();
|
| 35 | Function<T, T> liftedFunction = value -> function.apply(new Dynamic<>(ops, value)).getValue();
|
| 36 | return ops.get((T)v, oldKey).map(fieldValue -> ops.set((T)v, newKey, liftedFunction.apply((T)fieldValue))).result().orElse((T)v);
|
| 37 | });
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected Typed<?> fix(final Typed<?> typed) {
|
| 42 | return typed.update(
|
| 43 | DSL.remainderFinder(),
|
| 44 | remainder -> updateAndRename(
|
| 45 | remainder,
|
| 46 | this.fieldName,
|
| 47 | "variant",
|
| 48 | catType -> DataFixUtils.orElse(catType.asNumber().map(e -> catType.createString(this.idConversions.apply(e.intValue()))).result(), catType)
|
| 49 | )
|
| 50 | );
|
| 51 | }
|
| 52 | }
|
| 53 |
|