CustomToolConfig.java 4.2 KB

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