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-11 23:36:54 +02:00
|
|
|
import org.apache.commons.lang3.StringEscapeUtils;
|
2017-06-21 21:20:12 +02:00
|
|
|
import org.apache.commons.lang3.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
TranslateFiller.fillConfigs();
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:44:31 +02:00
|
|
|
public void wipeTranslations() {
|
|
|
|
translated.clear();
|
|
|
|
}
|
2017-06-19 11:23:02 +02:00
|
|
|
|
2017-06-19 19:44:31 +02:00
|
|
|
private void reload() {
|
2017-06-19 11:23:02 +02:00
|
|
|
translated.clear();
|
|
|
|
|
2017-06-19 19:44:31 +02:00
|
|
|
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
|
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;
|
|
|
|
}
|
|
|
|
|
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-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-21 21:20:38 +02:00
|
|
|
// String message1 = StringEscapeUtils.escapeJava(
|
|
|
|
// StringUtils.reverse(message).replaceAll("s%", "%s").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);
|
|
|
|
|
2017-06-19 19:44:31 +02:00
|
|
|
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) {
|
2017-06-19 19:44:31 +02:00
|
|
|
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-06-19 20:11:08 +02:00
|
|
|
if (translated == null)
|
|
|
|
return null;
|
|
|
|
|
2017-06-19 11:23:02 +02:00
|
|
|
translated = 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()) {
|
|
|
|
if (!Objects.equals(entry.getValue().toLowerCase(), translated))
|
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
|
|
|
}
|
|
|
|
}
|