55import cpw .mods .fml .relauncher .SideOnly ;
66import lombok .NonNull ;
77import lombok .val ;
8+ import lombok .var ;
89import net .minecraft .client .entity .EntityOtherPlayerMP ;
910import net .minecraft .client .entity .EntityPlayerSP ;
1011import net .minecraft .client .gui .FontRenderer ;
1617import net .minecraft .util .ChatComponentText ;
1718import net .minecraft .util .ChatStyle ;
1819import net .minecraft .util .EnumChatFormatting ;
20+ import net .minecraft .util .IChatComponent ;
1921
2022import java .awt .Color ;
2123import java .lang .reflect .Field ;
2224import 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