-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFormattedText.java
More file actions
337 lines (312 loc) · 11.1 KB
/
FormattedText.java
File metadata and controls
337 lines (312 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.text;
import lombok.NonNull;
import lombok.val;
import net.minecraft.client.entity.EntityOtherPlayerMP;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.server.CommandBlockLogic;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.rcon.RConConsoleSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.awt.Color;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
/**
* Universal escape sequence-based text rendering and chat messages.
*
* @since 0.6.0
*/
public final class FormattedText {
private static final Map<Character, EnumChatFormatting> reverseMap = new HashMap<>();
private static final Map<EnumChatFormatting, Color> colorMap = new HashMap<>();
private static final char ESCAPE = '\u00a7';
static {
for (val value : EnumChatFormatting.values()) {
reverseMap.put(value.getFormattingCode(), value);
if (value.isColor()) {
final int rgb;
switch (value.getFormattingCode()) {
default:
rgb = 0x000000;
break;
case '1':
rgb = 0x0000AA;
break;
case '2':
rgb = 0x00AA00;
break;
case '3':
rgb = 0x00AAAA;
break;
case '4':
rgb = 0xAA0000;
break;
case '5':
rgb = 0xAA00AA;
break;
case '6':
rgb = 0xFFAA00;
break;
case '7':
rgb = 0xAAAAAA;
break;
case '8':
rgb = 0x555555;
break;
case '9':
rgb = 0x5555FF;
break;
case 'a':
rgb = 0x55FF55;
break;
case 'b':
rgb = 0x55FFFF;
break;
case 'c':
rgb = 0xFF5555;
break;
case 'd':
rgb = 0xFF55FF;
break;
case 'e':
rgb = 0xFFFF55;
break;
case 'f':
rgb = 0xFFFFFF;
break;
}
colorMap.put(value, new Color(rgb));
}
try {
Field colorF = Color.class.getDeclaredField(value.getFriendlyName());
colorMap.put(value, (Color) colorF.get(null));
} catch (NoSuchFieldException | IllegalAccessException ignored) {
}
}
}
private final String text;
private final EnumChatFormatting colorStyle;
private final Set<EnumChatFormatting> fancyStyles = new HashSet<>();
private final List<FormattedText> siblings = new ArrayList<>();
private final boolean endLine;
private FormattedText(@NonNull String text, @NonNull EnumChatFormatting colorStyle, @NonNull Set<EnumChatFormatting> fancyStyles, boolean endLine) {
this.text = text;
this.fancyStyles.addAll(fancyStyles);
this.colorStyle = colorStyle;
this.endLine = endLine;
}
/**
* Parse a string with minecraft style escapes (\u00a7X) into a {@link FormattedText} instance, which can then be
* used to generate chat or render-able text.
*
* @param text The string to parse
*
* @return The parsed text structure
*/
public static FormattedText parse(String text) {
EnumChatFormatting currentColorStyle = EnumChatFormatting.WHITE;
val currentFancyStyle = new HashSet<EnumChatFormatting>();
FormattedText result = null;
val accumulator = new StringBuilder();
int length = text.length();
boolean format = false;
for (int i = 0; i < length; i++) {
char c = text.charAt(i);
if (format) {
val f = reverseMap.get(c);
if (f == null) {
continue;
}
if (f == EnumChatFormatting.RESET) {
currentColorStyle = EnumChatFormatting.WHITE;
currentFancyStyle.clear();
} else if (f.isColor()) {
currentColorStyle = f;
} else if (f.isFancyStyling()) {
currentFancyStyle.add(f);
}
format = false;
continue;
}
if (c == ESCAPE || c == '\n') {
format = c == ESCAPE;
val txt = new FormattedText(accumulator.toString(), currentColorStyle, currentFancyStyle, c == '\n');
if (result == null) {
result = txt;
} else {
result.siblings.add(txt);
}
accumulator.setLength(0);
continue;
}
accumulator.append(c);
}
if (accumulator.length() > 0) {
val txt = new FormattedText(accumulator.toString(), currentColorStyle, currentFancyStyle, true);
if (result == null) {
result = txt;
} else {
result.siblings.add(txt);
}
}
return result;
}
public void addChatMessage(ICommandSender target) {
addChatMessage(target::addChatMessage);
}
private void addChatMessage(Consumer<IChatComponent> consumer) {
for (val line : toChatText()) {
consumer.accept(line);
}
}
/**
* 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.
*
* @return The chat component.
*/
public List<ChatComponentText> toChatText() {
var thisComponent = toChatTextSingle();
val result = new ArrayList<ChatComponentText>();
result.add(thisComponent);
FormattedText prevSibling = this;
for (val sibling : siblings) {
val siblingResult = sibling.toChatTextSingle();
if (prevSibling.endLine) {
result.add(thisComponent = siblingResult);
} else {
thisComponent.appendSibling(siblingResult);
}
prevSibling = sibling;
}
return result;
}
private ChatComponentText toChatTextSingle() {
val thisComponent = new ChatComponentText(text);
val style = new ChatStyle();
if (colorStyle != null) {
style.setColor(colorStyle);
}
for (val fancyStyle : fancyStyles) {
switch (fancyStyle) {
case OBFUSCATED:
style.setObfuscated(true);
break;
case BOLD:
style.setBold(true);
break;
case STRIKETHROUGH:
style.setStrikethrough(true);
break;
case UNDERLINE:
style.setUnderlined(true);
break;
case ITALIC:
style.setItalic(true);
break;
}
}
thisComponent.setChatStyle(style);
return thisComponent;
}
@SideOnly(Side.CLIENT)
public void addChatMessage(EntityOtherPlayerMP target) {
addChatMessage(target::addChatMessage);
}
@SideOnly(Side.CLIENT)
public void addChatMessage(EntityPlayerSP target) {
addChatMessage(target::addChatMessage);
}
public void addChatMessage(CommandBlockLogic target) {
addChatMessage(target::addChatMessage);
}
public void addChatMessage(EntityPlayerMP target) {
addChatMessage(target::addChatMessage);
}
public void addChatMessage(RConConsoleSource target) {
addChatMessage(target::addChatMessage);
}
public void addChatMessage(MinecraftServer target) {
addChatMessage(target::addChatMessage);
}
/**
* {@link #draw(FontRenderer, int, int, boolean)} without drop shadows.
*
* @param renderer The font renderer to use
* @param x Left side
* @param y Top side
*/
@SideOnly(Side.CLIENT)
public void draw(FontRenderer renderer, int x, int y) {
draw(renderer, x, y, false);
}
/**
* Renders this structure as GUI text.
*
* @param renderer The font renderer to use
* @param x Left side
* @param y Top side
* @param shadow Whether to have drop shadow under the text
*/
@SideOnly(Side.CLIENT)
public void draw(FontRenderer renderer, int x, int y, boolean shadow) {
int startX = x;
x = renderer.drawString(text, x, y, colorMap.get(colorStyle).getRGB(), shadow);
if (endLine) {
y += renderer.FONT_HEIGHT;
x = startX;
}
for (val sibling : siblings) {
x = renderer.drawString(sibling.text, x, y, colorMap.get(sibling.colorStyle).getRGB(), shadow);
if (sibling.endLine) {
y += renderer.FONT_HEIGHT;
x = startX;
}
}
}
/**
* {@link #draw(FontRenderer, int, int, boolean)} with drop shadows.
*
* @param renderer The font renderer to use
* @param x Left side
* @param y Top side
*/
@SideOnly(Side.CLIENT)
public void drawWithShadow(FontRenderer renderer, int x, int y) {
draw(renderer, x, y, true);
}
}