diff --git a/plugin.yml b/plugin.yml index 58b2416..f3ec6f8 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,8 +1,7 @@ name: Brewery -version: 1.0 +version: 1.1 main: com.dre.brewery.P authors: [Milan Albrecht, Frank Baumann] -softdepend: [Vault] commands: brewery: description: Command for Administration diff --git a/pom.xml b/pom.xml index 4466f02..a11f278 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.dre brewery - 1.0 + 1.1 Brewery diff --git a/src/com/dre/brewery/ConfigUpdater.java b/src/com/dre/brewery/ConfigUpdater.java new file mode 100644 index 0000000..a85a1b4 --- /dev/null +++ b/src/com/dre/brewery/ConfigUpdater.java @@ -0,0 +1,250 @@ +package com.dre.brewery; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; + +public class ConfigUpdater { + + private ArrayList config = new ArrayList(); + private File file; + + public ConfigUpdater(File file) { + this.file = file; + getConfigString(); + } + + // Returns the index of the line that starts with 'lineStart', returns -1 if not found; + public int indexOfStart(String lineStart) { + for (int i = 0; i < config.size(); i++) { + if (config.get(i).startsWith(lineStart)) { + return i; + } + } + return -1; + } + + // Adds some lines to the end + public void appendLines(String... lines) { + config.addAll(Arrays.asList(lines)); + } + + // Replaces the line at the index with the new Line + public void setLine(int index, String newLine) { + config.set(index, newLine); + } + + // adds some Lines at the index + public void addLines(int index, String... newLines) { + config.addAll(index, Arrays.asList(newLines)); + } + + public void saveConfig() { + StringBuilder stringBuilder = new StringBuilder(""); + for (String line : config) { + stringBuilder.append(line + "\n"); + } + String configString = stringBuilder.toString().trim(); + + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write(configString); + writer.flush(); + writer.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void getConfigString() { + try { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String currentLine; + while((currentLine = reader.readLine()) != null) { + config.add(currentLine); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + + // ---- Updating to newer Versions ---- + + // Update from a specified Config version and language to the newest version + public void update(String fromVersion, String lang) { + if (fromVersion.equals("0.5")) { + // Version 0.5 was only released for de, but with en as setting, so default to de + if (!lang.equals("de")) { + lang = "de"; + } + } + if (fromVersion.equals("0.5") || fromVersion.equals("1.0")) { + if (lang.equals("de")) { + update05de(); + } else { + update10en(); + } + } else { + P.p.log(P.p.languageReader.get("Error_ConfigUpdate", fromVersion)); + return; + } + saveConfig(); + } + + // Updates de from 0.5 to 1.1 + private void update05de() { + // Update version String + int index = indexOfStart("version"); + String line = "version: '1.1'"; + if (index != -1) { + setLine(index, line); + } else { + index = indexOfStart("# Config Version"); + if (index == -1) { + index = indexOfStart("autosave"); + } + if (index == -1) { + appendLines(line); + } else { + addLines(index, line); + } + } + + // Default language to de + index = indexOfStart("language: en"); + if (index != -1) { + setLine(index, "language: de"); + P.p.language = "de"; + } + + // Add the new entries for the Word Distortion above the words section + String[] entries = { + "# -- Chat Veränderungs Einstellungen --", + "", + "# Text nach den angegebenen Kommandos wird bei Trunkenheit ebenfalls Verändert (Liste) [- /gl]", + "distortCommands:", + "- /gl", + "- /global", + "- /fl", + "- /s", + "- /letter", + "", + "# Geschriebenen Text auf Schildern bei Trunkenheit verändern [false]", + "distortSignText: false", + "", + "# Text, der zwischen diesen Buchstaben steht, wird nicht verändert (\",\" als Trennung verwenden) (Liste) [- '[,]']", + "distortBypass:", + "- '*,*'", + "- '[,]'", + "" + }; + index = indexOfStart("# words"); + if (index == -1) { + index = indexOfStart("# Diese werden von oben"); + } + if (index == -1) { + index = indexOfStart("# replace"); + } + if (index == -1) { + index = indexOfStart("words:"); + } + if (index == -1) { + appendLines(entries); + } else { + addLines(index, entries); + } + + // Add some new separators for overview + line = "# -- Verschiedene Einstellungen --"; + index = indexOfStart("# Verschiedene Einstellungen"); + if (index != -1) { + setLine(index, line); + } + + line = "# -- Rezepte für Getränke --"; + index = indexOfStart("# Rezepte für Getränke"); + if (index != -1) { + setLine(index, line); + } + } + + // Updates en from 1.0 to 1.1 + private void update10en() { + // Update version String + int index = indexOfStart("version"); + String line = "version: '1.1'"; + if (index != -1) { + setLine(index, line); + } else { + index = indexOfStart("# Config Version"); + if (index == -1) { + index = indexOfStart("autosave"); + } + if (index == -1) { + appendLines(line); + } else { + addLines(index, line); + } + } + + // Add the new entries for the Word Distortion above the words section + String[] entries = { + "# -- Chat Distortion Settings --", + "", + "# Text after specified commands will be distorted when drunk (list) [- /gl]", + "distortCommands:", + "- /gl", + "- /global", + "- /fl", + "- /s", + "- /letter", + "", + "# Distort the Text written on a Sign while drunk [false]", + "distortSignText: false", + "", + "# Enclose a text with these Letters to bypass Chat Distortion (Use \",\" as Separator) (list) [- '[,]']", + "distortBypass:", + "- '*,*'", + "- '[,]'", + "" + }; + index = indexOfStart("# words"); + if (index == -1) { + index = indexOfStart("# Will be processed"); + } + if (index == -1) { + index = indexOfStart("# replace"); + } + if (index == -1) { + index = indexOfStart("words:"); + } + if (index == -1) { + appendLines(entries); + } else { + addLines(index, entries); + } + + // Add some new separators for overview + line = "# -- Settings --"; + index = indexOfStart("# Settings"); + if (index != -1) { + setLine(index, line); + } + + line = "# -- Recipes for Potions --"; + index = indexOfStart("# Recipes for Potions"); + if (index != -1) { + setLine(index, line); + } + } + +} diff --git a/src/com/dre/brewery/LanguageReader.java b/src/com/dre/brewery/LanguageReader.java index 79e714f..228d57b 100644 --- a/src/com/dre/brewery/LanguageReader.java +++ b/src/com/dre/brewery/LanguageReader.java @@ -91,6 +91,7 @@ public class LanguageReader { defaults.put("Error_PlayerCommand", "&cThis command can only be executed as player"); defaults.put("Error_ItemNotPotion", "&cThe Item in your hand could not be identified as Potion"); defaults.put("Error_Recipeload", "&cNot all recipes could be restored: More information in the Serverlog!"); + defaults.put("Error_ConfigUpdate", "Unknown Brewery Config version: v&v1, Config was not Updated!"); /* Help */ defaults.put("Help_Help", "&6/br help &9Shows a specific help-page"); diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index 264638e..e1d14d6 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -9,7 +9,6 @@ import java.io.IOException; import java.io.File; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.ConfigurationSection; @@ -157,10 +156,23 @@ public class P extends JavaPlugin { } FileConfiguration config = YamlConfiguration.loadConfiguration(file); + // Set the Language + language = config.getString("language", "en"); + + // Check if config is the newest version + String version = config.getString("version", null); + if (version != null) { + String currentVersion = getDescription().getVersion(); + if (!version.equals(currentVersion)) { + new ConfigUpdater(file).update(version, language); + P.p.log("Config Updated to version: " + currentVersion); + config = YamlConfiguration.loadConfiguration(file); + } + } + // various Settings autosave = config.getInt("autosave", 3); debug = config.getBoolean("debug", false); - language = config.getString("language", "en"); BPlayer.pukeItemId = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND")).getId(); BPlayer.hangoverTime = config.getInt("hangoverDays", 0) * 24 * 60; BPlayer.overdrinkKick = config.getBoolean("enableKickOnOverdrink", false);