/
ChatMessage.java
189 lines (169 loc) · 5.33 KB
/
ChatMessage.java
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
package io.github.bananapuncher714.brickboard.api.chat;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.ChatColor;
import io.github.bananapuncher714.brickboard.util.MessageUtil;
/**
* Parallel implementation of vanilla style JSON formatting, except simpler
*
* @author BananaPuncher714
*/
public class ChatMessage implements Cloneable, Serializable {
private final static char PLACEHOLDER = '\u02A7';
private final List< ChatComponent > components = new ArrayList< ChatComponent >();
public List< ChatComponent > getComponents() {
return components;
}
public ChatMessage addComponent( ChatComponent component ) {
if ( component != null ) {
components.add( component );
}
return this;
}
public ChatMessage addComponents( String message ) {
ChatMessage cm = getMessageFromString( message );
components.addAll( cm.getComponents() );
return this;
}
public ChatMessage merge( ChatMessage... toMerge ) {
for ( ChatMessage message : toMerge ) {
if ( message == null ) {
continue;
}
components.addAll( message.clone().components );
}
return this;
}
public ChatMessage merge( Collection< ChatMessage > toMerge ) {
for ( ChatMessage message : toMerge ) {
if ( message == null ) {
continue;
}
components.addAll( message.clone().components );
}
return this;
}
public String getMessage() {
StringBuilder builder = new StringBuilder();
for ( ChatComponent component : components ) {
builder.append( component.getLegacyText() );
}
return builder.toString();
}
@Override
public ChatMessage clone() {
ChatMessage message = new ChatMessage();
for ( ChatComponent component : components ) {
message.addComponent( component.clone() );
}
return message;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for ( ChatComponent component : components ) {
builder.append( component.toString() );
}
return builder.toString();
}
public static ChatMessage getMessageFromString( String message ) {
return getMessageFromString( message, false );
}
public static ChatMessage getMessageFromString( String message, boolean readExtra ) {
ChatMessage chatMessage = new ChatMessage();
if ( message == null ) {
return chatMessage;
}
ChatComponent last = new ChatComponent( "" );
List< ChatMessage > actions = new ArrayList< ChatMessage >();
if ( readExtra ) {
// Thanks to StarShadow#3546 for negative look behind
List< String > caught = MessageUtil.getMatches( message, "<(.+?)(?<!\\\\)>" );
for ( String action : caught ) {
String hover = MessageUtil.getMatch( action, "\\{(.+?)(?<!\\\\)\\}" );
String click = MessageUtil.getMatch( action, "\\((.+?\\:.+?)(?<!\\\\)\\)" );
if ( hover == null && click == null ) {
// Requires a click or hover
continue;
}
String desc = action;
HoverAction hAction = null;
ClickAction cAction = null;
if ( click != null ) {
String[] clickSplit = click.split( "\\:", 2 );
ClickAction.Action cActionAction;
try {
cActionAction = ClickAction.Action.valueOf( clickSplit[ 0 ].toUpperCase() );
} catch ( IllegalArgumentException exception ) {
cActionAction = null;
}
if ( cActionAction == null ) {
if ( hover == null ) {
continue;
}
} else {
cAction = new ClickAction( cActionAction, clickSplit[ 1 ] );
desc = desc.replace( "(" + click + ")", "" );
}
}
if ( hover != null ) {
hAction = new HoverAction( HoverAction.Action.SHOW_TEXT, hover );
desc = desc.replace( "{" + hover + "}", "" );
}
ChatMessage description = ChatMessage.getMessageFromString( desc, false );
for ( ChatComponent component : description.components ) {
component.setHoverAction( hAction );
component.setClickAction( cAction );
}
message = message.replace( "<" + action + ">", "" + ChatColor.COLOR_CHAR + PLACEHOLDER );
actions.add( description );
}
}
// More efficient conversion
String[] parts = ( " " + message ).split( "" + ChatColor.COLOR_CHAR );
for ( String part : parts ) {
if ( part.isEmpty() ) {
continue;
}
char colorCharacter = part.charAt( 0 );
if ( colorCharacter == PLACEHOLDER ) {
chatMessage.merge( actions.remove( 0 ) );
} else {
ChatColor color = ChatColor.getByChar( colorCharacter );
if ( color != null ) {
if ( color.isColor() ) {
last.clearFormatting();
last.setColor( color );
} else {
if ( color == ChatColor.BOLD ) {
last.setBold( true );
}
if ( color == ChatColor.ITALIC ) {
last.setItalic( true );
}
if ( color == ChatColor.UNDERLINE ) {
last.setUnderline( true );
}
if ( color == ChatColor.MAGIC ) {
last.setRandom( true );
}
if ( color == ChatColor.RESET ) {
last.clearFormatting();
}
if ( color == ChatColor.STRIKETHROUGH ) {
last.setStrikethrough( true );
}
}
}
}
if ( part.length() > 1 ) {
last.setText( part.substring( 1 ) );
chatMessage.addComponent( last );
last = last.clone();
}
}
return chatMessage;
}
}