CustomToolConfig.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.gmail.nossr50.config.mods;
  2. import com.gmail.nossr50.config.ConfigLoader;
  3. import com.gmail.nossr50.datatypes.mods.CustomTool;
  4. import com.gmail.nossr50.datatypes.skills.ItemType;
  5. import com.gmail.nossr50.datatypes.skills.MaterialType;
  6. import com.gmail.nossr50.skills.repair.repairables.Repairable;
  7. import com.gmail.nossr50.skills.repair.repairables.RepairableFactory;
  8. import org.bukkit.Material;
  9. import org.bukkit.configuration.ConfigurationSection;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Set;
  14. public class CustomToolConfig extends ConfigLoader {
  15. private boolean needsUpdate = false;
  16. public List<Material> customAxes = new ArrayList<>();
  17. public List<Material> customBows = new ArrayList<>();
  18. public List<Material> customHoes = new ArrayList<>();
  19. public List<Material> customPickaxes = new ArrayList<>();
  20. public List<Material> customShovels = new ArrayList<>();
  21. public List<Material> customSwords = new ArrayList<>();
  22. public HashMap<Material, CustomTool> customToolMap = new HashMap<>();
  23. public List<Repairable> repairables = new ArrayList<>();
  24. protected CustomToolConfig(String fileName) {
  25. super("mods", fileName);
  26. loadKeys();
  27. }
  28. @Override
  29. protected void loadKeys() {
  30. loadTool("Axes", customAxes);
  31. loadTool("Bows", customBows);
  32. loadTool("Hoes", customHoes);
  33. loadTool("Pickaxes", customPickaxes);
  34. loadTool("Shovels", customShovels);
  35. loadTool("Swords", customSwords);
  36. if (needsUpdate) {
  37. needsUpdate = false;
  38. backup();
  39. }
  40. }
  41. private void loadTool(String toolType, List<Material> materialList) {
  42. if (needsUpdate) {
  43. return;
  44. }
  45. ConfigurationSection toolSection = config.getConfigurationSection(toolType);
  46. if (toolSection == null) {
  47. return;
  48. }
  49. Set<String> toolConfigSet = toolSection.getKeys(false);
  50. for (String toolName : toolConfigSet) {
  51. if (config.contains(toolType + "." + toolName + "." + ".ID")) {
  52. needsUpdate = true;
  53. return;
  54. }
  55. Material toolMaterial = Material.matchMaterial(toolName);
  56. if (toolMaterial == null) {
  57. plugin.getLogger().warning("Invalid material name. This item will be skipped. - " + toolName);
  58. continue;
  59. }
  60. boolean repairable = config.getBoolean(toolType + "." + toolName + ".Repairable");
  61. Material repairMaterial = Material.matchMaterial(config.getString(toolType + "." + toolName + ".Repair_Material", ""));
  62. if (repairable && (repairMaterial == null)) {
  63. plugin.getLogger().warning("Incomplete repair information. This item will be unrepairable. - " + toolName);
  64. repairable = false;
  65. }
  66. if (repairable) {
  67. String repairItemName = config.getString(toolType + "." + toolName + ".Repair_Material_Pretty_Name");
  68. int repairMinimumLevel = config.getInt(toolType + "." + toolName + ".Repair_MinimumLevel", 0);
  69. double repairXpMultiplier = config.getDouble(toolType + "." + toolName + ".Repair_XpMultiplier", 1);
  70. short durability = toolMaterial.getMaxDurability();
  71. if (durability == 0) {
  72. durability = (short) config.getInt(toolType + "." + toolName + ".Durability", 60);
  73. }
  74. repairables.add(RepairableFactory.getRepairable(toolMaterial, repairMaterial, repairItemName, repairMinimumLevel, durability, ItemType.TOOL, MaterialType.OTHER, repairXpMultiplier));
  75. }
  76. double multiplier = config.getDouble(toolType + "." + toolName + ".XP_Modifier", 1.0);
  77. boolean abilityEnabled = config.getBoolean(toolType + "." + toolName + ".Ability_Enabled", true);
  78. int tier = config.getInt(toolType + "." + toolName + ".Tier", 1);
  79. CustomTool tool = new CustomTool(tier, abilityEnabled, multiplier);
  80. materialList.add(toolMaterial);
  81. customToolMap.put(toolMaterial, tool);
  82. }
  83. }
  84. }