1
0
mirror of https://e.coding.net/circlecloud/ItemMamager.git synced 2024-12-24 07:18:53 +00:00

update...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-09-01 20:56:53 +08:00
parent 2ced17f917
commit 6250ddbeca
3 changed files with 112 additions and 185 deletions

View File

@ -1,55 +0,0 @@
package cn.citycraft.TimeItem.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.Plugin;
public class Config extends ConfigLoader {
private static String CONFIG_NAME = "config.yml";
private static FileConfig instance;
private static File file;
public Config(Plugin p) {
super(p, CONFIG_NAME);
file = new File(p.getDataFolder(), CONFIG_NAME);
instance = super.getInstance();
}
public Config(Plugin p, String ver) {
super(p, CONFIG_NAME, ver);
instance = super.getInstance();
}
public static void load(Plugin p) {
new Config(p);
}
public static void load(Plugin p, String ver) {
new Config(p, ver);
}
public static FileConfig getInstance() {
return instance;
}
public static String getMessage(String path) {
String message = instance.getString(path);
if (message != null)
message = message.replaceAll("&", "§");
return message;
}
public static String[] getStringArray(String path) {
return instance.getStringList(path).toArray(new String[0]);
}
public static void save(){
try {
instance.save(file);
} catch (IOException e) {
saveError(file);
e.printStackTrace();
}
}
}

View File

@ -1,102 +0,0 @@
package cn.citycraft.TimeItem.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.Plugin;
public class ConfigLoader extends FileConfig {
protected static FileConfig config;
protected static boolean tip = true;
protected static Plugin plugin;
public ConfigLoader(Plugin p, File file) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, null, true);
}
public ConfigLoader(Plugin p, File file, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, null, res);
}
public ConfigLoader(Plugin p, File file, String ver) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, ver, true);
}
public ConfigLoader(Plugin p, File file, String ver, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, ver, res);
}
public ConfigLoader(Plugin p, String filename) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), null,
true);
}
public ConfigLoader(Plugin p, String filename, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), null, res);
}
public ConfigLoader(Plugin p, String filename, String ver) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true);
}
public ConfigLoader(Plugin p, String filename, String ver, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true);
}
public static FileConfig getInstance() {
return config;
}
public FileConfig loadConfig(Plugin p, File file, String ver, boolean res) {
tip = res ;
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
p.getLogger().info("创建新的文件夹" + file.getParentFile().getAbsolutePath() + "...");
}
if (!file.exists()) {
fileCreate(p, file, res);
} else {
if (ver != null) {
FileConfig configcheck = init(file);
String version = configcheck.getString("version");
if (version == null || !version.equals(ver)) {
p.saveResource(file.getName(), true);
p.getLogger().warning(
"配置文件: " + file.getName() + " 版本过低 正在升级...");
}
}
}
if (tip)
p.getLogger().info(
"载入配置文件: " + file.getName()
+ (ver != null ? " 版本: " + ver : ""));
return init(file);
}
private void fileCreate(Plugin p, File file, boolean res) {
if (res) {
p.saveResource(file.getName(), false);
} else {
try {
p.getLogger().info("创建新的配置文件" + file.getAbsolutePath() + "...");
file.createNewFile();
} catch (IOException e) {
p.getLogger().info("配置文件" + file.getName() + "创建失败...");
e.printStackTrace();
}
}
}
public static void saveError(File file) {
plugin.getLogger().info("配置文件" + file.getName() + "保存错误...");
}
}

View File

