1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-24 06:58:51 +00:00

feat: 重构配置类模块

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-12 00:50:15 +08:00
parent 54f077b01d
commit 8ad3e181c9
6 changed files with 166 additions and 112 deletions

View File

@ -51,8 +51,23 @@ public abstract class AbstractConfig extends YamlConfiguration {
protected final Representer yamlRepresenter = new YamlRepresenter();
protected final Yaml yamlz = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
/**
* 配置文件内容MAP
*/
protected Map<?, ?> contentsMap;
/**
* 配置内容字符串
*/
protected String data;
/**
* @return 获得配置内容
*/
public Map<?, ?> getContentMap() {
return contentsMap;
}
@Override
public void load(final File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
Validate.notNull(file, FILE_NOT_BE_NULL);
@ -79,9 +94,8 @@ public abstract class AbstractConfig extends YamlConfiguration {
@Override
public void loadFromString(final String contents) throws InvalidConfigurationException {
Validate.notNull(contents, CONTENT_NOT_BE_NULL);
Map<?, ?> input;
try {
input = (Map<?, ?>) yamlz.load(contents);
contentsMap = (Map<?, ?>) yamlz.load(contents);
} catch (final YAMLException e) {
throw new InvalidConfigurationException(e);
} catch (final ClassCastException e) {
@ -91,8 +105,8 @@ public abstract class AbstractConfig extends YamlConfiguration {
if (header.length() > 0) {
options().header(header);
}
if (input != null) {
convertMapsToSections(input, this);
if (contentsMap != null) {
convertMapsToSections(contentsMap, this);
}
}

View File

@ -36,8 +36,9 @@ public class FileConfig extends AbstractConfig {
private static final String DEFAULT = "config.yml";
private static final String DATA_FORMANT = "yyyyMMddHHmmss";
private static final String CONFIG_BACKUP = "配置: %s 已备份为 %s !";
private static final String CONFIG_UPDATED = "配置: %s 升级成功 版本 %S !";
private static final String CONFIG_UPDATED = "配置: %s 升级成功 版本 %s !";
private static final String CONFIG_OVERRIDE = "配置: %s 将覆盖原有字段数据...";
private static final String CONFIG_NOT_FOUND = "配置: 文件 %s 不存在!";
private static final String CONFIG_READ_ERROR = "配置: %s 读取错误...";
private static final String CONFIG_SAVE_ERROR = "配置: %s 保存错误...";
private static final String CONFIG_UPDATE_WARN = "配置: %s 版本 %s 过低 正在升级到 %s ...";
@ -68,22 +69,19 @@ public class FileConfig extends AbstractConfig {
*/
public FileConfig(final File file) {
Validate.notNull(file, FILE_NOT_BE_NULL);
this.file = file;
init(file);
}
/**
* 从文件载入配置
*
* @param plugin
* 插件
* @param parent
* 文件夹
* @param filename
* 配置文件名称
*/
public FileConfig(final String filename) {
this.file = new File(plugin.getDataFolder(), filename);
check(file);
init(file);
public FileConfig(final File parent, final String filename) {
init(new File(parent, filename), true);
}
/**
@ -92,10 +90,32 @@ public class FileConfig extends AbstractConfig {
* @param stream
* 数据流
*/
private FileConfig(final InputStream stream) {
public FileConfig(final InputStream stream) {
init(stream);
}
/**
* 从文件载入配置
*
* @param filename
* 配置文件名称
*/
public FileConfig(final String filename) {
init(new File(plugin.getDataFolder(), filename), true);
}
/**
* 从文件载入配置
*
* @param parent
* 文件夹
* @param filename
* 配置文件名称
*/
public FileConfig(final String parent, final String filename) {
init(new File(parent, filename), true);
}
/**
* 添加到List末尾
*
@ -110,7 +130,7 @@ public class FileConfig extends AbstractConfig {
public <E> FileConfig addToList(final String path, final E obj) {
List<E> l = (List<E>) this.getList(path);
if (null == l) {
l = new ArrayList<E>();
l = new ArrayList<>();
}
l.add(obj);
return this;
@ -159,7 +179,7 @@ public class FileConfig extends AbstractConfig {
* @return 颜色转码后的文本
*/
public List<String> getColorList(final List<String> cfgmsg) {
final List<String> message = new ArrayList<String>();
final List<String> message = new ArrayList<>();
if (cfgmsg == null) {
return null;
}
@ -272,6 +292,32 @@ public class FileConfig extends AbstractConfig {
super.loadFromString(contents);
}
/**
* 比较版本号
*
* @param 新版本
* @param 旧版本
* @return 是否需要更新
*/
public boolean needUpdate(final String newver, final String oldver) {
if (newver == null || oldver == null) {
return false;
}
final String[] va1 = newver.split("\\.");// 注意此处为正则匹配不能用"."
final String[] va2 = oldver.split("\\.");
int idx = 0;
final int minLength = Math.min(va1.length, va2.length);// 取最小长度值
int diff = 0;
while (idx < minLength
&& (diff = va1[idx].length() - va2[idx].length()) == 0// 先比较长度
&& (diff = va1[idx].compareTo(va2[idx])) == 0) {// 再比较字符
++idx;
}
// 如果已经分出大小则直接返回如果未分出大小则再比较位数有子版本的为大
diff = (diff != 0) ? diff : va1.length - va2.length;
return diff > 0;
}
/**
* 重新载入配置文件
*
@ -353,21 +399,6 @@ public class FileConfig extends AbstractConfig {
super.set(path, value);
}
/**
* 检查配置文件版本
*
* @param newcfg
* 新配置文件
* @param oldcfg
* 旧配置文件
* @return 是否需要升级
* @throws IOException
*/
private boolean needUpdate(final FileConfig newcfg, final FileConfig oldcfg) throws IOException {
final String newver = newcfg.getString(VERSION);
return newver != null && !newver.equalsIgnoreCase(oldcfg.getString(VERSION));
}
/**
* 从Jar保存配置文件
*/
@ -464,12 +495,26 @@ public class FileConfig extends AbstractConfig {
* @return FileConfig
*/
protected FileConfig init(final File file) {
init(file, false);
return this;
}
/**
* 初始化FileConfig
*
* @param file
* 配置文件
* @return FileConfig
*/
protected FileConfig init(final File file, final boolean check) {
Validate.notNull(file, FILE_NOT_BE_NULL);
FileInputStream stream;
if (check) {
check(file);
}
try {
stream = new FileInputStream(file);
init(stream);
init(new FileInputStream(file));
} catch (final FileNotFoundException e) {
Log.debug(String.format(CONFIG_NOT_FOUND, file.toPath()));
}
return this;
}
@ -501,6 +546,20 @@ public class FileConfig extends AbstractConfig {
return this;
}
/**
* 检查配置文件版本
*
* @param newcfg
* 新配置文件
* @param oldcfg
* 旧配置文件
* @return 是否需要升级
* @throws IOException
*/
protected boolean needUpdate(final FileConfig newcfg, final FileConfig oldcfg) {
return needUpdate(newcfg.getString(VERSION), oldcfg.getString(VERSION));
}
/**
* @param newCfg
* @param oldCfg

View File

@ -8,7 +8,7 @@ import org.bukkit.configuration.ConfigurationSection;
* @since 2016年7月5日 上午8:53:57
* @author
*/
public class InjectConfigurationSection extends AbstractInjectConfig {
public abstract class InjectConfigurationSection extends AbstractInjectConfig {
public InjectConfigurationSection(final ConfigurationSection config) {
inject(config);

View File

@ -16,32 +16,18 @@ public class PlayerConfig extends FileConfig {
/**
* 获得玩家配置(保存在 指定 文件夹)
*
* @param plugin
* @param 指定目录
* 插件
* @param player
* 玩家
*/
public PlayerConfig(final File dir, final Player player) {
this(plugin.getDataFolder(), player.getName());
}
/**
* 获得玩家配置(保存在 指定 文件夹)
*
* @param plugin
* 插件
* @param playername
* 玩家名称
*/
public PlayerConfig(final File dir, final String playername) {
super(new File(dir, String.format("%s%s%s.yml", CONFIG_FOLDER, File.separatorChar, playername)));
super(dir, player.getName() + ".yml");
}
/**
* 获得玩家配置(保存在 CONFIG_FOLDER 文件夹)
*
* @param plugin
* 插件
* @param playername
* 玩家名称
*/
@ -52,12 +38,10 @@ public class PlayerConfig extends FileConfig {
/**
* 获得玩家配置(保存在 CONFIG_FOLDER 文件夹)
*
* @param plugin
* 插件
* @param player
* 玩家
*/
public PlayerConfig(final String playername) {
this(plugin.getDataFolder(), playername);
super(new File(plugin.getDataFolder(), CONFIG_FOLDER), playername + ".yml");
}
}

View File

@ -1,15 +1,8 @@
package pw.yumc.YumCore.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import pw.yumc.YumCore.bukkit.Log;
@ -20,65 +13,28 @@ import pw.yumc.YumCore.bukkit.Log;
* @author
*/
public class RemoteConfig extends FileConfig {
protected static String REMOTEFILECENTER = "http://data.yumc.pw/config/";
private static final String PLUGINHELPER = "PluginHelper";
private static final String fromYumc = "配置 %s 来自 YUMC 数据中心...";
private static final String createError = "尝试从 YUMC 数据中心下载 %s 失败 部分功能可能无法使用...";
private static final String updateError = "尝试从 YUMC 数据中心更新配置文件 %s 失败 部分数据可能已过时...";
public RemoteConfig(final String filename) {
this(filename, REMOTEFILECENTER + filename);
public RemoteConfig(final String url) throws MalformedURLException, IOException {
this(new URL(url));
}
public RemoteConfig(final String filename, final String url) {
this(filename, url, false);
}
public RemoteConfig(final String filename, final String url, final boolean force) {
file = new File(plugin.getDataFolder().getParentFile(), PLUGINHELPER + File.separator + filename);
if (!file.exists()) {
try {
// 尝试从YUMC下载配置文件
Files.copy(new URL(url).openStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
Log.info(String.format(fromYumc, filename));
} catch (final IOException e) {
Log.warning(String.format(createError, filename));
}
} else {
final FileConfig oldcfg = new FileConfig(file);
final FileConfig newcfg = getFileConfig(url);
final String newver = newcfg.getString(VERSION);
final String oldver = oldcfg.getString(VERSION);
if (newver != null && !newver.equals(oldver)) {
try {
file.renameTo(new File(plugin.getDataFolder().getParentFile(), PLUGINHELPER + File.separator + getBakName(filename)));
updateConfig(newcfg, oldcfg, force).save(file);
} catch (final IOException e) {
Log.warning(String.format(updateError, filename));
}
}
}
init(file);
public RemoteConfig(final URL url) throws IOException {
super(url.openStream());
}
/**
* 获得配置文件
* 获得配置文件(错误返回null)
*
* @param url
* 配置文件地址
* @return {@link FileConfig}
*/
public static FileConfig getFileConfig(final String url) {
final FileConfig config = new FileConfig();
public static FileConfig getConfig(final String url) {
try {
final BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream(), UTF_8));
config.load(br);
br.close();
} catch (final Exception e) {
return new RemoteConfig(url);
} catch (final IOException e) {
Log.debug("获取远程配置文件失败!", e);
return null;
}
return config;
}
/**
@ -95,12 +51,10 @@ public class RemoteConfig extends FileConfig {
public static String getYamlTag(final String url, final String tag, final String def) {
String result = def;
try {
final BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream(), UTF_8));
final FileConfiguration config = YamlConfiguration.loadConfiguration(br);
br.close();
result = config.getString(tag);
result = getConfig(url).getString(tag);
} catch (final Exception e) {
}
return result;
}
}

View File

@ -0,0 +1,43 @@
package pw.yumc.YumCore.config;
import java.io.File;
import java.io.IOException;
import pw.yumc.YumCore.bukkit.Log;
public class YumConfig {
protected static final String REMOTEFILECENTER = "http://data.yumc.pw/config/";
protected static final String DataFolder = "plugins" + File.separatorChar + "YumCore";
private static final String fromYumc = "配置 %s 来自 YUMC 数据中心...";
private static final String createError = "从 YUMC 数据中心下载配置 %s 失败...";
/**
* 获得本地配置文件
*
* @param url
* 配置文件地址
* @return {@link FileConfig}
*/
public static FileConfig getLocal(final String filename) {
final File file = new File(DataFolder, filename);
return new FileConfig(file);
}
/**
* 获得远程配置文件
*
* @param url
* 配置文件地址
* @return {@link FileConfig}
*/
public static FileConfig getRemote(final String filename) {
FileConfig config = null;
try {
config = new RemoteConfig(REMOTEFILECENTER + filename);
} catch (final IOException e) {
}
Log.info(String.format(config == null ? createError : fromYumc, filename));
return config;
}
}