From 87c40059c32b50925d2f63f51bf16f753a316b5e Mon Sep 17 00:00:00 2001 From: Jake Ward Date: Mon, 23 Aug 2021 15:28:38 +0100 Subject: [PATCH] Fix: Make "Trim Line Separators" copy styles of child components, closes #109 --- .../modules/modifiers/TrimLineBreakers.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/club/sk1er/hytilities/handlers/chat/modules/modifiers/TrimLineBreakers.java b/src/main/java/club/sk1er/hytilities/handlers/chat/modules/modifiers/TrimLineBreakers.java index e5285ca7..d85d64d3 100644 --- a/src/main/java/club/sk1er/hytilities/handlers/chat/modules/modifiers/TrimLineBreakers.java +++ b/src/main/java/club/sk1er/hytilities/handlers/chat/modules/modifiers/TrimLineBreakers.java @@ -22,6 +22,7 @@ import club.sk1er.hytilities.handlers.chat.ChatReceiveModule; import net.minecraft.client.Minecraft; import net.minecraft.util.ChatComponentText; +import net.minecraft.util.IChatComponent; import net.minecraftforge.client.event.ClientChatReceivedEvent; import org.jetbrains.annotations.NotNull; @@ -45,11 +46,16 @@ public void onMessageReceived(@NotNull ClientChatReceivedEvent event) { String message = event.message.getFormattedText(); Matcher regex = lineBreakerPattern.matcher(message); if (regex.find()) { - String linebreak = regex.group(); - int chatWidth = mc.ingameGUI.getChatGUI().getChatWidth(); - String newLineBreaker = mc.fontRendererObj.trimStringToWidth(linebreak, chatWidth); - message = regex.replaceAll(newLineBreaker); - event.message = new ChatComponentText(message); + ChatComponentText trimmedComponent = new ChatComponentText(this.trimLineBreaker(message)); + trimmedComponent.setChatStyle(event.message.getChatStyle().createShallowCopy()); + + for (IChatComponent sibling : event.message.getSiblings()) { + ChatComponentText trimmedSibling = new ChatComponentText(this.trimLineBreaker(sibling.getFormattedText())); + trimmedSibling.setChatStyle(sibling.getChatStyle().createShallowCopy()); + trimmedComponent.appendSibling(trimmedSibling); + } + + event.message = trimmedComponent; } } @@ -62,4 +68,15 @@ public boolean isEnabled() { public int getPriority() { return -1; } + + private String trimLineBreaker(String text) { + Matcher regex = lineBreakerPattern.matcher(text); + if (regex.find()) { + String linebreak = regex.group(); + int chatWidth = mc.ingameGUI.getChatGUI().getChatWidth(); + String newLineBreaker = mc.fontRendererObj.trimStringToWidth(linebreak, chatWidth); + text = regex.replaceAll(newLineBreaker); + } + return text; + } }