1
1
mirror of https://github.com/geekfrog/PermissionsTime.git synced 2024-11-22 07:28:47 +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
protected void init() {
saveConfig();
}
@Override

View File

@ -128,27 +128,39 @@ public abstract class PluginConfig {
protected void saveObj(String path, Map<String, ? extends IConfigBean> o) {
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) {
Map<String, T> map = new HashMap<>();
MemorySection configMap = (MemorySection) getConfig().get(path);
if (configMap != null) {
for (String key : configMap.getKeys(false)) {
MemorySection beanConfig = (MemorySection) configMap.get(key);
if (beanConfig != null) {
try {
T bean = clazz.newInstance();
bean.toConfigBean(beanConfig);
T bean = getObj(path + "." + key, clazz);
if (bean != null) {
map.put(key, bean);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
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;
}
}