2019-01-03 03:13:03 +01:00
|
|
|
package me.libraryaddict.disguise.utilities.translations;
|
2017-06-11 23:36:54 +02:00
|
|
|
|
2017-06-19 11:23:02 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseConfig;
|
2019-01-03 03:13:03 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
|
|
|
import me.libraryaddict.disguise.utilities.LibsPremium;
|
2017-06-23 23:21:34 +02:00
|
|
|
import org.apache.commons.lang.StringEscapeUtils;
|
2017-06-19 19:06:35 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2017-06-11 23:36:54 +02:00
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
|
|
|
import java.io.File;
|
2017-06-19 11:23:02 +02:00
|
|
|
import java.io.FileWriter;
|
2017-07-05 18:00:02 +02:00
|
|
|
import java.io.IOException;
|
2017-07-07 20:06:36 +02:00
|
|
|
import java.util.Iterator;
|
2018-07-11 20:05:36 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2017-06-11 23:36:54 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by libraryaddict on 10/06/2017.
|
|
|
|
*/
|
|
|
|
public enum TranslateType {
|
2017-06-19 20:11:08 +02:00
|
|
|
DISGUISES("disguises"),
|
|
|
|
MESSAGES("messages"),
|
|
|
|
DISGUISE_OPTIONS("disguise_options"),
|
|
|
|
DISGUISE_OPTIONS_PARAMETERS("disguise_option_parameters");
|
|
|
|
|
2017-06-11 23:36:54 +02:00
|
|
|
private File file;
|
2018-07-11 20:05:36 +02:00
|
|
|
private LinkedHashMap<String, String> translated = new LinkedHashMap<>();
|
2017-07-07 20:06:36 +02:00
|
|
|
private FileWriter writer;
|
2017-06-11 23:36:54 +02:00
|
|
|
|
|
|
|
TranslateType(String fileName) {
|
2017-06-19 11:23:02 +02:00
|
|
|
file = new File("plugins/LibsDisguises/Translations", fileName + ".yml");
|
2017-06-11 23:36:54 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 20:05:36 +02:00
|
|
|
public static void refreshTranslations() {
|
2017-06-19 11:23:02 +02:00
|
|
|
for (TranslateType type : values()) {
|
2018-07-11 20:05:36 +02:00
|
|
|
type.loadTranslations();
|
2017-06-19 11:23:02 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 07:58:05 +02:00
|
|
|
TranslateFiller.fillConfigs();
|
|
|
|
|
2017-06-22 18:14:19 +02:00
|
|
|
if (!LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger().severe("You must purchase the plugin to use translations!");
|
2017-07-07 20:06:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 20:05:36 +02:00
|
|
|
protected void saveTranslations() {
|
|
|
|
// First remove translations which are not different from each other. We don't need to store messages that
|
|
|
|
// were not translated.
|
|
|
|
|
2017-07-07 20:06:36 +02:00
|
|
|
Iterator<Map.Entry<String, String>> itel = translated.entrySet().iterator();
|
|
|
|
|
|
|
|
while (itel.hasNext()) {
|
|
|
|
Map.Entry<String, String> entry = itel.next();
|
|
|
|
|
|
|
|
if (!entry.getKey().equals(entry.getValue()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
itel.remove();
|
|
|
|
}
|
|
|
|
|
2018-07-11 20:05:36 +02:00
|
|
|
// Close the writer
|
|
|
|
|
2017-07-07 20:06:36 +02:00
|
|
|
try {
|
|
|
|
if (writer != null) {
|
|
|
|
writer.close();
|
|
|
|
writer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2017-06-22 18:14:19 +02:00
|
|
|
}
|
2017-06-19 11:23:02 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 20:05:36 +02:00
|
|
|
private void loadTranslations() {
|
2017-06-19 19:44:31 +02:00
|
|
|
translated.clear();
|
2017-06-19 11:23:02 +02:00
|
|
|
|
2017-06-19 19:44:31 +02:00
|
|
|
if (LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger().info("Loading translations: " + name());
|
2017-06-19 19:44:31 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 20:05:36 +02:00
|
|
|
if (!getFile().exists()) {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger().info("Translations for " + name() + " missing! Skipping...");
|
2017-06-19 11:23:02 +02:00
|
|
|
return;
|
2018-07-11 20:05:36 +02:00
|
|
|
}
|
2017-06-19 11:23:02 +02:00
|
|
|
|
|
|
|
YamlConfiguration config = new YamlConfiguration();
|
|
|
|
config.options().pathSeparator(Character.toChars(0)[0]);
|
2017-06-11 23:36:54 +02:00
|
|
|
|
|
|
|
try {
|
2017-07-07 20:06:36 +02:00
|
|
|
config.load(getFile());
|
2018-07-11 20:05:36 +02:00
|
|
|
int dupes = 0;
|
2020-03-25 03:53:28 +01:00
|
|
|
int diff = 0;
|
2017-06-19 11:23:02 +02:00
|
|
|
|
|
|
|
for (String key : config.getKeys(false)) {
|
|
|
|
String value = config.getString(key);
|
|
|
|
|
2018-07-11 20:05:36 +02:00
|
|
|
if (value == null) {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger()
|
|
|
|
.severe("Translation for " + name() + " has a null value for the key '" + key + "'");
|
2018-07-11 20:05:36 +02:00
|
|
|
} else {
|
|
|
|
String newKey = ChatColor.translateAlternateColorCodes('&', key);
|
|
|
|
|
|
|
|
if (translated.containsKey(newKey)) {
|
|
|
|
if (dupes++ < 5) {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger()
|
|
|
|
.severe("Alert! Duplicate translation entry for " + key + " in " + name() +
|
2018-07-11 20:05:36 +02:00
|
|
|
" translations!");
|
|
|
|
continue;
|
|
|
|
} else {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger()
|
|
|
|
.severe("Too many duplicated keys! It's likely that this file was mildly " +
|
2018-07-11 20:05:36 +02:00
|
|
|
"corrupted by a previous bug!");
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger()
|
|
|
|
.severe("Delete the file, or you can remove every line after the first " +
|
2018-07-11 20:05:36 +02:00
|
|
|
"duplicate message!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
translated.put(newKey, ChatColor.translateAlternateColorCodes('&', value));
|
2020-03-25 03:53:28 +01:00
|
|
|
|
|
|
|
if (!newKey.equals(translated.get(newKey))) {
|
|
|
|
diff++;
|
|
|
|
}
|
2017-07-07 20:06:36 +02:00
|
|
|
}
|
2017-06-19 11:23:02 +02:00
|
|
|
}
|
2020-03-25 03:53:28 +01:00
|
|
|
|
|
|
|
if (diff > 0 && !DisguiseConfig.isUseTranslations()) {
|
|
|
|
DisguiseUtilities.getLogger().info(diff +
|
|
|
|
" translated strings, but translations has been disabled in config. Is this intended?");
|
|
|
|
}
|
2017-06-11 23:36:54 +02:00
|
|
|
}
|
2017-06-19 11:23:02 +02:00
|
|
|
catch (Exception e) {
|
2017-06-11 23:36:54 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-02-15 22:58:58 +01:00
|
|
|
|
|
|
|
if (LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger().info("Loaded " + translated.size() + " translations for " + name());
|
2018-02-15 22:58:58 +01:00
|
|
|
}
|
2017-06-11 23:36:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private File getFile() {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:44:31 +02:00
|
|
|
public void save(String msg) {
|
2017-06-19 20:11:08 +02:00
|
|
|
if (this != TranslateType.MESSAGES)
|
2017-06-19 19:44:31 +02:00
|
|
|
throw new IllegalArgumentException("Can't set no comment for '" + msg + "'");
|
|
|
|
|
|
|
|
save(msg, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save(String message, String comment) {
|
2017-07-07 20:06:36 +02:00
|
|
|
if (translated.containsKey(message)) {
|
2017-06-11 23:36:54 +02:00
|
|
|
return;
|
2017-07-07 20:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
translated.put(message, message);
|
2017-06-11 23:36:54 +02:00
|
|
|
|
2018-07-11 20:05:36 +02:00
|
|
|
message = StringEscapeUtils.escapeJava(message.replace(ChatColor.COLOR_CHAR + "", "&"));
|
2017-06-19 11:23:02 +02:00
|
|
|
|
2017-06-11 23:36:54 +02:00
|
|
|
try {
|
2017-07-07 20:06:36 +02:00
|
|
|
boolean exists = getFile().exists();
|
2017-06-19 11:23:02 +02:00
|
|
|
|
|
|
|
if (!exists) {
|
2017-07-07 20:06:36 +02:00
|
|
|
getFile().getParentFile().mkdirs();
|
|
|
|
getFile().createNewFile();
|
2017-06-19 11:23:02 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 20:06:36 +02:00
|
|
|
if (writer == null) {
|
|
|
|
writer = new FileWriter(getFile(), true);
|
2017-06-19 11:23:02 +02:00
|
|
|
|
2017-07-07 20:06:36 +02:00
|
|
|
if (!exists) {
|
|
|
|
writer.write("# To use translations in Lib's Disguises, you must have the purchased plugin\n");
|
|
|
|
|
|
|
|
if (this == TranslateType.MESSAGES) {
|
|
|
|
writer.write(
|
|
|
|
"# %s is where text is inserted, look up printf format codes if you're interested\n");
|
|
|
|
}
|
|
|
|
}
|
2017-06-19 19:44:31 +02:00
|
|
|
}
|
|
|
|
|
2018-02-15 22:58:58 +01:00
|
|
|
writer.write("\n" + (comment != null ? "# " + comment + "\n" : "") + "\"" + message + "\": \"" + message +
|
|
|
|
"\"\n");
|
2017-06-11 23:36:54 +02:00
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String reverseGet(String translated) {
|
2017-07-02 07:12:39 +02:00
|
|
|
if (translated == null || !LibsPremium.isPremium() || !DisguiseConfig.isUseTranslations())
|
|
|
|
return translated;
|
2017-06-19 20:11:08 +02:00
|
|
|
|
2017-07-02 07:11:20 +02:00
|
|
|
String lowerCase = translated.toLowerCase();
|
2017-06-11 23:36:54 +02:00
|
|
|
|
2017-06-19 11:23:02 +02:00
|
|
|
for (Map.Entry<String, String> entry : this.translated.entrySet()) {
|
2017-07-02 07:11:20 +02:00
|
|
|
if (!Objects.equals(entry.getValue().toLowerCase(), lowerCase))
|
2017-06-11 23:36:54 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return entry.getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
return translated;
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:06:35 +02:00
|
|
|
public String get(String msg) {
|
2017-06-19 20:11:08 +02:00
|
|
|
if (msg == null || !LibsPremium.isPremium() || !DisguiseConfig.isUseTranslations())
|
2017-06-19 19:06:35 +02:00
|
|
|
return msg;
|
2017-06-19 11:23:02 +02:00
|
|
|
|
2017-06-19 19:06:35 +02:00
|
|
|
String toReturn = translated.get(msg);
|
2017-06-11 23:36:54 +02:00
|
|
|
|
2017-06-21 21:20:12 +02:00
|
|
|
return toReturn == null ? msg : toReturn;
|
2017-06-11 23:36:54 +02:00
|
|
|
}
|
|
|
|
}
|