Brewery/src/com/dre/brewery/filedata/ConfigUpdater.java

647 lines
18 KiB
Java
Raw Normal View History

2014-09-04 13:06:34 +02:00
package com.dre.brewery.filedata;
2013-11-21 22:08:47 +01:00
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;
2014-09-04 13:06:34 +02:00
import com.dre.brewery.P;
2013-11-21 22:08:47 +01:00
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).append("\n");
2013-11-21 22:08:47 +01:00
}
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";
}
}
2013-11-21 22:08:47 +01:00
if (fromVersion.equals("0.5") || fromVersion.equals("1.0")) {
if (lang.equals("de")) {
update05de();
} else {
update10en();
}
fromVersion = "1.1";
}
if (fromVersion.equals("1.1") || fromVersion.equals("1.1.1")) {
if (lang.equals("de")) {
update11de();
} else {
update11en();
}
fromVersion = "1.2";
}
if (fromVersion.equals("1.2")) {
if (lang.equals("de")) {
update12de();
} else {
update12en();
}
fromVersion = "1.3";
}
2015-01-09 18:08:38 +01:00
if (fromVersion.equals("1.3")) {
if (lang.equals("de")) {
update13de();
} else {
update13en();
}
fromVersion = "1.3.1";
}
if (!fromVersion.equals("1.3.1")) {
2013-11-21 22:08:47 +01:00
P.p.log(P.p.languageReader.get("Error_ConfigUpdate", fromVersion));
return;
}
saveConfig();
}
// Update the Version String
private void updateVersion(String to) {
2013-11-21 22:08:47 +01:00
int index = indexOfStart("version");
String line = "version: '" + to + "'";
2013-11-21 22:08:47 +01:00
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);
}
}
}
// Updates de from 0.5 to 1.1
private void update05de() {
updateVersion("1.1");
2013-11-21 22:08:47 +01:00
// Default language to de
int index = indexOfStart("language: en");
2013-11-21 22:08:47 +01:00
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
String line = "# -- Verschiedene Einstellungen --";
2013-11-21 22:08:47 +01:00
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
updateVersion("1.1");
2013-11-21 22:08:47 +01:00
// 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:",
"- '*,*'",
"- '[,]'",
""
};
int index = indexOfStart("# words");
2013-11-21 22:08:47 +01:00
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
String line = "# -- Settings --";
2013-11-21 22:08:47 +01:00
index = indexOfStart("# Settings");
if (index != -1) {
setLine(index, line);
}
line = "# -- Recipes for Potions --";
index = indexOfStart("# Recipes for Potions");
if (index != -1) {
setLine(index, line);
}
}
// Updates de from 1.1 to 1.2
private void update11de() {
updateVersion("1.2");
int index = indexOfStart("# Das Item kann nicht aufgesammelt werden");
if (index != -1) {
setLine(index, "# Das Item kann nicht aufgesammelt werden und bleibt bis zum Despawnen liegen. (Achtung: Kann nach Serverrestart aufgesammelt werden!)");
}
// Add the BarrelAccess Setting
String[] lines = {
"# Ob große Fässer an jedem Block geöffnet werden können, nicht nur an Zapfhahn und Schild. Bei kleinen Fässern geht dies immer. [true]",
"openLargeBarrelEverywhere: true",
""
};
index = indexOfStart("colorInBrewer") + 2;
if (index == 1) {
index = indexOfStart("colorInBarrels") + 2;
}
if (index == 1) {
index = indexOfStart("# Autosave");
}
if (index == -1) {
index = indexOfStart("language") + 2;
}
if (index == 1) {
addLines(3, lines);
} else {
addLines(index, lines);
}
// Add Plugin Support Settings
lines = new String[] {
"",
"# -- Plugin Kompatiblität --",
"",
"# Andere Plugins (wenn installiert) nach Rechten zum öffnen von Fässern checken [true]",
"useWorldGuard: true",
"useLWC: true",
"useGriefPrevention: true",
"",
2014-05-07 16:12:17 +02:00
"# Änderungen an Fassinventaren mit LogBlock aufzeichen [true]",
"useLogBlock: true",
"",
""
};
index = indexOfStart("# -- Chat Veränderungs Einstellungen");
if (index == -1) {
index = indexOfStart("# words");
}
if (index == -1) {
index = indexOfStart("distortCommands");
if (index > 4) {
index -= 4;
}
}
if (index != -1) {
addLines(index, lines);
} else {
appendLines(lines);
}
}
// Updates en from 1.1 to 1.2
private void update11en() {
updateVersion("1.2");
int index = indexOfStart("# The item can not be collected");
if (index != -1) {
setLine(index, "# The item can not be collected and stays on the ground until it despawns. (Warning: Can be collected after Server restart!)");
}
// Add the BarrelAccess Setting
String[] lines = {
"# If a Large Barrel can be opened by clicking on any of its blocks, not just Spigot or Sign. This is always true for Small Barrels. [true]",
"openLargeBarrelEverywhere: true",
""
};
index = indexOfStart("colorInBrewer") + 2;
if (index == 1) {
index = indexOfStart("colorInBarrels") + 2;
}
if (index == 1) {
index = indexOfStart("# Autosave");
}
if (index == -1) {
index = indexOfStart("language") + 2;
}
if (index == 1) {
addLines(3, lines);
} else {
addLines(index, lines);
}
// Add Plugin Support Settings
lines = new String[] {
"",
"# -- Plugin Compatibility --",
"",
"# Enable checking of other Plugins (if installed) for Barrel Permissions [true]",
"useWorldGuard: true",
"useLWC: true",
"useGriefPrevention: true",
"",
2014-05-07 16:12:17 +02:00
"# Enable the Logging of Barrel Inventories to LogBlock [true]",
"useLogBlock: true",
"",
""
};
index = indexOfStart("# -- Chat Distortion Settings");
if (index == -1) {
index = indexOfStart("# words");
}
if (index == -1) {
index = indexOfStart("distortCommands");
if (index > 4) {
index -= 4;
}
}
if (index != -1) {
addLines(index, lines);
} else {
appendLines(lines);
}
}
2014-08-20 17:40:10 +02:00
// Update de from 1.2 to 1.3
private void update12de() {
updateVersion("1.3");
// Add the new Wood Types to the Description
int index = indexOfStart("# wood:");
if (index != -1) {
2014-09-03 01:05:53 +02:00
setLine(index, "# wood: Holz des Fasses 0=alle Holzsorten 1=Birke 2=Eiche 3=Jungel 4=Fichte 5=Akazie 6=Schwarzeiche");
}
2014-08-20 17:40:10 +02:00
// Add the Example to the Cooked Section
index = indexOfStart("# cooked:");
2014-08-20 17:40:10 +02:00
if (index != -1) {
addLines(index + 1, "# [Beispiel] MATERIAL_oder_id: Name nach Gähren");
2014-08-20 17:40:10 +02:00
}
// Add new ingredients description
2014-09-03 01:05:53 +02:00
String replacedLine = "# ingredients: Auflistung von 'Material oder ID,Data/Anzahl'";
2014-08-20 17:40:10 +02:00
String[] lines = new String[] {
2014-09-03 01:05:53 +02:00
"# (Item-ids anstatt Material werden von Bukkit nicht mehr unterstützt und funktionieren möglicherweise in Zukunft nicht mehr!)",
2014-08-20 17:40:10 +02:00
"# Eine Liste von allen Materialien kann hier gefunden werden: http://jd.bukkit.org/beta/apidocs/org/bukkit/Material.html",
"# Es kann ein Data-Wert angegeben werden, weglassen ignoriert diesen beim hinzufügen einer Zutat"
};
index = indexOfStart("# ingredients:");
if (index != -1) {
setLine(index, replacedLine);
addLines(index + 1, lines);
2014-08-20 17:40:10 +02:00
} else {
index = indexOfStart("# name:");
if (index != -1) {
addLines(index + 1, lines);
addLines(index + 1, replacedLine);
2014-08-20 17:40:10 +02:00
} else {
index = indexOfStart("# -- Rezepte für Getränke --");
if (index != -1) {
addLines(index + 2, lines);
addLines(index + 2, "", replacedLine);
2014-08-20 17:40:10 +02:00
}
}
}
2014-09-03 01:05:53 +02:00
// Split the Color explanation into two lines
replacedLine = "# color: Farbe des Getränks nach destillieren/reifen.";
lines = new String[] {
"# Benutzbare Farben: DARK_RED, RED, BRIGHT_RED, ORANGE, PINK, BLUE, CYAN, WATER, GREEN, BLACK, GREY, BRIGHT_GREY"
};
index = indexOfStart("# color:");
if (index != -1) {
setLine(index, replacedLine);
addLines(index + 1, lines);
} else {
index = indexOfStart("# age:");
if (index != -1) {
addLines(index + 1, lines);
addLines(index + 1, replacedLine);
}
}
// Add all the new info to the effects description
replacedLine = "# effects: Auflistung Effekt/Level/Dauer Besonderere Trank-Effekte beim Trinken, Dauer in sek.";
lines = new String[] {
"# Ein 'X' an den Namen anhängen, um ihn zu verbergen. Bsp: 'POISONX/2/10' (WEAKNESS, INCREASE_DAMAGE, SLOW und SPEED sind immer verborgen.)",
"# Mögliche Effekte: http://jd.bukkit.org/rb/apidocs/org/bukkit/potion/PotionEffectType.html",
"# Minimale und Maximale Level/Dauer können durch \"-\" festgelegt werden, Bsp: 'SPEED/1-2/30-40' = Level 1 und 30 sek minimal, Level 2 und 40 sek maximal",
"# Diese Bereiche funktionieren auch umgekehrt, Bsp: 'POISON/3-1/20-5' für abschwächende Effekte bei guter Qualität",
"# Längste mögliche Effektdauer: 1638 sek. Es muss keine Dauer für Effekte mit sofortiger Wirkung angegeben werden."
};
index = indexOfStart("# effects:");
if (index != -1) {
setLine(index, replacedLine);
addLines(index + 1, lines);
} else {
index = indexOfStart("# alcohol:");
if (index != -1) {
addLines(index + 1, lines);
addLines(index + 1, replacedLine);
} else {
index = indexOfStart("# -- Rezepte für Getränke --");
if (index != -1) {
addLines(index + 2, lines);
addLines(index + 2, "", replacedLine);
}
}
}
if (index != -1) {
index = indexOfStart("# (WEAKNESS, INCREASE_DAMAGE, SLOW und SPEED sind immer verborgen.) Mögliche Effekte:");
if (index != -1) {
config.remove(index);
}
}
index = indexOfStart("# Bei Effekten mit sofortiger Wirkung ");
if (index != -1) {
config.remove(index);
}
2014-08-20 17:40:10 +02:00
}
// Update en from 1.2 to 1.3
private void update12en() {
updateVersion("1.3");
// Add the new Wood Types to the Description
int index = indexOfStart("# wood:");
if (index != -1) {
2014-09-03 01:05:53 +02:00
setLine(index, "# wood: Wood of the barrel 0=any 1=Birch 2=Oak 3=Jungle 4=Spruce 5=Acacia 6=Dark Oak");
}
2014-08-20 17:40:10 +02:00
// Add the Example to the Cooked Section
index = indexOfStart("# cooked:");
2014-08-20 17:40:10 +02:00
if (index != -1) {
addLines(index + 1, "# [Example] MATERIAL_or_id: Name after cooking");
2014-08-20 17:40:10 +02:00
}
// Add new ingredients description
2014-09-03 01:05:53 +02:00
String replacedLine = "# ingredients: List of 'material or id,data/amount'";
2014-08-20 17:40:10 +02:00
String[] lines = new String[] {
2014-09-03 01:05:53 +02:00
"# (Item-ids instead of material are deprecated by bukkit and may not work in the future!)",
2014-08-20 17:40:10 +02:00
"# A list of materials can be found here: http://jd.bukkit.org/beta/apidocs/org/bukkit/Material.html",
"# You can specify a data value, omitting it will ignore the data value of the added ingredient"
};
index = indexOfStart("# ingredients:");
if (index != -1) {
setLine(index, replacedLine);
addLines(index + 1, lines);
2014-08-20 17:40:10 +02:00
} else {
index = indexOfStart("# name:");
if (index != -1) {
addLines(index + 1, lines);
addLines(index + 1, replacedLine);
2014-08-20 17:40:10 +02:00
} else {
index = indexOfStart("# -- Recipes for Potions --");
if (index != -1) {
addLines(index + 2, lines);
addLines(index + 2, "", replacedLine);
2014-08-20 17:40:10 +02:00
}
}
}
2014-09-03 01:05:53 +02:00
// Split the Color explanation into two lines
replacedLine = "# color: Color of the potion after distilling/aging.";
lines = new String[] {
"# Usable Colors: DARK_RED, RED, BRIGHT_RED, ORANGE, PINK, BLUE, CYAN, WATER, GREEN, BLACK, GREY, BRIGHT_GREY"
};
index = indexOfStart("# color:");
if (index != -1) {
setLine(index, replacedLine);
addLines(index + 1, lines);
} else {
index = indexOfStart("# age:");
if (index != -1) {
addLines(index + 1, lines);
addLines(index + 1, replacedLine);
}
}
// Add all the new info to the effects description
replacedLine = "# effects: List of effect/level/duration Special potion-effect when drinking, duration in sek.";
lines = new String[] {
"# Suffix name with 'X' to hide effect from label. Sample: 'POISONX/2/10' (WEAKNESS, INCREASE_DAMAGE, SLOW and SPEED are always hidden.)",
"# Possible Effects: http://jd.bukkit.org/rb/apidocs/org/bukkit/potion/PotionEffectType.html",
"# Level or Duration ranges may be specified with a \"-\", ex. 'SPEED/1-2/30-40' = lvl 1 and 30 sec at worst and lvl 2 and 40 sec at best",
"# Ranges also work high-low, ex. 'POISON/3-1/20-5' for weaker effects at good quality.",
"# Highest possible Duration: 1638 sec. Instant Effects dont need any duration specified."
};
index = indexOfStart("# effects:");
if (index != -1) {
setLine(index, replacedLine);
addLines(index + 1, lines);
} else {
index = indexOfStart("# alcohol:");
if (index != -1) {
addLines(index + 1, lines);
addLines(index + 1, replacedLine);
} else {
index = indexOfStart("# -- Recipes for Potions --");
if (index != -1) {
addLines(index + 2, lines);
addLines(index + 2, "", replacedLine);
}
}
}
if (index != -1) {
index = indexOfStart("# (WEAKNESS, INCREASE_DAMAGE, SLOW and SPEED are always hidden.) Possible Effects:");
if (index != -1) {
config.remove(index);
}
}
index = indexOfStart("# instant effects ");
if (index != -1) {
config.remove(index);
}
2014-08-20 17:40:10 +02:00
}
2015-01-09 18:08:38 +01:00
private void update13de() {
updateVersion("1.3.1");
int index = indexOfStart("# Autosave");
String[] lines = new String[] { "# Aktiviert das Suchen nach Updates für Brewery mit der curseforge api [true]",
"# Wenn ein Update gefunden wurde, wird dies bei Serverstart im log angezeigt, sowie ops benachrichtigt",
"updateCheck: true",
"" };
if (index == -1) {
index = indexOfStart("autosave:");
if (index == -1) {
index = indexOfStart("# Sprachedatei");
if (index == -1) {
index = indexOfStart("language:");
}
}
}
if (index == -1) {
appendLines(lines);
} else {
addLines(index, lines);
}
}
private void update13en() {
updateVersion("1.3.1");
int index = indexOfStart("# Autosave");
String[] lines = new String[] { "# Enable checking for Updates, Checks the curseforge api for updates to Brewery [true]",
"# If an Update is found a Message is logged on Server-start and displayed to ops joining the game",
"updateCheck: true",
"" };
if (index == -1) {
index = indexOfStart("autosave:");
if (index == -1) {
index = indexOfStart("# Languagefile");
if (index == -1) {
index = indexOfStart("language:");
}
}
}
if (index == -1) {
appendLines(lines);
} else {
addLines(index, lines);
}
}
2013-11-21 22:08:47 +01:00
}