/
NotableManager.java
206 lines (147 loc) · 6.04 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
package net.aufdemrand.denizen.objects.notable;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
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.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NotableManager {
public NotableManager() {
try { _initialize(); } catch (IOException e) {
} catch (ClassNotFoundException e) {
}
}
public static Map<String, Notable>
notableObjects = new ConcurrentHashMap<String, Notable>();
public static Map<String, Class>
typeTracker = new ConcurrentHashMap<String, Class>();
public static Map<Notable, String>
reverseObjects = new ConcurrentHashMap<Notable, String>();
public static boolean isSaved(String id) {
return notableObjects.containsKey(id.toLowerCase());
}
public static boolean isSaved(Notable object) {
return reverseObjects.containsKey(object);
}
public static Notable getSavedObject(String id) {
if (notableObjects.containsKey(id.toLowerCase()))
return notableObjects.get(id.toLowerCase());
else return null;
}
public static String getSavedId(Notable object) {
if (reverseObjects.containsKey(object))
return reverseObjects.get(reverseObjects);
return null;
}
public static boolean isType(String id, Class type) {
return (typeTracker.containsKey(id.toLowerCase())) && typeTracker.get(id.toLowerCase()) == type;
}
public static void saveAs(Notable object, String id) {
if (object == null) return;
notableObjects.put(id.toLowerCase(), object);
reverseObjects.put(object, id.toLowerCase());
typeTracker.put(id.toLowerCase(), object.getClass());
}
public static void remove(String id) {
Notable obj = notableObjects.get(id.toLowerCase());
notableObjects.remove(id.toLowerCase());
reverseObjects.remove(obj);
typeTracker.remove(id.toLowerCase());
}
public static void remove(Notable obj) {
String id = reverseObjects.get(obj);
notableObjects.remove(id.toLowerCase());
reverseObjects.remove(obj);
typeTracker.remove(id.toLowerCase());
}
/*
* Called on '/denizen reload notables'.
*/
private static void _recallNotables() {
// Find each type of notable
for (String keys : DenizenAPI.getCurrentInstance().notableManager().getNotables().getKeys(false)) {
}
}
/*
* Called on by '/denizen save'.
*/
private static void _saveNotables() {
for (Map.Entry<String, Notable> notable : notableObjects.entrySet()) {
// If the object is serializable, save that info... fetching the objects back
// will require this information
if (notable.getValue().getSaveObject() instanceof ConfigurationSerializable)
DenizenAPI.getCurrentInstance().notableManager().getNotables()
.set(((dObject) notable.getValue()).getObjectType().toLowerCase() + "." + "_serializable", true);
DenizenAPI.getCurrentInstance().notableManager().getNotables()
.set(((dObject) notable.getValue()).getObjectType().toLowerCase() + "." + notable.getKey().toLowerCase(),
notable.getValue());
}
}
/*
* Reloads, retrieves and saves notable information from/to 'notables.yml'.
*/
private FileConfiguration notablesSave = null;
private File notablesFile = null;
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<Class, String>();
public static void _initialize() throws IOException, ClassNotFoundException {
objects.clear();
for (Class dClass : notable_objects)
for (Method method : dClass.getMethods())
if (method.isAnnotationPresent(net.aufdemrand.denizen.objects.notable.Note.class)) {
String[] identifiers = method.getAnnotation(net.aufdemrand.denizen.objects.notable.Note.class).value().split(",");
for (String identifer : identifiers)
objects.put(dClass, identifer.trim().toLowerCase());
}
}
private static ArrayList<Class> notable_objects = new ArrayList<Class>();
public static void registerWithNotableManager(Class notable) {
notable_objects.add(notable);
}
public static boolean canFetch(Class notable) {
return objects.containsKey(notable);
}
public static String getClassId(Class notable) {
if (canFetch(notable))
return objects.get(notable).toLowerCase();
else
return null;
}
}