Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Created September 22, 2024 10:11
Show Gist options
  • Save Densamisten/3ae6ca542adc67c105d053cbb3576beb to your computer and use it in GitHub Desktop.
Save Densamisten/3ae6ca542adc67c105d053cbb3576beb to your computer and use it in GitHub Desktop.
// 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) {
// Create a new style with a click event to copy text and a hover event with instructions.
Style style = mutableText.getStyle().withClickEvent(
new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, mutableText.getString())
).withHoverEvent(
new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal("Click to copy this message"))
);
// Apply the new style to the text.
mutableText.setStyle(style);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment