Extract new default configs when config is updating

This commit is contained in:
Sn0wStorm 2016-05-30 01:00:52 +02:00
parent 20c68454f4
commit 8aed3df0c6

View File

@ -241,6 +241,7 @@ public class P extends JavaPlugin {
String version = config.getString("version", null); String version = config.getString("version", null);
if (version != null) { if (version != null) {
if (!version.equals(configVersion)) { if (!version.equals(configVersion)) {
copyDefaultConfigs(true);
new ConfigUpdater(file).update(version, language); new ConfigUpdater(file).update(version, language);
P.p.log("Config Updated to version: " + configVersion); P.p.log("Config Updated to version: " + configVersion);
config = YamlConfiguration.loadConfiguration(file); config = YamlConfiguration.loadConfiguration(file);
@ -591,13 +592,14 @@ public class P extends JavaPlugin {
File cfg = new File(p.getDataFolder(), "config.yml"); File cfg = new File(p.getDataFolder(), "config.yml");
if (!cfg.exists()) { if (!cfg.exists()) {
errorLog("No config.yml found, creating default file! You may want to choose a config according to your language!"); errorLog("No config.yml found, creating default file! You may want to choose a config according to your language!");
errorLog("You can find them in plugins/Brewery/configs/");
InputStream defconf = getResource("config/en/config.yml"); InputStream defconf = getResource("config/en/config.yml");
if (defconf == null) { if (defconf == null) {
errorLog("default config file not found, your jarfile may be corrupt. Disabling Brewery!"); errorLog("default config file not found, your jarfile may be corrupt. Disabling Brewery!");
return false; return false;
} }
try { try {
saveFile(defconf, getDataFolder(), "config.yml"); saveFile(defconf, getDataFolder(), "config.yml", false);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
@ -608,25 +610,14 @@ public class P extends JavaPlugin {
return false; return false;
} }
File configs = new File(getDataFolder(), "configs"); copyDefaultConfigs(false);
if (!configs.exists()) {
String lang[] = new String[] {"de", "en", "fr"};
for (String l : lang) {
File lfold = new File(configs, l);
try {
saveFile(getResource("config/" + l + "/config.yml"), lfold, "config.yml");
} catch (IOException e) {
e.printStackTrace();
}
}
}
File languages = new File(getDataFolder(), "languages"); File languages = new File(getDataFolder(), "languages");
if (!languages.exists()) { if (!languages.exists()) {
String lang[] = new String[] {"de", "en", "fr", "no"}; String lang[] = new String[] {"de", "en", "fr", "no"};
for (String l : lang) { for (String l : lang) {
try { try {
saveFile(getResource("languages/" + l + ".yml"), languages, l + ".yml"); saveFile(getResource("languages/" + l + ".yml"), languages, l + ".yml", false);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -635,6 +626,20 @@ public class P extends JavaPlugin {
return true; return true;
} }
private void copyDefaultConfigs(boolean overwrite) {
File configs = new File(getDataFolder(), "configs");
if (overwrite || !configs.exists()) {
for (String l : new String[] {"de", "en", "fr"}) {
File lfold = new File(configs, l);
try {
saveFile(getResource("config/" + l + "/config.yml"), lfold, "config.yml", overwrite);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// Utility // Utility
public int parseInt(String string) { public int parseInt(String string) {
@ -761,14 +766,18 @@ public class P extends JavaPlugin {
} }
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
public static void saveFile(InputStream in, File dest, String name) throws IOException { public static void saveFile(InputStream in, File dest, String name, boolean overwrite) throws IOException {
if (in == null) return; if (in == null) return;
if (!dest.exists()) { if (!dest.exists()) {
dest.mkdirs(); dest.mkdirs();
} }
File result = new File(dest, name); File result = new File(dest, name);
if (result.exists()) { if (result.exists()) {
return; if (overwrite) {
result.delete();
} else {
return;
}
} }
OutputStream out = new FileOutputStream(result); OutputStream out = new FileOutputStream(result);