5
5
import cpw .mods .fml .relauncher .SideOnly ;
6
6
import lombok .NonNull ;
7
7
import lombok .val ;
8
+ import lombok .var ;
8
9
import net .minecraft .client .entity .EntityOtherPlayerMP ;
9
10
import net .minecraft .client .entity .EntityPlayerSP ;
10
11
import net .minecraft .client .gui .FontRenderer ;
16
17
import net .minecraft .util .ChatComponentText ;
17
18
import net .minecraft .util .ChatStyle ;
18
19
import net .minecraft .util .EnumChatFormatting ;
20
+ import net .minecraft .util .IChatComponent ;
19
21
20
22
import java .awt .Color ;
21
23
import java .lang .reflect .Field ;
22
24
import java .util .*;
25
+ import java .util .function .Consumer ;
23
26
24
27
/**
25
28
* Universal escape sequence-based text rendering and chat messages.
@@ -131,11 +134,28 @@ public static FormattedText parse(String text) {
131
134
}
132
135
133
136
/**
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 .
135
138
* @return The chat component.
136
139
*/
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 );
139
159
val style = new ChatStyle ();
140
160
if (colorStyle != null ) {
141
161
style .setColor (colorStyle );
@@ -159,41 +179,44 @@ public ChatComponentText toChatText() {
159
179
break ;
160
180
}
161
181
}
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 );
165
189
}
166
- return result ;
167
190
}
168
191
169
192
public void addChatMessage (ICommandSender target ) {
170
- target . addChatMessage (this . toChatText () );
193
+ addChatMessage (target :: addChatMessage );
171
194
}
172
195
173
196
@ SideOnly (Side .CLIENT )
174
197
public void addChatMessage (EntityOtherPlayerMP target ) {
175
- target . addChatMessage (this . toChatText () );
198
+ addChatMessage (target :: addChatMessage );
176
199
}
177
200
178
201
@ SideOnly (Side .CLIENT )
179
202
public void addChatMessage (EntityPlayerSP target ) {
180
- target . addChatMessage (this . toChatText () );
203
+ addChatMessage (target :: addChatMessage );
181
204
}
182
205
183
206
public void addChatMessage (CommandBlockLogic target ) {
184
- target . addChatMessage (this . toChatText () );
207
+ addChatMessage (target :: addChatMessage );
185
208
}
186
209
187
210
public void addChatMessage (EntityPlayerMP target ) {
188
- target . addChatMessage (this . toChatText () );
211
+ addChatMessage (target :: addChatMessage );
189
212
}
190
213
191
214
public void addChatMessage (RConConsoleSource target ) {
192
- target . addChatMessage (this . toChatText () );
215
+ addChatMessage (target :: addChatMessage );
193
216
}
194
217
195
218
public void addChatMessage (MinecraftServer target ) {
196
- target . addChatMessage (this . toChatText () );
219
+ addChatMessage (target :: addChatMessage );
197
220
}
198
221
199
222
/**
0 commit comments