summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinGuiChat.java
blob: ff744b7d0677586d46f2e8be64942aac1c295bdc (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
package at.hannibal2.skyhanni.mixins.transformers;

import at.hannibal2.skyhanni.events.ChatHoverEvent;
import at.hannibal2.skyhanni.events.TabCompletionEvent;
import at.hannibal2.skyhanni.mixins.hooks.GuiChatHook;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Arrays;

@Mixin(GuiChat.class)
public class MixinGuiChat {

    @Shadow
    protected GuiTextField inputField;

    @ModifyVariable(
        method = "onAutocompleteResponse",
        at = @At(
            value = "SKYHANNI_FORLOOP_LOCAL_VAR",
            shift = At.Shift.BEFORE,
            args = "lvIndex=1"
        ),
        index = 1,
        argsOnly = true
    )
    private String[] renderItemOverlayPost(String[] originalArray) {
        String inputFieldText = this.inputField.getText();
        String beforeCursor = inputFieldText.substring(0, this.inputField.getCursorPosition());
        TabCompletionEvent tabCompletionEvent = new TabCompletionEvent(beforeCursor, inputFieldText, Arrays.asList(originalArray));
        tabCompletionEvent.postAndCatch();
        String[] newSuggestions = tabCompletionEvent.intoSuggestionArray();
        if (newSuggestions == null)
            newSuggestions = originalArray;
        return newSuggestions;
    }

    @Inject(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiChat;handleComponentHover(Lnet/minecraft/util/IChatComponent;II)V"), locals = LocalCapture.CAPTURE_FAILHARD)
    public void chatHoverEvent(int mouseX, int mouseY, float partialTicks, CallbackInfo ci, IChatComponent component) {
        // Only ChatComponentText components can make it to this point

        // Always set the replacement, so if someone is no longer editing the replacement
        // we get the original component back
        GuiChatHook.INSTANCE.setReplacement((ChatComponentText) component);

        new ChatHoverEvent((ChatComponentText) component).postAndCatch();
    }

    @ModifyArg(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiChat;handleComponentHover(Lnet/minecraft/util/IChatComponent;II)V"), index = 0)
    public IChatComponent replaceWithNewComponent(IChatComponent originalComponent) {
        return GuiChatHook.INSTANCE.getReplacementAsIChatComponent();
    }
}