-
Notifications
You must be signed in to change notification settings - Fork 21
/
AbstractConfig.java
152 lines (129 loc) · 3.73 KB
/
AbstractConfig.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
package cn.jiongjionger.neverlag.config;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.IOException;
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
/**
* @author andylizi
*/
public abstract class AbstractConfig {
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
protected @interface F {
String value();
}
protected Logger logger = Bukkit.getLogger();
protected FileConfiguration config;
protected Map<String, Field> fields;
public AbstractConfig(FileConfiguration config) {
this.config = config;
}
protected AbstractConfig(FileConfiguration config, Logger logger) {
this(config);
this.logger = logger;
}
protected boolean checkValue(String key, Object value) {
return true;
}
protected Map<String, Field> getConfigFields() {
if (fields != null) {
return fields;
}
Class<? extends AbstractConfig> clazz = getClass();
Map<String, Field> map = new LinkedHashMap<>();
for (Field field : clazz.getFields()) {
F info = field.getAnnotation(F.class);
if (info == null) {
continue;
}
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) {
logger.log(Level.WARNING, "Invalid config field: {0}", field.toGenericString());
continue;
}
field.setAccessible(true);
map.put(info.value(), field);
}
return fields = Collections.unmodifiableMap(map);
}
public void load() {
try {
load0();
} catch (IOException ex) {
logger.log(Level.WARNING, "Unable to load config", ex);
}
for (Entry<String, Field> entry : getConfigFields().entrySet()) {
String key = entry.getKey();
Field f = entry.getValue();
try {
Object def = f.get(this);
if (!config.contains(key)) {
config.addDefault(key, def);
return;
}
Object v = loadData(key, def);
try {
if (!checkValue(key, v)) {
logger.log(Level.WARNING, "Illegal value \"{0}\" of {1}, use default \"{2}\"!",
new Object[]{ v, key, def });
return;
}
} catch (Exception ex) {
LogRecord record = new LogRecord(Level.WARNING, "Illegal value \"{0}\" of {1}, use default \"{2}\"!");
record.setParameters(new Object[]{ v, key, def });
record.setThrown(ex);
logger.log(record);
}
f.set(this, v);
} catch (ReflectiveOperationException ex) {
LogRecord record = new LogRecord(Level.WARNING, "Unable to update config field:{0}");
record.setParameters(new Object[]{ f.toGenericString() });
record.setThrown(ex);
logger.log(record);
} catch (Exception ex) {
logger.log(Level.WARNING, key, ex);
}
}
}
protected abstract void load0() throws IOException;
protected Object loadData(String key, Object def) {
return config.get(key, def);
}
public void reload() {
load();
save();
}
public void save() {
for (Entry<String, Field> entry : getConfigFields().entrySet()) {
String key = entry.getKey();
Field f = entry.getValue();
try {
saveData(key, f.get(this));
} catch (ReflectiveOperationException ex) {
LogRecord record = new LogRecord(Level.WARNING, "Unable to update config field:{0}");
record.setParameters(new Object[]{ f.toGenericString() });
record.setThrown(ex);
logger.log(record);
}
}
try {
save0();
} catch (IOException ex) {
logger.log(Level.WARNING, "Unable to save config", ex);
}
}
protected abstract void save0() throws IOException;
protected void saveData(String key, Object value) {
config.set(key, value);
}
}