Fix slow startup, improve translate stuff

This commit is contained in:
libraryaddict 2017-07-08 06:06:36 +12:00
parent c378dc8e78
commit 69977f33bd
2 changed files with 52 additions and 47 deletions

@ -1,6 +1,5 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -69,17 +68,15 @@ public class TranslateFiller {
} }
TranslateType.DISGUISES.save("EntityType", "Used for the disgiuse radius command to list all entitytypes"); TranslateType.DISGUISES.save("EntityType", "Used for the disgiuse radius command to list all entitytypes");
TranslateType.DISGUISES.save("DisgiseType", "Used for the disgiuse modify radius command to list all " + TranslateType.DISGUISES
"disguisetypes"); .save("DisgiseType", "Used for the disgiuse modify radius command to list all " + "disguisetypes");
for (LibsMsg msg : LibsMsg.values()) { for (LibsMsg msg : LibsMsg.values()) {
TranslateType.MESSAGES.save(msg.getRaw()); TranslateType.MESSAGES.save(msg.getRaw());
} }
if (!LibsPremium.isPremium() || !DisguiseConfig.isUseTranslations()) { for (TranslateType type : TranslateType.values()) {
for (TranslateType type : TranslateType.values()) { type.removeDuplicates();
type.wipeTranslations();
}
} }
} }
} }

@ -10,6 +10,7 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -24,6 +25,7 @@ public enum TranslateType {
private File file; private File file;
private HashMap<String, String> translated = new HashMap<>(); private HashMap<String, String> translated = new HashMap<>();
private FileWriter writer;
TranslateType(String fileName) { TranslateType(String fileName) {
file = new File("plugins/LibsDisguises/Translations", fileName + ".yml"); file = new File("plugins/LibsDisguises/Translations", fileName + ".yml");
@ -36,8 +38,31 @@ 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();
}
protected void removeDuplicates() {
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();
}
try {
if (writer != null) {
writer.close();
writer = null;
}
}
catch (IOException e) {
e.printStackTrace();
} }
} }
@ -50,43 +75,26 @@ public enum TranslateType {
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 (!getFile().exists())
return; return;
YamlConfiguration config = new YamlConfiguration(); YamlConfiguration config = new YamlConfiguration();
config.options().pathSeparator(Character.toChars(0)[0]); config.options().pathSeparator(Character.toChars(0)[0]);
try { try {
config.load(file); config.load(getFile());
for (String key : config.getKeys(false)) { for (String key : config.getKeys(false)) {
String value = config.getString(key); String value = config.getString(key);
if (value == null) if (value == null)
System.err.println("Translation for " + name() + " has a null value for the key '" + key + "'"); 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 else {
translated.put(ChatColor.translateAlternateColorCodes('&', key), translated.put(ChatColor.translateAlternateColorCodes('&', key),
ChatColor.translateAlternateColorCodes('&', value)); ChatColor.translateAlternateColorCodes('&', value));
}
} }
} }
catch (Exception e) { catch (Exception e) {
@ -106,43 +114,43 @@ public enum TranslateType {
} }
public void save(String message, String comment) { public void save(String message, String comment) {
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 {
boolean exists = file.exists(); boolean exists = getFile().exists();
if (!exists) { if (!exists) {
file.getParentFile().mkdirs(); getFile().getParentFile().mkdirs();
file.createNewFile(); getFile().createNewFile();
} }
FileWriter writer = new FileWriter(getFile(), true); if (writer == null) {
writer = new FileWriter(getFile(), true);
if (!exists) { if (!exists) {
write(writer); 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" : writer.write("\n" + (comment != null ? "# " + comment + "\n" :
"") + "\"" + message + "\": \"" + message + "\"\n"); "") + "\"" + message + "\": \"" + message + "\"\n");
writer.close();
} }
catch (Exception ex) { catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
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;