| 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.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.serialization.Dynamic;
|
| 12 | import java.util.function.UnaryOperator;
|
| 13 |
|
| 14 | public class AttributesRenameLegacy extends DataFix {
|
| 15 | private final String name;
|
| 16 | private final UnaryOperator<String> renames;
|
| 17 |
|
| 18 | public AttributesRenameLegacy(final Schema outputSchema, final String name, final UnaryOperator<String> renames) {
|
| 19 | super(outputSchema, false);
|
| 20 | this.name = name;
|
| 21 | this.renames = renames;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | protected TypeRewriteRule makeRule() {
|
| 26 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 27 | OpticFinder<?> tagF = itemStackType.findField("tag");
|
| 28 | return TypeRewriteRule.seq(
|
| 29 | this.fixTypeEverywhereTyped(this.name + " (ItemStack)", itemStackType, itemStack -> itemStack.updateTyped(tagF, this::fixItemStackTag)),
|
| 30 | this.fixTypeEverywhereTyped(this.name + " (Entity)", this.getInputSchema().getType(References.ENTITY), this::fixEntity),
|
| 31 | this.fixTypeEverywhereTyped(this.name + " (Player)", this.getInputSchema().getType(References.PLAYER), this::fixEntity)
|
| 32 | );
|
| 33 | }
|
| 34 |
|
| 35 | private Dynamic<?> fixName(final Dynamic<?> name) {
|
| 36 | return DataFixUtils.orElse(name.asString().result().map(this.renames).map(name::createString), name);
|
| 37 | }
|
| 38 |
|
| 39 | private Typed<?> fixItemStackTag(final Typed<?> itemStack) {
|
| 40 | return itemStack.update(
|
| 41 | DSL.remainderFinder(),
|
| 42 | tag -> tag.update(
|
| 43 | "AttributeModifiers",
|
| 44 | modifiers -> DataFixUtils.orElse(
|
| 45 | modifiers.asStreamOpt().result().map(s -> s.map(modifier -> modifier.update("AttributeName", this::fixName))).map(modifiers::createList), modifiers
|
| 46 | )
|
| 47 | )
|
| 48 | );
|
| 49 | }
|
| 50 |
|
| 51 | private Typed<?> fixEntity(final Typed<?> entity) {
|
| 52 | return entity.update(
|
| 53 | DSL.remainderFinder(),
|
| 54 | tag -> tag.update(
|
| 55 | "Attributes",
|
| 56 | attributeList -> DataFixUtils.orElse(
|
| 57 | attributeList.asStreamOpt().result().map(s -> s.map(attribute -> attribute.update("Name", this::fixName))).map(attributeList::createList), attributeList
|
| 58 | )
|
| 59 | )
|
| 60 | );
|
| 61 | }
|
| 62 | }
|
| 63 |
|