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

150 lines
4.4 KiB
Java
Raw Normal View History

2017-06-11 23:36:54 +02:00
package me.libraryaddict.disguise.utilities;
2017-06-19 11:23:02 +02:00
import me.libraryaddict.disguise.DisguiseConfig;
2017-06-23 23:21:34 +02:00
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
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.util.HashMap;
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;
2017-06-19 11:23:02 +02:00
private HashMap<String, String> translated = new HashMap<>();
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
}
2017-06-19 11:23:02 +02:00
public static void reloadTranslations() {
for (TranslateType type : values()) {
type.reload();
}
2017-06-22 18:14:19 +02:00
if (!LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
System.out.println("[LibsDisguises] You must purchase the plugin to use translations!");
}
2017-06-19 11:23:02 +02:00
TranslateFiller.fillConfigs();
}
public void wipeTranslations() {
translated.clear();
}
2017-06-19 11:23:02 +02:00
private void reload() {
2017-06-19 11:23:02 +02:00
translated.clear();
if (LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
System.out.println("[LibsDisguises] Loading translations: " + name());
}
2017-06-11 23:36:54 +02:00
if (!file.exists())
2017-06-19 11:23:02 +02:00
return;
YamlConfiguration config = new YamlConfiguration();
config.options().pathSeparator(Character.toChars(0)[0]);
2017-06-11 23:36:54 +02:00
try {
2017-06-19 11:23:02 +02:00
config.load(file);
for (String key : config.getKeys(false)) {
String value = config.getString(key);
if (value == null)
System.err.println("Translation for " + name() + " has a null value for the key '" + key + "'");
else if (!Objects.equals(key, value)) // Don't store useless information
2017-06-19 20:11:08 +02:00
translated.put(ChatColor.translateAlternateColorCodes('&', key),
ChatColor.translateAlternateColorCodes('&', value));
2017-06-19 11:23:02 +02:00
}
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();
}
}
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) {
2017-06-19 11:23:02 +02:00
if (translated.containsKey(message))
2017-06-11 23:36:54 +02:00
return;
2017-06-19 11:23:02 +02:00
translated.put(message, message);
2017-06-19 19:06:35 +02:00
message = StringEscapeUtils.escapeJava(message.replaceAll(ChatColor.COLOR_CHAR + "", "&"));
2017-06-19 11:23:02 +02:00
2017-06-11 23:36:54 +02:00
try {
2017-06-19 11:23:02 +02:00
boolean exists = file.exists();
if (!exists) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileWriter writer = new FileWriter(getFile(), true);
if (!exists) {
2017-06-19 11:23:02 +02:00
writer.write("# To use translations in Lib's Disguises, you must have the purchased plugin\n");
2017-06-19 20:11:08 +02:00
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 11:23:02 +02:00
writer.write("\n" + (comment != null ? "# " + comment + "\n" :
2017-06-21 21:20:38 +02:00
"") + "\"" + message + "\": \"" + message + "\"\n");
2017-06-11 23:36:54 +02:00
writer.close();
}
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
}
}