aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java
blob: 9079fc9b7b032bbd0e89d05b070c33678d5cd363 (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
package moe.nea.firmament.mixins;

import moe.nea.firmament.features.chat.CopyChat;
import moe.nea.firmament.mixins.accessor.AccessorChatHud;
import moe.nea.firmament.util.ClipboardUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.client.gui.components.ChatComponent;
import net.minecraft.client.GuiMessage;
import net.minecraft.client.gui.screens.ChatScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.ChatFormatting;
import net.minecraft.util.Mth;
import org.spongepowered.asm.mixin.Mixin;
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.CallbackInfoReturnable;
import java.util.List;

@Mixin(ChatScreen.class)
public class CopyChatPatch {
	@Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true)
	private void onRightClick(MouseButtonEvent click, boolean doubled, CallbackInfoReturnable<Boolean> cir) throws NoSuchFieldException, IllegalAccessException {
		if (click.button() != 1 || !CopyChat.TConfig.INSTANCE.getCopyChat()) return;
		Minecraft client = Minecraft.getInstance();
		ChatComponent chatHud = client.gui.getChat();
		int lineIndex = getChatLineIndex(chatHud, click.y());
		if (lineIndex < 0) return;
		List<GuiMessage.Line> visible = ((AccessorChatHud) chatHud).getVisibleMessages_firmament();
		if (lineIndex >= visible.size()) return;
		GuiMessage.Line line = visible.get(lineIndex);
		String text = CopyChat.INSTANCE.orderedTextToString(line.content());
		ClipboardUtils.INSTANCE.setTextContent(text);
		chatHud.addMessage(Component.literal("Copied: ").append(text).withStyle(ChatFormatting.GRAY));
		cir.setReturnValue(true);
		cir.cancel();
	}

	@Unique
	private int getChatLineIndex(ChatComponent chatHud, double mouseY) {
		double chatLineY = ((AccessorChatHud) chatHud).toChatLineY_firmament(mouseY);
		return Mth.floor(chatLineY + ((AccessorChatHud) chatHud).getScrolledLines_firmament());
	}
}