/
dColor.java
163 lines (118 loc) · 3.46 KB
/
dColor.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
package net.aufdemrand.denizen.objects;
import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.aufdemrand.denizen.utilities.Utilities;
import org.bukkit.Color;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
public class dColor implements dObject {
final static Pattern rgbPattern = Pattern.compile("(\\d+),(\\d+),(\\d+)");
//////////////////
// OBJECT FETCHER
////////////////
/**
* Gets a Color Object from a string form.
*
* @param string the string
* @return a Color, or null if incorrectly formatted
*
*/
public static dColor valueOf(String string) {
if (string.toUpperCase().matches("RANDOM")) {
// Get a color using random RGB values
return new dColor(Utilities.getRandom().nextInt(256), Utilities.getRandom().nextInt(256), Utilities.getRandom().nextInt(256));
}
Matcher m = rgbPattern.matcher(string);
if (m.matches()) {
return new dColor(aH.getIntegerFrom(m.group(1)), aH.getIntegerFrom(m.group(2)), aH.getIntegerFrom(m.group(3)));
}
Field colorField = null;
try {
colorField = Color.class.getField(string.toUpperCase());
} catch (SecurityException e1) {
dB.echoError("Security exception getting color field!");
} catch (NoSuchFieldException e1) {
dB.echoError("No such color field!");
}
if (colorField != null) {
return new dColor(colorField);
}
// No match
return null;
}
/**
* Determine whether a string is a valid color.
*
* @param string the string
* @return true if matched, otherwise false
*
*/
public static boolean matches(String arg) {
if (arg.toUpperCase().matches("RANDOM"))
return true;
Matcher m = rgbPattern.matcher(arg);
if (m.matches())
return true;
for (Field field : Color.class.getFields()) {
if (arg.toUpperCase().matches(field.getName())) {
return true;
}
}
return false;
}
///////////////
// Constructors
/////////////
public dColor(int red, int green, int blue) {
color = Color.fromRGB(red, green, blue);
}
public dColor(Field field) {
try {
color = (Color) field.get(null);
} catch (IllegalArgumentException e) {
dB.echoError("Illegal argument for color!");
} catch (IllegalAccessException e) {
dB.echoError("Illegal access for color!");
}
}
public dColor(Color color) {
this.color = color;
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
// Bukkit Color associated
private Color color;
public Color getColor() {
return color;
}
@Override
public String getPrefix() {
return null;
}
@Override
public String debug() {
return null;
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getType() {
return "color";
}
@Override
public String identify() {
return null;
}
@Override
public dObject setPrefix(String prefix) {
return null;
}
@Override
public String getAttribute(Attribute attribute) {
return null;
}
}