File size: 2,713 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.DSL;
import com.mojang.datafixers.DataFix;
import com.mojang.datafixers.DataFixUtils;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.Typed;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.serialization.Dynamic;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import net.minecraft.util.datafix.ExtraDataFixUtils;

public class AttributesRenameFix extends DataFix {
    private final String name;
    private final UnaryOperator<String> renames;

    public AttributesRenameFix(Schema p_364410_, String p_366408_, UnaryOperator<String> p_366138_) {
        super(p_364410_, false);
        this.name = p_366408_;
        this.renames = p_366138_;
    }

    @Override
    protected TypeRewriteRule makeRule() {
        return TypeRewriteRule.seq(
            this.fixTypeEverywhereTyped(this.name + " (Components)", this.getInputSchema().getType(References.DATA_COMPONENTS), this::fixDataComponents),
            this.fixTypeEverywhereTyped(this.name + " (Entity)", this.getInputSchema().getType(References.ENTITY), this::fixEntity),
            this.fixTypeEverywhereTyped(this.name + " (Player)", this.getInputSchema().getType(References.PLAYER), this::fixEntity)
        );
    }

    private Typed<?> fixDataComponents(Typed<?> p_365772_) {
        return p_365772_.update(
            DSL.remainderFinder(),
            p_366382_ -> p_366382_.update(
                    "minecraft:attribute_modifiers",
                    p_369365_ -> p_369365_.update(
                            "modifiers",
                            p_366575_ -> DataFixUtils.orElse(
                                    p_366575_.asStreamOpt().result().map(p_363756_ -> p_363756_.map(this::fixTypeField)).map(p_366575_::createList), p_366575_
                                )
                        )
                )
        );
    }

    private Typed<?> fixEntity(Typed<?> p_364704_) {
        return p_364704_.update(
            DSL.remainderFinder(),
            p_362710_ -> p_362710_.update(
                    "attributes",
                    p_366472_ -> DataFixUtils.orElse(
                            p_366472_.asStreamOpt().result().map(p_362930_ -> p_362930_.map(this::fixIdField)).map(p_366472_::createList), p_366472_
                        )
                )
        );
    }

    private Dynamic<?> fixIdField(Dynamic<?> p_366579_) {
        return ExtraDataFixUtils.fixStringField(p_366579_, "id", this.renames);
    }

    private Dynamic<?> fixTypeField(Dynamic<?> p_367667_) {
        return ExtraDataFixUtils.fixStringField(p_367667_, "type", this.renames);
    }
}