Added ConfigUpdater

This commit is contained in:
Sn0wStorm 2013-11-21 22:08:47 +01:00
parent 200f41f0c9
commit 9d5b90254a
5 changed files with 267 additions and 5 deletions

View File

@ -1,8 +1,7 @@
name: Brewery name: Brewery
version: 1.0 version: 1.1
main: com.dre.brewery.P main: com.dre.brewery.P
authors: [Milan Albrecht, Frank Baumann] authors: [Milan Albrecht, Frank Baumann]
softdepend: [Vault]
commands: commands:
brewery: brewery:
description: Command for Administration description: Command for Administration

View File

@ -3,7 +3,7 @@
<groupId>com.dre</groupId> <groupId>com.dre</groupId>
<artifactId>brewery</artifactId> <artifactId>brewery</artifactId>
<version>1.0</version> <version>1.1</version>
<name>Brewery</name> <name>Brewery</name>
<build> <build>

View File

@ -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<String> config = new ArrayList<String>();
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);
}
}
}

View File

@ -91,6 +91,7 @@ public class LanguageReader {
defaults.put("Error_PlayerCommand", "&cThis command can only be executed as player"); 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_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_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 */ /* Help */
defaults.put("Help_Help", "&6/br help <Page> &9Shows a specific help-page"); defaults.put("Help_Help", "&6/br help <Page> &9Shows a specific help-page");

View File

@ -9,7 +9,6 @@ import java.io.IOException;
import java.io.File; import java.io.File;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -157,10 +156,23 @@ public class P extends JavaPlugin {
} }
FileConfiguration config = YamlConfiguration.loadConfiguration(file); 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 // various Settings
autosave = config.getInt("autosave", 3); autosave = config.getInt("autosave", 3);
debug = config.getBoolean("debug", false); debug = config.getBoolean("debug", false);
language = config.getString("language", "en");
BPlayer.pukeItemId = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND")).getId(); BPlayer.pukeItemId = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND")).getId();
BPlayer.hangoverTime = config.getInt("hangoverDays", 0) * 24 * 60; BPlayer.hangoverTime = config.getInt("hangoverDays", 0) * 24 * 60;
BPlayer.overdrinkKick = config.getBoolean("enableKickOnOverdrink", false); BPlayer.overdrinkKick = config.getBoolean("enableKickOnOverdrink", false);