mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2024-11-22 11:35:16 +01:00
Inline updater for Language file
This commit is contained in:
parent
3b9f9ae26d
commit
a5f73ceacc
@ -1,15 +1,15 @@
|
||||
# Brew
|
||||
Brew_-times: -fach
|
||||
Brew_BadPotion: Verdorbenes Getränk
|
||||
Brew_BadPotion: Verdorbenes Gebräu
|
||||
Brew_BarrelRiped: Fassgereift
|
||||
Brew_DistillUndefined: Undefinierbares Destillat
|
||||
Brew_DistillUndefined: Graues Destillat
|
||||
Brew_Distilled: Destilliert
|
||||
Brew_HundredsOfYears: Hunderte Jahre
|
||||
Brew_Ingredients: Zutaten
|
||||
Brew_MinutePluralPostfix: n
|
||||
Brew_OneYear: Ein Jahr
|
||||
Brew_ThickBrew: Schlammiger Sud
|
||||
Brew_Undefined: Undefinierbarer Sud
|
||||
Brew_Undefined: Kesselsud
|
||||
Brew_Woodtype: Holzart
|
||||
Brew_Years: Jahre
|
||||
Brew_fermented: gegärt
|
||||
@ -23,6 +23,7 @@ CMD_Info_NotDrunk: '&v1 ist nicht betrunken'
|
||||
CMD_Player: '&a&v1 ist nun &6&v2% &abetrunken, mit einer Qualität von &6&v3'
|
||||
CMD_Player_Error: '&cDie Qualität muss zwischen 1 und 10 liegen!'
|
||||
CMD_Reload: '&aConfig wurde neu eingelesen'
|
||||
CMD_Created: '&aTrank erstellt'
|
||||
CMD_Configname: '&aName für Config ist: &f&v1'
|
||||
CMD_Configname_Error: '&cDu hast kein Item in der Hand'
|
||||
CMD_UnLabel: '&aDas Label wurde entfernt'
|
||||
|
@ -2,14 +2,14 @@
|
||||
Brew_-times: -times
|
||||
Brew_BadPotion: Ruined Potion
|
||||
Brew_BarrelRiped: Barrel aged
|
||||
Brew_DistillUndefined: Indefinable Distillate
|
||||
Brew_DistillUndefined: Murky Distillate
|
||||
Brew_Distilled: Distilled
|
||||
Brew_HundredsOfYears: Hundreds of Years
|
||||
Brew_Ingredients: Ingredients
|
||||
Brew_MinutePluralPostfix: s
|
||||
Brew_OneYear: One Year
|
||||
Brew_ThickBrew: Muddy Brew
|
||||
Brew_Undefined: Indefinable Brew
|
||||
Brew_Undefined: Cauldron Brew
|
||||
Brew_Woodtype: Woodtype
|
||||
Brew_Years: Years
|
||||
Brew_fermented: fermented
|
||||
@ -24,6 +24,7 @@ CMD_NonStatic: '&ePotion is not static anymore and will normally age in barrels.
|
||||
CMD_Player: '&a&v1 is now &6&v2% &adrunk, with a quality of &6&v3'
|
||||
CMD_Player_Error: '&cThe quality has to be between 1 and 10!'
|
||||
CMD_Reload: '&aConfig was successfully reloaded'
|
||||
CMD_Created: '&aBrew Created'
|
||||
CMD_Configname: '&aName for the Config is: &f&v1'
|
||||
CMD_Configname_Error: '&cCould not find item in your hand'
|
||||
CMD_Static: '&aPotion is now static and will not change in barrels or brewing stands.'
|
||||
|
@ -565,9 +565,6 @@ public class P extends JavaPlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
// load LanguageReader
|
||||
languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + language + ".yml"));
|
||||
|
||||
// Reload Recipes
|
||||
boolean successful = true;
|
||||
for (Brew brew : Brew.legacyPotions.values()) {
|
||||
|
@ -148,7 +148,7 @@ public class BConfig {
|
||||
p.language = config.getString("language", "en");
|
||||
|
||||
// Load LanguageReader
|
||||
p.languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + p.language + ".yml"));
|
||||
p.languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + p.language + ".yml"), "languages/" + p.language + ".yml");
|
||||
|
||||
// Has to config still got old materials
|
||||
boolean oldMat = config.getBoolean("oldMat", false);
|
||||
|
@ -1,26 +1,26 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.dre.brewery.utility.Tuple;
|
||||
import com.dre.brewery.P;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class LanguageReader {
|
||||
private Map<String, String> entries = new HashMap<>(128);
|
||||
|
||||
private File file;
|
||||
private boolean changed;
|
||||
|
||||
public LanguageReader(File file) {
|
||||
List<Tuple<String, String>> defaults = getDefaults();
|
||||
|
||||
public LanguageReader(File file, String defaultPath) {
|
||||
/* Load */
|
||||
this.file = file;
|
||||
|
||||
@ -32,151 +32,56 @@ public class LanguageReader {
|
||||
}
|
||||
|
||||
/* Check */
|
||||
check(defaults);
|
||||
if (changed) {
|
||||
save();
|
||||
}
|
||||
check(defaultPath);
|
||||
}
|
||||
|
||||
private List<Tuple<String, String>> getDefaults() {
|
||||
List<Tuple<String, String>> defaults = new ArrayList<>(128);
|
||||
|
||||
/* Player */
|
||||
defaults.add(new Tuple<>("Player_BarrelCreated", "Barrel created"));
|
||||
defaults.add(new Tuple<>("Player_BarrelFull", "&cThis barrel can''t hold any more drinks"));
|
||||
defaults.add(new Tuple<>("Player_CauldronInfo1", "This cauldron has been boiling for &v1 minutes."));
|
||||
defaults.add(new Tuple<>("Player_CauldronInfo2", "This cauldron has just started boiling."));
|
||||
defaults.add(new Tuple<>("Player_CantDrink", "You can't drink any more."));
|
||||
defaults.add(new Tuple<>("Player_DrunkPassOut", "You drank too much and passed out."));
|
||||
defaults.add(new Tuple<>("Player_LoginDeny", "Your character tries to log in, but is too drunk to find the server. Try again!"));
|
||||
defaults.add(new Tuple<>("Player_LoginDenyLong", "Your character is really drunk and has passed out. Try again in 10 minutes!"));
|
||||
defaults.add(new Tuple<>("Player_Wake", "Ohh no! I cannot remember how I got here..."));
|
||||
defaults.add(new Tuple<>("Player_WakeCreated", "&aWakeup Point with id: &6&v1 &awas created successfully!"));
|
||||
defaults.add(new Tuple<>("Player_WakeNotExist", "&cThe Wakeup Point with the id: &6&v1 &cdoesn't exist!"));
|
||||
defaults.add(new Tuple<>("Player_WakeDeleted", "&aThe Wakeup Point with the id: &6&v1 &awas successfully deleted!"));
|
||||
defaults.add(new Tuple<>("Player_WakeAlreadyDeleted", "&cThe Wakeup Point with the id: &6&v1 &chas already been deleted!"));
|
||||
defaults.add(new Tuple<>("Player_WakeFilled", "&cThe Wakeup Point with the id: &6&v1&c at position &6&v2 &v3, &v4, &v5&c is filled with Blocks!"));
|
||||
defaults.add(new Tuple<>("Player_WakeNoPoints", "&cThere are no Wakeup Points!"));
|
||||
defaults.add(new Tuple<>("Player_WakeLast", "&aThis was the last Wakeup Point"));
|
||||
defaults.add(new Tuple<>("Player_WakeTeleport", "Teleport to Wakeup Point with the id: &6&v1&f At position: &6&v2 &v3, &v4, &v5"));
|
||||
defaults.add(new Tuple<>("Player_WakeHint1", "To Next Wakeup Point: Punch your fist in the air"));
|
||||
defaults.add(new Tuple<>("Player_WakeHint2", "To Cancel: &9/br wakeup cancel"));
|
||||
defaults.add(new Tuple<>("Player_WakeCancel", "&6Wakeup Point Check was cancelled"));
|
||||
defaults.add(new Tuple<>("Player_WakeNoCheck", "&cNo Wakeup Point Check is currently active"));
|
||||
defaults.add(new Tuple<>("Player_TriedToSay", "&v1 tried to say: &0&v2"));
|
||||
|
||||
/* Brew */
|
||||
defaults.add(new Tuple<>("Brew_Distilled", "Distilled"));
|
||||
defaults.add(new Tuple<>("Brew_BarrelRiped", "Barrel aged"));
|
||||
defaults.add(new Tuple<>("Brew_Undefined", "Indefinable Brew"));
|
||||
defaults.add(new Tuple<>("Brew_DistillUndefined", "Indefinable Distillate"));
|
||||
defaults.add(new Tuple<>("Brew_BadPotion", "Ruined Potion"));
|
||||
defaults.add(new Tuple<>("Brew_Ingredients", "Ingredients"));
|
||||
defaults.add(new Tuple<>("Brew_minute", "minute"));
|
||||
defaults.add(new Tuple<>("Brew_MinutePluralPostfix", "s"));
|
||||
defaults.add(new Tuple<>("Brew_fermented", "fermented"));
|
||||
defaults.add(new Tuple<>("Brew_-times", "-times"));
|
||||
defaults.add(new Tuple<>("Brew_OneYear", "One Year"));
|
||||
defaults.add(new Tuple<>("Brew_Years", "Years"));
|
||||
defaults.add(new Tuple<>("Brew_HundredsOfYears", "Hundreds of Years"));
|
||||
defaults.add(new Tuple<>("Brew_Woodtype", "Woodtype"));
|
||||
defaults.add(new Tuple<>("Brew_ThickBrew", "Muddy Brew"));
|
||||
defaults.add(new Tuple<>("Brew_Alc", "Alc &v1ml"));
|
||||
|
||||
/* Commands */
|
||||
defaults.add(new Tuple<>("CMD_Reload", "&aConfig was successfully reloaded"));
|
||||
defaults.add(new Tuple<>("CMD_Configname", "&aName for the Config is: &f&v1"));
|
||||
defaults.add(new Tuple<>("CMD_Configname_Error", "&cCould not find item in your hand"));
|
||||
defaults.add(new Tuple<>("CMD_Player", "&a&v1 is now &6&v2% &adrunk, with a quality of &6&v3"));
|
||||
defaults.add(new Tuple<>("CMD_Player_Error", "&cThe quality has to be between 1 and 10!"));
|
||||
defaults.add(new Tuple<>("CMD_Info_NotDrunk", "&v1 is not drunk"));
|
||||
defaults.add(new Tuple<>("CMD_Info_Drunk", "&v1 is &6&v2% &fdrunk, with a quality of &6&v3"));
|
||||
defaults.add(new Tuple<>("CMD_UnLabel", "&aLabel removed!"));
|
||||
defaults.add(new Tuple<>("CMD_Copy_Error", "&6&v1 &cPotions did not fit into your inventory"));
|
||||
defaults.add(new Tuple<>("CMD_Static", "&aPotion is now static and will not change in barrels or brewing stands."));
|
||||
defaults.add(new Tuple<>("CMD_NonStatic", "&ePotion is not static anymore and will normally age in barrels."));
|
||||
|
||||
/* Error */
|
||||
defaults.add(new Tuple<>("Error_UnknownCommand", "Unknown Command"));
|
||||
defaults.add(new Tuple<>("Error_ShowHelp", "Use &6/brew help &fto display the help"));
|
||||
defaults.add(new Tuple<>("Error_PlayerCommand", "&cThis command can only be executed as a player!"));
|
||||
defaults.add(new Tuple<>("Error_ItemNotPotion", "&cThe item in your hand could not be identified as a potion!"));
|
||||
defaults.add(new Tuple<>("Error_NoBrewName", "&cNo Recipe with Name: '&v1&c' found!"));
|
||||
defaults.add(new Tuple<>("Error_Recipeload", "&cNot all recipes could be restored: More information in the server log!"));
|
||||
defaults.add(new Tuple<>("Error_ConfigUpdate", "Unknown Brewery config version: v&v1, config was not updated!"));
|
||||
defaults.add(new Tuple<>("Error_YmlRead", "Could not read File config.yml, please make sure the file is in valid yml format (correct spaces etc.)"));
|
||||
|
||||
/* Permissions */
|
||||
defaults.add(new Tuple<>("Error_NoPermissions", "&cYou don't have permissions to do this!"));
|
||||
defaults.add(new Tuple<>("Error_NoBarrelAccess", "&cYou don't have permissions to access this barrel!"));
|
||||
defaults.add(new Tuple<>("Perms_NoBarrelCreate", "&cYou don't have permissions to create barrels!"));
|
||||
defaults.add(new Tuple<>("Perms_NoSmallBarrelCreate", "&cYou don't have permissions to create small barrels!"));
|
||||
defaults.add(new Tuple<>("Perms_NoBigBarrelCreate", "&cYou don't have permissions to create big barrels!"));
|
||||
defaults.add(new Tuple<>("Perms_NoCauldronInsert", "&cYou don't have permissions to put ingredients into cauldrons!"));
|
||||
defaults.add(new Tuple<>("Perms_NoCauldronFill", "&cYou don't have permissions to fill bottles from this cauldron!"));
|
||||
|
||||
/* Help */
|
||||
defaults.add(new Tuple<>("Help_Help", "&6/brew help [Page] &9Shows a specific help-page"));
|
||||
defaults.add(new Tuple<>("Help_Player", "&6/brew <Player> <%Drunkeness> [Quality]&9 Sets Drunkeness (and Quality) of a Player"));
|
||||
defaults.add(new Tuple<>("Help_Info", "&6/brew info&9 Displays your current Drunkeness and Quality"));
|
||||
defaults.add(new Tuple<>("Help_UnLabel", "&6/brew unlabel &9Removes the detailled label of a potion"));
|
||||
defaults.add(new Tuple<>("Help_Copy", "&6/brew copy [Quantity]>&9 Copies the potion in your hand"));
|
||||
defaults.add(new Tuple<>("Help_Delete", "&6/brew delete &9Deletes the potion in your hand"));
|
||||
defaults.add(new Tuple<>("Help_InfoOther", "&6/brew info [Player]&9 Displays the current Drunkeness and Quality of [Player]"));
|
||||
defaults.add(new Tuple<>("Help_Wakeup", "&6/brew wakeup list <Page>&9 Lists all wakeup points"));
|
||||
defaults.add(new Tuple<>("Help_WakeupList", "&6/brew wakeup list <Page> [World]&9 Lists all wakeup points of [world]"));
|
||||
defaults.add(new Tuple<>("Help_WakeupCheck", "&6/brew wakeup check &9Teleports to all wakeup points"));
|
||||
defaults.add(new Tuple<>("Help_WakeupCheckSpecific", "&6/brew wakeup check <id> &9Teleports to the wakeup point with <id>"));
|
||||
defaults.add(new Tuple<>("Help_WakeupAdd", "&6/brew wakeup add &9Adds a wakeup point at your current position"));
|
||||
defaults.add(new Tuple<>("Help_WakeupRemove", "&6/brew wakeup remove <id> &9Removes the wakeup point with <id>"));
|
||||
defaults.add(new Tuple<>("Help_Reload", "&6/brew reload &9Reload config"));
|
||||
defaults.add(new Tuple<>("Help_Configname", "&6/brew ItemName &9Display name of item in hand for the config"));
|
||||
defaults.add(new Tuple<>("Help_Static", "&6/brew static &9Make Brew static -> No further ageing or distilling"));
|
||||
defaults.add(new Tuple<>("Help_Create", "&6/brew create <Recipe> [Quality] [Player] &9Create a Brew with optional quality (1-10)"));
|
||||
|
||||
/* Etc. */
|
||||
defaults.add(new Tuple<>("Etc_Usage", "Usage:"));
|
||||
defaults.add(new Tuple<>("Etc_Page", "Page"));
|
||||
defaults.add(new Tuple<>("Etc_Barrel", "Barrel"));
|
||||
|
||||
return defaults;
|
||||
private void check(String defaultPath) {
|
||||
FileConfiguration defaults = null;
|
||||
ConfigUpdater updater = null;
|
||||
int i = 0;
|
||||
String line;
|
||||
InputStream resource = P.p.getResource(defaultPath);
|
||||
if (resource == null) return;
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) {
|
||||
while ((line = reader.readLine()) != null) {
|
||||
int index = line.indexOf(':');
|
||||
if (index != -1) {
|
||||
line = line.substring(0, index);
|
||||
if (!entries.containsKey(line)) {
|
||||
if (defaults == null) {
|
||||
defaults = new YamlConfiguration();
|
||||
defaults.load(new BufferedReader(new InputStreamReader(Objects.requireNonNull(P.p.getResource(defaultPath)))));
|
||||
updater = new ConfigUpdater(file);
|
||||
}
|
||||
|
||||
private void check(List<Tuple<String, String>> defaults) {
|
||||
for (Tuple<String, String> def : defaults) {
|
||||
if (!entries.containsKey(def.a())) {
|
||||
entries.put(def.a(), def.b());
|
||||
changed = true;
|
||||
entries.put(line, defaults.getString(line));
|
||||
updater.addLines(i, line + ": '" + entries.get(line) + "'");
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (updater != null) {
|
||||
createBackup();
|
||||
updater.saveConfig();
|
||||
P.p.log("Language file updated");
|
||||
}
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
P.p.errorLog("Language File could not be updated");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private void save() {
|
||||
if (changed) {
|
||||
private void createBackup() {
|
||||
/* 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");
|
||||
File backup = 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));
|
||||
if (backup.exists()) {
|
||||
backup.delete();
|
||||
}
|
||||
|
||||
try {
|
||||
configFile.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
source.renameTo(backup);
|
||||
}
|
||||
|
||||
public String get(String key, String... args) {
|
||||
|
@ -12,6 +12,7 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
@ -21,7 +22,7 @@ public class CommandListener implements CommandExecutor {
|
||||
public P p = P.p;
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
|
||||
String cmd = "help";
|
||||
if (args.length > 0) {
|
||||
@ -560,6 +561,7 @@ public class CommandListener implements CommandExecutor {
|
||||
ItemStack item = recipe.create(quality);
|
||||
if (item != null) {
|
||||
player.getInventory().addItem(item);
|
||||
p.msg(sender, p.languageReader.get("CMD_Created"));
|
||||
}
|
||||
} else {
|
||||
p.msg(sender, p.languageReader.get("Error_NoBrewName", name));
|
||||
|
Loading…
Reference in New Issue
Block a user