mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 11:18:57 +00:00
Threaded savefile writes
This commit is contained in:
@@ -0,0 +1,587 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
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;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
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");
|
||||
}
|
||||
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();
|
||||
}
|
||||
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";
|
||||
}
|
||||
|
||||
if (!fromVersion.equals("1.3")) {
|
||||
P.p.log(P.p.languageReader.get("Error_ConfigUpdate", fromVersion));
|
||||
return;
|
||||
}
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
// Update the Version String
|
||||
private void updateVersion(String to) {
|
||||
int index = indexOfStart("version");
|
||||
String line = "version: '" + to + "'";
|
||||
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");
|
||||
|
||||
// Default language to de
|
||||
int 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
|
||||
String 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
|
||||
updateVersion("1.1");
|
||||
|
||||
// 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");
|
||||
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 --";
|
||||
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",
|
||||
"",
|
||||
"# Ä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",
|
||||
"",
|
||||
"# 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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
setLine(index, "# wood: Holz des Fasses 0=alle Holzsorten 1=Birke 2=Eiche 3=Jungel 4=Fichte 5=Akazie 6=Schwarzeiche");
|
||||
}
|
||||
|
||||
// Add the Example to the Cooked Section
|
||||
index = indexOfStart("# cooked:");
|
||||
if (index != -1) {
|
||||
addLines(index + 1, "# [Beispiel] MATERIAL_oder_id: Name nach Gähren");
|
||||
}
|
||||
|
||||
// Add new ingredients description
|
||||
String replacedLine = "# ingredients: Auflistung von 'Material oder ID,Data/Anzahl'";
|
||||
String[] lines = new String[] {
|
||||
"# (Item-ids anstatt Material werden von Bukkit nicht mehr unterstützt und funktionieren möglicherweise in Zukunft nicht mehr!)",
|
||||
"# 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);
|
||||
} else {
|
||||
index = indexOfStart("# name:");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 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) {
|
||||
setLine(index, "# wood: Wood of the barrel 0=any 1=Birch 2=Oak 3=Jungle 4=Spruce 5=Acacia 6=Dark Oak");
|
||||
}
|
||||
|
||||
// Add the Example to the Cooked Section
|
||||
index = indexOfStart("# cooked:");
|
||||
if (index != -1) {
|
||||
addLines(index + 1, "# [Example] MATERIAL_or_id: Name after cooking");
|
||||
}
|
||||
|
||||
// Add new ingredients description
|
||||
String replacedLine = "# ingredients: List of 'material or id,data/amount'";
|
||||
String[] lines = new String[] {
|
||||
"# (Item-ids instead of material are deprecated by bukkit and may not work in the future!)",
|
||||
"# 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);
|
||||
} else {
|
||||
index = indexOfStart("# name:");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import com.dre.brewery.BCauldron;
|
||||
import com.dre.brewery.BPlayer;
|
||||
import com.dre.brewery.Barrel;
|
||||
import com.dre.brewery.Brew;
|
||||
import com.dre.brewery.P;
|
||||
import com.dre.brewery.Wakeup;
|
||||
|
||||
public class DataSave extends BukkitRunnable {
|
||||
|
||||
public static int lastBackup = 0;
|
||||
public static int lastSave = 1;
|
||||
public static int autosave = 3;
|
||||
final public static String dataVersion = "1.1";
|
||||
public static DataSave running;
|
||||
|
||||
public ReadOldData read;
|
||||
private long time;
|
||||
public boolean collected = false;
|
||||
|
||||
// Not Thread-Safe! Needs to be run in main thread but uses async Read/Write
|
||||
public DataSave(ReadOldData read) {
|
||||
this.read = read;
|
||||
time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
FileConfiguration oldData;
|
||||
if (read != null) {
|
||||
if (!read.done) {
|
||||
// Wait for async thread to load old data
|
||||
if (System.currentTimeMillis() - time > 30000) {
|
||||
P.p.errorLog("Old Data took too long to load!");
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
oldData = read.getData();
|
||||
} else {
|
||||
oldData = new YamlConfiguration();
|
||||
}
|
||||
try {
|
||||
cancel();
|
||||
} catch (IllegalStateException e) {
|
||||
}
|
||||
|
||||
FileConfiguration configFile = new YamlConfiguration();
|
||||
|
||||
if (!Brew.potions.isEmpty()) {
|
||||
Brew.save(configFile.createSection("Brew"));
|
||||
}
|
||||
|
||||
if (!BCauldron.bcauldrons.isEmpty() || oldData.contains("BCauldron")) {
|
||||
BCauldron.save(configFile.createSection("BCauldron"), oldData.getConfigurationSection("BCauldron"));
|
||||
}
|
||||
|
||||
if (!Barrel.barrels.isEmpty() || oldData.contains("Barrel")) {
|
||||
Barrel.save(configFile.createSection("Barrel"), oldData.getConfigurationSection("Barrel"));
|
||||
}
|
||||
|
||||
if (!BPlayer.isEmpty()) {
|
||||
BPlayer.save(configFile.createSection("Player"));
|
||||
}
|
||||
|
||||
if (!Wakeup.wakeups.isEmpty() || oldData.contains("Wakeup")) {
|
||||
Wakeup.save(configFile.createSection("Wakeup"), oldData.getConfigurationSection("Wakeup"));
|
||||
}
|
||||
|
||||
saveWorldNames(configFile, oldData.getConfigurationSection("Worlds"));
|
||||
configFile.set("Version", dataVersion);
|
||||
|
||||
collected = true;
|
||||
if (P.p.isEnabled()) {
|
||||
P.p.getServer().getScheduler().runTaskAsynchronously(P.p, new WriteData(configFile));
|
||||
} else {
|
||||
new WriteData(configFile).run();
|
||||
}
|
||||
}
|
||||
|
||||
// Finish the collection of data immediately
|
||||
public void now() {
|
||||
if (!read.done) {
|
||||
read.cancel();
|
||||
read.run();
|
||||
}
|
||||
if (!collected) {
|
||||
cancel();
|
||||
run();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Save all data. Takes a boolean whether all data should be collected in instantly
|
||||
public static void save(boolean collectInstant) {
|
||||
long time = System.nanoTime();
|
||||
if (running != null) {
|
||||
P.p.log("Another Save was started while a Save was in Progress");
|
||||
if (collectInstant) {
|
||||
running.now();
|
||||
}
|
||||
return;
|
||||
}
|
||||
File datafile = new File(P.p.getDataFolder(), "data.yml");
|
||||
|
||||
if (datafile.exists()) {
|
||||
ReadOldData read = new ReadOldData();
|
||||
if (collectInstant) {
|
||||
read.run();
|
||||
running = new DataSave(read);
|
||||
running.run();
|
||||
} else {
|
||||
read.runTaskAsynchronously(P.p);
|
||||
running = new DataSave(read);
|
||||
running.runTaskTimer(P.p, 1, 2);
|
||||
}
|
||||
} else {
|
||||
running = new DataSave(null);
|
||||
running.runTask(P.p);
|
||||
}
|
||||
P.p.debugLog("saving: " + ((System.nanoTime() - time) / 1000000.0) + "ms");
|
||||
}
|
||||
|
||||
public static void autoSave() {
|
||||
if (lastSave >= autosave) {
|
||||
save(false);// save all data
|
||||
} else {
|
||||
lastSave++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveWorldNames(FileConfiguration root, ConfigurationSection old) {
|
||||
if (old != null) {
|
||||
root.set("Worlds", old);
|
||||
}
|
||||
for (World world : P.p.getServer().getWorlds()) {
|
||||
String worldName = world.getName();
|
||||
if (worldName.startsWith("DXL_")) {
|
||||
worldName = P.p.getDxlName(worldName);
|
||||
root.set("Worlds." + worldName, 0);
|
||||
} else {
|
||||
worldName = world.getUID().toString();
|
||||
root.set("Worlds." + worldName, world.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
public class DataUpdater {
|
||||
|
||||
private FileConfiguration data;
|
||||
private File file;
|
||||
|
||||
public DataUpdater(FileConfiguration data, File file) {
|
||||
this.data = data;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void update(String fromVersion) {
|
||||
if (fromVersion.equalsIgnoreCase("1.0")) {
|
||||
update10();
|
||||
//fromVersion = "1.1";
|
||||
}
|
||||
|
||||
try {
|
||||
data.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void update10() {
|
||||
|
||||
data.set("Version", DataSave.dataVersion);
|
||||
|
||||
ConfigurationSection section = data.getConfigurationSection("Ingredients");
|
||||
try {
|
||||
if (section != null) {
|
||||
for (String id : section.getKeys(false)) {
|
||||
ConfigurationSection matSection = section.getConfigurationSection(id + ".mats");
|
||||
if (matSection != null) {
|
||||
// matSection has all the materials + amount as Integers
|
||||
Map<String, Integer> ingredients = new HashMap<String, Integer>();
|
||||
for (String ingredient : matSection.getKeys(false)) {
|
||||
// convert to Material
|
||||
Material mat = Material.getMaterial(P.p.parseInt(ingredient));
|
||||
if (mat != null) {
|
||||
ingredients.put(mat.name(), matSection.getInt(ingredient));
|
||||
}
|
||||
}
|
||||
section.set(id + ".mats", ingredients);
|
||||
} else {
|
||||
P.p.errorLog("Ingredient id: '" + id + "' incomplete in data.yml");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Getting Material by id may not work in the future
|
||||
P.p.errorLog("Error Converting Ingredient Section of the Data File, newer versions of Bukkit may not support the old Save File anymore:");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
section = data.getConfigurationSection("BCauldron");
|
||||
if (section != null) {
|
||||
try {
|
||||
for (String uuid : section.getKeys(false)) {
|
||||
ConfigurationSection cauldrons = section.getConfigurationSection(uuid);
|
||||
if (cauldrons != null) {
|
||||
for (String id : cauldrons.getKeys(false)) {
|
||||
ConfigurationSection ingredientSection = cauldrons.getConfigurationSection(id + ".ingredients");
|
||||
if (ingredientSection != null) {
|
||||
// has all the materials + amount as Integers
|
||||
Map<String, Integer> ingredients = new HashMap<String, Integer>();
|
||||
for (String ingredient : ingredientSection.getKeys(false)) {
|
||||
// convert to Material
|
||||
Material mat = Material.getMaterial(P.p.parseInt(ingredient));
|
||||
if (mat != null) {
|
||||
ingredients.put(mat.name(), ingredientSection.getInt(ingredient));
|
||||
}
|
||||
}
|
||||
cauldrons.set(id + ".ingredients", ingredients);
|
||||
} else {
|
||||
P.p.errorLog("BCauldron " + id + " is missing Ingredient Section");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Getting Material by id may not work in the future
|
||||
P.p.errorLog("Error Converting Ingredient Section of Cauldrons, newer versions of Bukkit may not support the old Save File anymore:");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class LanguageReader {
|
||||
private Map<String, String> entries = new TreeMap<String, String>();
|
||||
private Map<String, String> defaults = new TreeMap<String, String>();
|
||||
|
||||
private File file;
|
||||
private boolean changed;
|
||||
|
||||
public LanguageReader(File file) {
|
||||
this.setDefaults();
|
||||
|
||||
/* Load */
|
||||
this.file = file;
|
||||
|
||||
FileConfiguration configFile = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
Set<String> keySet = configFile.getKeys(false);
|
||||
for (String key : keySet) {
|
||||
entries.put(key, configFile.getString(key));
|
||||
}
|
||||
|
||||
/* Check */
|
||||
this.check();
|
||||
}
|
||||
|
||||
private void setDefaults() {
|
||||
|
||||
/* Player */
|
||||
defaults.put("Player_BarrelCreated", "Barrel created");
|
||||
defaults.put("Player_CauldronInfo1", "This cauldron has been boiling for &v1 minutes.");
|
||||
defaults.put("Player_CauldronInfo2", "This cauldron has just started boiling.");
|
||||
defaults.put("Player_CantDrink", "You can't drink any more.");
|
||||
defaults.put("Player_DrunkPassOut", "You drank too much and passed out.");
|
||||
defaults.put("Player_LoginDeny", "Your character tries to log in, but is too drunk to find the server. Try again!");
|
||||
defaults.put("Player_LoginDenyLong", "Your character is really drunk and has passed out. Try again in 10 minutes!");
|
||||
defaults.put("Player_Wake", "Ohh no! I cannot remember how I got here...");
|
||||
defaults.put("Player_WakeCreated", "&aWakeup Point with id: &6&v1 &awas created successfully!");
|
||||
defaults.put("Player_WakeNotExist", "&cThe Wakeup Point with the id: &6&v1 &cdoesn't exist!");
|
||||
defaults.put("Player_WakeDeleted", "&aThe Wakeup Point with the id: &6&v1 &awas successfully deleted!");
|
||||
defaults.put("Player_WakeAlreadyDeleted", "&cThe Wakeup Point with the id: &6&v1 &chas already been deleted!");
|
||||
defaults.put("Player_WakeFilled", "&cThe Wakeup Point with the id: &6&v1&c at position &6&v2 &v3, &v4, &v5&c is filled with Blocks!");
|
||||
defaults.put("Player_WakeNoPoints", "&cThere are no Wakeup Points!");
|
||||
defaults.put("Player_WakeLast", "&aThis was the last Wakeup Point");
|
||||
defaults.put("Player_WakeTeleport", "Teleport to Wakeup Point with the id: &6&v1&f At position: &6&v2 &v3, &v4, &v5");
|
||||
defaults.put("Player_WakeHint1", "To Next Wakeup Point: Punch your fist in the air");
|
||||
defaults.put("Player_WakeHint2", "To Cancel: &9/br wakeup cancel");
|
||||
defaults.put("Player_WakeCancel", "&6Wakeup Point Check was cancelled");
|
||||
defaults.put("Player_WakeNoCheck", "&cNo Wakeup Point Check is currently active");
|
||||
defaults.put("Player_TriedToSay", "&v1 tried to say: &0&v2");
|
||||
|
||||
/* Brew */
|
||||
defaults.put("Brew_Distilled", "Distilled");
|
||||
defaults.put("Brew_BarrelRiped", "Barrel aged");
|
||||
defaults.put("Brew_Undefined", "Indefinable Brew");
|
||||
defaults.put("Brew_DistillUndefined", "Indefinable Distillate");
|
||||
defaults.put("Brew_BadPotion", "Ruined Potion");
|
||||
defaults.put("Brew_Ingredients", "Ingredients");
|
||||
defaults.put("Brew_minute", "minute");
|
||||
defaults.put("Brew_MinutePluralPostfix", "s");
|
||||
defaults.put("Brew_fermented", "fermented");
|
||||
defaults.put("Brew_-times", "-times");
|
||||
defaults.put("Brew_OneYear", "One Year");
|
||||
defaults.put("Brew_Years", "Years");
|
||||
defaults.put("Brew_HundredsOfYears", "Hundreds of Years");
|
||||
defaults.put("Brew_Woodtype", "Woodtype");
|
||||
defaults.put("Brew_ThickBrew", "Muddy Brew");
|
||||
|
||||
/* Commands */
|
||||
defaults.put("CMD_Reload", "&aConfig was successfully reloaded");
|
||||
defaults.put("CMD_Player", "&a&v1 is now &6&v2% &adrunk, with a quality of &6&v3");
|
||||
defaults.put("CMD_Player_Error", "&cThe quality has to be between 1 and 10!");
|
||||
defaults.put("CMD_Info_NotDrunk", "&v1 is not drunk");
|
||||
defaults.put("CMD_Info_Drunk", "&v1 is &6&v2% &fdrunk, with a quality of &6&v3");
|
||||
defaults.put("CMD_UnLabel", "&aLabel removed!");
|
||||
defaults.put("CMD_Persistent", "&aPotion is now Persistent and Static and may now be copied like any other item. You can remove the persistence with the same command.");
|
||||
defaults.put("CMD_PersistRemove", "&cPersistent Brews cannot be removed from the Database. It would render any copies of them useless!");
|
||||
defaults.put("CMD_UnPersist", "&aPersistence and static Removed. &eEvery Potential copy NOT made with '/brew copy' could become useless now!");
|
||||
defaults.put("CMD_Copy_Error", "&6&v1 &cPotions did not fit into your inventory");
|
||||
defaults.put("CMD_CopyNotPersistent", "&eThese copies of this Brew will not be persistent or static!");
|
||||
defaults.put("CMD_Static", "&aPotion is now static and will not change in barrels or brewing stands.");
|
||||
defaults.put("CMD_NonStatic", "&ePotion is not static anymore and will normally age in barrels.");
|
||||
|
||||
/* Error */
|
||||
defaults.put("Error_UnknownCommand", "Unknown Command");
|
||||
defaults.put("Error_ShowHelp", "Use &6/brew help &fto display the help");
|
||||
defaults.put("Error_PlayerCommand", "&cThis command can only be executed as a player!");
|
||||
defaults.put("Error_ItemNotPotion", "&cThe item in your hand could not be identified as a potion!");
|
||||
defaults.put("Error_NoBrewName", "&cNo Recipe with Name: '&v1&c' found!");
|
||||
defaults.put("Error_Recipeload", "&cNot all recipes could be restored: More information in the server log!");
|
||||
defaults.put("Error_ConfigUpdate", "Unknown Brewery config version: v&v1, config was not updated!");
|
||||
defaults.put("Error_PersistStatic", "&cPersistent potions are always static!");
|
||||
|
||||
/* Permissions */
|
||||
defaults.put("Error_NoPermissions", "&cYou don't have permissions to do this!");
|
||||
defaults.put("Error_NoBarrelAccess", "&cYou don't have permissions to access this barrel!");
|
||||
defaults.put("Perms_NoBarrelCreate", "&cYou don't have permissions to create barrels!");
|
||||
defaults.put("Perms_NoSmallBarrelCreate", "&cYou don't have permissions to create small barrels!");
|
||||
defaults.put("Perms_NoBigBarrelCreate", "&cYou don't have permissions to create big barrels!");
|
||||
defaults.put("Perms_NoCauldronInsert", "&cYou don't have permissions to put ingredients into cauldrons!");
|
||||
defaults.put("Perms_NoCauldronFill", "&cYou don't have permissions to fill bottles from this cauldron!");
|
||||
|
||||
/* Help */
|
||||
defaults.put("Help_Help", "&6/brew help <Page> &9Shows a specific help-page");
|
||||
defaults.put("Help_Player", "&6/brew <Player> <%Drunkeness> <Quality>&9 Sets Drunkeness (and Quality) of a Player");
|
||||
defaults.put("Help_Info", "&6/brew info&9 Displays your current Drunkeness and Quality");
|
||||
defaults.put("Help_UnLabel", "&6/brew unlabel &9Removes the detailled label of a potion");
|
||||
defaults.put("Help_Copy", "&6/brew copy <Quanitiy>&9 Copies the potion in your hand");
|
||||
defaults.put("Help_Delete", "&6/brew delete &9Deletes the potion in your hand");
|
||||
defaults.put("Help_InfoOther", "&6/brew info <Player>&9 Displays the current Drunkeness and Quality of <Player>");
|
||||
defaults.put("Help_Wakeup", "&6/brew wakeup list <Page>&9 Lists all wakeup points");
|
||||
defaults.put("Help_WakeupList", "&6/brew wakeup list <Page> <World>&9 Lists all wakeup points of <world>");
|
||||
defaults.put("Help_WakeupCheck", "&6/brew wakeup check &9Teleports to all wakeup points");
|
||||
defaults.put("Help_WakeupCheckSpecific", "&6/brew wakeup check <id> &9Teleports to the wakeup point with <id>");
|
||||
defaults.put("Help_WakeupAdd", "&6/brew wakeup add &9Adds a wakeup point at your current position");
|
||||
defaults.put("Help_WakeupRemove", "&6/brew wakeup remove <id> &9Removes the wakeup point with <id>");
|
||||
defaults.put("Help_Reload", "&6/brew reload &9Reload config");
|
||||
defaults.put("Help_Persist", "&6/brew persist &9Make Brew persistent -> copyable by any plugin and technique");
|
||||
defaults.put("Help_Static", "&6/brew static &9Make Brew static -> No further ageing or distilling");
|
||||
defaults.put("Help_Create", "&6/brew create <Recipe> <Quality> &9Create a Brew with optional quality (1-10)");
|
||||
|
||||
/* Etc. */
|
||||
defaults.put("Etc_Usage", "Usage:");
|
||||
defaults.put("Etc_Page", "Page");
|
||||
defaults.put("Etc_Barrel", "Barrel");
|
||||
}
|
||||
|
||||
private void check() {
|
||||
for (String defaultEntry : defaults.keySet()) {
|
||||
if (!entries.containsKey(defaultEntry)) {
|
||||
entries.put(defaultEntry, defaults.get(defaultEntry));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (changed) {
|
||||
/* Copy old File */
|
||||
File source = new File(file.getPath());
|
||||
String filePath = file.getPath();
|
||||
File temp = new File(filePath.substring(0, filePath.length() - 4) + "_old.yml");
|
||||
|
||||
if (temp.exists())
|
||||
temp.delete();
|
||||
|
||||
source.renameTo(temp);
|
||||
|
||||
/* Save */
|
||||
FileConfiguration configFile = new YamlConfiguration();
|
||||
|
||||
for (String key : entries.keySet()) {
|
||||
configFile.set(key, entries.get(key));
|
||||
}
|
||||
|
||||
try {
|
||||
configFile.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String get(String key, String... args) {
|
||||
String entry = entries.get(key);
|
||||
|
||||
if (entry != null) {
|
||||
int i = 0;
|
||||
for (String arg : args) {
|
||||
if (arg != null) {
|
||||
i++;
|
||||
entry = entry.replace("&v" + i, arg);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
entry = "%placeholder%";
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
public class ReadOldData extends BukkitRunnable {
|
||||
|
||||
public FileConfiguration data;
|
||||
public boolean done = false;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
File datafile = new File(P.p.getDataFolder(), "data.yml");
|
||||
data = YamlConfiguration.loadConfiguration(datafile);
|
||||
|
||||
if (DataSave.lastBackup > 10) {
|
||||
datafile.renameTo(new File(P.p.getDataFolder(), "dataBackup.yml"));
|
||||
DataSave.lastBackup = 0;
|
||||
} else {
|
||||
DataSave.lastBackup++;
|
||||
}
|
||||
|
||||
done = true;
|
||||
}
|
||||
|
||||
public FileConfiguration getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
public class WriteData implements Runnable {
|
||||
|
||||
private FileConfiguration data;
|
||||
|
||||
public WriteData(FileConfiguration data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
File datafile = new File(P.p.getDataFolder(), "data.yml");
|
||||
|
||||
try {
|
||||
data.save(datafile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
DataSave.lastSave = 1;
|
||||
DataSave.running = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user