/
SentinelTargetLabel.java
257 lines (236 loc) · 7.47 KB
/
SentinelTargetLabel.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
package org.mcmonkey.sentinel.targeting;
import org.bukkit.ChatColor;
import org.mcmonkey.sentinel.SentinelPlugin;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
/**
* Helper for prefix labeled targets.
*/
public class SentinelTargetLabel {
/**
* The label prefix.
*/
public String prefix;
/**
* The target value.
*/
public String value;
/**
* Whether this label is regex based.
*/
public boolean isRegex;
/**
* Prefixes that need regex validation.
*/
public static HashSet<String> regexPrefixes = new HashSet<>(
Arrays.asList("player", "npc", "entityname", "helditem"));
/**
* All default prefixes (anything else handled by an integration object).
*/
public static HashSet<String> corePrefixes = new HashSet<>(
Arrays.asList("player", "npc", "entityname", "helditem", "group", "event", "multi", "allinone")
);
/**
* Helper to ensure code won't be optimized away (Java isn't likely to do this anyway, but just in case).
*/
public static long ignoreMe = 0;
/**
* Returns whether the RegEx is valid (or the target is not a RegEx target).
*/
public boolean isValidRegex() {
try {
if (isRegex && "Sentinel".matches(value)) {
ignoreMe++;
}
return true;
}
catch (Exception e) {
return false;
}
}
/**
* Returns whether the target is valid.
* True for all prefixed targets.
*/
public boolean isValidTarget() {
if (prefix != null) {
return true;
}
return SentinelTarget.forName(value) != null;
}
/**
* Returns whether the prefix is valid - if 'false', the prefix doesn't exist.
* True for all non-prefixed targets.
*/
public boolean isValidPrefix() {
if (prefix == null) {
return true;
}
if (corePrefixes.contains(prefix)) {
return true;
}
if (SentinelPlugin.integrationPrefixMap.containsKey(prefix)) {
return true;
}
return false;
}
/**
* Gets the target collection from a list set for this label.
*/
public Collection<String> getTargetsList(SentinelTargetList listSet) {
if (prefix == null) {
return listSet.targets;
}
else if (prefix.equals("player")) {
return listSet.byPlayerName;
}
else if (prefix.equals("npc")) {
return listSet.byNpcName;
}
else if (prefix.equals("entityname")) {
return listSet.byEntityName;
}
else if (prefix.equals("helditem")) {
return listSet.byHeldItem;
}
else if (prefix.equals("group")) {
return listSet.byGroup;
}
else if (prefix.equals("event")) {
return listSet.byEvent;
}
else if (prefix.equals("multi")) {
return null;
}
else if (prefix.equals("allinone")) {
return null;
}
else {
return listSet.byOther;
}
}
private static SentinelTargetList helperList = new SentinelTargetList();
/**
* Gets the addable string for this label.
*/
public String addable() {
if (getTargetsList(helperList) == helperList.byOther) {
return prefix + ":" + value;
}
return value;
}
/**
* Returns whether the label is a valid multi-target.
* True for all non-multi targets.
*/
public boolean isValidMulti() {
if (prefix == null) {
return true;
}
if (prefix.equals("multi")) {
return getMulti(",").totalTargetsCount() > 0;
}
else if (prefix.equals("allinone")) {
return getMulti("|").totalTargetsCount() > 0;
}
return true;
}
/**
* Gets the TargetList created by this multi-target label.
*/
public SentinelTargetList getMulti(String splitter) {
SentinelTargetList newList = new SentinelTargetList();
for (String str : value.split(splitter)) {
SentinelTargetLabel label = new SentinelTargetLabel(str);
label.addToList(newList, false);
}
newList.recalculateCacheNoClear();
return newList;
}
/**
* Adds this target label to a list set.
*/
public boolean addToList(SentinelTargetList listSet) {
return addToList(listSet, true);
}
/**
* Adds this target label to a list set.
*/
public boolean addToList(SentinelTargetList listSet, boolean doRecache) {
if (prefix != null && prefix.equals("multi")) {
listSet.byMultiple.add(getMulti(","));
return true;
}
if (prefix != null && prefix.equals("allinone")) {
listSet.byAllInOne.add(getMulti("\\|"));
return true;
}
Collection<String> list = getTargetsList(listSet);
String addable = addable();
if (doRecache && list.contains(addable)) {
return false;
}
getTargetsList(listSet).add(addable());
if (doRecache && (list == listSet.targets || list == listSet.byOther)) {
listSet.recalculateTargetsCache();
}
return true;
}
/**
* Removes this target label from a list set.
*/
public boolean removeFromList(SentinelTargetList listSet) {
if (prefix != null && prefix.equals("multi")) {
try {
int integerValue = Integer.parseInt(value);
if (integerValue >= 0 && integerValue < listSet.byMultiple.size()) {
listSet.byMultiple.remove(integerValue);
return true;
}
return false;
}
catch (NumberFormatException ex) {
return false;
}
}
if (prefix != null && prefix.equals("allinone")) {
try {
int integerValue = Integer.parseInt(value);
if (integerValue >= 0 && integerValue < listSet.byAllInOne.size()) {
listSet.byAllInOne.remove(integerValue);
return true;
}
return false;
}
catch (NumberFormatException ex) {
return false;
}
}
Collection<String> list = getTargetsList(listSet);
String addable = addable();
if (!list.contains(addable)) {
return false;
}
getTargetsList(listSet).remove(addable());
if (list == listSet.targets || list == listSet.byOther) {
listSet.recalculateTargetsCache();
}
return true;
}
/**
* Constructs the target label instance.
*/
public SentinelTargetLabel(String label) {
int index = label.indexOf(':');
if (index >= 0) {
prefix = label.substring(0, index).toLowerCase();
value = label.substring(index + 1);
isRegex = regexPrefixes.contains(prefix);
value = ChatColor.translateAlternateColorCodes('&', value);
}
else {
value = label.toUpperCase();
}
}
}