aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/firmament/mixins/custommodels/JsonUnbakedModelDataHolder.java
blob: 5fe3dec4452b6216b39e0756f9820f2fa3928a8d (plain)
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
68
package moe.nea.firmament.mixins.custommodels;

import com.google.gson.annotations.SerializedName;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import moe.nea.firmament.features.texturepack.BakedModelExtra;
import moe.nea.firmament.features.texturepack.JsonUnbakedModelFirmExtra;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.Baker;
import net.minecraft.client.render.model.ModelRotation;
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Collection;
import java.util.Objects;

@Mixin(JsonUnbakedModel.class)
public class JsonUnbakedModelDataHolder implements JsonUnbakedModelFirmExtra {
    @Shadow
    @Nullable
    protected JsonUnbakedModel parent;
    @Unique
    @Nullable
    public Identifier headModel;

    @Override
    public void setHeadModel_firmament(@Nullable Identifier identifier) {
        this.headModel = identifier;
    }

    @Override
    public @Nullable Identifier getHeadModel_firmament() {
        if (this.headModel != null) return this.headModel;
        if (this.parent == null) return null;
        return ((JsonUnbakedModelFirmExtra) this.parent).getHeadModel_firmament();
    }

    @ModifyReturnValue(method = "getModelDependencies", at = @At("RETURN"))
    private Collection<Identifier> addDependencies(Collection<Identifier> original) {
        var headModel = getHeadModel_firmament();
        if (headModel != null) {
            original.add(headModel);
        }
        return original;
    }

    @ModifyReturnValue(
        method = "bake(Lnet/minecraft/client/render/model/Baker;Lnet/minecraft/client/render/model/json/JsonUnbakedModel;Ljava/util/function/Function;Lnet/minecraft/client/render/model/ModelBakeSettings;Z)Lnet/minecraft/client/render/model/BakedModel;",
        at = @At(value = "RETURN"))
    private BakedModel bakeExtraInfo(BakedModel original, @Local(argsOnly = true) Baker baker) {
        var headModel = getHeadModel_firmament();
        if (headModel != null && original instanceof BakedModelExtra extra) {
            UnbakedModel unbakedModel = baker.getOrLoadModel(headModel);
            extra.setHeadModel_firmament(
                Objects.equals(unbakedModel, parent)
                    ? null
                    : baker.bake(headModel, ModelRotation.X0_Y0));
        }
        return original;
    }
}