Skip to content

Commit

Permalink
Fix: Make "Trim Line Separators" copy styles of child components, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
GamingGeek committed Aug 23, 2021
1 parent 86106ca commit 87c4005
Showing 1 changed file with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}

Expand All @@ -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;
}
}

0 comments on commit 87c4005

Please sign in to comment.