feat: copy chat messages (#155)
Co-authored-by: Linnea Gräf <nea@nea.moe>
This commit is contained in:
44
src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java
Normal file
44
src/main/java/moe/nea/firmament/mixins/CopyChatPatch.java
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
25
src/main/kotlin/features/chat/CopyChat.kt
Normal file
25
src/main/kotlin/features/chat/CopyChat.kt
Normal file
@@ -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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user