-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
NotableManager.java
225 lines (200 loc) · 7.93 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
package com.denizenscript.denizen.objects.notable;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.Note;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
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.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NotableManager {
public NotableManager() {
registerWithNotableManager(CuboidTag.class);
registerWithNotableManager(EllipsoidTag.class);
registerWithNotableManager(InventoryTag.class);
registerWithNotableManager(ItemTag.class);
registerWithNotableManager(LocationTag.class);
registerWithNotableManager(PolygonTag.class);
}
public static HashMap<String, Notable> notableObjects = new HashMap<>();
public static HashMap<Notable, String> reverseObjects = new HashMap<>();
public static HashMap<Class, HashSet<Notable>> notesByType = new HashMap<>();
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;
}
return notableObjects.get(id) == object;
}
public static Notable getSavedObject(String id) {
return notableObjects.get(CoreUtilities.toLowerCase(id));
}
public static String getSavedId(Notable object) {
return reverseObjects.get(object);
}
public static void saveAs(Notable object, String id) {
if (object == null) {
return;
}
id = CoreUtilities.toLowerCase(id);
notableObjects.put(id, object);
reverseObjects.put(object, id);
notesByType.get(object.getClass()).add(object);
}
public static Notable remove(String id) {
id = CoreUtilities.toLowerCase(id);
Notable obj = notableObjects.get(id);
if (obj == null) {
return null;
}
notableObjects.remove(id);
reverseObjects.remove(obj);
notesByType.get(obj.getClass()).remove(obj);
return obj;
}
public static void remove(Notable obj) {
String id = reverseObjects.get(obj);
notableObjects.remove(id);
reverseObjects.remove(obj);
notesByType.get(obj.getClass()).remove(obj);
}
public static <T extends Notable> Set<T> getAllType(Class<T> type) {
return (Set<T>) notesByType.get(type);
}
/**
* Called on '/denizen reload notables'.
*/
private static void _recallNotables() {
notableObjects.clear();
for (Set set : notesByType.values()) {
set.clear();
}
reverseObjects.clear();
// Find each type of notable
for (String key : Denizen.getInstance().notableManager().getNotables().getKeys(false)) {
Class<? extends ObjectTag> clazz = reverse_objects.get(key);
ConfigurationSection section = Denizen.getInstance().notableManager().getNotables().getConfigurationSection(key);
if (section == null) {
continue;
}
for (String notableRaw : section.getKeys(false)) {
String notable = EscapeTagBase.unEscape(notableRaw.replace("DOT", "."));
String objText;
String flagText = null;
if (section.isConfigurationSection(notableRaw)) {
objText = section.getConfigurationSection(notableRaw).getString("object");
flagText = section.getConfigurationSection(notableRaw).getString("flags");
}
else {
objText = section.getString(notableRaw);
}
Notable obj = (Notable) ObjectFetcher.getObjectFrom(clazz, objText, CoreUtilities.errorButNoDebugContext);
if (obj != null) {
obj.makeUnique(notable);
obj = getSavedObject(notable);
if (flagText != null && obj instanceof FlaggableObject) {
((FlaggableObject) obj).reapplyTracker(new SavableMapFlagTracker(flagText));
}
}
else {
Debug.echoError("Notable '" + notable + "' failed to load!");
}
}
}
}
/**
* Called on by '/denizen save'.
*/
private static void _saveNotables() {
FileConfiguration notables = Denizen.getInstance().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())) + "." + EscapeTagBase.escape(CoreUtilities.toLowerCase(notable.getKey())), notable.getValue().getSaveObject());
}
catch (Exception e) {
Debug.echoError("Notable '" + notable.getKey() + "' failed to save!");
Debug.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(Denizen.getInstance().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);
notesByType.put(notable, new HashSet<>());
}
}
}
public static String getClassId(Class notable) {
return objects.get(notable);
}
public static Map<String, Class> getReverseClassIdMap() {
return reverse_objects;
}
}