LibsDisguises/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateType.java

217 lines
7.1 KiB
Java
Raw Normal View History

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;
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;
import java.io.IOException;
import java.util.Iterator;
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;
private LinkedHashMap<String, String> translated = new LinkedHashMap<>();
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
}
public static void refreshTranslations() {
2017-06-19 11:23:02 +02:00
for (TranslateType type : values()) {
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()) {
DisguiseUtilities.getLogger().severe("You must purchase the plugin to use translations!");
}
}
protected void saveTranslations() {
// First remove translations which are not different from each other. We don't need to store messages that
// were not translated.
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();
}
// Close the writer
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
}
private void loadTranslations() {
translated.clear();
2017-06-19 11:23:02 +02:00
if (LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
DisguiseUtilities.getLogger().info("Loading translations: " + name());
}
if (!getFile().exists()) {
DisguiseUtilities.getLogger().info("Translations for " + name() + " missing! Skipping...");
2017-06-19 11:23:02 +02:00
return;
}
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 {
config.load(getFile());
int dupes = 0;
int diff = 0;
2017-06-19 11:23:02 +02:00
for (String key : config.getKeys(false)) {
String value = config.getString(key);
if (value == null) {
DisguiseUtilities.getLogger()
.severe("Translation for " + name() + " has a null value for the key '" + key + "'");
} else {
String newKey = ChatColor.translateAlternateColorCodes('&', key);
if (translated.containsKey(newKey)) {
if (dupes++ < 5) {
DisguiseUtilities.getLogger()
.severe("Alert! Duplicate translation entry for " + key + " in " + name() +
" translations!");
continue;
} else {
DisguiseUtilities.getLogger()
.severe("Too many duplicated keys! It's likely that this file was mildly " +
"corrupted by a previous bug!");
DisguiseUtilities.getLogger()
.severe("Delete the file, or you can remove every line after the first " +
"duplicate message!");
break;
}
}
translated.put(newKey, ChatColor.translateAlternateColorCodes('&', value));
if (!newKey.equals(translated.get(newKey))) {
diff++;
}
}
2017-06-19 11:23:02 +02: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();
}
if (LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
DisguiseUtilities.getLogger().info("Loaded " + translated.size() + " translations for " + name());
}
2017-06-11 23:36:54 +02:00
}
private File getFile() {
return file;
}
public void save(String msg) {
2017-06-19 20:11:08 +02:00
if (this != TranslateType.MESSAGES)
throw new IllegalArgumentException("Can't set no comment for '" + msg + "'");
save(msg, null);
}
public void save(String message, String comment) {
if (translated.containsKey(message)) {
2017-06-11 23:36:54 +02:00
return;
}
translated.put(message, message);
2017-06-11 23:36:54 +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 {
boolean exists = getFile().exists();
2017-06-19 11:23:02 +02:00
if (!exists) {
getFile().getParentFile().mkdirs();
getFile().createNewFile();
2017-06-19 11:23:02 +02:00
}
if (writer == null) {
writer = new FileWriter(getFile(), true);
2017-06-19 11:23:02 +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");
}
}
}
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
}
}