1
1
mirror of https://github.com/geekfrog/PermissionsTime.git synced 2024-11-22 15:48:51 +00:00

配置文件处理

This commit is contained in:
GeekFrog 2017-07-10 11:05:45 +08:00
parent 760f10595f
commit 5400ac92ee
2 changed files with 23 additions and 10 deletions

View File

@ -19,6 +19,7 @@ public class PackagesCfg extends PluginConfig {
@Override @Override
protected void init() { protected void init() {
saveConfig();
} }
@Override @Override

View File

@ -128,27 +128,39 @@ public abstract class PluginConfig {
protected void saveObj(String path, Map<String, ? extends IConfigBean> o) { protected void saveObj(String path, Map<String, ? extends IConfigBean> o) {
for (Entry<String, ? extends IConfigBean> configBean : o.entrySet()) { for (Entry<String, ? extends IConfigBean> configBean : o.entrySet()) {
getConfig().set(path + "." + configBean.getKey(), configBean.getValue().toConfig()); saveObj(path + "." + configBean.getKey(), configBean.getValue());
} }
} }
protected void saveObj(String path, IConfigBean o) {
getConfig().set(path, o.toConfig());
}
protected <T extends IConfigBean> Map<String, T> getObjMap(String path, Class<T> clazz) { protected <T extends IConfigBean> Map<String, T> getObjMap(String path, Class<T> clazz) {
Map<String, T> map = new HashMap<>(); Map<String, T> map = new HashMap<>();
MemorySection configMap = (MemorySection) getConfig().get(path); MemorySection configMap = (MemorySection) getConfig().get(path);
if (configMap != null) { if (configMap != null) {
for (String key : configMap.getKeys(false)) { for (String key : configMap.getKeys(false)) {
MemorySection beanConfig = (MemorySection) configMap.get(key); T bean = getObj(path + "." + key, clazz);
if (beanConfig != null) { if (bean != null) {
try {
T bean = clazz.newInstance();
bean.toConfigBean(beanConfig);
map.put(key, bean); map.put(key, bean);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
} }
} }
} }
return map; return map;
} }
protected <T extends IConfigBean> T getObj(String path, Class<T> clazz) {
Object beanConfig = getConfig().get(path);
if (beanConfig != null && beanConfig instanceof MemorySection) {
try {
T bean = clazz.newInstance();
bean.toConfigBean((MemorySection) beanConfig);
return bean;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
return null;
}
} }