minecraft-src/net/minecraft/util/datafix/fixes/NamedEntityWriteReadFix.java
2025-07-04 02:00:41 +03:00

49 lines
2 KiB
Java

package net.minecraft.util.datafix.fixes;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.DataFix;
import com.mojang.datafixers.OpticFinder;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.Typed;
import com.mojang.datafixers.DSL.TypeReference;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.Type;
import com.mojang.serialization.Dynamic;
import net.minecraft.Util;
import net.minecraft.util.datafix.ExtraDataFixUtils;
public abstract class NamedEntityWriteReadFix extends DataFix {
private final String name;
private final String entityName;
private final TypeReference type;
public NamedEntityWriteReadFix(Schema outputSchema, boolean changesType, String name, TypeReference type, String entityName) {
super(outputSchema, changesType);
this.name = name;
this.type = type;
this.entityName = entityName;
}
@Override
public TypeRewriteRule makeRule() {
Type<?> type = this.getInputSchema().getType(this.type);
Type<?> type2 = this.getInputSchema().getChoiceType(this.type, this.entityName);
Type<?> type3 = this.getOutputSchema().getType(this.type);
Type<?> type4 = this.getOutputSchema().getChoiceType(this.type, this.entityName);
OpticFinder<?> opticFinder = DSL.namedChoice(this.entityName, type2);
Type<?> type5 = ExtraDataFixUtils.patchSubType(type2, type, type3);
return this.fix(type, type3, opticFinder, type4, type5);
}
private <S, T, A, B> TypeRewriteRule fix(Type<S> inputType, Type<T> outputType, OpticFinder<A> finder, Type<B> outputChoiceType, Type<?> newType) {
return this.fixTypeEverywhere(this.name, inputType, outputType, dynamicOps -> object -> {
Typed<S> typed = new Typed<>(inputType, dynamicOps, (S)object);
return typed.update(finder, outputChoiceType, objectx -> {
Typed<A> typedx = new Typed<>((Type<A>)newType, dynamicOps, (A)objectx);
return Util.<A, B>writeAndReadTypedOrThrow(typedx, outputChoiceType, this::fix).getValue();
}).getValue();
});
}
protected abstract <T> Dynamic<T> fix(Dynamic<T> tag);
}