Inline updater for Language file

This commit is contained in:
Sn0wStorm 2019-11-25 15:36:48 +01:00
parent 3b9f9ae26d
commit a5f73ceacc
6 changed files with 65 additions and 159 deletions

View File

@ -1,15 +1,15 @@
# Brew # Brew
Brew_-times: -fach Brew_-times: -fach
Brew_BadPotion: Verdorbenes Getränk Brew_BadPotion: Verdorbenes Gebräu
Brew_BarrelRiped: Fassgereift Brew_BarrelRiped: Fassgereift
Brew_DistillUndefined: Undefinierbares Destillat Brew_DistillUndefined: Graues Destillat
Brew_Distilled: Destilliert Brew_Distilled: Destilliert
Brew_HundredsOfYears: Hunderte Jahre Brew_HundredsOfYears: Hunderte Jahre
Brew_Ingredients: Zutaten Brew_Ingredients: Zutaten
Brew_MinutePluralPostfix: n Brew_MinutePluralPostfix: n
Brew_OneYear: Ein Jahr Brew_OneYear: Ein Jahr
Brew_ThickBrew: Schlammiger Sud Brew_ThickBrew: Schlammiger Sud
Brew_Undefined: Undefinierbarer Sud Brew_Undefined: Kesselsud
Brew_Woodtype: Holzart Brew_Woodtype: Holzart
Brew_Years: Jahre Brew_Years: Jahre
Brew_fermented: gegärt 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: '&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_Player_Error: '&cDie Qualität muss zwischen 1 und 10 liegen!'
CMD_Reload: '&aConfig wurde neu eingelesen' CMD_Reload: '&aConfig wurde neu eingelesen'
CMD_Created: '&aTrank erstellt'
CMD_Configname: '&aName für Config ist: &f&v1' CMD_Configname: '&aName für Config ist: &f&v1'
CMD_Configname_Error: '&cDu hast kein Item in der Hand' CMD_Configname_Error: '&cDu hast kein Item in der Hand'
CMD_UnLabel: '&aDas Label wurde entfernt' CMD_UnLabel: '&aDas Label wurde entfernt'

View File

@ -2,14 +2,14 @@
Brew_-times: -times Brew_-times: -times
Brew_BadPotion: Ruined Potion Brew_BadPotion: Ruined Potion
Brew_BarrelRiped: Barrel aged Brew_BarrelRiped: Barrel aged
Brew_DistillUndefined: Indefinable Distillate Brew_DistillUndefined: Murky Distillate
Brew_Distilled: Distilled Brew_Distilled: Distilled
Brew_HundredsOfYears: Hundreds of Years Brew_HundredsOfYears: Hundreds of Years
Brew_Ingredients: Ingredients Brew_Ingredients: Ingredients
Brew_MinutePluralPostfix: s Brew_MinutePluralPostfix: s
Brew_OneYear: One Year Brew_OneYear: One Year
Brew_ThickBrew: Muddy Brew Brew_ThickBrew: Muddy Brew
Brew_Undefined: Indefinable Brew Brew_Undefined: Cauldron Brew
Brew_Woodtype: Woodtype Brew_Woodtype: Woodtype
Brew_Years: Years Brew_Years: Years
Brew_fermented: fermented 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: '&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_Player_Error: '&cThe quality has to be between 1 and 10!'
CMD_Reload: '&aConfig was successfully reloaded' CMD_Reload: '&aConfig was successfully reloaded'
CMD_Created: '&aBrew Created'
CMD_Configname: '&aName for the Config is: &f&v1' CMD_Configname: '&aName for the Config is: &f&v1'
CMD_Configname_Error: '&cCould not find item in your hand' 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.' CMD_Static: '&aPotion is now static and will not change in barrels or brewing stands.'

View File

