aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java
blob: 6996818752da217d772b4f4214a6607e84ddc59e (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
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.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.gui.hud.ChatHudLine;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper;
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(double mouseX, double mouseY, int button, CallbackInfoReturnable<Boolean> cir) throws NoSuchFieldException, IllegalAccessException {
		if (button != 1 || !CopyChat.TConfig.INSTANCE.getCopyChat()) return;
		MinecraftClient client = MinecraftClient.getInstance();
		ChatHud chatHud = client.inGameHud.getChatHud();
		int lineIndex = getChatLineIndex(chatHud, mouseY);
		if (lineIndex < 0) return;
		List<ChatHudLine.Visible> visible = ((AccessorChatHud) chatHud).getVisibleMessages_firmament();
		if (lineIndex >= visible.size()) return;
		ChatHudLine.Visible line = visible.get(lineIndex);
		String text = CopyChat.INSTANCE.orderedTextToString(line.content());
		ClipboardUtils.INSTANCE.setTextContent(text);
		chatHud.addMessage(Text.literal("Copied: ").append(text).formatted(Formatting.GRAY));
		cir.setReturnValue(true);
		cir.cancel();
	}

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