Fix custom language files.

- Now actually using custom language file when indicated in the config
file.
- Set default language file to read-only.
- Added warning message to not alter the default language file as it'll
be regenerated.
This commit is contained in:
Pim van der Loos 2019-04-28 17:31:08 +02:00
parent 45d5f90fef
commit ecd0340cd6
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
4 changed files with 64 additions and 36 deletions

View File

@ -92,9 +92,6 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Y u do dis? :(
myLogger(Level.INFO, "Stats disabled, not loading stats :(... Please consider enabling it! I am a simple man, seeing higher user numbers helps me stay motivated!");
locale = config.getString("languageFile");
// Load the files for the correct version of Minecraft.
if (compatibleMCVer())
{
@ -139,6 +136,8 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Check if the plugin should go into uninstall mode.
uninstallMode = config.getBool("uninstallMode");
locale = config.getString("languageFile");
}
public Messages getMyMessages()
@ -223,6 +222,10 @@ public class ArmoredElytra extends JavaPlugin implements Listener
public String getLocale()
{
if (locale == null)
System.out.println("locale is null!");
else
System.out.println("Locale is " + locale);
return locale == null ? "en_US" : locale;
}

View File

@ -12,7 +12,6 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ConfigOption;
public class ConfigLoader
{
@ -28,17 +27,17 @@ public class ConfigLoader
private int DIAMONDS_TO_FULL;
private boolean noFlightDurability;
private List<String> allowedEnchantments;
private ArrayList<ConfigOption> configOptionsList;
private ArmoredElytra plugin;
public ConfigLoader(ArmoredElytra plugin)
public ConfigLoader(ArmoredElytra plugin)
{
this.plugin = plugin;
configOptionsList = new ArrayList<ConfigOption>();
configOptionsList = new ArrayList<>();
makeConfig();
}
// Read the current config, the make a new one based on the old one or default values, whichever is applicable.
public void makeConfig()
{
@ -53,12 +52,12 @@ public class ConfigLoader
"Setting this to true will cause armored elytras to not lose any durability while flying.",
"This is not a permanent option and will affect ALL elytras."
};
String[] repairComment =
String[] repairComment =
{
"Amount of items it takes to fully repair an armored elytra",
"Repair cost for every tier of armored elytra in number of items to repair 100%."
};
String[] enchantmentsComment =
String[] enchantmentsComment =
{
"List of enchantments that are allowed to be put on an armored elytra.",
"If you do not want to allow any enchantments at all, remove them all and add \"NONE\"",
@ -66,11 +65,11 @@ public class ConfigLoader
"https://hub.spigotmc.org/javadocs/spigot/org/bukkit/enchantments/Enchantment.html",
"Note that only 1 protection enchantment (PROTECTION_FIRE, PROTECTION_ENVIRONMENTAL etc) can be active on an elytra."
};
String[] updateComment =
String[] updateComment =
{
"Allow this plugin to check for updates on startup. It will not download new versions!"
};
String[] bStatsComment =
String[] bStatsComment =
{
"Allow this plugin to send (anonymised) stats using bStats. Please consider keeping it enabled.",
"It has a negligible impact on performance and more users on stats keeps me more motivated to support this plugin!"
@ -91,18 +90,18 @@ public class ConfigLoader
{
"Specify a language file to be used. Note that en_US.txt will get regenerated!"
};
FileConfiguration config = plugin.getConfig();
// Read all the options from the config, then put them in a configOption with their name, value and comment.
// THen put all configOptions into an ArrayList.
unbreakable = config.getBoolean ("unbreakable" , false);
configOptionsList.add(new ConfigOption ("unbreakable" , unbreakable , unbreakableComment ));
noFlightDurability = config.getBoolean ("noFlightDurability", false);
configOptionsList.add(new ConfigOption ("noFlightDurability", noFlightDurability, flyDurabilityComment));
LEATHER_TO_FULL = config.getInt ("leatherRepair" , 6);
configOptionsList.add(new ConfigOption ("leatherRepair" , LEATHER_TO_FULL, repairComment));
GOLD_TO_FULL = config.getInt ("goldRepair" , 5);
@ -111,10 +110,10 @@ public class ConfigLoader
configOptionsList.add(new ConfigOption ("ironRepair" , IRON_TO_FULL));
DIAMONDS_TO_FULL = config.getInt ("diamondsRepair", 3);
configOptionsList.add(new ConfigOption ("diamondsRepair", DIAMONDS_TO_FULL));
allowedEnchantments = config.getStringList("allowedEnchantments");
configOptionsList.add(new ConfigOption ("allowedEnchantments", allowedEnchantments, enchantmentsComment));
checkForUpdates = config.getBoolean ("checkForUpdates", true );
configOptionsList.add(new ConfigOption ("checkForUpdates", checkForUpdates, updateComment));
allowStats = config.getBoolean ("allowStats" , true );
@ -125,10 +124,10 @@ public class ConfigLoader
configOptionsList.add(new ConfigOption ("uninstallMode" , uninstallMode, uninstallComment));
languageFile = config.getString ("languageFile" , "en_US");
configOptionsList.add(new ConfigOption ("languageFile" , languageFile, languageFileComment));
writeConfig();
}
// Write new config file.
public void writeConfig()
{
@ -149,10 +148,10 @@ public class ConfigLoader
}
FileWriter fw = new FileWriter(saveTo, true);
PrintWriter pw = new PrintWriter(fw);
for (ConfigOption configOption : configOptionsList)
pw.println(configOption.toString());
pw.flush();
pw.close();
}
@ -162,7 +161,7 @@ public class ConfigLoader
e.printStackTrace();
}
}
public Integer getInt(String path)
{
for (ConfigOption configOption : configOptionsList)
@ -170,7 +169,7 @@ public class ConfigLoader
return configOption.getInt();
return null;
}
public Boolean getBool(String path)
{
for (ConfigOption configOption : configOptionsList)
@ -178,7 +177,7 @@ public class ConfigLoader
return configOption.getBool();
return null;
}
public String getString(String path)
{
for (ConfigOption configOption : configOptionsList)
@ -186,7 +185,7 @@ public class ConfigLoader
return configOption.getString();
return null;
}
public List<String> getStringList(String path)
{
for (ConfigOption configOption : configOptionsList)

View File

@ -15,22 +15,30 @@ public class Messages
{
private Map<String, String> messageMap = new HashMap<>();
private ArmoredElytra plugin;
private String locale;
private File textFile;
public Messages(ArmoredElytra plugin)
{
this.plugin = plugin;
locale = plugin.getLocale();
textFile = new File(plugin.getDataFolder(), locale + ".txt");
textFile = new File(plugin.getDataFolder(), plugin.getLocale() + ".txt");
readFile();
}
private void writeDefaultFile()
{
File defaultFile = new File(plugin.getDataFolder(), "en_US.txt");
if (!defaultFile.setWritable(true))
plugin.myLogger(Level.SEVERE, "Failed to make file \"" + defaultFile + "\" writable!");
// Load the default en_US from the resources.
plugin.saveResource("en_US.txt", true);
defaultFile.setWritable(false);
}
// Read locale file.
private void readFile()
{
// Load the default en_US from the resources.
plugin.saveResource("en_US.txt", true);
writeDefaultFile();
try (BufferedReader br = new BufferedReader(new FileReader(textFile)))
{
@ -38,6 +46,9 @@ public class Messages
while ((sCurrentLine = br.readLine()) != null)
{
// Ignore comments.
if (sCurrentLine.startsWith("#"))
continue;
String key, value;
String[] parts = sCurrentLine.split("=", 2);
key = parts[0];
@ -55,11 +66,11 @@ public class Messages
}
catch (FileNotFoundException e)
{
plugin.myLogger(Level.SEVERE, "Locale file " + locale + ".txt does not exist!");
plugin.myLogger(Level.SEVERE, "Locale file \"" + textFile + "\" does not exist!");
}
catch (IOException e)
{
plugin.myLogger(Level.SEVERE, "Could not read locale file! (" + locale + ".txt)");
plugin.myLogger(Level.SEVERE, "Could not read locale file: \"" + textFile + "\"");
e.printStackTrace();
}
}
@ -71,9 +82,18 @@ public class Messages
value = messageMap.get(key);
if (value == null)
{
value = "ArmoredElytra: Translation not found! Contact server admin!";
value = "BigDoors: Translation not found! Contact server admin!";
plugin.myLogger(Level.WARNING, "Failed to get translation for key " + key);
}
return value;
}
public String getStringReverse(String value)
{
return messageMap.entrySet().stream()
.filter(e -> e.getValue().equals(value))
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);
}
}

View File

@ -1,3 +1,9 @@
# This file contains all the (partial) sentences used in this plugin.
# If you want to modify anything, make sure to do so in a copy, as this file will be regenerated on startup!
# You can change which file will be used in the config.yml.
# The format is "key=value" (without quotation marks). You can modify the values, but not the keys.
# Order doesn't matter and you can use comments if you so desire.
# Please do note that white space does matter! (so spaces at the end of lines, for example).
TIER.Leather=&2Leather Armored Elytra
TIER.Gold=&EGolden Armored Elytra
TIER.Chain=&8Chain Armored Elytra