feat: 重构配置类模块

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
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 Representer yamlRepresenter = new YamlRepresenter();
protected final Yaml yamlz = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions); protected final Yaml yamlz = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
/**
* MAP
*/
protected Map<?, ?> contentsMap;
/**
*
*/
protected String data; protected String data;
/**
* @return
*/
public Map<?, ?> getContentMap() {
return contentsMap;
}
@Override @Override
public void load(final File file) throws FileNotFoundException, IOException, InvalidConfigurationException { public void load(final File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
Validate.notNull(file, FILE_NOT_BE_NULL); Validate.notNull(file, FILE_NOT_BE_NULL);
@ -79,9 +94,8 @@ public abstract class AbstractConfig extends YamlConfiguration {
@Override @Override
public void loadFromString(final String contents) throws InvalidConfigurationException { public void loadFromString(final String contents) throws InvalidConfigurationException {
Validate.notNull(contents, CONTENT_NOT_BE_NULL); Validate.notNull(contents, CONTENT_NOT_BE_NULL);
Map<?, ?> input;
try { try {
input = (Map<?, ?>) yamlz.load(contents); contentsMap = (Map<?, ?>) yamlz.load(contents);
} catch (final YAMLException e) { } catch (final YAMLException e) {
throw new InvalidConfigurationException(e); throw new InvalidConfigurationException(e);
} catch (final ClassCastException e) { } catch (final ClassCastException e) {
@ -91,8 +105,8 @@ public abstract class AbstractConfig extends YamlConfiguration {
if (header.length() > 0) { if (header.length() > 0) {
options().header(header); options().header(header);
} }
if (input != null) { if (contentsMap != null) {
convertMapsToSections(input, this); 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 DEFAULT = "config.yml";
private static final String DATA_FORMANT = "yyyyMMddHHmmss"; private static final String DATA_FORMANT = "yyyyMMddHHmmss";
private static final String CONFIG_BACKUP = "配置: %s 已备份为 %s !"; 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_OVERRIDE = "配置: %s 将覆盖原有字段数据...";
private static final String CONFIG_NOT_FOUND = "配置: 文件 %s 不存在!";
private static final String CONFIG_READ_ERROR = "配置: %s 读取错误..."; private static final String CONFIG_READ_ERROR = "配置: %s 读取错误...";
private static final String CONFIG_SAVE_ERROR = "配置: %s 保存错误..."; private static final String CONFIG_SAVE_ERROR = "配置: %s 保存错误...";
private static final String CONFIG_UPDATE_WARN = "配置: %s 版本 %s 过低 正在升级到 %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) { public FileConfig(final File file) {
Validate.notNull(file, FILE_NOT_BE_NULL); Validate.notNull(file, FILE_NOT_BE_NULL);
this.file = file;
init(file); init(file);
} }
/** /**
* *
* *
* @param plugin * @param parent
* *
* @param filename * @param filename
* *
*/ */
public FileConfig(final String filename) { public FileConfig(final File parent, final String filename) {
this.file = new File(plugin.getDataFolder(), filename); init(new File(parent, filename), true);
check(file);
init(file);
} }
/** /**
@ -92,10 +90,32 @@ public class FileConfig extends AbstractConfig {
* @param stream * @param stream
* *
*/ */
private FileConfig(final InputStream stream) { public FileConfig(final InputStream stream) {
init(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 * List
* *
@ -110,7 +130,7 @@ public class FileConfig extends AbstractConfig {
public <E> FileConfig addToList(final String path, final E obj) { public <E> FileConfig addToList(final String path, final E obj) {
List<E> l = (List<E>) this.getList(path); List<E> l = (List<E>) this.getList(path);
if (null == l) { if (null == l) {
l = new ArrayList<E>(); l = new ArrayList<>();
} }
l.add(obj); l.add(obj);
return this; return this;
@ -159,7 +179,7 @@ public class FileConfig extends AbstractConfig {
* @return * @return
*/ */
public List<String> getColorList(final List<String> cfgmsg) { public List<String> getColorList(final List<String> cfgmsg) {
final List<String> message = new ArrayList<String>(); final List<String> message = new ArrayList<>();
if (cfgmsg == null) { if (cfgmsg == null) {
return null; return null;
} }
@ -272,6 +292,32 @@ public class FileConfig extends AbstractConfig {
super.loadFromString(contents); 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); 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 * Jar
*/ */
@ -464,12 +495,26 @@ public class FileConfig extends AbstractConfig {
* @return FileConfig * @return FileConfig
*/ */
protected FileConfig init(final File file) { 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); Validate.notNull(file, FILE_NOT_BE_NULL);
FileInputStream stream; if (check) {
check(file);
}
try { try {
stream = new FileInputStream(file); init(new FileInputStream(file));
init(stream);
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
Log.debug(String.format(CONFIG_NOT_FOUND, file.toPath()));
} }
return this; return this;
} }
@ -501,6 +546,20 @@ public class FileConfig extends AbstractConfig {
return this; 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 newCfg
* @param oldCfg * @param oldCfg

View File

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

View File

@ -16,32 +16,18 @@ public class PlayerConfig extends FileConfig {
/** /**
* ( ) * ( )
* *
* @param plugin * @param
* *
* @param player * @param player
* *
*/ */
public PlayerConfig(final File dir, final Player player) { public PlayerConfig(final File dir, final Player player) {
this(plugin.getDataFolder(), player.getName()); super(dir, player.getName() + ".yml");
}
/**
* ( )
*
* @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)));
} }
/** /**
* ( CONFIG_FOLDER ) * ( CONFIG_FOLDER )
* *
* @param plugin
*
* @param playername * @param playername
* *
*/ */
@ -52,12 +38,10 @@ public class PlayerConfig extends FileConfig {
/** /**
* ( CONFIG_FOLDER ) * ( CONFIG_FOLDER )
* *
* @param plugin
*
* @param player * @param player
* *
*/ */
public PlayerConfig(final String playername) { 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; package pw.yumc.YumCore.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.net.MalformedURLException;
import java.net.URL; 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; import pw.yumc.YumCore.bukkit.Log;
@ -20,65 +13,28 @@ import pw.yumc.YumCore.bukkit.Log;
* @author * @author
*/ */
public class RemoteConfig extends FileConfig { public class RemoteConfig extends FileConfig {
public RemoteConfig(final String url) throws MalformedURLException, IOException {
protected static String REMOTEFILECENTER = "http://data.yumc.pw/config/"; this(new URL(url));
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 filename, final String url) { public RemoteConfig(final URL url) throws IOException {
this(filename, url, false); super(url.openStream());
}
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);
} }
/** /**
* * (null)
* *
* @param url * @param url
* *
* @return {@link FileConfig} * @return {@link FileConfig}
*/ */
public static FileConfig getFileConfig(final String url) { public static FileConfig getConfig(final String url) {
final FileConfig config = new FileConfig();
try { try {
final BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream(), UTF_8)); return new RemoteConfig(url);
config.load(br); } catch (final IOException e) {
br.close(); Log.debug("获取远程配置文件失败!", e);
} catch (final Exception 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) { public static String getYamlTag(final String url, final String tag, final String def) {
String result = def; String result = def;
try { try {
final BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream(), UTF_8)); result = getConfig(url).getString(tag);
final FileConfiguration config = YamlConfiguration.loadConfiguration(br);
br.close();
result = config.getString(tag);
} catch (final Exception e) { } catch (final Exception e) {
} }
return result; 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;
}
}