/
TextElement.java
318 lines (287 loc) · 8.97 KB
/
TextElement.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
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
package net.ME1312.Galaxi.Log;
import net.ME1312.Galaxi.Library.Config.YAMLSection;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Util;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* Text Element Builder Class
*/
public class TextElement {
protected final LinkedList<TextElement> before = new LinkedList<TextElement>();
protected final LinkedList<TextElement> prepend = new LinkedList<TextElement>();
protected final LinkedList<TextElement> append = new LinkedList<TextElement>();
protected final LinkedList<TextElement> after = new LinkedList<TextElement>();
protected final ObjectMap<String> element;
/**
* Create a new Text Element
*/
public TextElement() {
this((String) null);
}
/**
* Create a new Text Element
*
* @param text Text
*/
public TextElement(String text) {
this(generate(text));
}
private static ObjectMap<String> generate(String text) {
ObjectMap<String> element = new ObjectMap<String>();
if (text != null) element.set("msg", text);
return element;
}
/**
* Load a Text Element
*
* @param element Raw Element
*/
public TextElement(ObjectMap<String> element) {
Util.nullpo(element);
this.element = element;
load();
}
/**
* Decode your text here
*/
protected void load() {
for (ObjectMap<String> e : element.getMapList("before", new LinkedList<ObjectMap<String>>())) before.add(new TextElement(e));
for (ObjectMap<String> e : element.getMapList("pre", new LinkedList<ObjectMap<String>>())) prepend.add(new TextElement(e));
for (ObjectMap<String> e : element.getMapList("post", new LinkedList<ObjectMap<String>>())) append.add(new TextElement(e));
for (ObjectMap<String> e : element.getMapList("after", new LinkedList<ObjectMap<String>>())) after.add(new TextElement(e));
}
/**
* Get the message
*
* @return Message
*/
public String message() {
return element.getString("msg");
}
/**
* Set whether the text will be bold
*
* @param value Bold Status
* @return Text Element
*/
public TextElement bold(boolean value) {
element.set("b", value);
return this;
}
/**
* Whether the text will be bold
*
* @return Bold Status
*/
public boolean bold() {
return element.getBoolean("b", false);
}
/**
* Set whether the text will be italic
*
* @param value Italic Status
* @return Text Element
*/
public TextElement italic(boolean value) {
element.set("i", value);
return this;
}
/**
* Whether the text will be italic
*
* @return Italic Status
*/
public boolean italic() {
return element.getBoolean("i", false);
}
/**
* Set whether the text will be underlined
*
* @param value Underline Status
* @return Text Element
*/
public TextElement underline(boolean value) {
element.set("u", value);
return this;
}
/**
* Whether the text will be underlined
*
* @return Bold Status
*/
public boolean underline() {
return element.getBoolean("u", false);
}
/**
* Set whether the text will be struck through
*
* @param value Strike through Status
* @return Text Element
*/
public TextElement strikethrough(boolean value) {
element.set("s", value);
return this;
}
/**
* Whether the text will be struck through
*
* @return Strike through Status
*/
public boolean strikethrough() {
return element.getBoolean("s", false);
}
/**
* Set the color of the text
*
* @param color Text Color (or null for default)
* @return Text Element
*/
public TextElement color(Color color) {
if (color == null) {
element.set("c", null);
} else {
YAMLSection c = new YAMLSection();
c.set("r", color.getRed());
c.set("g", color.getGreen());
c.set("b", color.getBlue());
c.set("a", color.getAlpha());
element.set("c", c);
}
return this;
}
/**
* Get the color of the text
*
* @return Text Color (or null for default)
*/
public Color color() {
if (element.getObject("c", null) == null) {
return null;
} else {
ObjectMap<String> c = element.getMap("c");
return new Color(c.getInt("r"), c.getInt("g"), c.getInt("b"), c.getInt("a"));
}
}
/**
* Place elements before this element
*
* @param elements Elements to prepend
* @return Text Element
*/
public TextElement before(TextElement... elements) {
for (int i = elements.length; i > 0;) before.addFirst(elements[--i]);
return this;
}
/**
* Place elements inside this element, but in front of the text
*
* @param elements Elements to insert
* @return Text Element
*/
public TextElement prepend(TextElement... elements) {
for (int i = elements.length; i > 0;) prepend.addFirst(elements[--i]);
return this;
}
/**
* Add additional text before the existing text
*
* @param text Text to add
* @return Text Element
*/
public TextElement prepend(String... text) {
StringBuilder msg = new StringBuilder(element.getString("msg"));
for (int i = text.length; i > 0;) msg.insert(0, text[--i]);
element.set("msg", msg.toString());
return this;
}
/**
* Add additional text behind the existing text
*
* @param text Text to add
* @return Text Element
*/
public TextElement append(String... text) {
StringBuilder msg = new StringBuilder(element.getString("msg"));
for (String s : text) msg.append(s);
element.set("msg", msg.toString());
return this;
}
/**
* Place elements inside this element, but behind the text
*
* @param elements Elements to insert
* @return Text Element
*/
public TextElement append(TextElement... elements) {
append.addAll(Arrays.asList(elements));
return this;
}
/**
* Place elements after this element
*
* @param elements Elements to append
* @return Text Element
*/
public TextElement after(TextElement... elements) {
after.addAll(Arrays.asList(elements));
return this;
}
/**
* Get the Raw Element
*
* @return Raw Element
*/
public ObjectMap<String> toRaw() {
return toRaw(new ArrayList<TextElement>());
}
/**
* Encode your text here
*
* @param stack Text Elements we've already seen before
* @return Encoded Text
*/
protected ObjectMap<String> toRaw(List<TextElement> stack) {
stack.add(this);
LinkedList<ObjectMap<String>> before = new LinkedList<ObjectMap<String>>();
for (TextElement e : this.before) {
if (stack.contains(e)) throw new IllegalStateException("Infinite text before loop");
List<TextElement> p = new ArrayList<TextElement>();
p.addAll(stack);
before.add(e.toRaw(p));
}
if (!before.isEmpty()) element.set("before", before);
else if (element.contains("before")) element.remove("before");
LinkedList<ObjectMap<String>> prepend = new LinkedList<ObjectMap<String>>();
for (TextElement e : this.prepend) {
if (stack.contains(e)) throw new IllegalStateException("Infinite text prepend loop");
List<TextElement> p = new ArrayList<TextElement>();
p.addAll(stack);
prepend.add(e.toRaw(p));
}
if (!prepend.isEmpty()) element.set("pre", prepend);
else if (element.contains("pre")) element.remove("pre");
LinkedList<ObjectMap<String>> append = new LinkedList<ObjectMap<String>>();
for (TextElement e : this.append) {
if (stack.contains(e)) throw new IllegalStateException("Infinite text append loop");
List<TextElement> p = new ArrayList<TextElement>();
p.addAll(stack);
append.add(e.toRaw(p));
}
if (!append.isEmpty()) element.set("post", append);
else if (element.contains("post")) element.remove("post");
LinkedList<ObjectMap<String>> after = new LinkedList<ObjectMap<String>>();
for (TextElement e : this.after) {
if (stack.contains(e)) throw new IllegalStateException("Infinite text after loop");
List<TextElement> p = new ArrayList<TextElement>();
p.addAll(stack);
after.add(e.toRaw(p));
}
if (!after.isEmpty()) element.set("after", after);
else if (element.contains("after")) element.remove("after");
return element.clone();
}
}