aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMapMixin.java
blob: ca1c0df6d12aa0db4d4f2b13f9eed6ce95f8995e (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
package org.polyfrost.chatting.mixin;

import net.minecraft.client.gui.ChatLine;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.util.IChatComponent;
import org.polyfrost.chatting.hook.GuiNewChatHook;
import org.spongepowered.asm.mixin.Final;
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 org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mixin(value = GuiNewChat.class, priority = Integer.MIN_VALUE)
public abstract class GuiNewChatMapMixin implements GuiNewChatHook {

    @Unique private final Map<ChatLine, ChatLine> drawnToFull = new HashMap<>();
    @Unique private final List<ChatLine> tempDrawnLines = new ArrayList<>();
    @Unique private ChatLine lastTempLine = null;

    @Shadow @Final private List<ChatLine> drawnChatLines;
    @Shadow @Final private List<ChatLine> chatLines;

    @Inject(method = "setChatLine", at = @At("HEAD"))
    private void handleSetChatLine(IChatComponent chatComponent, int chatLineId, int updateCounter, boolean displayOnly, CallbackInfo ci) {
        tempDrawnLines.clear();
    }

    @Inject(method = "setChatLine", at = @At(value = "INVOKE", target = "Ljava/util/List;add(ILjava/lang/Object;)V", ordinal = 0, shift = At.Shift.AFTER, remap = false))
    private void handleDrawnLineAdded(IChatComponent chatComponent, int chatLineId, int updateCounter, boolean displayOnly, CallbackInfo ci) {
        if (!displayOnly) tempDrawnLines.add(drawnChatLines.get(0));
        else if (lastTempLine != null) {
            drawnToFull.put(drawnChatLines.get(0), lastTempLine);
        }
    }

    @Inject(method = "setChatLine", at = @At(value = "INVOKE", target = "Ljava/util/List;remove(I)Ljava/lang/Object;", ordinal = 0, shift = At.Shift.BEFORE, remap = false))
    private void handleDrawnLineRemoved(IChatComponent chatComponent, int chatLineId, int updateCounter, boolean displayOnly, CallbackInfo ci) {
        drawnToFull.remove(drawnChatLines.get(drawnChatLines.size() - 1));
    }

    @Inject(method = "setChatLine", at = @At("RETURN"))
    private void handleLineAdded(IChatComponent chatComponent, int chatLineId, int updateCounter, boolean displayOnly, CallbackInfo ci) {
        if (!displayOnly) {
            ChatLine masterLine = chatLines.get(0);
            for (ChatLine tempDrawnLine : tempDrawnLines) drawnToFull.put(tempDrawnLine, masterLine);
        }else {
            lastTempLine = null;
        }
    }

    @Inject(method = "refreshChat", at = @At("HEAD"))
    private void handleRefreshedChat(CallbackInfo ci) {
        drawnToFull.clear();
    }

    @Inject(method = "refreshChat", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;setChatLine(Lnet/minecraft/util/IChatComponent;IIZ)V", ordinal = 0, shift = At.Shift.BEFORE))
    private void handleLineRefresh(CallbackInfo ci, int i, ChatLine chatline) {
        lastTempLine = chatline;
    }

    @Inject(method = "clearChatMessages", at = @At("HEAD"))
    private void handleChatCleared(CallbackInfo ci) {
        drawnToFull.clear();
    }

    @Override
    public ChatLine getFullMessage(ChatLine line) {
        return drawnToFull.getOrDefault(line, null);
    }
}