@ -6,11 +6,12 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
@ -19,6 +20,7 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
@ -28,38 +30,105 @@ import com.google.common.io.Files;
/**
* An implementation of {@link Configuration} which saves all files in Yaml.
* Note that this implementation is not synchronized.
* Note that this
* implementation is not synchronized.
*/
public class FileConfig extends YamlConfiguration {
public static FileConfig init(File file) {
return FileConfig.loadConfiguration(file);
}
public static FileConfig loadConfiguration(File file) {
Validate.notNull(file, "File cannot be null");
FileConfig config = new FileConfig();
try {
config.load(file);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
} catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
}
return config;
}
protected File file;
protected Logger loger;
protected Plugin plugin;
protected final DumperOptions yamlOptions = new DumperOptions();
protected final Representer yamlRepresenter = new YamlRepresenter();
protected final Yaml yaml = new Yaml(new YamlConstructor(),
yamlRepresenter, yamlOptions);
protected final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
private FileConfig(File file) {
Validate.notNull(file, "File cannot be null");
this.file = file;
loger = Bukkit.getLogger();
init(file);
}
private FileConfig(InputStream stream) {
loger = Bukkit.getLogger();
init(stream);
}
public FileConfig(Plugin plugin, File file) {
Validate.notNull(file, "File cannot be null");
Validate.notNull(plugin, "Plugin cannot be null");
this.plugin = plugin;
this.file = file;
loger = plugin.getLogger();
check(file);
init(file);
}
public FileConfig(Plugin plugin, String filename) {
this(plugin, new File(plugin.getDataFolder(), filename));
}
private void check(File file) {
String filename = file.getName();
InputStream stream = plugin.getResource(filename);
try {
if (!file.exists()) {
file.getParentFile().mkdirs();
if (stream == null) {
file.createNewFile();
loger.info("配置文件 " + filename + " 不存在 创建新文件...");
} else {
plugin.saveResource(filename, true);
loger.info("配置文件 " + filename + " 不存在 从插件释放...");
}
} else {
FileConfig newcfg = new FileConfig(stream);
FileConfig oldcfg = new FileConfig(file);
String newver = newcfg.getString("version");
String oldver = oldcfg.getString("version");
if (newver != null && newver != oldver) {
loger.warning("配置文件: " + filename + " 版本 " + oldver + " 过低 正在升级到 " + newver + " ...");
try {
oldcfg.save(new File(file.getParent(), filename + ".backup"));
loger.warning("配置文件: " + filename + " 已备份为 " + filename + ".backup !");
} catch (IOException e) {
loger.warning("配置文件: " + filename + "备份失败!");
}
plugin.saveResource(filename, true);
loger.info("配置文件: " + filename + "升级成功!");
}
}
} catch (IOException e) {
loger.info("配置文件 " + filename + " 创建失败...");
}
}
private void init(File file) {
Validate.notNull(file, "File cannot be null");
FileInputStream stream;
try {
stream = new FileInputStream(file);
init(stream);
} catch (FileNotFoundException e) {
loger.info("配置文件 " + file.getName() + " 不存在...");
}
}
private void init(InputStream stream) {
Validate.notNull(stream, "Stream cannot be null");
try {
this.load(new InputStreamReader(stream, Charsets.UTF_8));
} catch (IOException ex) {
loger.info("配置文件 " + file.getName() + " 读取错误...");
} catch (InvalidConfigurationException ex) {
loger.info("配置文件 " + file.getName() + " 格式错误...");
}
}
@Override
public void load(File file) throws FileNotFoundException, IOException,
InvalidConfigurationException {
public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
Validate.notNull(file, "File cannot be null");
final FileInputStream stream = new FileInputStream(file);
load(new InputStreamReader(stream, Charsets.UTF_8));
@ -67,8 +136,7 @@ public class FileConfig extends YamlConfiguration {
@Override
public void load(Reader reader) throws IOException, InvalidConfigurationException {
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
: new BufferedReader(reader);
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader : new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
try {
String line;
@ -81,13 +149,29 @@ public class FileConfig extends YamlConfiguration {
}
loadFromString(builder.toString());
}
public void reload() {
init(file);
}
public void save() {
if (file == null) {
loger.info("未定义配置文件路径 保存失败!");
}
try {
this.save(file);
} catch (IOException e) {
loger.info("配置文件 " + file.getName() + " 保存错误...");
e.printStackTrace();
}
}
@Override
public void save(File file) throws IOException {
Validate.notNull(file, "File cannot be null");
Files.createParentDirs(file);
String data = saveToString();
Writer writer = new OutputStreamWriter(new FileOutputStream(file),
Charsets.UTF_8);
Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
try {
writer.write(data);
} finally {