Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Created September 22, 2024 11:15
Show Gist options
  • Save Densamisten/bb02468e3f3f53c2bbdc4d29d3a94bc8 to your computer and use it in GitHub Desktop.
Save Densamisten/bb02468e3f3f53c2bbdc4d29d3a94bc8 to your computer and use it in GitHub Desktop.
works"!!
package exonihility.client.mixin;
import com.google.common.collect.Lists;
import exonihility.client.util.time.Timestamp;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.gui.hud.MessageIndicator;
import net.minecraft.client.util.ChatMessages;
import net.minecraft.client.util.TextCollector;
import net.minecraft.network.message.MessageSignatureData;
import net.minecraft.network.message.MessageType;
import net.minecraft.text.*;
import net.minecraft.util.Language;
import net.minecraft.util.collection.ArrayListDeque;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Mixin(ChatHud.class)
public abstract class ChatHudMixin {
@ModifyVariable(method = "addMessage(Lnet/minecraft/text/Text;)V", at = @At("HEAD"), argsOnly = true)
private Text onAddMessage(Text message) {
return Timestamp.addToMessage(message);
}
@ModifyVariable(at = @At("HEAD"),
method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V",
argsOnly = true)
public Text addMessage(Text message) {
return (message);
}
// This injects into the addMessage method of ChatHud, which handles adding messages to the chat.
@Inject(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V", at = @At("HEAD"))
private void onAddMessage(Text message, MessageSignatureData signatureData, MessageIndicator indicator, CallbackInfo ci) {
if (message instanceof MutableText mutableText) {
// Extract the main message content without player name
String mainMessage = extractMainMessage(mutableText);
// Create a new style with a click event to copy the main message and a hover event with instructions.
Style style = mutableText.getStyle().withClickEvent(
new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, mainMessage)
).withHoverEvent(
new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal("Click to copy this message"))
);
// Apply the new style to the text.
mutableText.setStyle(style);
}
}
// Helper method to extract the main message, excluding the player name
private String extractMainMessage(MutableText mutableText) {
String fullText = mutableText.getString(); // Get the entire message as a string
// Regex pattern to match typical player name format <PlayerName>
String pattern = "^<[^>]+>\\s*"; // Matches patterns like "<PlayerName> " at the start of the string
// Replace the player name with an empty string, leaving only the message content
return fullText.replaceFirst(pattern, "").trim();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment