diff options
Diffstat (limited to 'src/main/java')
5 files changed, 269 insertions, 133 deletions
diff --git a/src/main/java/com/leonardobishop/quests/Quests.java b/src/main/java/com/leonardobishop/quests/Quests.java index 6c198ade..fac08e53 100644 --- a/src/main/java/com/leonardobishop/quests/Quests.java +++ b/src/main/java/com/leonardobishop/quests/Quests.java @@ -28,6 +28,7 @@ import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -52,6 +53,7 @@ public class Quests extends JavaPlugin { private static Updater updater; private static Title title; private boolean brokenConfig = false; + private QuestsConfigLoader questsConfigLoader; public static Quests getInstance() { return instance; @@ -73,6 +75,10 @@ public class Quests extends JavaPlugin { return brokenConfig; } + public void setBrokenConfig(boolean brokenConfig) { + this.brokenConfig = brokenConfig; + } + public static Title getTitle() { return title; } @@ -81,6 +87,10 @@ public class Quests extends JavaPlugin { return updater; } + public QuestsConfigLoader getQuestsConfigLoader() { + return questsConfigLoader; + } + public static String convertToFormat(long m) { long hours = m / 60; long minutesLeft = m - hours * 60; @@ -135,7 +145,7 @@ public class Quests extends JavaPlugin { Metrics metrics = new Metrics(this); this.getLogger().log(Level.INFO, "Metrics started. This can be disabled at /plugins/bStats/config.yml."); - SimilarBlocks.addBlocks(); + questsConfigLoader = new QuestsConfigLoader(Quests.this); new BukkitRunnable() { @Override @@ -174,6 +184,12 @@ public class Quests extends JavaPlugin { } reloadQuests(); + if (!questsConfigLoader.getBrokenFiles().isEmpty()) { + Quests.this.getLogger().warning("Quests has failed to load the following files:"); + for (Map.Entry<String, QuestsConfigLoader.ConfigLoadError> entry : questsConfigLoader.getBrokenFiles().entrySet()) { + Quests.this.getLogger().warning(" - " + entry.getKey() + ": " + entry.getValue().getMessage()); + } + } for (Player player : Bukkit.getOnlinePlayers()) { qPlayerManager.loadPlayer(player.getUniqueId()); @@ -243,136 +259,13 @@ public class Quests extends JavaPlugin { questManager.getCategories().clear(); taskTypeManager.resetTaskTypes(); - // test file integrity - try { - YamlConfiguration config = new YamlConfiguration(); - config.load(new File(String.valueOf(Quests.this.getDataFolder() + File.separator + "config.yml"))); - } catch (Exception ex) { - Quests.this.getLogger().log(Level.SEVERE, "You have a YAML error in your Quests config. If this is your first time using Quests, please remove the Quests folder and RESTART (not reload!) the server and try again."); - brokenConfig = true; - } - - for (String id : getConfig().getConfigurationSection("categories").getKeys(false)) { - ItemStack displayItem = getItemStack("categories." + id + ".display"); - boolean permissionRequired = getConfig().getBoolean("categories." + id + ".permission-required", false); - - Category category = new Category(id, displayItem, permissionRequired); - questManager.registerCategory(category); - } - - for (String id : getConfig().getConfigurationSection("quests").getKeys(false)) { - String root = "quests." + id; - - QItemStack displayItem = getQItemStack(root + ".display"); - List<String> rewards = getConfig().getStringList(root + ".rewards"); - List<String> requirements = getConfig().getStringList(root + ".options.requires"); - List<String> rewardString = getConfig().getStringList(root + ".rewardstring"); - boolean repeatable = getConfig().getBoolean(root + ".options.repeatable", false); - boolean cooldown = getConfig().getBoolean(root + ".options.cooldown.enabled", false); - boolean permissionRequired = getConfig().getBoolean(root + ".options.permission-required", false); - int cooldownTime = getConfig().getInt(root + ".options.cooldown.time", 10); - String category = getConfig().getString(root + ".options.category"); - - if (rewardString == null) { - rewardString = new ArrayList<>(); - } - if (requirements == null) { - requirements = new ArrayList<>(); - } - if (rewards == null) { - rewards = new ArrayList<>(); - } - if (category == null) { - category = ""; - } - - - Quest quest; - if (category.equals("")) { - quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString); - } else { - quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString, category); - Category c = questManager.getCategoryById(category); - if (c != null) { - c.registerQuestId(id); - } - } - - for (String taskId : getConfig().getConfigurationSection(root + ".tasks").getKeys(false)) { - String taskRoot = root + ".tasks." + taskId; - String taskType = getConfig().getString(taskRoot + ".type"); - - Task task = new Task(taskId, taskType); - - for (String key : getConfig().getConfigurationSection(taskRoot).getKeys(false)) { - task.addConfigValue(key, getConfig().get(taskRoot + "." + key)); - } - - quest.registerTask(task); - } - - if (getConfig().getBoolean("options.show-quest-registrations")) { - this.getLogger().log(Level.INFO, "Registering quest " + quest.getId() + " with " + quest.getTasks().size() + " tasks."); - } - questManager.registerQuest(quest); - taskTypeManager.registerQuestTasksWithTaskTypes(quest); - } - } - - private QItemStack getQItemStack(String path) { - String cName = this.getConfig().getString(path + ".name", path + ".name"); - String cType = this.getConfig().getString(path + ".type", path + ".type"); - List<String> cLoreNormal = this.getConfig().getStringList(path + ".lore-normal"); - List<String> cLoreStarted = this.getConfig().getStringList(path + ".lore-started"); - - String name; - Material type = null; - int data = 0; - List<String> loreNormal = new ArrayList<>(); - if (cLoreNormal != null) { - for (String s : cLoreNormal) { - loreNormal.add(ChatColor.translateAlternateColorCodes('&', s)); - } - } - List<String> loreStarted = new ArrayList<>(); - if (cLoreStarted != null) { - for (String s : cLoreStarted) { - loreStarted.add(ChatColor.translateAlternateColorCodes('&', s)); - } - } - name = ChatColor.translateAlternateColorCodes('&', cName); - - if (StringUtils.isNumeric(cType)) { - type = Material.getMaterial(Integer.parseInt(cType)); - } else if (Material.getMaterial(cType) != null) { - type = Material.getMaterial(cType); - } else if (cType.contains(":")) { - String[] parts = cType.split(":"); - if (parts.length > 1) { - if (StringUtils.isNumeric(parts[0])) { - type = Material.getMaterial(Integer.parseInt(parts[0])); - } else if (Material.getMaterial(parts[0]) != null) { - type = Material.getMaterial(parts[0]); - } - if (StringUtils.isNumeric(parts[1])) { - data = Integer.parseInt(parts[1]); - } - } - } - - if (type == null) { - type = Material.STONE; - } - - QItemStack is = new QItemStack(name, loreNormal, loreStarted, type, data); - - return is; + questsConfigLoader.loadConfig(); } - public ItemStack getItemStack(String path) { - String cName = this.getConfig().getString(path + ".name", path + ".name"); - String cType = this.getConfig().getString(path + ".type", path + ".type"); - List<String> cLore = this.getConfig().getStringList(path + ".lore"); + public ItemStack getItemStack(String path, FileConfiguration config) { + String cName = config.getString(path + ".name", path + ".name"); + String cType = config.getString(path + ".type", path + ".type"); + List<String> cLore = config.getStringList(path + ".lore"); String name; Material type = null; @@ -489,7 +382,35 @@ public class Quests extends JavaPlugin { } } catch (IOException e) { e.printStackTrace(); - return; + } + } + + File questsDirectory = new File(String.valueOf(this.getDataFolder() + File.separator + "quests")); + if (!questsDirectory.exists() && !questsDirectory.isDirectory()) { + questsDirectory.mkdir(); + + ArrayList<String> examples = new ArrayList<>(); + examples.add("example1.yml"); + examples.add("example2.yml"); + examples.add("example3.yml"); + examples.add("example4.yml"); + examples.add("example5.yml"); + examples.add("example6.yml"); + examples.add("README.txt"); + + for (String name : examples) { + File file = new File(this.getDataFolder() + File.separator + "quests" + File.separator + name); + try { + file.createNewFile(); + try (InputStream in = Quests.class.getClassLoader().getResourceAsStream("quests/" + name)) { + OutputStream out = new FileOutputStream(file); + ByteStreams.copy(in, out); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (IOException e) { + e.printStackTrace(); + } } } } diff --git a/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java b/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java new file mode 100644 index 00000000..a8abb580 --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java @@ -0,0 +1,206 @@ +package com.leonardobishop.quests; + +import com.leonardobishop.quests.obj.misc.QItemStack; +import com.leonardobishop.quests.quests.Category; +import com.leonardobishop.quests.quests.Quest; +import com.leonardobishop.quests.quests.Task; +import org.apache.commons.lang.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; + +public class QuestsConfigLoader { + + private Map<String, ConfigLoadError> brokenFiles = new HashMap<>(); + private Quests quests; + + public QuestsConfigLoader(Quests quests) { + this.quests = quests; + } + + /** + * Loads and parses config into memory including the quests folder. + */ + public void loadConfig() { + brokenFiles.clear(); + + // test CONFIG file integrity + try { + YamlConfiguration config = new YamlConfiguration(); + config.load(new File(String.valueOf(quests.getDataFolder() + File.separator + "config.yml"))); + } catch (Exception ex) { + brokenFiles.put("<MAIN CONFIG> config.yml", ConfigLoadError.MAIN_CONFIG_ERROR); + quests.setBrokenConfig(true); + return; + } + + for (String id : quests.getConfig().getConfigurationSection("categories").getKeys(false)) { + ItemStack displayItem = quests.getItemStack("categories." + id + ".display", quests.getConfig()); + boolean permissionRequired = quests.getConfig().getBoolean("categories." + id + ".permission-required", false); + + Category category = new Category(id, displayItem, permissionRequired); + Quests.getQuestManager().registerCategory(category); + } + + File questDirectory = new File(quests.getDataFolder() + File.separator + "quests"); + if (questDirectory.isDirectory()) { + File[] fileList = questDirectory.listFiles(); + for (File questFile : fileList) { + if (!questFile.getName().toLowerCase().endsWith(".yml")) continue; + + YamlConfiguration config = new YamlConfiguration(); + // test QUEST file integrity + try { + config.load(questFile); + } catch (Exception ex) { + brokenFiles.put(questFile.getName(), ConfigLoadError.MALFORMED_YAML); + continue; + } + + String id = questFile.getName().replace(".yml", ""); + + if (!StringUtils.isAlphanumeric(id)) { + brokenFiles.put(questFile.getName(), ConfigLoadError.INVALID_QUEST_ID); + continue; + } + + QItemStack displayItem = getQItemStack("display", config); + List<String> rewards = config.getStringList("rewards"); + List<String> requirements = config.getStringList("options.requires"); + List<String> rewardString = config.getStringList("rewardstring"); + boolean repeatable = config.getBoolean("options.repeatable", false); + boolean cooldown = config.getBoolean("options.cooldown.enabled", false); + boolean permissionRequired = config.getBoolean("options.permission-required", false); + int cooldownTime = config.getInt("options.cooldown.time", 10); + String category = config.getString("options.category"); + + if (rewardString == null) { + rewardString = new ArrayList<>(); + } + if (requirements == null) { + requirements = new ArrayList<>(); + } + if (rewards == null) { + rewards = new ArrayList<>(); + } + if (category == null) { + category = ""; + } + + + Quest quest; + if (category.equals("")) { + quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString); + } else { + quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString, category); + Category c = Quests.getQuestManager().getCategoryById(category); + if (c != null) { + c.registerQuestId(id); + } + } + + for (String taskId : config.getConfigurationSection("tasks").getKeys(false)) { + String taskRoot = "tasks." + taskId; + String taskType = config.getString(taskRoot + ".type"); + + Task task = new Task(taskId, taskType); + + for (String key : config.getConfigurationSection(taskRoot).getKeys(false)) { + task.addConfigValue(key, config.get(taskRoot + "." + key)); + } + + quest.registerTask(task); + } + + if (quests.getConfig().getBoolean("options.show-quest-registrations")) { + quests.getLogger().log(Level.INFO, "Registering quest " + quest.getId() + " with " + quest.getTasks().size() + " tasks."); + } + Quests.getQuestManager().registerQuest(quest); + Quests.getTaskTypeManager().registerQuestTasksWithTaskTypes(quest); + } + } + } + + /** + * Gets recent file errors during load. + * + * @return Errors during load, Map<String, ConfigLoadError> of file name and error + */ + public Map<String, ConfigLoadError> getBrokenFiles() { + return brokenFiles; + } + + private QItemStack getQItemStack(String path, FileConfiguration config) { + String cName = config.getString(path + ".name", path + ".name"); + String cType = config.getString(path + ".type", path + ".type"); + List<String> cLoreNormal = config.getStringList(path + ".lore-normal"); + List<String> cLoreStarted = config.getStringList(path + ".lore-started"); + + String name; + Material type = null; + int data = 0; + List<String> loreNormal = new ArrayList<>(); + if (cLoreNormal != null) { + for (String s : cLoreNormal) { + loreNormal.add(ChatColor.translateAlternateColorCodes('&', s)); + } + } + List<String> loreStarted = new ArrayList<>(); + if (cLoreStarted != null) { + for (String s : cLoreStarted) { + loreStarted.add(ChatColor.translateAlternateColorCodes('&', s)); + } + } + name = ChatColor.translateAlternateColorCodes('&', cName); + + if (StringUtils.isNumeric(cType)) { + type = Material.getMaterial(Integer.parseInt(cType)); + } else if (Material.getMaterial(cType) != null) { + type = Material.getMaterial(cType); + } else if (cType.contains(":")) { + String[] parts = cType.split(":"); + if (parts.length > 1) { + if (StringUtils.isNumeric(parts[0])) { + type = Material.getMaterial(Integer.parseInt(parts[0])); + } else if (Material.getMaterial(parts[0]) != null) { + type = Material.getMaterial(parts[0]); + } + if (StringUtils.isNumeric(parts[1])) { + data = Integer.parseInt(parts[1]); + } + } + } + + if (type == null) { + type = Material.STONE; + } + + return new QItemStack(name, loreNormal, loreStarted, type, data); + } + + public enum ConfigLoadError { + + MAIN_CONFIG_ERROR("You have a YAML error in your Quests config. If this is your first time using Quests, please remove the Quests folder and RESTART (not reload!) the server and try again."), + MALFORMED_YAML("Malformed YAML"), + INVALID_QUEST_ID("Invalid quest ID (must be alphanumeric)"); + + private String message; + + ConfigLoadError(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + } +} diff --git a/src/main/java/com/leonardobishop/quests/blocktype/SimilarBlocks.java b/src/main/java/com/leonardobishop/quests/blocktype/SimilarBlocks.java index caa71333..4e6571d4 100644 --- a/src/main/java/com/leonardobishop/quests/blocktype/SimilarBlocks.java +++ b/src/main/java/com/leonardobishop/quests/blocktype/SimilarBlocks.java @@ -9,7 +9,7 @@ public class SimilarBlocks { private static HashMap<Block, Block> similarBlocks = new HashMap<>(); - public static void addBlocks() { + static { // Redstone Ore similarBlocks.put(new Block(Material.REDSTONE_ORE), new Block(Material.GLOWING_REDSTONE_ORE)); similarBlocks.put(new Block(Material.GLOWING_REDSTONE_ORE), new Block(Material.REDSTONE_ORE)); diff --git a/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java b/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java index deea2e94..fe6e5c92 100644 --- a/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java +++ b/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java @@ -1,6 +1,7 @@ package com.leonardobishop.quests.commands; import com.leonardobishop.quests.Quests; +import com.leonardobishop.quests.QuestsConfigLoader; import com.leonardobishop.quests.obj.Messages; import com.leonardobishop.quests.obj.Options; import com.leonardobishop.quests.player.QPlayer; @@ -17,6 +18,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; +import java.util.Map; import java.util.UUID; public class CommandQuests implements CommandExecutor { @@ -50,7 +52,14 @@ public class CommandQuests implements CommandExecutor { } else if (args[1].equalsIgnoreCase("reload")) { Quests.getInstance().reloadConfig(); Quests.getInstance().reloadQuests(); - sender.sendMessage(ChatColor.GRAY + "Quests was reloaded."); + if (!Quests.getInstance().getQuestsConfigLoader().getBrokenFiles().isEmpty()) { + sender.sendMessage(ChatColor.RED + "Quests has failed to load the following files:"); + for (Map.Entry<String, QuestsConfigLoader.ConfigLoadError> entry : Quests.getInstance().getQuestsConfigLoader().getBrokenFiles().entrySet()) { + sender.sendMessage(ChatColor.DARK_GRAY + " * " + ChatColor.RED + entry.getKey() + ": " + ChatColor.GRAY + entry.getValue().getMessage()); + } + } else { + sender.sendMessage(ChatColor.GRAY + "Quests was reloaded."); + } return true; } else if (args[1].equalsIgnoreCase("types")) { sender.sendMessage(ChatColor.GRAY + "Registered task types:"); diff --git a/src/main/java/com/leonardobishop/quests/obj/Items.java b/src/main/java/com/leonardobishop/quests/obj/Items.java index 9b660c5d..9b5f4430 100644 --- a/src/main/java/com/leonardobishop/quests/obj/Items.java +++ b/src/main/java/com/leonardobishop/quests/obj/Items.java @@ -23,7 +23,7 @@ public enum Items { } public ItemStack getItem() { - return Quests.getInstance().getItemStack(path); + return Quests.getInstance().getItemStack(path, Quests.getInstance().getConfig()); } } |
