aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/firmament/mixins/CustomDurabilityBarPatch.java
blob: f396ce6f6649044b389e81320ce816785c9f3831 (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
/*
 * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.mixins;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Share;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import moe.nea.firmament.util.DurabilityBarEvent;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(DrawContext.class)
public class CustomDurabilityBarPatch {
    @WrapOperation(
        method = "drawItemInSlot(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V",
        at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isItemBarVisible()Z")
    )
    private boolean onIsItemBarVisible(
        ItemStack instance, Operation<Boolean> original,
        @Share("barOverride") LocalRef<DurabilityBarEvent.DurabilityBar> barOverride
    ) {
        if (original.call(instance))
            return true;
        DurabilityBarEvent event = new DurabilityBarEvent(instance);
        DurabilityBarEvent.Companion.publish(event);
        barOverride.set(event.getBarOverride());
        return barOverride.get() != null;
    }

    @WrapOperation(method = "drawItemInSlot(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V",
        at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getItemBarStep()I"))
    private int overrideItemStep(
        ItemStack instance, Operation<Integer> original,
        @Share("barOverride") LocalRef<DurabilityBarEvent.DurabilityBar> barOverride
    ) {
        if (barOverride.get() != null)
            return Math.round(barOverride.get().getPercentage() * 13);
        return original.call(instance);
    }

    @WrapOperation(method = "drawItemInSlot(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V",
        at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getItemBarColor()I"))
    private int overrideItemColor(
        ItemStack instance, Operation<Integer> original,
        @Share("barOverride") LocalRef<DurabilityBarEvent.DurabilityBar> barOverride
    ) {
        if (barOverride.get() != null)
            return barOverride.get().getColor().getColor();
        return original.call(instance);
    }
}