aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/firmament/mixins/MixinConditionComponents.java
blob: 602c13ddce57d35c2595cf5461e15d33916ecd8d (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package moe.nea.firmament.mixins;

// People are complaining but this really is not my place to fix things

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import moe.nea.firmament.features.texturepack.CustomSkyBlockTextures;
import moe.nea.firmament.util.ConditionNBTMixin;
import net.minecraft.component.ComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import shcm.shsupercm.fabric.citresewn.CITResewn;
import shcm.shsupercm.fabric.citresewn.cit.CITContext;
import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionComponents;
import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionNBT;
import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup;
import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey;
import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue;

@Mixin(ConditionComponents.class)
@Pseudo
public class MixinConditionComponents {
    @Shadow
    private ComponentType<?> componentType;

    @Shadow(remap = false)
    private ConditionNBT fallbackNBTCheck;
    @Unique
    private String[] pathCheck;
    @Unique
    private int loreInt = -1;

    @Inject(method = "load", at = @At("HEAD"), remap = false)
    public void addExtraAttributeSupport(PropertyKey key, PropertyValue value, PropertyGroup properties, CallbackInfo ci,
                                         @Local(argsOnly = true) LocalRef<PropertyKey> keyRef,
                                         @Local(argsOnly = true) LocalRef<PropertyValue> valueRef) {
        if (!CustomSkyBlockTextures.TConfig.INSTANCE.getEnableLegacyCIT()) return;
        if (!"nbt".equals(key.path())) return;
        if (!value.keyMetadata().startsWith("ExtraAttributes.")) return;
        keyRef.set(new PropertyKey(key.namespace(), "component"));
        valueRef.set(new PropertyValue(
            "minecraft:custom_data" + value.keyMetadata().substring("ExtraAttributes".length()),
            value.value(),
            value.separator(),
            value.position(),
            value.propertiesIdentifier(),
            value.packName()
        ));
        CITResewn.logWarnLoading(properties.messageWithDescriptorOf("NBT condition is not supported since 1.21. THIS IS A FIRMAMENT SPECIAL FEATURE ALLOWING YOU TO CONTINUE TO USE nbt.ExtraAttributes.* PROPERTIES FOR A LIMITED TIME! UPDATE YOUR .PROPERTIES FILES OR SWITCH TO FIRMAMENT CIT (https://github.com/FirmamentMC/CitToFirm)",
                                                                    value.position()));
    }

    @Inject(method = "load",
        at = @At(value = "INVOKE", remap = false, target = "Lshcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT;loadNbtCondition(Lshcm/shsupercm/fabric/citresewn/pack/format/PropertyValue;Lshcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup;[Ljava/lang/String;Ljava/lang/String;)V"),
        remap = false)
    private void onLoadSavePath(PropertyKey key, PropertyValue value, PropertyGroup properties, CallbackInfo ci,
                                @Local String[] path) {
        this.pathCheck = path;
        this.loreInt = -1;
    }

    @Unique
    private boolean matchStringDirect(String directString, CITContext context) {
        return ConditionNBTMixin.invokeDirectConditionNBTStringMatch(fallbackNBTCheck, directString);
    }

    @WrapOperation(method = "test", at = @At(value = "INVOKE", target = "Lcom/mojang/serialization/Codec;encodeStart(Lcom/mojang/serialization/DynamicOps;Ljava/lang/Object;)Lcom/mojang/serialization/DataResult;"), remap = false)
    DataResult fastPathUnsafeNbtComponent(
        Codec instance,
        DynamicOps dynamicOps,
        Object o,
        Operation<DataResult> original) {
        if (o instanceof NbtComponent nbtComponent) {
            return DataResult.success(nbtComponent.getNbt());
        }
        return original.call(instance, dynamicOps, o);
    }

    @Inject(method = "test", at = @At("HEAD"), cancellable = true, remap = false)
    void fastPathDisplayName(CITContext context, CallbackInfoReturnable<Boolean> cir) {
        if (this.componentType == DataComponentTypes.CUSTOM_NAME && pathCheck.length == 0) {
            var displayName = context.stack.getComponents().get(DataComponentTypes.CUSTOM_NAME);
            if (displayName != null) {
                cir.setReturnValue(matchStringDirect((displayName.getString()), context));
            }
        }
        if (this.componentType == DataComponentTypes.LORE && pathCheck.length == 1) {
            var lore = context.stack.getComponents().get(DataComponentTypes.LORE);
            if (lore != null) {
                var loreLines = lore.lines();
                if (pathCheck[0].equals("*")) {
                    for (var loreLine : loreLines) {
                        if (matchStringDirect((loreLine.getString()), context)) {
                            cir.setReturnValue(true);
                            return;
                        }
                    }
                    cir.setReturnValue(false);
                } else {
                    if (loreInt < 0)
                        loreInt = Integer.parseInt(pathCheck[0]);
                    cir.setReturnValue(0 <= loreInt && loreInt < loreLines.size() &&
                                           matchStringDirect((loreLines.get(loreInt).getString()), context));
                }
            }
        }
    }


}