-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
NoteManager.java
199 lines (178 loc) · 7.4 KB
/
NoteManager.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
package com.denizenscript.denizencore.objects.notable;
import com.denizenscript.denizencore.DenizenCore;
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.tags.core.EscapeTagBase;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class NoteManager {
public static HashMap<String, Notable> nameToObject = new HashMap<>();
public static HashMap<Notable, String> objectToName = new HashMap<>();
public static HashMap<Class, HashSet<Notable>> notesByType = new HashMap<>();
public static boolean isSaved(Notable object) {
return objectToName.containsKey(object);
}
public static Notable getSavedObject(String id) {
return nameToObject.get(CoreUtilities.toLowerCase(id));
}
public static String getSavedId(Notable object) {
return objectToName.get(object);
}
public static void saveAs(Notable object, String id) {
if (object == null) {
return;
}
id = CoreUtilities.toLowerCase(id);
Notable noted = nameToObject.get(id);
if (noted != null) {
noted.forget();
}
nameToObject.put(id, object);
objectToName.put(object, id);
notesByType.get(object.getClass()).add(object);
}
public static Notable remove(String id) {
id = CoreUtilities.toLowerCase(id);
Notable obj = nameToObject.get(id);
if (obj == null) {
return null;
}
nameToObject.remove(id);
objectToName.remove(obj);
notesByType.get(obj.getClass()).remove(obj);
return obj;
}
public static void remove(Notable obj) {
String id = objectToName.get(obj);
nameToObject.remove(id);
objectToName.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);
}
private static void loadFromConfig() {
nameToObject.clear();
for (Set set : notesByType.values()) {
set.clear();
}
objectToName.clear();
for (StringHolder key : getSaveConfig().getKeys(false)) {
Class<? extends ObjectTag> clazz = namesToTypes.get(key.str);
YamlConfiguration section = getSaveConfig().getConfigurationSection(key.str);
if (section == null) {
continue;
}
for (StringHolder noteNameHolder : section.getKeys(false)) {
String noteName = noteNameHolder.str;
String note = EscapeTagBase.unEscape(noteName.replace("DOT", "."));
String objText;
String flagText = null;
Object rawPart = section.get(noteName);
if (rawPart instanceof YamlConfiguration || rawPart instanceof Map) {
objText = section.getConfigurationSection(noteName).getString("object");
flagText = section.getConfigurationSection(noteName).getString("flags");
}
else {
objText = section.getString(noteName);
}
Notable obj = (Notable) ObjectFetcher.getObjectFrom(clazz, objText, CoreUtilities.errorButNoDebugContext);
if (obj != null) {
obj.makeUnique(note);
if (flagText != null && obj instanceof FlaggableObject) {
SavableMapFlagTracker tracker = new SavableMapFlagTracker(flagText);
if (!CoreConfiguration.skipAllFlagCleanings) {
tracker.doTotalClean();
}
((FlaggableObject) getSavedObject(note)).reapplyTracker(tracker);
}
}
else {
Debug.echoError("Note '" + note + "' failed to load!");
}
}
}
}
private static void saveToConfig() {
YamlConfiguration saveConfig = getSaveConfig();
for (StringHolder key : saveConfig.getKeys(false)) {
saveConfig.set(key.str, null);
}
for (Map.Entry<String, Notable> note : nameToObject.entrySet()) {
try {
saveConfig.set(typesToNames.get(getClass(note.getValue())) + "." + EscapeTagBase.escape(CoreUtilities.toLowerCase(note.getKey())), note.getValue().getSaveObject());
}
catch (Exception e) {
Debug.echoError("Notable '" + note.getKey() + "' failed to save!");
Debug.echoError(e);
}
}
}
private static <T extends Notable> Class<T> getClass(Notable note) {
for (Class clazz : typesToNames.keySet()) {
if (clazz.isInstance(note)) {
return clazz;
}
}
return null;
}
private static YamlConfiguration saveConfig = null;
private static String saveFilePath = null;
public static void reload() {
if (saveFilePath == null) {
saveFilePath = new File(DenizenCore.implementation.getDataFolder(), "notables.yml").getPath();
}
String rawFileData = CoreUtilities.journallingLoadFile(saveFilePath);
saveConfig = rawFileData == null ? new YamlConfiguration() : YamlConfiguration.load(rawFileData);
if (saveConfig == null) {
saveConfig = new YamlConfiguration();
}
loadFromConfig();
}
public static YamlConfiguration getSaveConfig() {
if (saveConfig == null) {
reload();
}
return saveConfig;
}
public static void save() {
if (saveConfig == null || saveFilePath == null) {
return;
}
saveToConfig();
if (nameToObject.isEmpty()) {
if (new File(saveFilePath).exists()) {
new File(saveFilePath).delete();
}
}
else {
CoreUtilities.journallingFileSave(saveFilePath, saveConfig.saveToString(false));
}
}
///////////////////
// Note Annotation Handler
///////////////////
public static Map<Class, String> typesToNames = new HashMap<>();
public static Map<String, Class> namesToTypes = new HashMap<>();
public static void registerObjectTypeAsNotable(Class notable) {
for (Method method : notable.getMethods()) {
if (method.isAnnotationPresent(Note.class)) {
String note = method.getAnnotation(Note.class).value();
typesToNames.put(notable, note);
namesToTypes.put(note, notable);
notesByType.put(notable, new HashSet<>());
}
}
}
}