From c2d2359a8c7f54d890bab483abe09e953d6ea734 Mon Sep 17 00:00:00 2001 From: NuclearW Date: Tue, 22 May 2012 00:56:14 -0400 Subject: [PATCH] RepairConfig loader and small fix for RepairConfigManager --- .../gmail/nossr50/config/RepairConfig.java | 82 ++++++++++++++++++- .../nossr50/config/RepairConfigManager.java | 2 + 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/RepairConfig.java b/src/main/java/com/gmail/nossr50/config/RepairConfig.java index 46780173d..fb2186296 100644 --- a/src/main/java/com/gmail/nossr50/config/RepairConfig.java +++ b/src/main/java/com/gmail/nossr50/config/RepairConfig.java @@ -2,9 +2,16 @@ package com.gmail.nossr50.config; import java.util.ArrayList; import java.util.List; +import java.util.Set; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.skills.repair.RepairItemType; +import com.gmail.nossr50.skills.repair.RepairMaterialType; import com.gmail.nossr50.skills.repair.Repairable; +import com.gmail.nossr50.skills.repair.RepairableFactory; public class RepairConfig extends ConfigLoader { private final String fileName; @@ -13,21 +20,92 @@ public class RepairConfig extends ConfigLoader { public RepairConfig(mcMMO plugin, String fileName) { super(plugin, fileName); this.fileName = fileName; + this.config = YamlConfiguration.loadConfiguration(this.configFile); } @Override protected void load() { - if(plugin.isInJar(fileName)) addDefaults(); loadKeys(); } @Override protected void loadKeys() { - // TODO Auto-generated method stub + repairables = new ArrayList(); + + ConfigurationSection section = config.getConfigurationSection("Repairables"); + Set keys = section.getKeys(false); + for(String key : keys) { + // Validate all the things! + List reason = new ArrayList(); + + if(!config.contains("Repairables." + key + ".ItemId")) { + reason.add(key + " is missing ItemId"); + } + + if(!config.contains("Repairables." + key + ".RepairMaterialId")) { + reason.add(key + " is missing RepairMaterialId"); + } + + if(!config.contains("Repairables." + key + ".MaximumDurability")) { + reason.add(key + " is missing MaximumDurability"); + } + + int itemId = config.getInt("Repairables." + key + ".ItemId", 0); + int repairMaterialId = config.getInt("Repairables." + key + ".RepairMaterialId", 0); + int maximumDurability = config.getInt("Repairables." + key + ".MaximumDurability", 0); + + int repairMetadata = config.getInt("Repairables." + key + ".RepairMaterialMetadata", -1); + int minimumLevel = config.getInt("Repairables." + key + ".MinimumLevel", 0); + int minimumQuantity = config.getInt("Repairables." + key + ".MinimumQuantity", 0); + double xpMultiplier = config.getDouble("Repairables." + key + ".XpMultiplier", 1); + + RepairItemType repairItemType = RepairItemType.OTHER; + RepairMaterialType repairMaterialType = RepairMaterialType.OTHER; + + String repairItemTypeString = config.getString("Repairables." + key + ".ItemType", "OTHER"); + String repairMaterialTypeString = config.getString("Repairables." + key + ".MaterialType", "OTHER"); + + if(minimumLevel < 0) { + reason.add(key + " has an invalid MinimumLevel of " + minimumLevel); + } + + if(minimumQuantity < 0) { + reason.add(key + " has an invalid MinimumQuantity of " + minimumQuantity); + } + + try { + repairItemType = RepairItemType.valueOf(repairItemTypeString); + } catch (IllegalArgumentException ex) { + reason.add(key + " has an invalid ItemType of " + repairItemTypeString); + } + + try { + repairMaterialType = RepairMaterialType.valueOf(repairMaterialTypeString); + } catch (IllegalArgumentException ex) { + reason.add(key + " has an invalid MaterialType of " + repairMaterialTypeString); + } + + if(noErrorsInRepairable(reason)) { + Repairable repairable = RepairableFactory.getRepairable(itemId, repairMaterialId, (byte) repairMetadata, minimumLevel, minimumQuantity, (short) maximumDurability, repairItemType, repairMaterialType, xpMultiplier); + repairables.add(repairable); + } + } } protected List getLoadedRepairables() { if(repairables == null) return new ArrayList(); return repairables; } + + private boolean noErrorsInRepairable(List issues) { + if (issues.isEmpty()) { + return true; + } + else { + for (String issue : issues) { + plugin.getLogger().warning(issue); + } + return false; + } + } } diff --git a/src/main/java/com/gmail/nossr50/config/RepairConfigManager.java b/src/main/java/com/gmail/nossr50/config/RepairConfigManager.java index c9b4e34d2..cf32aaf54 100644 --- a/src/main/java/com/gmail/nossr50/config/RepairConfigManager.java +++ b/src/main/java/com/gmail/nossr50/config/RepairConfigManager.java @@ -12,6 +12,8 @@ public class RepairConfigManager { private List repairables; public RepairConfigManager(mcMMO plugin) { + repairables = new ArrayList(); + Pattern pattern = Pattern.compile("repair\\.(?:.+)\\.yml"); File dataFolder = plugin.getDataFolder();