/
NotableManager.java
266 lines (216 loc) · 8.31 KB
/
NotableManager.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
package com.denizenscript.denizen.objects.notable;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.dObject;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.Note;
import com.denizenscript.denizencore.tags.core.EscapeTags;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NotableManager {
public NotableManager() {
registerWithNotableManager(dCuboid.class);
registerWithNotableManager(dEllipsoid.class);
registerWithNotableManager(dInventory.class);
registerWithNotableManager(dItem.class);
registerWithNotableManager(dLocation.class);
}
public static Map<String, Notable>
notableObjects = new ConcurrentHashMap<>();
public static Map<String, Class>
typeTracker = new ConcurrentHashMap<>();
public static Map<Notable, String>
reverseObjects = new ConcurrentHashMap<>();
public static boolean isSaved(String id) {
return notableObjects.containsKey(CoreUtilities.toLowerCase(id));
}
public static boolean isSaved(Notable object) {
return reverseObjects.containsKey(object);
}
public static boolean isExactSavedObject(Notable object) {
String id = reverseObjects.get(object);
if (id == null) {
return false;
}
// Reference equality
return notableObjects.get(id) == object;
}
public static Notable getSavedObject(String id) {
if (notableObjects.containsKey(CoreUtilities.toLowerCase(id))) {
return notableObjects.get(CoreUtilities.toLowerCase(id));
}
else {
return null;
}
}
public static String getSavedId(Notable object) {
if (reverseObjects.containsKey(object)) {
return reverseObjects.get(object);
}
return null;
}
public static boolean isType(String id, Class type) {
return (typeTracker.containsKey(CoreUtilities.toLowerCase(id)))
&& typeTracker.get(CoreUtilities.toLowerCase(id)) == type;
}
public static void saveAs(Notable object, String id) {
if (object == null) {
return;
}
notableObjects.put(CoreUtilities.toLowerCase(id), object);
reverseObjects.put(object, CoreUtilities.toLowerCase(id));
typeTracker.put(CoreUtilities.toLowerCase(id), object.getClass());
}
public static void remove(String id) {
Notable obj = notableObjects.get(CoreUtilities.toLowerCase(id));
notableObjects.remove(CoreUtilities.toLowerCase(id));
reverseObjects.remove(obj);
typeTracker.remove(CoreUtilities.toLowerCase(id));
}
public static void remove(Notable obj) {
String id = reverseObjects.get(obj);
notableObjects.remove(CoreUtilities.toLowerCase(id));
reverseObjects.remove(obj);
typeTracker.remove(CoreUtilities.toLowerCase(id));
}
public static <T extends dObject> List<T> getAllType(Class<T> type) {
List<T> objects = new ArrayList<>();
for (Map.Entry<String, Notable> notable : notableObjects.entrySet()) {
// dB.log(notable.toString());
if (isType(notable.getKey(), type)) {
objects.add((T) notable.getValue());
}
}
return objects;
}
/**
* Called on '/denizen reload notables'.
*/
private static void _recallNotables() {
notableObjects.clear();
typeTracker.clear();
reverseObjects.clear();
// Find each type of notable
for (String key : DenizenAPI.getCurrentInstance().notableManager().getNotables().getKeys(false)) {
Class<? extends dObject> clazz = reverse_objects.get(key);
ConfigurationSection section = DenizenAPI.getCurrentInstance().notableManager().getNotables()
.getConfigurationSection(key);
if (section == null) {
continue;
}
for (String notableRaw : section.getKeys(false)) {
String notable = EscapeTags.unEscape(notableRaw.replace("DOT", "."));
Notable obj = (Notable) ObjectFetcher.getObjectFrom(clazz, section.getString(notableRaw), Utilities.noDebugContext);
if (obj != null) {
obj.makeUnique(notable);
}
else {
dB.echoError("Notable '" + notable + "' failed to load!");
}
}
}
}
/**
* Called on by '/denizen save'.
*/
private static void _saveNotables() {
FileConfiguration notables = DenizenAPI.getCurrentInstance().notableManager().getNotables();
for (String key : notables.getKeys(false)) {
notables.set(key, null);
}
for (Map.Entry<String, Notable> notable : notableObjects.entrySet()) {
try {
notables.set(getClassId(getClass(notable.getValue())) + "." + EscapeTags.escape(CoreUtilities.toLowerCase(notable.getKey())),
notable.getValue().getSaveObject());
}
catch (Exception e) {
dB.echoError("Notable '" + notable.getKey() + "' failed to save!");
dB.echoError(e);
}
}
}
private static <T extends Notable> Class<T> getClass(Notable notable) {
for (Class clazz : objects.keySet()) {
if (clazz.isInstance(notable)) {
return clazz;
}
}
return null;
}
private FileConfiguration notablesSave = null;
private File notablesFile = null;
/**
* Reloads, retrieves and saves notable information from/to 'notables.yml'.
*/
public void reloadNotables() {
if (notablesFile == null) {
notablesFile = new File(DenizenAPI.getCurrentInstance().getDataFolder(), "notables.yml");
}
notablesSave = YamlConfiguration.loadConfiguration(notablesFile);
// Reload notables from notables.yml
_recallNotables();
}
public FileConfiguration getNotables() {
if (notablesSave == null) {
reloadNotables();
}
return notablesSave;
}
public void saveNotables() {
if (notablesSave == null || notablesFile == null) {
return;
}
try {
// Save notables to notables.yml
_saveNotables();
notablesSave.save(notablesFile);
}
catch (IOException ex) {
Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save to " + notablesFile, ex);
}
}
///////////////////
// Note Annotation Handler
///////////////////
private static Map<Class, String> objects = new HashMap<>();
private static Map<String, Class> reverse_objects = new HashMap<>();
public static void registerWithNotableManager(Class notable) {
for (Method method : notable.getMethods()) {
if (method.isAnnotationPresent(Note.class)) {
String note = method.getAnnotation(Note.class).value();
objects.put(notable, note);
reverse_objects.put(note, notable);
}
}
}
public static boolean canFetch(Class notable) {
return objects.containsKey(notable);
}
public static String getClassId(Class notable) {
if (canFetch(notable)) {
return objects.get(notable);
}
else {
return null;
}
}
public static Map<String, Class> getReverseClassIdMap() {
return reverse_objects;
}
}