Better file loading

This commit is contained in:
HappyPikachu 2018-03-02 02:51:30 -05:00
parent 078fe182c9
commit 220e0c0e67
3 changed files with 121 additions and 133 deletions

View File

@ -13,10 +13,12 @@
package me.blackvein.quests;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.net.URISyntaxException;
@ -2067,114 +2069,116 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
boolean failedToLoad;
totalQuestPoints = 0;
needsSaving = false;
FileConfiguration config = new YamlConfiguration();
FileConfiguration config = null;
File file = new File(this.getDataFolder(), "quests.yml");
try {
config.load(file);
config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
ConfigurationSection questsSection;
if (config.contains("quests")) {
questsSection = config.getConfigurationSection("quests");
} else {
questsSection = config.createSection("quests");
needsSaving = true;
}
for (String key : questsSection.getKeys(false)) {
try { // main "skip quest" try/catch block
questName = key;
quest = new Quest();
failedToLoad = false;
if (config.contains("quests." + questName + ".name")) {
quest.name = parseString(config.getString("quests." + questName + ".name"), quest);
} else {
skipQuestProcess("Quest block \'" + questName + "\' is missing " + ChatColor.RED + "name:");
}
if (citizens != null && config.contains("quests." + questName + ".npc-giver-id")) {
if (CitizensAPI.getNPCRegistry().getById(config.getInt("quests." + questName + ".npc-giver-id")) != null) {
quest.npcStart = CitizensAPI.getNPCRegistry().getById(config.getInt("quests." + questName + ".npc-giver-id"));
questNPCs.add(CitizensAPI.getNPCRegistry().getById(config.getInt("quests." + questName + ".npc-giver-id")));
} else {
skipQuestProcess("npc-giver-id: for Quest " + quest.name + " is not a valid NPC id!");
}
}
if (config.contains("quests." + questName + ".block-start")) {
Location location = getLocation(config.getString("quests." + questName + ".block-start"));
if (location != null) {
quest.blockStart = location;
} else {
skipQuestProcess(new String[] { "block-start: for Quest " + quest.name + " is not in proper location format!", "Proper location format is: \"WorldName x y z\"" });
}
}
if (config.contains("quests." + questName + ".region")) {
String region = config.getString("quests." + questName + ".region");
boolean exists = regionFound(quest, region);
if (!exists) {
skipQuestProcess("region: for Quest " + quest.name + " is not a valid WorldGuard region!");
}
}
if (config.contains("quests." + questName + ".gui-display")) {
String item = config.getString("quests." + questName + ".gui-display");
try {
ItemStack stack = ItemUtil.readItemStack(item);
if (stack != null) {
quest.guiDisplay = stack;
}
} catch (Exception e) {
instance.getLogger().warning(item + " in items: GUI Display in Quest " + quest.name + "is not properly formatted!");
}
}
if (config.contains("quests." + questName + ".redo-delay")) {
if (config.getInt("quests." + questName + ".redo-delay", -999) != -999) {
quest.redoDelay = config.getInt("quests." + questName + ".redo-delay") * 1000;
} else {
skipQuestProcess("redo-delay: for Quest " + quest.name + " is not a number!");
}
}
if (config.contains("quests." + questName + ".finish-message")) {
quest.finished = parseString(config.getString("quests." + questName + ".finish-message"), quest);
} else {
skipQuestProcess("Quest " + quest.name + " is missing finish-message:");
}
if (config.contains("quests." + questName + ".ask-message")) {
quest.description = parseString(config.getString("quests." + questName + ".ask-message"), quest);
} else {
skipQuestProcess("Quest " + quest.name + " is missing ask-message:");
}
if (config.contains("quests." + questName + ".event")) {
Event evt = Event.loadEvent(config.getString("quests." + questName + ".event"), this);
if (evt != null) {
quest.initialEvent = evt;
} else {
skipQuestProcess("Initial Event in Quest " + quest.name + " failed to load.");
}
}
if (config.contains("quests." + questName + ".requirements")) {
loadQuestRequirements(config, questsSection);
}
quest.plugin = this;
processStages(quest, config, questName); // needsSaving may be modified as a side-effect
loadRewards(config);
quests.add(quest);
if (needsSaving) {
try {
config.save(file);
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Failed to save Quest \"" + questName + "\"");
e.printStackTrace();
}
}
if (failedToLoad == true) {
getLogger().log(Level.SEVERE, "Failed to load Quest \"" + questName + "\". Skipping.");
}
} catch (SkipQuest ex) {
continue;
} catch (StageFailedException ex) {
continue;
if (config != null) {
ConfigurationSection questsSection;
if (config.contains("quests")) {
questsSection = config.getConfigurationSection("quests");
} else {
questsSection = config.createSection("quests");
needsSaving = true;
}
for (String key : questsSection.getKeys(false)) {
try { // main "skip quest" try/catch block
questName = key;
quest = new Quest();
failedToLoad = false;
if (config.contains("quests." + questName + ".name")) {
quest.name = parseString(config.getString("quests." + questName + ".name"), quest);
} else {
skipQuestProcess("Quest block \'" + questName + "\' is missing " + ChatColor.RED + "name:");
}
if (citizens != null && config.contains("quests." + questName + ".npc-giver-id")) {
if (CitizensAPI.getNPCRegistry().getById(config.getInt("quests." + questName + ".npc-giver-id")) != null) {
quest.npcStart = CitizensAPI.getNPCRegistry().getById(config.getInt("quests." + questName + ".npc-giver-id"));
questNPCs.add(CitizensAPI.getNPCRegistry().getById(config.getInt("quests." + questName + ".npc-giver-id")));
} else {
skipQuestProcess("npc-giver-id: for Quest " + quest.name + " is not a valid NPC id!");
}
}
if (config.contains("quests." + questName + ".block-start")) {
Location location = getLocation(config.getString("quests." + questName + ".block-start"));
if (location != null) {
quest.blockStart = location;
} else {
skipQuestProcess(new String[] { "block-start: for Quest " + quest.name + " is not in proper location format!", "Proper location format is: \"WorldName x y z\"" });
}
}
if (config.contains("quests." + questName + ".region")) {
String region = config.getString("quests." + questName + ".region");
boolean exists = regionFound(quest, region);
if (!exists) {
skipQuestProcess("region: for Quest " + quest.name + " is not a valid WorldGuard region!");
}
}
if (config.contains("quests." + questName + ".gui-display")) {
String item = config.getString("quests." + questName + ".gui-display");
try {
ItemStack stack = ItemUtil.readItemStack(item);
if (stack != null) {
quest.guiDisplay = stack;
}
} catch (Exception e) {
instance.getLogger().warning(item + " in items: GUI Display in Quest " + quest.name + "is not properly formatted!");
}
}
if (config.contains("quests." + questName + ".redo-delay")) {
if (config.getInt("quests." + questName + ".redo-delay", -999) != -999) {
quest.redoDelay = config.getInt("quests." + questName + ".redo-delay") * 1000;
} else {
skipQuestProcess("redo-delay: for Quest " + quest.name + " is not a number!");
}
}
if (config.contains("quests." + questName + ".finish-message")) {
quest.finished = parseString(config.getString("quests." + questName + ".finish-message"), quest);
} else {
skipQuestProcess("Quest " + quest.name + " is missing finish-message:");
}
if (config.contains("quests." + questName + ".ask-message")) {
quest.description = parseString(config.getString("quests." + questName + ".ask-message"), quest);
} else {
skipQuestProcess("Quest " + quest.name + " is missing ask-message:");
}
if (config.contains("quests." + questName + ".event")) {
Event evt = Event.loadEvent(config.getString("quests." + questName + ".event"), this);
if (evt != null) {
quest.initialEvent = evt;
} else {
skipQuestProcess("Initial Event in Quest " + quest.name + " failed to load.");
}
}
if (config.contains("quests." + questName + ".requirements")) {
loadQuestRequirements(config, questsSection);
}
quest.plugin = this;
processStages(quest, config, questName); // needsSaving may be modified as a side-effect
loadRewards(config);
quests.add(quest);
if (needsSaving) {
try {
config.save(file);
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Failed to save Quest \"" + questName + "\"");
e.printStackTrace();
}
}
if (failedToLoad == true) {
getLogger().log(Level.SEVERE, "Failed to load Quest \"" + questName + "\". Skipping.");
}
} catch (SkipQuest ex) {
continue;
} catch (StageFailedException ex) {
continue;
}
}
} else {
getLogger().severe("Unable to load quests.yml");
}
}

