ChatUtilities.java
3.97 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
package com.mumfrey.liteloader.util;
import java.util.List;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
/**
* Utility functions for chat
*
* @author Adam Mummery-Smith
*/
public abstract class ChatUtilities
{
private static String formattingCodeLookup;
static
{
StringBuilder formattingCodes = new StringBuilder();
for (EnumChatFormatting chatFormat : EnumChatFormatting.values())
{
formattingCodes.append(chatFormat.toString().charAt(1));
}
ChatUtilities.formattingCodeLookup = formattingCodes.toString();
}
private ChatUtilities() {}
/**
* Get a chat style from a legacy formatting code
*
* @param code Code
* @return chat style
*/
public static ChatStyle getChatStyleFromCode(char code)
{
int pos = ChatUtilities.formattingCodeLookup.indexOf(code);
if (pos < 0) return null;
EnumChatFormatting format = EnumChatFormatting.values()[pos];
ChatStyle style = new ChatStyle();
if (format.isColor())
{
style.setColor(format);
}
else if (format.isFancyStyling())
{
switch (format)
{
case BOLD: style.setBold(true); break;
case ITALIC: style.setItalic(true); break;
case STRIKETHROUGH: style.setStrikethrough(true); break;
case UNDERLINE: style.setUnderlined(true); break;
case OBFUSCATED: style.setObfuscated(true); break;
default: return style;
}
}
return style;
}
/**
* Convert a component containing text formatted with legacy codes to a
* native ChatComponent structure.
*/
public static IChatComponent convertLegacyCodes(IChatComponent chat)
{
return ChatUtilities.covertCodesInPlace(chat);
}
private static List<IChatComponent> covertCodesInPlace(List<IChatComponent> siblings)
{
for (int index = 0; index < siblings.size(); index++)
{
siblings.set(index, ChatUtilities.covertCodesInPlace(siblings.get(index)));
}
return siblings;
}
private static IChatComponent covertCodesInPlace(IChatComponent component)
{
IChatComponent newComponent = null;
if (component instanceof ChatComponentText)
{
ChatComponentText textComponent = (ChatComponentText)component;
ChatStyle style = textComponent.getChatStyle();
String text = textComponent.getChatComponentText_TextValue();
int pos = text.indexOf('\247');
while (pos > -1 && text != null)
{
if (pos < text.length() - 1)
{
IChatComponent head = new ChatComponentText(pos > 0 ? text.substring(0, pos) : "").setChatStyle(style);
style = ChatUtilities.getChatStyleFromCode(text.charAt(pos + 1));
text = text.substring(pos + 2);
newComponent = (newComponent == null) ? head : newComponent.appendSibling(head);
pos = text.indexOf('\247');
}
else
{
text = null;
}
}
if (text != null)
{
IChatComponent tail = new ChatComponentText(text).setChatStyle(style);
newComponent = (newComponent == null) ? tail : newComponent.appendSibling(tail);
}
}
if (newComponent == null)
{
ChatUtilities.covertCodesInPlace(component.getSiblings());
return component;
}
for (IChatComponent oldSibling : ChatUtilities.covertCodesInPlace((List<IChatComponent>)component.getSiblings()))
{
newComponent.appendSibling(oldSibling);
}
return newComponent;
}
}