RepairConfig.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.gmail.nossr50.config;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Set;
  5. import org.bukkit.configuration.ConfigurationSection;
  6. import org.bukkit.configuration.file.YamlConfiguration;
  7. import com.gmail.nossr50.mcMMO;
  8. import com.gmail.nossr50.skills.repair.RepairItemType;
  9. import com.gmail.nossr50.skills.repair.RepairMaterialType;
  10. import com.gmail.nossr50.skills.repair.Repairable;
  11. import com.gmail.nossr50.skills.repair.RepairableFactory;
  12. public class RepairConfig extends ConfigLoader {
  13. private List<Repairable> repairables;
  14. public RepairConfig(mcMMO plugin, String fileName) {
  15. super(plugin, fileName);
  16. this.config = YamlConfiguration.loadConfiguration(this.configFile);
  17. }
  18. @Override
  19. public void load() {
  20. loadKeys();
  21. }
  22. @Override
  23. protected void loadKeys() {
  24. repairables = new ArrayList<Repairable>();
  25. ConfigurationSection section = config.getConfigurationSection("Repairables");
  26. Set<String> keys = section.getKeys(false);
  27. for (String key : keys) {
  28. // Validate all the things!
  29. List<String> reason = new ArrayList<String>();
  30. if (!config.contains("Repairables." + key + ".ItemId")) {
  31. reason.add(key + " is missing ItemId");
  32. }
  33. if (!config.contains("Repairables." + key + ".RepairMaterialId")) {
  34. reason.add(key + " is missing RepairMaterialId");
  35. }
  36. if (!config.contains("Repairables." + key + ".MaximumDurability")) {
  37. reason.add(key + " is missing MaximumDurability");
  38. }
  39. int itemId = config.getInt("Repairables." + key + ".ItemId", 0);
  40. int repairMaterialId = config.getInt("Repairables." + key + ".RepairMaterialId", 0);
  41. int maximumDurability = config.getInt("Repairables." + key + ".MaximumDurability", 0);
  42. int repairMetadata = config.getInt("Repairables." + key + ".RepairMaterialMetadata", -1);
  43. int minimumLevel = config.getInt("Repairables." + key + ".MinimumLevel", 0);
  44. int minimumQuantity = config.getInt("Repairables." + key + ".MinimumQuantity", 2);
  45. double xpMultiplier = config.getDouble("Repairables." + key + ".XpMultiplier", 1);
  46. RepairItemType repairItemType = RepairItemType.OTHER;
  47. RepairMaterialType repairMaterialType = RepairMaterialType.OTHER;
  48. String repairItemTypeString = config.getString("Repairables." + key + ".ItemType", "OTHER");
  49. String repairMaterialTypeString = config.getString("Repairables." + key + ".MaterialType", "OTHER");
  50. if (minimumLevel < 0) {
  51. reason.add(key + " has an invalid MinimumLevel of " + minimumLevel);
  52. }
  53. if (minimumQuantity < 0) {
  54. reason.add(key + " has an invalid MinimumQuantity of " + minimumQuantity);
  55. }
  56. try {
  57. repairItemType = RepairItemType.valueOf(repairItemTypeString);
  58. }
  59. catch (IllegalArgumentException ex) {
  60. reason.add(key + " has an invalid ItemType of " + repairItemTypeString);
  61. }
  62. try {
  63. repairMaterialType = RepairMaterialType.valueOf(repairMaterialTypeString);
  64. }
  65. catch (IllegalArgumentException ex) {
  66. reason.add(key + " has an invalid MaterialType of " + repairMaterialTypeString);
  67. }
  68. if (noErrorsInRepairable(reason)) {
  69. Repairable repairable = RepairableFactory.getRepairable(itemId, repairMaterialId, (byte) repairMetadata, minimumLevel, minimumQuantity, (short) maximumDurability, repairItemType, repairMaterialType, xpMultiplier);
  70. repairables.add(repairable);
  71. }
  72. }
  73. }
  74. protected List<Repairable> getLoadedRepairables() {
  75. if (repairables == null) {
  76. return new ArrayList<Repairable>();
  77. }
  78. return repairables;
  79. }
  80. private boolean noErrorsInRepairable(List<String> issues) {
  81. if (issues.isEmpty()) {
  82. return true;
  83. }
  84. else {
  85. for (String issue : issues) {
  86. plugin.getLogger().warning(issue);
  87. }
  88. return false;
  89. }
  90. }
  91. }