View File

@ -101,6 +101,7 @@ public class ItemUtil {
try {
stack = new ItemStack(Material.matchMaterial(arg.substring(5)));
} catch (NullPointerException npe) {
Bukkit.getLogger().severe("[Quests] The item name \'" + arg.substring(5) + "\' is invalid. Make sure quests.yml is UTF-8 encoded");
return null;
}
meta = stack.getItemMeta();
@ -110,8 +111,14 @@ public class ItemUtil {
stack.setDurability(Short.parseShort(arg.substring(5)));
} else if (arg.startsWith("enchantment-")) {
String[] enchs = arg.substring(12).split(" ");
Enchantment e = Quests.getEnchantment(enchs[0]);
meta.addEnchant(e, Integer.parseInt(enchs[1]), true);
try {
System.out.println("enchs[0] " + enchs[0]);
Enchantment e = Quests.getEnchantment(enchs[0]);
meta.addEnchant(e, Integer.parseInt(enchs[1]), true);
} catch (IllegalArgumentException e) {
Bukkit.getLogger().severe("[Quests] The enchantment name \'" + enchs[0] + "\' is invalid. Make sure quests.yml is UTF-8 encoded");
return null;
}
} else if (arg.startsWith("displayname-")) {
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', arg.substring(12)));
} else if (arg.startsWith("lore-")) {

View File

@ -102,11 +102,11 @@ public class Lang {
File langFile = new File(plugin.getDataFolder(), File.separator + "lang" + File.separator + iso + File.separator + "strings.yml");
File langFile_new = new File(plugin.getDataFolder(), File.separator + "lang" + File.separator + iso + File.separator + "strings_new.yml");
LinkedHashMap<String, String> allStrings = new LinkedHashMap<String, String>();
FileConfiguration config = new YamlConfiguration();
FileConfiguration config_new = new YamlConfiguration();
FileConfiguration config;
FileConfiguration config_new;
if (langFile.exists() && langFile_new.exists()) {
config = loadYamlUTF8(langFile);
config_new = loadYamlUTF8(langFile_new);
config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(langFile), "UTF-8"));
config_new = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(langFile_new), "UTF-8"));
//Load user's lang file and determine new strings
for (String key : config.getKeys(false)) {
allStrings.put(key, config.getString(key));
@ -116,7 +116,7 @@ public class Lang {
for (String key : config_new.getKeys(false)) {
String value = config_new.getString(key);
if (value != null) {
allStrings.put(key, config_new.getString(key));
allStrings.put(key, value);
plugin.getLogger().warning("There are new language phrases in /lang/" + iso + "/strings_new.yml for the current version!"
+ " You must transfer them to, or regenerate, strings.yml to remove this warning!");
}
@ -128,7 +128,7 @@ public class Lang {
} else {
plugin.getLogger().severe("Failed loading lang files for " + iso + " because they were not found. Using default en-US");
iso = "en-US";
config = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource("strings.yml"), "UTF-8")); //TODO better than loadYamlUTF*() ?
config = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource("strings.yml"), "UTF-8"));
for (String key : config.getKeys(false)) {
allStrings.put(key, config.getString(key));
}
@ -168,29 +168,6 @@ public class Lang {
langMap.putAll(allStrings);
plugin.getLogger().info("Loaded language " + iso + ". Translations via Crowdin");
}
/**
* Load YAML file using UTF8 format to allow extended characters
* @param file system file in YAML format
* @return yaml
* @throws InvalidConfigurationException
* @throws IOException
*/
public static YamlConfiguration loadYamlUTF8(File file) throws InvalidConfigurationException, IOException {
StringBuilder sb = new StringBuilder((int) file.length());
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
char[] buf = new char[1024];
int l;
while ((l = in.read(buf, 0, buf.length)) > -1) {
sb = sb.append(buf, 0, l);
}
in.close();
YamlConfiguration yaml = new YamlConfiguration();
yaml.loadFromString(sb.toString());
return yaml;
}
private static class LangToken {