diff options
author | Jacob <55346310+Kathund@users.noreply.github.com> | 2025-07-01 20:08:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-01 14:08:29 +0200 |
commit | e37d29ef86067ea861b9f624f180cdd074b51abe (patch) | |
tree | ab4a3ac1077db6141d5306ca33dc95f43e658c11 | |
parent | 383970ba997e8b51a6e117e4df362bfb2cb972a1 (diff) | |
download | Firmament-e37d29ef86067ea861b9f624f180cdd074b51abe.tar.gz Firmament-e37d29ef86067ea861b9f624f180cdd074b51abe.tar.bz2 Firmament-e37d29ef86067ea861b9f624f180cdd074b51abe.zip |
feat: copy chat messages (#155)
Co-authored-by: Linnea Gräf <nea@nea.moe>
-rw-r--r-- | src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java | 44 | ||||
-rw-r--r-- | src/main/java/moe/nea/firmament/mixins/accessor/AccessorChatHud.java | 10 | ||||
-rw-r--r-- | src/main/kotlin/features/chat/CopyChat.kt | 25 | ||||
-rw-r--r-- | translations/en_us.json | 3 |
4 files changed, 82 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java b/src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java new file mode 100644 index 0000000..6996818 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java @@ -0,0 +1,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()); + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/accessor/AccessorChatHud.java b/src/main/java/moe/nea/firmament/mixins/accessor/AccessorChatHud.java index 72a72f0..d164aac 100644 --- a/src/main/java/moe/nea/firmament/mixins/accessor/AccessorChatHud.java +++ b/src/main/java/moe/nea/firmament/mixins/accessor/AccessorChatHud.java @@ -4,6 +4,7 @@ import net.minecraft.client.gui.hud.ChatHud; import net.minecraft.client.gui.hud.ChatHudLine; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; import java.util.List; @@ -11,4 +12,13 @@ import java.util.List; public interface AccessorChatHud { @Accessor("messages") List<ChatHudLine> getMessages_firmament(); + + @Accessor("visibleMessages") + List<ChatHudLine.Visible> getVisibleMessages_firmament(); + + @Accessor("scrolledLines") + int getScrolledLines_firmament(); + + @Invoker("toChatLineY") + double toChatLineY_firmament(double y); } diff --git a/src/main/kotlin/features/chat/CopyChat.kt b/src/main/kotlin/features/chat/CopyChat.kt new file mode 100644 index 0000000..c1df78b --- /dev/null +++ b/src/main/kotlin/features/chat/CopyChat.kt @@ -0,0 +1,25 @@ +package moe.nea.firmament.features.chat + +import net.minecraft.text.OrderedText +import moe.nea.firmament.features.FirmamentFeature +import moe.nea.firmament.gui.config.ManagedConfig +import moe.nea.firmament.util.reconstitute + + +object CopyChat : FirmamentFeature { + override val identifier: String + get() = "copy-chat" + + object TConfig : ManagedConfig(identifier, Category.CHAT) { + val copyChat by toggle("copy-chat") { false } + } + + override val config: ManagedConfig? + get() = TConfig + + fun orderedTextToString(orderedText: OrderedText): String { + return orderedText.reconstitute().string + } + + +} diff --git a/translations/en_us.json b/translations/en_us.json index baa6454..c628824 100644 --- a/translations/en_us.json +++ b/translations/en_us.json @@ -79,6 +79,9 @@ "firmament.config.configconfig.enable-moulconfig.description": "Uses the MoulConfig config UI. Turn off to fall back to the built in config.", "firmament.config.configconfig.enable-yacl": "Use YACL Config", "firmament.config.configconfig.enable-yacl.description": "Uses the YACL config UI. Turn off to fall back to the built in config. Needs YACL to be installed separately.", + "firmament.config.copy-chat": "Copy Chat", + "firmament.config.copy-chat.copy-chat": "Copy Chat", + "firmament.config.copy-chat.copy-chat.description": "Right click a message to copy", "firmament.config.custom-skyblock-textures": "Custom SkyBlock Item Textures", "firmament.config.custom-skyblock-textures.armor-overrides": "Enable Armor re-texturing", "firmament.config.custom-skyblock-textures.armor-overrides.description": "Allows texture pack authors to re-texture (but not re-model) SkyBlock armors.", |