2011-09-06 19:01:57 +02:00
|
|
|
package com.Acrobot.ChestShop.Config;
|
|
|
|
|
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uLongName;
|
2012-03-01 22:03:59 +01:00
|
|
|
import org.bukkit.configuration.Configuration;
|
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
2011-09-06 19:01:57 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class ConfigObject {
|
|
|
|
private final File configFile = new File(ChestShop.folder, "config.yml");
|
|
|
|
private final File langFile = new File(ChestShop.folder, "local.yml");
|
2012-03-01 22:03:59 +01:00
|
|
|
private final YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
|
|
|
|
private final YamlConfiguration language = YamlConfiguration.loadConfiguration(langFile);
|
2011-09-06 19:01:57 +02:00
|
|
|
|
|
|
|
public ConfigObject() {
|
|
|
|
if (!ChestShop.folder.exists()) ChestShop.folder.mkdir();
|
|
|
|
|
|
|
|
reloadConfig();
|
2012-03-01 22:03:59 +01:00
|
|
|
load(config, configFile);
|
2011-09-06 19:01:57 +02:00
|
|
|
|
|
|
|
reloadLanguage();
|
2012-03-01 22:03:59 +01:00
|
|
|
load(language, langFile);
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
uLongName.configFile = new File(ChestShop.folder, "longName.storage");
|
|
|
|
uLongName.config = YamlConfiguration.loadConfiguration(uLongName.configFile);
|
2011-09-06 19:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void reloadConfig() {
|
|
|
|
for (Property def : Property.values()) {
|
2012-03-01 22:03:59 +01:00
|
|
|
if (config.get(def.name()) == null) {
|
2011-09-06 19:01:57 +02:00
|
|
|
writeToFile('\n' + def.name() + ": " + def.getValue() + "\n#" + def.getComment(), configFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reloadLanguage() {
|
|
|
|
for (Language def : Language.values()) {
|
2012-03-01 22:03:59 +01:00
|
|
|
if (language.get(def.name()) == null) {
|
2011-09-06 19:01:57 +02:00
|
|
|
writeToFile('\n' + def.name() + ": \"" + def.toString() + '\"', langFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void writeToFile(String string, File file) {
|
|
|
|
try {
|
|
|
|
FileWriter fw = new FileWriter(file, true);
|
|
|
|
fw.write(string);
|
|
|
|
fw.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.out.println("Couldn't write to file - " + file.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Configuration getLanguageConfig() {
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getProperty(String property) {
|
2012-03-01 22:03:59 +01:00
|
|
|
return config.get(property);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void load(FileConfiguration config, File file) {
|
|
|
|
try {
|
|
|
|
config.load(file);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void save(FileConfiguration config, File file) {
|
|
|
|
try {
|
|
|
|
config.save(file);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void reloadConfig(FileConfiguration config, File file) {
|
|
|
|
save(config, file);
|
|
|
|
load(config, file);
|
2011-09-06 19:01:57 +02:00
|
|
|
}
|
|
|
|
}
|