Faster translations loading for non-users

This commit is contained in:
libraryaddict 2017-07-06 04:00:02 +12:00
parent ccd0cc8528
commit c8b908edd3

View File

@ -8,6 +8,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -35,20 +36,38 @@ public enum TranslateType {
if (!LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) { if (!LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
System.out.println("[LibsDisguises] You must purchase the plugin to use translations!"); System.out.println("[LibsDisguises] You must purchase the plugin to use translations!");
} } else {
TranslateFiller.fillConfigs(); TranslateFiller.fillConfigs();
} }
}
public void wipeTranslations() { public void wipeTranslations() {
translated.clear(); translated.clear();
} }
private void reload() { private void reload() {
translated.clear(); wipeTranslations();
if (LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) { if (LibsPremium.isPremium() && DisguiseConfig.isUseTranslations()) {
System.out.println("[LibsDisguises] Loading translations: " + name()); System.out.println("[LibsDisguises] Loading translations: " + name());
} else {
if (!file.exists()) {
try {
file.getParentFile().mkdirs();
file.createNewFile();
FileWriter writer = new FileWriter(getFile(), true);
write(writer);
writer.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return;
} }
if (!file.exists()) if (!file.exists())
@ -90,8 +109,6 @@ public enum TranslateType {
if (translated.containsKey(message)) if (translated.containsKey(message))
return; return;
translated.put(message, message);
message = StringEscapeUtils.escapeJava(message.replaceAll(ChatColor.COLOR_CHAR + "", "&")); message = StringEscapeUtils.escapeJava(message.replaceAll(ChatColor.COLOR_CHAR + "", "&"));
try { try {
@ -105,11 +122,7 @@ public enum TranslateType {
FileWriter writer = new FileWriter(getFile(), true); FileWriter writer = new FileWriter(getFile(), true);
if (!exists) { if (!exists) {
writer.write("# To use translations in Lib's Disguises, you must have the purchased plugin\n"); write(writer);
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" : writer.write("\n" + (comment != null ? "# " + comment + "\n" :
@ -122,6 +135,14 @@ public enum TranslateType {
} }
} }
private void write(FileWriter writer) throws IOException {
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");
}
}
public String reverseGet(String translated) { public String reverseGet(String translated) {
if (translated == null || !LibsPremium.isPremium() || !DisguiseConfig.isUseTranslations()) if (translated == null || !LibsPremium.isPremium() || !DisguiseConfig.isUseTranslations())
return translated; return translated;