@ -565,9 +565,6 @@ public class P extends JavaPlugin {
return; return;
} }
// load LanguageReader
languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + language + ".yml"));
// Reload Recipes // Reload Recipes
boolean successful = true; boolean successful = true;
for (Brew brew : Brew.legacyPotions.values()) { for (Brew brew : Brew.legacyPotions.values()) {

View File

@ -148,7 +148,7 @@ public class BConfig {
p.language = config.getString("language", "en"); p.language = config.getString("language", "en");
// Load LanguageReader // 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 // Has to config still got old materials
boolean oldMat = config.getBoolean("oldMat", false); boolean oldMat = config.getBoolean("oldMat", false);

View File

@ -1,26 +1,26 @@
package com.dre.brewery.filedata; package com.dre.brewery.filedata;
import java.io.File; import com.dre.brewery.P;
import java.io.IOException; import org.bukkit.configuration.InvalidConfigurationException;
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 org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; 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 { public class LanguageReader {
private Map<String, String> entries = new HashMap<>(128); private Map<String, String> entries = new HashMap<>(128);
private File file; private File file;
private boolean changed;
public LanguageReader(File file) {
List<Tuple<String, String>> defaults = getDefaults();
public LanguageReader(File file, String defaultPath) {
/* Load */ /* Load */
this.file = file; this.file = file;
@ -32,151 +32,56 @@ public class LanguageReader {
} }
/* Check */ /* Check */
check(defaults); check(defaultPath);
if (changed) {
save();
}
} }
private List<Tuple<String, String>> getDefaults() { private void check(String defaultPath) {
List<Tuple<String, String>> defaults = new ArrayList<>(128); FileConfiguration defaults = null;
ConfigUpdater updater = null;
/* Player */ int i = 0;
defaults.add(new Tuple<>("Player_BarrelCreated", "Barrel created")); String line;
defaults.add(new Tuple<>("Player_BarrelFull", "&cThis barrel can''t hold any more drinks")); InputStream resource = P.p.getResource(defaultPath);
defaults.add(new Tuple<>("Player_CauldronInfo1", "This cauldron has been boiling for &v1 minutes.")); if (resource == null) return;
defaults.add(new Tuple<>("Player_CauldronInfo2", "This cauldron has just started boiling.")); try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) {
defaults.add(new Tuple<>("Player_CantDrink", "You can't drink any more.")); while ((line = reader.readLine()) != null) {
defaults.add(new Tuple<>("Player_DrunkPassOut", "You drank too much and passed out.")); int index = line.indexOf(':');
defaults.add(new Tuple<>("Player_LoginDeny", "Your character tries to log in, but is too drunk to find the server. Try again!")); if (index != -1) {
defaults.add(new Tuple<>("Player_LoginDenyLong", "Your character is really drunk and has passed out. Try again in 10 minutes!")); line = line.substring(0, index);
defaults.add(new Tuple<>("Player_Wake", "Ohh no! I cannot remember how I got here...")); if (!entries.containsKey(line)) {
defaults.add(new Tuple<>("Player_WakeCreated", "&aWakeup Point with id: &6&v1 &awas created successfully!")); if (defaults == null) {
defaults.add(new Tuple<>("Player_WakeNotExist", "&cThe Wakeup Point with the id: &6&v1 &cdoesn't exist!")); defaults = new YamlConfiguration();
defaults.add(new Tuple<>("Player_WakeDeleted", "&aThe Wakeup Point with the id: &6&v1 &awas successfully deleted!")); defaults.load(new BufferedReader(new InputStreamReader(Objects.requireNonNull(P.p.getResource(defaultPath)))));
defaults.add(new Tuple<>("Player_WakeAlreadyDeleted", "&cThe Wakeup Point with the id: &6&v1 &chas already been deleted!")); updater = new ConfigUpdater(file);
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;
} }
entries.put(line, defaults.getString(line));
private void check(List<Tuple<String, String>> defaults) { updater.addLines(i, line + ": '" + entries.get(line) + "'");
for (Tuple<String, String> def : defaults) {
if (!entries.containsKey(def.a())) {
entries.put(def.a(), def.b());
changed = true;
} }
} }
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") @SuppressWarnings("ResultOfMethodCallIgnored")
private void save() { private void createBackup() {
if (changed) {
/* Copy old File */ /* Copy old File */
File source = new File(file.getPath()); File source = new File(file.getPath());
String filePath = 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()) if (backup.exists()) {
temp.delete(); backup.delete();
source.renameTo(temp);
/* Save */
FileConfiguration configFile = new YamlConfiguration();
for (String key : entries.keySet()) {
configFile.set(key, entries.get(key));
} }
try { source.renameTo(backup);
configFile.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
} }
public String get(String key, String... args) { public String get(String key, String... args) {

View File

@ -12,6 +12,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
@ -21,7 +22,7 @@ public class CommandListener implements CommandExecutor {
public P p = P.p; public P p = P.p;
@Override @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"; String cmd = "help";
if (args.length > 0) { if (args.length > 0) {
@ -560,6 +561,7 @@ public class CommandListener implements CommandExecutor {
ItemStack item = recipe.create(quality); ItemStack item = recipe.create(quality);
if (item != null) { if (item != null) {
player.getInventory().addItem(item); player.getInventory().addItem(item);
p.msg(sender, p.languageReader.get("CMD_Created"));
} }
} else { } else {
p.msg(sender, p.languageReader.get("Error_NoBrewName", name)); p.msg(sender, p.languageReader.get("Error_NoBrewName", name));