Skip to content
This repository has been archived by the owner on Jan 2, 2022. It is now read-only.

Fix: Make "Trim Line Separators" copy styles of child components #115

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}