Permalink
Browse files

Separated Config Function From The Main

  • Loading branch information...
1 parent 3ab3fc3 commit 51e5253f8cc3142bd9cc8edfe7d76e04f06de4b6 Sam Schooler committed Feb 15, 2012
@@ -29,81 +29,63 @@ public void buildQuests()
currentQuests.clear();
for (Object questName :
- plugin.getQuestConfig()
+ plugin.Config.getQuestConfig()
.getKeys(false))
{
String root = questName.toString();
- plugin.logger.info(plugin.getQuestConfig().getString("d.setup.messageStart"));
- // TODO
- if(plugin.getQuestConfig().getString(questName + ".setup.messageStart") == null)
- plugin.getQuestConfig().set(questName + ".setup.messageStart", "Hey! Can you go get my 5 diamonds! I'll pay you $500");
-
- if(plugin.getQuestConfig().getString(questName + ".setup.messageEnd") == null)
- plugin.getQuestConfig().set(questName + ".setup.messageEnd", "End Message");
-
- if(plugin.getQuestConfig().getBoolean(questName + ".setup.tasksOrdered") == false)
- plugin.getQuestConfig().set(questName + ".setup.tasksOrdered", false);
-
- if(plugin.getQuestConfig().getInt(questName + ".setup.repeated") == 0)
- plugin.getQuestConfig().set(questName + ".setup.repeated", 0);
-
- if(plugin.getQuestConfig().getBoolean(questName + ".setup.invisible") == false)
- plugin.getQuestConfig().set(questName + ".setup.invisible", false);
-
- if(plugin.getQuestConfig().getString(questName + ".setup.nextQuest") == null)
- plugin.getQuestConfig().set(questName + ".setup.nextQuest", "");
-
- plugin.saveQuestConfig();
+
+ // Validate The Quest
+ plugin.Config.validate(root);
BuildQuest quest = new BuildQuest(root);
- quest.messageStart(plugin.getQuestConfig().getString(questName + ".setup.messageStart"));
- quest.messageEnd(plugin.getQuestConfig().getString(questName + ".setup.messageEnd"));
- quest.tasksOrdered(plugin.getQuestConfig().getBoolean(questName + ".setup.tasksOrdered"));
- quest.repeated(plugin.getQuestConfig().getInt(questName + ".setup.repeated"));
- quest.nextQuest(plugin.getQuestConfig().getString(questName + ".setup.nextQuest"));
- for (Object taskNo : plugin.getQuestConfig().createSection(questName + ".tasks").getKeys(false))
+ quest.messageStart(plugin.Config.getQuestConfig().getString(questName + ".setup.messageStart"));
+ quest.messageEnd(plugin.Config.getQuestConfig().getString(questName + ".setup.messageEnd"));
+ quest.tasksOrdered(plugin.Config.getQuestConfig().getBoolean(questName + ".setup.tasksOrdered"));
+ quest.repeated(plugin.Config.getQuestConfig().getInt(questName + ".setup.repeated"));
+ quest.nextQuest(plugin.Config.getQuestConfig().getString(questName + ".setup.nextQuest"));
+ for (Object taskNo : plugin.Config.getQuestConfig().createSection(questName + ".tasks").getKeys(false))
{
try
{
Integer tRoot = Integer.parseInt(taskNo.toString().trim());
BuildTask task = new BuildTask(tRoot);
- task.type(plugin.getQuestConfig().getString(questName + ".tasks." + tRoot + ".type"));
+ task.type(plugin.Config.getQuestConfig().getString(questName + ".tasks." + tRoot + ".type"));
// TODO
- if(plugin.getQuestConfig().getString(questName + ".tasks." + tRoot + ".type").equalsIgnoreCase("kill"))
+ if(plugin.Config.getQuestConfig().getString(questName + ".tasks." + tRoot + ".type").equalsIgnoreCase("kill"))
{
- //task.id(plugin.getQuestConfig().getString(questName + ".tasks." + tRoot + ".id"));
- task.name(plugin.getQuestConfig().getString(questName + ".tasks." + tRoot + ".name"));
+ //task.id(plugin.Config.getQuestConfig().getString(questName + ".tasks." + tRoot + ".id"));
+ task.name(plugin.Config.getQuestConfig().getString(questName + ".tasks." + tRoot + ".name"));
}
else
{
- task.id(plugin.getQuestConfig().getInt(questName + ".tasks." + tRoot + ".id"));
- task.name(plugin.getQuestConfig().getString(questName + ".tasks." + tRoot + ".name"));
+ task.id(plugin.Config.getQuestConfig().getInt(questName + ".tasks." + tRoot + ".id"));
+ task.name(plugin.Config.getQuestConfig().getString(questName + ".tasks." + tRoot + ".name"));
}
- task.amount(plugin.getQuestConfig().getInt(questName + ".tasks." + tRoot + ".amount"));
+ task.amount(plugin.Config.getQuestConfig().getInt(questName + ".tasks." + tRoot + ".amount"));
this.rememberTask(tRoot, task.create(), quest);
}
catch (NumberFormatException nfe)
{
qQuests.plugin.logger.severe(qQuests.plugin.prefix + "The tasks of quest '" + root + "' are not correctly formatted!");
}
}
- quest.toJoin().put("money", plugin.getQuestConfig().getInt(questName + ".market.toJoin.money"));
- quest.toJoin().put("health", plugin.getQuestConfig().getInt(questName + ".market.toJoin.health"));
- quest.toJoin().put("hunger", plugin.getQuestConfig().getInt(questName + ".market.toJoin.hunger"));
+ quest.toJoin().put("money", plugin.Config.getQuestConfig().getInt(questName + ".market.toJoin.money"));
+ quest.toJoin().put("health", plugin.Config.getQuestConfig().getInt(questName + ".market.toJoin.health"));
+ quest.toJoin().put("hunger", plugin.Config.getQuestConfig().getInt(questName + ".market.toJoin.hunger"));
- quest.toDrop().put("money", plugin.getQuestConfig().getInt(questName + ".market.toJoin.money"));
- quest.toDrop().put("health", plugin.getQuestConfig().getInt(questName + ".market.toJoin.health"));
- quest.toDrop().put("hunger", plugin.getQuestConfig().getInt(questName + ".market.toJoin.hunger"));
+ quest.toDrop().put("money", plugin.Config.getQuestConfig().getInt(questName + ".market.toJoin.money"));
+ quest.toDrop().put("health", plugin.Config.getQuestConfig().getInt(questName + ".market.toJoin.health"));
+ quest.toDrop().put("hunger", plugin.Config.getQuestConfig().getInt(questName + ".market.toJoin.hunger"));
- for (Object rewardNo : plugin.getQuestConfig().createSection(questName + ".market.toComplete").getKeys(false))
+ for (Object rewardNo : plugin.Config.getQuestConfig().createSection(questName + ".market.toComplete").getKeys(false))
{
try
{
Integer rRoot = Integer.parseInt(rewardNo.toString().trim());
BuildReward reward = new BuildReward(rRoot);
- reward.money = plugin.getQuestConfig().getInt(questName + ".market.toComplete." + rewardNo + ".money");
- reward.health = plugin.getQuestConfig().getInt(questName + ".market.toComplete." + rewardNo + ".health");
- reward.hunger = plugin.getQuestConfig().getInt(questName + ".market.toComplete." + rewardNo + ".hunger");
+ reward.money = plugin.Config.getQuestConfig().getInt(questName + ".market.toComplete." + rewardNo + ".money");
+ reward.health = plugin.Config.getQuestConfig().getInt(questName + ".market.toComplete." + rewardNo + ".health");
+ reward.hunger = plugin.Config.getQuestConfig().getInt(questName + ".market.toComplete." + rewardNo + ".hunger");
this.rememberReward(rRoot, reward.create(), quest);
}
catch (NumberFormatException nfe)
@@ -0,0 +1,163 @@
+package me.quaz3l.qQuests.Util;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import me.quaz3l.qQuests.qQuests;
+
+import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.configuration.file.YamlConfiguration;
+
+public class Config {
+ private qQuests plugin;
+
+ // Configuration Files Variables
+ private FileConfiguration qConfig = null;
+ private File qConfigFile = null;
+ private FileConfiguration cConfig = null;
+ private File cConfigFile = null;
+
+ public Config(qQuests plugin) {
+ this.plugin = plugin;
+ }
+
+ // Configuration Functions
+ public FileConfiguration getQuestConfig() {
+ if (qConfig == null) {
+ reloadQuestConfig();
+ }
+ return qConfig;
+ }
+ public void reloadQuestConfig() {
+ if (qConfigFile == null)
+ {
+ qConfigFile = new File(plugin.getDataFolder(), "quests.yml");
+ }
+ qConfig = YamlConfiguration.loadConfiguration(qConfigFile);
+
+ InputStream defConfigStream = plugin.getResource("quests.yml");
+ if (defConfigStream != null) {
+ YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
+ qConfig.setDefaults(defConfig);
+ }
+ }
+ public void saveQuestConfig() {
+ if (qConfig == null || qConfigFile == null) {
+ return;
+ }
+ try {
+ qConfig.save(qConfigFile);
+ } catch (IOException ex) {
+ plugin.logger.severe(plugin.prefix + "Could not save config to " + qConfigFile);
+ }
+ }
+ public FileConfiguration getConfig() {
+ if (cConfig == null) {
+ reloadConfig();
+ }
+ return cConfig;
+ }
+ public void reloadConfig() {
+ if (cConfigFile == null)
+ {
+ cConfigFile = new File(plugin.getDataFolder(), "config.yml");
+ }
+ cConfig = YamlConfiguration.loadConfiguration(cConfigFile);
+
+ InputStream defConfigStream = plugin.getResource("config.yml");
+ if (defConfigStream != null) {
+ YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
+ cConfig.setDefaults(defConfig);
+ cConfig.options().copyDefaults(true);
+ }
+ }
+ public void saveConfig() {
+ if (cConfig == null || cConfigFile == null) {
+ return;
+ }
+ try {
+ cConfig.save(cConfigFile);
+ } catch (IOException ex) {
+ plugin.logger.severe(plugin.prefix + "Could not save config to " + cConfigFile);
+ }
+ }
+ public void initialize() {
+ if(this.getQuestConfig().getKeys(false).size() < 1) {
+ this.getQuestConfig().options().copyDefaults(true);
+
+ // Set Setup Nodes
+ if(this.getQuestConfig().getString("Diamonds!.setup.messageStart") == null)
+ this.getQuestConfig().set("Diamonds!.setup.messageStart", "Hey! Can you go get my 5 diamonds! I'll pay you $500");
+ if(this.getQuestConfig().getString("Diamonds!.setup.messageEnd") == null)
+ this.getQuestConfig().set("Diamonds!.setup.messageEnd", "Thanks! Now I can feed my lava dragon! ;)");
+ if(this.getQuestConfig().getBoolean("Diamonds!.setup.tasksOrdered") == false)
+ this.getQuestConfig().set("Diamonds!.setup.tasksOrdered", false);
+ if(this.getQuestConfig().getInt("Diamonds!.setup.repeated") == 0)
+ this.getQuestConfig().set("Diamonds!.setup.repeated", 0);
+ if(this.getQuestConfig().getBoolean("Diamonds!.setup.invisible") == false)
+ this.getQuestConfig().set("Diamonds!.setup.invisible", false);
+ if(this.getQuestConfig().getString("Diamonds!.setup.nextQuest") == null)
+ this.getQuestConfig().set("Diamonds!.setup.nextQuest", "");
+
+ // Set Task Nodes
+ if(this.getQuestConfig().getString("Diamonds!.tasks.0.type") == null)
+ this.getQuestConfig().set("Diamonds!.tasks.0.type", "collect");
+ if(this.getQuestConfig().getInt("Diamonds!.tasks.0.id") == 0)
+ this.getQuestConfig().set("Diamonds!.tasks.0.id", 264);
+ if(this.getQuestConfig().getString("Diamonds!.tasks.0.name") == null)
+ this.getQuestConfig().set("Diamonds!.tasks.0.name", "Diamonds");
+ if(this.getQuestConfig().getInt("Diamonds!.tasks.0.amount") == 0)
+ this.getQuestConfig().set("Diamonds!.tasks.0.amount", 5);
+ if(this.getQuestConfig().getString("Diamonds!.tasks.0.nextTask") == null)
+ this.getQuestConfig().set("Diamonds!.tasks.0.nextTask", "");
+
+ // Set toJoin Nodes
+ if(this.getQuestConfig().getInt("Diamonds!.market.toJoin.money") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toJoin.money", 0);
+ if(this.getQuestConfig().getInt("Diamonds!.market.toJoin.health") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toJoin.health", 0);
+ if(this.getQuestConfig().getInt("Diamonds!.market.toJoin.hunger") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toJoin.hunger", 0);
+
+ // Set toDrop Nodes
+ if(this.getQuestConfig().getInt("Diamonds!.market.toDrop.money") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toDrop.money", -50);
+ if(this.getQuestConfig().getInt("Diamonds!.market.toDrop.health") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toDrop.health", 0);
+ if(this.getQuestConfig().getInt("Diamonds!.market.toDrop.hunger") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toDrop.hunger", 0);
+
+ // Set toComplete Nodes
+ if(this.getQuestConfig().getInt("Diamonds!.market.toComplete.0.money") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toComplete.0.money", 500);
+ if(this.getQuestConfig().getInt("Diamonds!.market.toComplete.0.health") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toComplete.0.health", 0);
+ if(this.getQuestConfig().getInt("Diamonds!.market.toComplete.0.hunger") == 0)
+ this.getQuestConfig().set("Diamonds!.market.toComplete.0.hunger", 0);
+
+ this.saveQuestConfig();
+ }
+ }
+ public void validate(String questName) {
+ if(this.getQuestConfig().getString(questName + ".setup.messageStart") == null)
+ this.getQuestConfig().set(questName + ".setup.messageStart", "Hey! Can you go get my 5 diamonds! I'll pay you $500");
+
+ if(this.getQuestConfig().getString(questName + ".setup.messageEnd") == null)
+ this.getQuestConfig().set(questName + ".setup.messageEnd", "End Message");
+
+ if(this.getQuestConfig().getBoolean(questName + ".setup.tasksOrdered") == false)
+ this.getQuestConfig().set(questName + ".setup.tasksOrdered", false);
+
+ if(this.getQuestConfig().getInt(questName + ".setup.repeated") == 0)
+ this.getQuestConfig().set(questName + ".setup.repeated", 0);
+
+ if(this.getQuestConfig().getBoolean(questName + ".setup.invisible") == false)
+ this.getQuestConfig().set(questName + ".setup.invisible", false);
+
+ if(this.getQuestConfig().getString(questName + ".setup.nextQuest") == null)
+ this.getQuestConfig().set(questName + ".setup.nextQuest", "");
+
+ this.saveQuestConfig();
+ }
+}
@@ -11,7 +11,8 @@
import me.quaz3l.qQuests.qQuests;
public class qListener implements Listener {
- public qQuests plugin;
+ @SuppressWarnings("unused")
+ private qQuests plugin;
public qListener(qQuests plugin) {
this.plugin = plugin;
Oops, something went wrong.

0 comments on commit 51e5253

Please sign in to comment.