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-11 02:21:50 +08:00
parent af7e7ce4e0
commit e2be910d1d
4 changed files with 68 additions and 19 deletions

View File

@ -19,13 +19,12 @@ public class LangCfg extends PluginConfig {
@Override @Override
protected void init() { protected void init() {
saveConfig();
} }
@Override @Override
protected void loadToDo() { protected void loadToDo() {
NO_PERMISSION = getConfig().getString("nopermission","&4你没有权限这么做"); NO_PERMISSION = setGetDefault("nopermission","&4你没有权限这么做");
CONFIG_RELOADED = getConfig().getString("configReloaded","&a配置重载完成"); CONFIG_RELOADED = setGetDefault("configReloaded","&a配置重载完成");
} }
} }

View File

@ -19,13 +19,12 @@ public class PackagesCfg extends PluginConfig {
@Override @Override
protected void init() { protected void init() {
saveConfig();
} }
@Override @Override
protected void loadToDo() { protected void loadToDo() {
PACKAGES_VERSION = getConfig().getString("version", "?"); PACKAGES_VERSION = setGetDefault("version", "?");
DEFAULT_GROUP = getConfig().getString("defaultGroup", "Default"); DEFAULT_GROUP = setGetDefault("defaultGroup", "Default");
PACKAGES = getObjMap("packages", PermissionPackageBean.class); PACKAGES = getObjMap("packages", PermissionPackageBean.class);
if (PluginCfg.IS_DEBUG) { if (PluginCfg.IS_DEBUG) {
System.out.println("packages vresion:" + PACKAGES_VERSION); System.out.println("packages vresion:" + PACKAGES_VERSION);

View File

@ -11,15 +11,17 @@ import gg.frog.mc.permissionstime.utils.config.PluginConfig;
*/ */
public class PluginCfg extends PluginConfig { public class PluginCfg extends PluginConfig {
public static String PLUGIN_PREFIX = null; public static String PLUGIN_PREFIX ;
public static Boolean IS_METRICS = null; public static boolean IS_METRICS = true;
public static Boolean IS_DEBUG = null; public static boolean IS_DEBUG = false;
public static String LANG = null; public static String LANG;
public static boolean USE_MYSQL = false;
public static String SQL_HOSTNAME; public static String SQL_HOSTNAME;
public static int SQL_PORT; public static int SQL_PORT;
public static String SQL_DATABASE; public static String SQL_DATABASE;
public static String SQL_USERNAME; public static String SQL_USERNAME;
public static String SQL_PASSWORD; public static String SQL_PASSWORD;
public static String SQL_TABLE_PREFIX;
public PluginCfg() { public PluginCfg() {
super(); super();
@ -27,18 +29,33 @@ public class PluginCfg extends PluginConfig {
@Override @Override
protected void init() { protected void init() {
getConfig().set("lang","zh-cn"); getConfig().set("lang", "zh-cn");
getConfig().set("metrics", true); getConfig().set("metrics", true);
getConfig().set("debug", false); getConfig().set("debug", false);
saveConfig(); getConfig().set("mysql.enable", false);
getConfig().set("mysql.hostname", "localhost");
getConfig().set("mysql.port", 3306);
getConfig().set("mysql.database", "minecraft");
getConfig().set("mysql.username", "user");
getConfig().set("mysql.password", "123456");
getConfig().set("mysql.tablePrefix", "pt_");
} }
@Override @Override
protected void loadToDo() { protected void loadToDo() {
PLUGIN_PREFIX = getConfig().getString("pluginPrefix","&b["+PluginMain.PLUGIN_NAME+"]&r"); PLUGIN_PREFIX = setGetDefault("pluginPrefix", "&b[" + PluginMain.PLUGIN_NAME + "]&r");
IS_DEBUG = getConfig().getBoolean("debug", false); IS_DEBUG = setGetDefault("debug", false);
IS_METRICS = getConfig().getBoolean("metrics", true); IS_METRICS = setGetDefault("metrics", true);
LANG = getConfig().getString("lang","zh-cn"); LANG = setGetDefault("lang", "zh-cn");
USE_MYSQL = setGetDefault("mysql.enable", false);
if (!USE_MYSQL) {
SQL_HOSTNAME = setGetDefault("mysql.hostname", "localhost");
SQL_PORT = setGetDefault("mysql.port", 3306);
SQL_DATABASE = setGetDefault("mysql.database", "minecraft");
SQL_USERNAME = setGetDefault("mysql.username", "user");
SQL_PASSWORD = setGetDefault("mysql.password", "123456");
SQL_TABLE_PREFIX = setGetDefault("mysql.tablePrefix", "pt_");
}
} }
} }

View File

@ -57,6 +57,7 @@ public abstract class PluginConfig {
if (!configFile.exists()) { if (!configFile.exists()) {
getConfig(folder, fileName).options().copyDefaults(true); getConfig(folder, fileName).options().copyDefaults(true);
init(); init();
saveAndReloadConfig();
} else { } else {
reloadConfig(); reloadConfig();
} }
@ -97,6 +98,14 @@ public abstract class PluginConfig {
* 保存配置 * 保存配置
*/ */
public void saveConfig() { public void saveConfig() {
try {
getConfig().save(configFile);
} catch (IOException ex) {
PluginMain.LOG.log(Level.SEVERE, "Could not save config to " + configFile, ex);
}
}
public void saveAndReloadConfig() {
try { try {
getConfig().save(configFile); getConfig().save(configFile);
reloadConfig(); reloadConfig();
@ -124,6 +133,7 @@ public abstract class PluginConfig {
} }
config.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8))); config.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8)));
loadToDo(); loadToDo();
saveConfig();
} }
protected void saveObj(String path, Map<String, ? extends IConfigBean> o) { protected void saveObj(String path, Map<String, ? extends IConfigBean> o) {
@ -163,4 +173,28 @@ public abstract class PluginConfig {
} }
return null; return null;
} }
protected String setGetDefault(String path, String def){
if(!getConfig().contains(path)){
getConfig().set(path, def);
return def;
}
return getConfig().getString(path);
}
protected int setGetDefault(String path, int def){
if(!getConfig().contains(path)){
getConfig().set(path, def);
return def;
}
return getConfig().getInt(path);
}
protected boolean setGetDefault(String path, boolean def){
if(!getConfig().contains(path)){
getConfig().set(path, def);
return def;
}
return getConfig().getBoolean(path);
}
} }