aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/polyfrost/chatting/mixin/ChatLineMixin.java
blob: cde09eaba9a9bd558e9b375e7531622cad7a2cfa (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
/*
 * This file is from chat_heads is licensed under MPL-2.0, which can be found at https://www.mozilla.org/en-US/MPL/2.0/
 * See: https://github.com/dzwdz/chat_heads/blob/fabric-1.16.x/LICENSE
 */

package org.polyfrost.chatting.mixin;

import org.polyfrost.chatting.config.ChattingConfig;
import org.polyfrost.chatting.hook.ChatLineHook;
import net.minecraft.client.gui.ChatLine;
import net.minecraft.client.network.NetworkPlayerInfo;
import net.minecraft.util.IChatComponent;
import org.polyfrost.chatting.utils.ChatHeadHooks;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.lang.ref.WeakReference;

@Mixin(ChatLine.class)
public class ChatLineMixin implements ChatLineHook {
    private boolean detected = false;
    private boolean firstDetection = true;
    private NetworkPlayerInfo playerInfo;
    private NetworkPlayerInfo detectedPlayerInfo;
    private static long lastUniqueId = 0;
    private long uniqueId = 0;

    @Inject(method = "<init>", at = @At("RETURN"))
    private void onInit(int i, IChatComponent iChatComponent, int j, CallbackInfo ci) {
        lastUniqueId++;
        uniqueId = lastUniqueId;
        chatLines.add(new WeakReference<>((ChatLine) (Object) this));
        ChatHeadHooks.INSTANCE.detect(iChatComponent.getFormattedText(), (ChatLine) (Object) this);
    }

    @Override
    public boolean isDetected() {
        return detected;
    }

    @Override
    public void setDetected(boolean detected) {
        this.detected = detected;
    }

    @Override
    public NetworkPlayerInfo getPlayerInfo() {
        return playerInfo;
    }

    @Override
    public void setPlayerInfo(NetworkPlayerInfo playerInfo) {
        this.playerInfo = playerInfo;
    }

    @Override
    public NetworkPlayerInfo getDetectedPlayerInfo() {
        return detectedPlayerInfo;
    }

    @Override
    public void setDetectedPlayerInfo(NetworkPlayerInfo detectedPlayerInfo) {
        this.detectedPlayerInfo = detectedPlayerInfo;
    }

    @Override
    public boolean isFirstDetection() {
        return firstDetection;
    }

    @Override
    public void setFirstDetection(boolean firstDetection) {
        this.firstDetection = firstDetection;
    }

    @Override
    public void updatePlayerInfo() {
        if (ChattingConfig.INSTANCE.getHideChatHeadOnConsecutiveMessages() && !firstDetection) {
            playerInfo = null;
        } else {
            playerInfo = detectedPlayerInfo;
        }
    }

    @Override
    public long getUniqueId() {
        return uniqueId;
    }
}