From e2be910d1db3fc83fcf3164385e2813b234aaa46 Mon Sep 17 00:00:00 2001 From: GeekFrog Date: Tue, 11 Jul 2017 02:21:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mc/permissionstime/config/LangCfg.java | 5 +-- .../permissionstime/config/PackagesCfg.java | 5 +-- .../mc/permissionstime/config/PluginCfg.java | 43 +++++++++++++------ .../utils/config/PluginConfig.java | 34 +++++++++++++++ 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/main/gg/frog/mc/permissionstime/config/LangCfg.java b/src/main/gg/frog/mc/permissionstime/config/LangCfg.java index 2197948..44684c2 100644 --- a/src/main/gg/frog/mc/permissionstime/config/LangCfg.java +++ b/src/main/gg/frog/mc/permissionstime/config/LangCfg.java @@ -19,13 +19,12 @@ public class LangCfg extends PluginConfig { @Override protected void init() { - saveConfig(); } @Override protected void loadToDo() { - NO_PERMISSION = getConfig().getString("nopermission","&4你没有权限这么做"); - CONFIG_RELOADED = getConfig().getString("configReloaded","&a配置重载完成"); + NO_PERMISSION = setGetDefault("nopermission","&4你没有权限这么做"); + CONFIG_RELOADED = setGetDefault("configReloaded","&a配置重载完成"); } } diff --git a/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java b/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java index e3766d0..610a1c2 100644 --- a/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java +++ b/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java @@ -19,13 +19,12 @@ public class PackagesCfg extends PluginConfig { @Override protected void init() { - saveConfig(); } @Override protected void loadToDo() { - PACKAGES_VERSION = getConfig().getString("version", "?"); - DEFAULT_GROUP = getConfig().getString("defaultGroup", "Default"); + PACKAGES_VERSION = setGetDefault("version", "?"); + DEFAULT_GROUP = setGetDefault("defaultGroup", "Default"); PACKAGES = getObjMap("packages", PermissionPackageBean.class); if (PluginCfg.IS_DEBUG) { System.out.println("packages vresion:" + PACKAGES_VERSION); diff --git a/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java b/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java index 19a2e06..1819e55 100644 --- a/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java +++ b/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java @@ -10,35 +10,52 @@ import gg.frog.mc.permissionstime.utils.config.PluginConfig; * */ public class PluginCfg extends PluginConfig { - - public static String PLUGIN_PREFIX = null; - public static Boolean IS_METRICS = null; - public static Boolean IS_DEBUG = null; - public static String LANG = null; + + public static String PLUGIN_PREFIX ; + public static boolean IS_METRICS = true; + public static boolean IS_DEBUG = false; + public static String LANG; + public static boolean USE_MYSQL = false; public static String SQL_HOSTNAME; public static int SQL_PORT; public static String SQL_DATABASE; public static String SQL_USERNAME; public static String SQL_PASSWORD; - + public static String SQL_TABLE_PREFIX; + public PluginCfg() { super(); } @Override protected void init() { - getConfig().set("lang","zh-cn"); + getConfig().set("lang", "zh-cn"); getConfig().set("metrics", true); 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 protected void loadToDo() { - PLUGIN_PREFIX = getConfig().getString("pluginPrefix","&b["+PluginMain.PLUGIN_NAME+"]&r"); - IS_DEBUG = getConfig().getBoolean("debug", false); - IS_METRICS = getConfig().getBoolean("metrics", true); - LANG = getConfig().getString("lang","zh-cn"); + PLUGIN_PREFIX = setGetDefault("pluginPrefix", "&b[" + PluginMain.PLUGIN_NAME + "]&r"); + IS_DEBUG = setGetDefault("debug", false); + IS_METRICS = setGetDefault("metrics", true); + 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_"); + } } - + } diff --git a/src/main/gg/frog/mc/permissionstime/utils/config/PluginConfig.java b/src/main/gg/frog/mc/permissionstime/utils/config/PluginConfig.java index bbb8a24..7aa6e1a 100644 --- a/src/main/gg/frog/mc/permissionstime/utils/config/PluginConfig.java +++ b/src/main/gg/frog/mc/permissionstime/utils/config/PluginConfig.java @@ -57,6 +57,7 @@ public abstract class PluginConfig { if (!configFile.exists()) { getConfig(folder, fileName).options().copyDefaults(true); init(); + saveAndReloadConfig(); } else { reloadConfig(); } @@ -97,6 +98,14 @@ public abstract class PluginConfig { * 保存配置 */ 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 { getConfig().save(configFile); reloadConfig(); @@ -124,6 +133,7 @@ public abstract class PluginConfig { } config.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8))); loadToDo(); + saveConfig(); } protected void saveObj(String path, Map o) { @@ -163,4 +173,28 @@ public abstract class PluginConfig { } 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); + } }