mirror of
https://e.coding.net/circlecloud/Residence.git
synced 2025-11-24 21:46:16 +00:00
move src path and modify config file...
Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
|
<classpathentry kind="src" path="src/main/java"/>
|
||||||
<attributes>
|
<classpathentry kind="src" path="src/main/resources"/>
|
||||||
<attribute name="optional" value="true"/>
|
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
|||||||
16
pom.xml
16
pom.xml
@@ -1,21 +1,19 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>cn.CityCraft</groupId>
|
<groupId>cn.citycraft</groupId>
|
||||||
<artifactId>Residence</artifactId>
|
<artifactId>Residence</artifactId>
|
||||||
<version>2.7.0.6-SNAPSHOT</version>
|
<version>2.7.0.6-SNAPSHOT</version>
|
||||||
<name>Residence</name>
|
<name>Residence</name>
|
||||||
<build>
|
<build>
|
||||||
<finalName>${project.name}</finalName>
|
<finalName>${project.name}</finalName>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
<sourceDirectory>src</sourceDirectory>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src</directory>
|
<directory>src/main/resources</directory>
|
||||||
<excludes>
|
<filtering>true</filtering>
|
||||||
<exclude>**/*.java</exclude>
|
</resource>
|
||||||
</excludes>
|
</resources>
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
package com.bekvon.bukkit.residence.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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
package com.bekvon.bukkit.residence.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 static FileConfig getInstance() {
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void saveError(File file) {
|
|
||||||
plugin.getLogger().info("配置文件" + file.getName() + "保存错误...");
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 FileConfig loadConfig(Plugin p, File file, String ver, boolean res) {
|
|
||||||
tip = res;
|
|
||||||
FileConfig tempConfig;
|
|
||||||
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 : ""));
|
|
||||||
}
|
|
||||||
tempConfig = init(file);
|
|
||||||
if (cantload) {
|
|
||||||
p.getLogger().warning("配置文件错误,写入默认的配置文件!");
|
|
||||||
fileCreate(p, file, res);
|
|
||||||
tempConfig = init(file);
|
|
||||||
}
|
|
||||||
return tempConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
package com.bekvon.bukkit.residence.config;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
import org.apache.commons.lang.Validate;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.configuration.Configuration;
|
|
||||||
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.yaml.snakeyaml.DumperOptions;
|
|
||||||
import org.yaml.snakeyaml.Yaml;
|
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
|
||||||
|
|
||||||
import com.google.common.base.Charsets;
|
|
||||||
import com.google.common.io.Files;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An implementation of {@link Configuration} which saves all files in Yaml. Note that this
|
|
||||||
* implementation is not synchronized.
|
|
||||||
*/
|
|
||||||
public class FileConfig extends YamlConfiguration {
|
|
||||||
|
|
||||||
public static boolean cantload = false;
|
|
||||||
|
|
||||||
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) {
|
|
||||||
cantload = true;
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
|
||||||
cantload = true;
|
|
||||||
} catch (InvalidConfigurationException ex) {
|
|
||||||
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
|
||||||
cantload = true;
|
|
||||||
}
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final DumperOptions yamlOptions = new DumperOptions();
|
|
||||||
|
|
||||||
protected final Representer yamlRepresenter = new YamlRepresenter();
|
|
||||||
|
|
||||||
protected final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
|
||||||
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
|
|
||||||
: new BufferedReader(reader);
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
try {
|
|
||||||
String line;
|
|
||||||
while ((line = input.readLine()) != null) {
|
|
||||||
builder.append(line);
|
|
||||||
builder.append('\n');
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
input.close();
|
|
||||||
}
|
|
||||||
loadFromString(builder.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
try {
|
|
||||||
writer.write(data);
|
|
||||||
} finally {
|
|
||||||
writer.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String saveToString() {
|
|
||||||
yamlOptions.setIndent(options().indent());
|
|
||||||
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
String header = buildHeader();
|
|
||||||
String dump = yaml.dump(getValues(false));
|
|
||||||
if (dump.equals(BLANK_CONFIG)) {
|
|
||||||
dump = "";
|
|
||||||
}
|
|
||||||
return header + dump;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -32,7 +32,6 @@ import org.bukkit.plugin.PluginManager;
|
|||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import com.bekvon.bukkit.residence.chat.ChatManager;
|
import com.bekvon.bukkit.residence.chat.ChatManager;
|
||||||
import com.bekvon.bukkit.residence.config.Config;
|
|
||||||
import com.bekvon.bukkit.residence.config.FileConfig;
|
import com.bekvon.bukkit.residence.config.FileConfig;
|
||||||
import com.bekvon.bukkit.residence.economy.EconomyInterface;
|
import com.bekvon.bukkit.residence.economy.EconomyInterface;
|
||||||
import com.bekvon.bukkit.residence.economy.EssentialsEcoAdapter;
|
import com.bekvon.bukkit.residence.economy.EssentialsEcoAdapter;
|
||||||
@@ -55,8 +54,8 @@ import com.bekvon.bukkit.residence.selection.WorldEditSelectionManager;
|
|||||||
import com.bekvon.bukkit.residence.text.Language;
|
import com.bekvon.bukkit.residence.text.Language;
|
||||||
import com.bekvon.bukkit.residence.text.help.HelpEntry;
|
import com.bekvon.bukkit.residence.text.help.HelpEntry;
|
||||||
import com.bekvon.bukkit.residence.text.help.InformationPager;
|
import com.bekvon.bukkit.residence.text.help.InformationPager;
|
||||||
import com.bekvon.bukkit.residence.utils.VersionChecker;
|
|
||||||
import com.bekvon.bukkit.residence.utils.DataBackup;
|
import com.bekvon.bukkit.residence.utils.DataBackup;
|
||||||
|
import com.bekvon.bukkit.residence.utils.VersionChecker;
|
||||||
import com.bekvon.bukkit.residence.vaultinterface.ResidenceVaultAdapter;
|
import com.bekvon.bukkit.residence.vaultinterface.ResidenceVaultAdapter;
|
||||||
import com.earth2me.essentials.Essentials;
|
import com.earth2me.essentials.Essentials;
|
||||||
|
|
||||||
@@ -96,6 +95,7 @@ public class Residence extends JavaPlugin {
|
|||||||
protected static int autosaveBukkitId = -1;
|
protected static int autosaveBukkitId = -1;
|
||||||
protected static boolean initsuccess = false;
|
protected static boolean initsuccess = false;
|
||||||
protected static List<String> resadminToggle;
|
protected static List<String> resadminToggle;
|
||||||
|
protected static FileConfig config;
|
||||||
private final static String[] validLanguages = {
|
private final static String[] validLanguages = {
|
||||||
"English",
|
"English",
|
||||||
"Chinese"
|
"Chinese"
|
||||||
@@ -280,12 +280,11 @@ public class Residence extends JavaPlugin {
|
|||||||
File outFile = new File(new File(this.getDataFolder(), "Language"), lang + ".yml");
|
File outFile = new File(new File(this.getDataFolder(), "Language"), lang + ".yml");
|
||||||
File checkFile = new File(new File(this.getDataFolder(), "Language"), "temp-" + lang + ".yml");
|
File checkFile = new File(new File(this.getDataFolder(), "Language"), "temp-" + lang + ".yml");
|
||||||
if (outFile.isFile()) {
|
if (outFile.isFile()) {
|
||||||
FileConfig testconfig = new FileConfig();
|
FileConfig testconfig = new FileConfig(this, outFile);
|
||||||
testconfig.load(outFile);
|
|
||||||
int oldversion = testconfig.getInt("FieldsVersion", 0);
|
int oldversion = testconfig.getInt("FieldsVersion", 0);
|
||||||
if (!this.writeDefaultFileFromJar(checkFile, "languagefiles/" + lang + ".yml", false))
|
if (!this.writeDefaultFileFromJar(checkFile, "languagefiles/" + lang + ".yml", false))
|
||||||
return false;
|
return false;
|
||||||
FileConfig testconfig2 = new FileConfig();
|
FileConfig testconfig2 = new FileConfig(this, checkFile);
|
||||||
testconfig2.load(checkFile);
|
testconfig2.load(checkFile);
|
||||||
int newversion = testconfig2.getInt("FieldsVersion", oldversion);
|
int newversion = testconfig2.getInt("FieldsVersion", oldversion);
|
||||||
if (checkFile.isFile()) {
|
if (checkFile.isFile()) {
|
||||||
@@ -310,10 +309,7 @@ public class Residence extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileConfiguration getConfig() {
|
public FileConfiguration getConfig() {
|
||||||
if (Config.getInstance() == null) {
|
return config;
|
||||||
Config.load(this);
|
|
||||||
}
|
|
||||||
return Config.getInstance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadEssentialsEconomy() {
|
private void loadEssentialsEconomy() {
|
||||||
@@ -327,8 +323,7 @@ public class Residence extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadLang(File langFile) throws FileNotFoundException, IOException, InvalidConfigurationException {
|
public void loadLang(File langFile) throws FileNotFoundException, IOException, InvalidConfigurationException {
|
||||||
FileConfig langconfig = new FileConfig();
|
FileConfig langconfig = new FileConfig(this, langFile);
|
||||||
langconfig.load(langFile);
|
|
||||||
helppages = HelpEntry.parseHelp(langconfig, "CommandHelp");
|
helppages = HelpEntry.parseHelp(langconfig, "CommandHelp");
|
||||||
HelpEntry.setLinesPerPage(langconfig.getInt("HelpLinesPerPage", 7));
|
HelpEntry.setLinesPerPage(langconfig.getInt("HelpLinesPerPage", 7));
|
||||||
InformationPager.setLinesPerPage(langconfig.getInt("HelpLinesPerPage", 7));
|
InformationPager.setLinesPerPage(langconfig.getInt("HelpLinesPerPage", 7));
|
||||||
@@ -595,7 +590,7 @@ public class Residence extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reloadConfig() {
|
public void reloadConfig() {
|
||||||
Config.load(this);
|
config.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reloadPlugin() {
|
public void reloadPlugin() {
|
||||||
@@ -607,7 +602,7 @@ public class Residence extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveConfig() {
|
public void saveConfig() {
|
||||||
Config.save();
|
config.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveYml() throws IOException {
|
private void saveYml() throws IOException {
|
||||||
198
src/main/java/com/bekvon/bukkit/residence/config/FileConfig.java
Normal file
198
src/main/java/com/bekvon/bukkit/residence/config/FileConfig.java
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
package com.bekvon.bukkit.residence.config;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
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.Logger;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.configuration.Configuration;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
import com.google.common.io.Files;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An implementation of {@link Configuration} which saves all files in Yaml.
|
||||||
|
* Note that this
|
||||||
|
* implementation is not synchronized.
|
||||||
|
*/
|
||||||
|
public class FileConfig extends YamlConfiguration {
|
||||||
|
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);
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if (stream == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
Validate.notNull(file, "File cannot be null");
|
||||||
|
final FileInputStream stream = new FileInputStream(file);
|
||||||
|
load(new InputStreamReader(stream, Charsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
||||||
|
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader : new BufferedReader(reader);
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
try {
|
||||||
|
String line;
|
||||||
|
while ((line = input.readLine()) != null) {
|
||||||
|
builder.append(line);
|
||||||
|
builder.append('\n');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
try {
|
||||||
|
writer.write(data);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveToString() {
|
||||||
|
yamlOptions.setIndent(options().indent());
|
||||||
|
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
|
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
|
String header = buildHeader();
|
||||||
|
String dump = yaml.dump(getValues(false));
|
||||||
|
if (dump.equals(BLANK_CONFIG)) {
|
||||||
|
dump = "";
|
||||||
|
}
|
||||||
|
return header + dump;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ website: http://ci.citycraft.cn:8800/jenkins/job/Residence/
|
|||||||
description: Cuboid Residence Plugin
|
description: Cuboid Residence Plugin
|
||||||
authors: [bekvon,nate302,t00thpick1,喵♂呜]
|
authors: [bekvon,nate302,t00thpick1,喵♂呜]
|
||||||
contributors: [lemon42,smbarbour,inorixu,Shayana_fr]
|
contributors: [lemon42,smbarbour,inorixu,Shayana_fr]
|
||||||
softdepend: [Vault,Essentials,RealPlugin,BOSEconomy,iConomy,bPermissions,PermissionsBukkit,Permissions,WorldEdit,My Worlds]
|
softdepend: [Vault,Essentials,RealPlugin,BOSEconomy,iConomy,bPermissions,PermissionsBukkit,Permissions,WorldEdit]
|
||||||
commands:
|
commands:
|
||||||
res:
|
res:
|
||||||
description: Manage Residences
|
description: Manage Residences
|
||||||
Reference in New Issue
Block a user