/
ColorTag.java
352 lines (304 loc) · 11 KB
/
ColorTag.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
public class ColorTag implements ObjectTag {
// <--[language]
// @name ColorTag Objects
// @group Object System
// @description
// A ColorTag represents an RGB color code.
//
// Note that a ColorTag is NOT a base dye color (used by wool, etc). That is handled by a separate naming system.
//
// These use the object notation "co@".
// The identity format for colors is <red>,<green>,<blue> or the name of a color.
// For example, 'co@50,64,128' or 'co@red'.
//
// A list of accepted color names can be found at
// <@link url https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Color.html>
//
// Red/green/blue values are each from 0 to 255.
//
// -->
//////////////////
// OBJECT FETCHER
////////////////
@Deprecated
public static ColorTag valueOf(String string) {
return valueOf(string, null);
}
public static HashMap<String, Color> colorsByName = new HashMap<>();
static {
for (Field field : Color.class.getDeclaredFields()) {
if (field.getType() == Color.class) {
try {
colorsByName.put(CoreUtilities.toLowerCase(field.getName()), (Color) field.get(null));
}
catch (Exception ex) {
Debug.echoError(ex);
}
}
}
}
/**
* Gets a Color Object from a string form.
*
* @param string the string
* @return a Color, or null if incorrectly formatted
*/
@Fetchable("co")
public static ColorTag valueOf(String string, TagContext context) {
if (string.startsWith("co@")) {
string = string.substring("co@".length());
}
if (string.equals("random")) {
// Get a color using random RGB values
return new ColorTag(CoreUtilities.getRandom().nextInt(256),
CoreUtilities.getRandom().nextInt(256),
CoreUtilities.getRandom().nextInt(256));
}
List<String> split = CoreUtilities.split(string, ',');
if (split.size() == 3) {
if (!ArgumentHelper.matchesInteger(split.get(0)) || !ArgumentHelper.matchesInteger(split.get(1)) || !ArgumentHelper.matchesInteger(split.get(2))) {
return null;
}
return new ColorTag(Integer.parseInt(split.get(0)), Integer.parseInt(split.get(1)), Integer.parseInt(split.get(2)));
}
Color col = colorsByName.get(CoreUtilities.toLowerCase(string));
if (col != null) {
return new ColorTag(col);
}
// No match
return null;
}
/**
* Determines whether a string is a valid color.
*
* @param arg the string
* @return true if matched, otherwise false
*/
public static boolean matches(String arg) {
if (arg.startsWith("co@")) {
return true;
}
if (valueOf(arg, CoreUtilities.noDebugContext) != null) {
return true;
}
return false;
}
///////////////
// Constructors
/////////////
public ColorTag(int red, int green, int blue) {
color = Color.fromRGB(red, green, blue);
}
public ColorTag(Color color) {
this.color = color;
}
public ColorTag(DyeColor dyeColor) {
this.color = dyeColor.getColor();
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
// Associated with Bukkit Color
private Color color;
public Color getColor() {
return color;
}
String prefix = "color";
@Override
public String getPrefix() {
return prefix;
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "Color";
}
@Override
public String identify() {
for (Field field : Color.class.getFields()) {
try {
if (Color.class.isAssignableFrom(field.getType()) && ((Color) field.get(null)).asRGB() == getColor().asRGB()) {
return "co@" + field.getName();
}
}
catch (Exception e) {
Debug.echoError("Exception trying to fetch color: " + e.getClass().getCanonicalName() + ": " + e.getMessage());
}
}
return "co@" + getColor().getRed() + "," + getColor().getGreen() + "," + getColor().getBlue();
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public ObjectTag setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}
public static void registerTags() {
// <--[tag]
// @attribute <ColorTag.red>
// @returns ElementTag(Number)
// @description
// Returns the red value of this color.
// -->
registerTag("red", (attribute, object) -> {
return new ElementTag(object.color.getRed());
});
// <--[tag]
// @attribute <ColorTag.green>
// @returns ElementTag(Number)
// @description
// Returns the green value of this color.
// -->
registerTag("green", (attribute, object) -> {
return new ElementTag(object.color.getGreen());
});
// <--[tag]
// @attribute <ColorTag.blue>
// @returns ElementTag(Number)
// @description
// Returns the blue value of this color.
// -->
registerTag("blue", (attribute, object) -> {
return new ElementTag(object.color.getBlue());
});
// <--[tag]
// @attribute <ColorTag.rgb>
// @returns ElementTag
// @description
// Returns the RGB value of this color.
// EG, 255,0,255
// -->
registerTag("rgb", (attribute, object) -> {
Color color = object.color;
return new ElementTag(color.getRed() + "," + color.getGreen() + "," + color.getBlue());
});
// <--[tag]
// @attribute <ColorTag.hue>
// @returns ElementTag(Number)
// @description
// Returns the hue value of this color.
// -->
registerTag("hue", (attribute, object) -> {
return new ElementTag(object.ToHSB()[0]);
});
// <--[tag]
// @attribute <ColorTag.saturation>
// @returns ElementTag(Number)
// @description
// Returns the saturation value of this color.
// -->
registerTag("saturation", (attribute, object) -> {
return new ElementTag(object.ToHSB()[1]);
});
// <--[tag]
// @attribute <ColorTag.brightness>
// @returns ElementTag(Number)
// @description
// Returns the brightness value of this color.
// -->
registerTag("brightness", (attribute, object) -> {
return new ElementTag(object.ToHSB()[2]);
});
// <--[tag]
// @attribute <ColorTag.hsv>
// @returns ElementTag
// @description
// Returns the HSV value of this color.
// EG, 100,100,255
// -->
registerTag("hsv", (attribute, object) -> {
int[] HSV = object.ToHSB();
return new ElementTag(HSV[1] + "," + HSV[1] + "," + HSV[2]);
});
// <--[tag]
// @attribute <ColorTag.name>
// @returns ElementTag
// @description
// Returns the name of this color (or red,green,blue if none).
// -->
registerTag("name", (attribute, object) -> {
return new ElementTag(object.identify().substring(3));
});
// <--[tag]
// @attribute <ColorTag.mix[<color>]>
// @returns ColorTag
// @description
// Returns the color that results if you mix this color with another.
// -->
registerTag("mix", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag ListTag.insert[...] must have a value.");
return null;
}
ColorTag mixed_with = attribute.contextAsType(1, ColorTag.class);
if (mixed_with != null) {
return new ColorTag(object.color.mixColors(mixed_with.getColor()));
}
else {
Debug.echoError("'" + attribute.getContext(1) + "' is not a valid color!");
return null;
}
});
// <--[tag]
// @attribute <ColorTag.to_particle_offset>
// @returns LocationTag
// @description
// Returns the color as a particle offset, for use with PlayEffect.
// -->
registerTag("to_particle_offset", (attribute, object) -> {
Color valid = object.color;
if (valid.asRGB() == 0) {
valid = Color.fromRGB(1, 0, 0);
}
return new LocationTag(null, valid.getRed() / 255F, valid.getGreen() / 255F, valid.getBlue() / 255F);
});
// <--[tag]
// @attribute <ColorTag.type>
// @returns ElementTag
// @description
// Always returns 'Color' for ColorTag objects. All objects fetchable by the Object Fetcher will return the
// type of object that is fulfilling this attribute.
// -->
registerTag("type", (attribute, object) -> {
return new ElementTag("Color");
});
}
public static ObjectTagProcessor<ColorTag> tagProcessor = new ObjectTagProcessor<>();
public static void registerTag(String name, TagRunnable.ObjectInterface<ColorTag> runnable, String... variants) {
tagProcessor.registerTag(name, runnable, variants);
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
int[] ToHSB() {
float[] base = java.awt.Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
return new int[] {(int) (base[0] * 255), (int) (base[1] * 255), (int) (base[2] * 255)};
}
}