/
ConsoleText.java
244 lines (216 loc) · 6.29 KB
/
ConsoleText.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
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.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.UUID;
/**
* Console Text Element Builder Class
*/
public class ConsoleText extends TextElement {
private static final HashMap<String, Runnable> callbacks = new HashMap<String, Runnable>();
/**
* Create a new Text Element
*/
public ConsoleText() {}
/**
* Create a new Text Element
*
* @param text Text
*/
public ConsoleText(String text) {
super(text);
}
@Override
protected void load() {
for (ObjectMap<String> e : element.getMapList("before", new LinkedList<ObjectMap<String>>())) before.add(new ConsoleText(e));
for (ObjectMap<String> e : element.getMapList("pre", new LinkedList<ObjectMap<String>>())) prepend.add(new ConsoleText(e));
for (ObjectMap<String> e : element.getMapList("post", new LinkedList<ObjectMap<String>>())) append.add(new ConsoleText(e));
for (ObjectMap<String> e : element.getMapList("after", new LinkedList<ObjectMap<String>>())) after.add(new ConsoleText(e));
}
/**
* Load a Text Element
*
* @param element Raw Element
*/
public ConsoleText(ObjectMap<String> element) {
super(element);
}
@Override
public ConsoleText bold(boolean value) {
return (ConsoleText) super.bold(value);
}
@Override
public ConsoleText italic(boolean value) {
return (ConsoleText) super.italic(value);
}
@Override
public ConsoleText underline(boolean value) {
return (ConsoleText) super.underline(value);
}
@Override
public ConsoleText strikethrough(boolean value) {
return (ConsoleText) super.strikethrough(value);
}
/**
* Set whether the text will be in superscript
*
* @param value Superscript Status
* @return Text Element
*/
public ConsoleText superscript(boolean value) {
if (value && subscript()) subscript(false);
element.set("sup", value);
return this;
}
/**
* Whether the text will be in superscript
*
* @return Superscript Status
*/
public boolean superscript() {
return element.getBoolean("sup", false);
}
/**
* Set whether the text will be in subscript
*
* @param value Subscript Status
* @return Text Element
*/
public ConsoleText subscript(boolean value) {
if (value && superscript()) superscript(false);
element.set("sub", value);
return this;
}
/**
* Whether the text will be in subscript
*
* @return Subscript Status
*/
public boolean subscript() {
return element.getBoolean("sub", false);
}
/**
* Set the color of the text
*
* @param color 8-bit Text Color
* @return Text Element
*/
public ConsoleText color(int color) {
if (Logger.writer != null) {
color(Logger.writer.parse256(color));
element.getMap("c").set("8", color);
}
return this;
}
@Override
public ConsoleText color(Color color) {
return (ConsoleText) super.color(color);
}
/**
* Set the background color of the text
*
* @param color 8-bit Background Color
* @return Text Element
*/
public ConsoleText backgroundColor(int color) {
if (Logger.writer != null) {
backgroundColor(Logger.writer.parse256(color));
element.getMap("bc").set("8", color);
}
return this;
}
/**
* Set the background color of the text
*
* @param color Text Background Color (or null for default)
* @return Text Element
*/
public ConsoleText backgroundColor(Color color) {
if (color == null) {
element.set("bc", null);
} else {
YAMLSection bc = new YAMLSection();
bc.set("r", color.getRed());
bc.set("g", color.getGreen());
bc.set("b", color.getBlue());
bc.set("a", color.getAlpha());
element.set("bc", bc);
}
return this;
}
/**
* Get the background color of the text
*
* @return Text Background Color (or null for default)
*/
public Color backgroundColor() {
if (element.getObject("bc", null) == null) {
return null;
} else {
ObjectMap<String> bc = element.getMap("bc");
return new Color(bc.getInt("r"), bc.getInt("g"), bc.getInt("b"), bc.getInt("a"));
}
}
/**
* Run some code on click
*
* @param value Code to run
* @return Text Element
*/
public ConsoleText onClick(Runnable value) {
String id;
if (callbacks.values().contains(value)) {
id = Util.getBackwards(callbacks, value).get(0);
} else {
id = Util.getNew(callbacks.keySet(), UUID::randomUUID).toString();
callbacks.put(id, value);
}
element.set("a", "mailto:execute@galaxi.engine?" + id);
return this;
}
/**
* Open a link on click
*
* @param value Link to open
* @return Text Element
*/
public ConsoleText onClick(URL value) {
element.set("a", value);
return this;
}
/**
* Get the link that will open on click
*
* @return Link to open
*/
public URL onClick() {
try {
if (element.contains("a")) {
return new URL(element.getRawString("a"));
} else return null;
} catch (MalformedURLException e) {
return null;
}
}
@Override
public ConsoleText before(TextElement... elements) {
return (ConsoleText) super.before(elements);
}
@Override
public ConsoleText prepend(TextElement... elements) {
return (ConsoleText) super.prepend(elements);
}
@Override
public ConsoleText append(TextElement... elements) {
return (ConsoleText) super.append(elements);
}
@Override
public ConsoleText after(TextElement... elements) {
return (ConsoleText) super.after(elements);
}
}