Skip to content

Commit 860f106

Browse files
committed
multiline chat fix
1 parent ed5557a commit 860f106

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/main/java/com/falsepattern/lib/text/FormattedText.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import cpw.mods.fml.relauncher.SideOnly;
66
import lombok.NonNull;
77
import lombok.val;
8+
import lombok.var;
89
import net.minecraft.client.entity.EntityOtherPlayerMP;
910
import net.minecraft.client.entity.EntityPlayerSP;
1011
import net.minecraft.client.gui.FontRenderer;
@@ -16,10 +17,12 @@
1617
import net.minecraft.util.ChatComponentText;
1718
import net.minecraft.util.ChatStyle;
1819
import net.minecraft.util.EnumChatFormatting;
20+
import net.minecraft.util.IChatComponent;
1921

2022
import java.awt.Color;
2123
import java.lang.reflect.Field;
2224
import java.util.*;
25+
import java.util.function.Consumer;
2326

2427
/**
2528
* Universal escape sequence-based text rendering and chat messages.
@@ -131,11 +134,28 @@ public static FormattedText parse(String text) {
131134
}
132135

133136
/**
134-
* Converts this text structure into a chat component that can be sent to clients.
137+
* Converts this text structure into chat components that can be sent to the client. This is a list, because chat components can't have newlines.
135138
* @return The chat component.
136139
*/
137-
public ChatComponentText toChatText() {
138-
val result = new ChatComponentText(endLine ? text + "\n" : "");
140+
public List<ChatComponentText> toChatText() {
141+
var thisComponent = toChatTextSingle();
142+
val result = new ArrayList<ChatComponentText>();
143+
result.add(thisComponent);
144+
FormattedText prevSibling = this;
145+
for (val sibling: siblings) {
146+
val siblingResult = sibling.toChatTextSingle();
147+
if (prevSibling.endLine) {
148+
result.add(thisComponent = siblingResult);
149+
} else {
150+
thisComponent.appendSibling(siblingResult);
151+
}
152+
prevSibling = sibling;
153+
}
154+
return result;
155+
}
156+
157+
private ChatComponentText toChatTextSingle() {
158+
val thisComponent = new ChatComponentText(text);
139159
val style = new ChatStyle();
140160
if (colorStyle != null) {
141161
style.setColor(colorStyle);
@@ -159,41 +179,44 @@ public ChatComponentText toChatText() {
159179
break;
160180
}
161181
}
162-
result.setChatStyle(style);
163-
for (val sibling: siblings) {
164-
result.appendSibling(sibling.toChatText());
182+
thisComponent.setChatStyle(style);
183+
return thisComponent;
184+
}
185+
186+
private void addChatMessage(Consumer<IChatComponent> consumer) {
187+
for (val line: toChatText()) {
188+
consumer.accept(line);
165189
}
166-
return result;
167190
}
168191

169192
public void addChatMessage(ICommandSender target) {
170-
target.addChatMessage(this.toChatText());
193+
addChatMessage(target::addChatMessage);
171194
}
172195

173196
@SideOnly(Side.CLIENT)
174197
public void addChatMessage(EntityOtherPlayerMP target) {
175-
target.addChatMessage(this.toChatText());
198+
addChatMessage(target::addChatMessage);
176199
}
177200

178201
@SideOnly(Side.CLIENT)
179202
public void addChatMessage(EntityPlayerSP target) {
180-
target.addChatMessage(this.toChatText());
203+
addChatMessage(target::addChatMessage);
181204
}
182205

183206
public void addChatMessage(CommandBlockLogic target) {
184-
target.addChatMessage(this.toChatText());
207+
addChatMessage(target::addChatMessage);
185208
}
186209

187210
public void addChatMessage(EntityPlayerMP target) {
188-
target.addChatMessage(this.toChatText());
211+
addChatMessage(target::addChatMessage);
189212
}
190213

191214
public void addChatMessage(RConConsoleSource target) {
192-
target.addChatMessage(this.toChatText());
215+
addChatMessage(target::addChatMessage);
193216
}
194217

195218
public void addChatMessage(MinecraftServer target) {
196-
target.addChatMessage(this.toChatText());
219+
addChatMessage(target::addChatMessage);
197220
}
198221

199222
/**

0 commit comments

Comments
 (0)