-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
SentinelTargetLabel.java
179 lines (161 loc) · 4.86 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
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")
);
/**
* 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.
*/
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 {
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;
}
/**
* Adds this target label to a list set.
*/
public boolean addToList(SentinelTargetList listSet) {
Collection<String> list = getTargetsList(listSet);
String addable = addable();
if (list.contains(addable)) {
return false;
}
getTargetsList(listSet).add(addable());
if (list == listSet.targets || list == listSet.byOther) {
listSet.recalculateTargetsCache();
}
return true;
}
/**
* Removes this target label from a list set.
*/
public boolean removeFromList(SentinelTargetList listSet) {
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();
}
}
}