| 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.DataFixUtils;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.Typed;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.serialization.Dynamic;
|
| 10 | import java.util.function.UnaryOperator;
|
| 11 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 12 |
|
| 13 | public class AttributesRenameFix extends DataFix {
|
| 14 | private final String name;
|
| 15 | private final UnaryOperator<String> renames;
|
| 16 | private final boolean oldDataComponentFormat;
|
| 17 |
|
| 18 | public AttributesRenameFix(final Schema outputSchema, final String name, final UnaryOperator<String> renames) {
|
| 19 | this(outputSchema, name, renames, false);
|
| 20 | }
|
| 21 |
|
| 22 | public AttributesRenameFix(final Schema outputSchema, final String name, final UnaryOperator<String> renames, final boolean oldDataComponentFormat) {
|
| 23 | super(outputSchema, false);
|
| 24 | this.name = name;
|
| 25 | this.renames = renames;
|
| 26 | this.oldDataComponentFormat = oldDataComponentFormat;
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | protected TypeRewriteRule makeRule() {
|
| 31 | return TypeRewriteRule.seq(
|
| 32 | this.fixTypeEverywhereTyped(
|
| 33 | this.name + " (Components)",
|
| 34 | this.getInputSchema().getType(References.DATA_COMPONENTS),
|
| 35 | this.oldDataComponentFormat ? this::fixDataComponentsOld : this::fixDataComponents
|
| 36 | ),
|
| 37 | this.fixTypeEverywhereTyped(this.name + " (Entity)", this.getInputSchema().getType(References.ENTITY), this::fixEntity),
|
| 38 | this.fixTypeEverywhereTyped(this.name + " (Player)", this.getInputSchema().getType(References.PLAYER), this::fixEntity)
|
| 39 | );
|
| 40 | }
|
| 41 |
|
| 42 | private Typed<?> fixDataComponents(final Typed<?> components) {
|
| 43 | return components.update(
|
| 44 | DSL.remainderFinder(),
|
| 45 | componentData -> componentData.update(
|
| 46 | "minecraft:attribute_modifiers",
|
| 47 | attributeModifiers -> DataFixUtils.orElse(
|
| 48 | attributeModifiers.asStreamOpt().result().map(modifierStream -> modifierStream.map(this::fixTypeField)).map(attributeModifiers::createList),
|
| 49 | attributeModifiers
|
| 50 | )
|
| 51 | )
|
| 52 | );
|
| 53 | }
|
| 54 |
|
| 55 | private Typed<?> fixDataComponentsOld(final Typed<?> components) {
|
| 56 | return components.update(
|
| 57 | DSL.remainderFinder(),
|
| 58 | componentData -> componentData.update(
|
| 59 | "minecraft:attribute_modifiers",
|
| 60 | attributeModifiers -> attributeModifiers.update(
|
| 61 | "modifiers",
|
| 62 | modifiers -> DataFixUtils.orElse(
|
| 63 | modifiers.asStreamOpt().result().map(modifierStream -> modifierStream.map(this::fixTypeField)).map(modifiers::createList), modifiers
|
| 64 | )
|
| 65 | )
|
| 66 | )
|
| 67 | );
|
| 68 | }
|
| 69 |
|
| 70 | private Typed<?> fixEntity(final Typed<?> entity) {
|
| 71 | return entity.update(
|
| 72 | DSL.remainderFinder(),
|
| 73 | tag -> tag.update(
|
| 74 | "attributes",
|
| 75 | attributeList -> DataFixUtils.orElse(attributeList.asStreamOpt().result().map(s -> s.map(this::fixIdField)).map(attributeList::createList), attributeList)
|
| 76 | )
|
| 77 | );
|
| 78 | }
|
| 79 |
|
| 80 | private Dynamic<?> fixIdField(final Dynamic<?> dynamic) {
|
| 81 | return ExtraDataFixUtils.fixStringField(dynamic, "id", this.renames);
|
| 82 | }
|
| 83 |
|
| 84 | private Dynamic<?> fixTypeField(final Dynamic<?> dynamic) {
|
| 85 | return ExtraDataFixUtils.fixStringField(dynamic, "type", this.renames);
|
| 86 | }
|
| 87 | }
|
| 88 |
|