ModManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package com.gmail.nossr50.util;
  2. import com.gmail.nossr50.config.Config;
  3. import com.gmail.nossr50.config.mods.CustomArmorConfig;
  4. import com.gmail.nossr50.config.mods.CustomBlockConfig;
  5. import com.gmail.nossr50.config.mods.CustomEntityConfig;
  6. import com.gmail.nossr50.config.mods.CustomToolConfig;
  7. import com.gmail.nossr50.datatypes.mods.CustomBlock;
  8. import com.gmail.nossr50.datatypes.mods.CustomEntity;
  9. import com.gmail.nossr50.datatypes.mods.CustomTool;
  10. import com.gmail.nossr50.mcMMO;
  11. import com.gmail.nossr50.skills.repair.repairables.Repairable;
  12. import org.bukkit.Material;
  13. import org.bukkit.block.BlockState;
  14. import org.bukkit.configuration.file.YamlConfiguration;
  15. import org.bukkit.entity.Entity;
  16. import org.bukkit.inventory.ItemStack;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. public class ModManager {
  22. private final List<Repairable> repairables = new ArrayList<>();
  23. // Armor Mods
  24. private final List<Material> customBoots = new ArrayList<>();
  25. private final List<Material> customChestplates = new ArrayList<>();
  26. private final List<Material> customHelmets = new ArrayList<>();
  27. private final List<Material> customLeggings = new ArrayList<>();
  28. // Block Mods
  29. private final List<Material> customExcavationBlocks = new ArrayList<>();
  30. private final List<Material> customHerbalismBlocks = new ArrayList<>();
  31. private final List<Material> customMiningBlocks = new ArrayList<>();
  32. private final List<Material> customOres = new ArrayList<>();
  33. private final List<Material> customLogs = new ArrayList<>();
  34. private final List<Material> customLeaves = new ArrayList<>();
  35. private final List<Material> customAbilityBlocks = new ArrayList<>();
  36. private final HashMap<Material, CustomBlock> customBlockMap = new HashMap<>();
  37. // Entity Mods
  38. private final HashMap<String, CustomEntity> customEntityClassMap = new HashMap<>();
  39. private final HashMap<String, CustomEntity> customEntityTypeMap = new HashMap<>();
  40. // Tool Mods
  41. private final List<Material> customAxes = new ArrayList<>();
  42. private final List<Material> customBows = new ArrayList<>();
  43. private final List<Material> customHoes = new ArrayList<>();
  44. private final List<Material> customPickaxes = new ArrayList<>();
  45. private final List<Material> customShovels = new ArrayList<>();
  46. private final List<Material> customSwords = new ArrayList<>();
  47. private final HashMap<Material, CustomTool> customToolMap = new HashMap<>();
  48. public void registerCustomArmor(CustomArmorConfig config) {
  49. customBoots.addAll(config.customBoots);
  50. customChestplates.addAll(config.customChestplates);
  51. customHelmets.addAll(config.customHelmets);
  52. customLeggings.addAll(config.customLeggings);
  53. repairables.addAll(config.repairables);
  54. }
  55. public void registerCustomBlocks(CustomBlockConfig config) {
  56. customExcavationBlocks.addAll(config.customExcavationBlocks);
  57. customHerbalismBlocks.addAll(config.customHerbalismBlocks);
  58. customMiningBlocks.addAll(config.customMiningBlocks);
  59. customOres.addAll(config.customOres);
  60. customLogs.addAll(config.customLogs);
  61. customLeaves.addAll(config.customLeaves);
  62. customAbilityBlocks.addAll(config.customAbilityBlocks);
  63. customBlockMap.putAll(config.customBlockMap);
  64. }
  65. public void registerCustomEntities(CustomEntityConfig config) {
  66. customEntityClassMap.putAll(config.customEntityClassMap);
  67. customEntityTypeMap.putAll(config.customEntityTypeMap);
  68. }
  69. public void registerCustomTools(CustomToolConfig config) {
  70. customAxes.addAll(config.customAxes);
  71. customBows.addAll(config.customBows);
  72. customHoes.addAll(config.customHoes);
  73. customPickaxes.addAll(config.customPickaxes);
  74. customShovels.addAll(config.customShovels);
  75. customSwords.addAll(config.customSwords);
  76. customToolMap.putAll(config.customToolMap);
  77. repairables.addAll(config.repairables);
  78. }
  79. public boolean isCustomBoots(Material material) {
  80. return Config.getInstance().getArmorModsEnabled() && customBoots.contains(material);
  81. }
  82. public boolean isCustomChestplate(Material material) {
  83. return Config.getInstance().getArmorModsEnabled() && customChestplates.contains(material);
  84. }
  85. public boolean isCustomHelmet(Material material) {
  86. return Config.getInstance().getArmorModsEnabled() && customHelmets.contains(material);
  87. }
  88. public boolean isCustomLeggings(Material material) {
  89. return Config.getInstance().getArmorModsEnabled() && customLeggings.contains(material);
  90. }
  91. public boolean isCustomAxe(Material material) {
  92. return Config.getInstance().getToolModsEnabled() && customAxes.contains(material);
  93. }
  94. public boolean isCustomBow(Material material) {
  95. return Config.getInstance().getToolModsEnabled() && customBows.contains(material);
  96. }
  97. public boolean isCustomHoe(Material material) {
  98. return Config.getInstance().getToolModsEnabled() && customHoes.contains(material);
  99. }
  100. public boolean isCustomPickaxe(Material material) {
  101. return Config.getInstance().getToolModsEnabled() && customPickaxes.contains(material);
  102. }
  103. public boolean isCustomShovel(Material material) {
  104. return Config.getInstance().getToolModsEnabled() && customShovels.contains(material);
  105. }
  106. public boolean isCustomSword(Material material) {
  107. return Config.getInstance().getToolModsEnabled() && customSwords.contains(material);
  108. }
  109. public boolean isCustomOre(Material data) {
  110. return Config.getInstance().getBlockModsEnabled() && customOres.contains(data);
  111. }
  112. public boolean isCustomLog(BlockState state) {
  113. return Config.getInstance().getBlockModsEnabled() && customLogs.contains(state.getType());
  114. }
  115. public boolean isCustomAbilityBlock(BlockState state) {
  116. return Config.getInstance().getBlockModsEnabled() && customAbilityBlocks.contains(state.getType());
  117. }
  118. public boolean isCustomExcavationBlock(BlockState state) {
  119. return Config.getInstance().getBlockModsEnabled() && customExcavationBlocks.contains(state.getType());
  120. }
  121. public boolean isCustomHerbalismBlock(BlockState state) {
  122. return Config.getInstance().getBlockModsEnabled() && customHerbalismBlocks.contains(state.getType());
  123. }
  124. public boolean isCustomMiningBlock(BlockState state) {
  125. return Config.getInstance().getBlockModsEnabled() && customMiningBlocks.contains(state.getType());
  126. }
  127. public CustomBlock getBlock(BlockState state) {
  128. return customBlockMap.get(state.getType());
  129. }
  130. public CustomBlock getBlock(Material data) {
  131. return customBlockMap.get(data);
  132. }
  133. /**
  134. * Checks to see if an item is a custom tool.
  135. *
  136. * @param item Item to check
  137. * @return true if the item is a custom tool, false otherwise
  138. */
  139. public boolean isCustomTool(ItemStack item) {
  140. return Config.getInstance().getToolModsEnabled() && item != null && customToolMap.containsKey(item.getType());
  141. }
  142. /**
  143. * Get the custom tool associated with an item.
  144. *
  145. * @param item The item to check
  146. * @return the tool if it exists, null otherwise
  147. */
  148. public CustomTool getTool(ItemStack item) {
  149. return item == null ? null : customToolMap.get(item.getType());
  150. }
  151. public List<Repairable> getLoadedRepairables() {
  152. return repairables;
  153. }
  154. public boolean isCustomEntity(Entity entity) {
  155. if (!Config.getInstance().getEntityModsEnabled()) {
  156. return false;
  157. }
  158. if (customEntityTypeMap.containsKey(entity.getType().toString())) {
  159. return true;
  160. }
  161. try {
  162. return customEntityClassMap.containsKey(((Class<?>) entity.getClass().getDeclaredField("entityClass").get(entity)).getName());
  163. }
  164. catch (Exception e) {
  165. if (e instanceof NoSuchFieldException || e instanceof IllegalArgumentException || e instanceof IllegalAccessException) {
  166. return customEntityClassMap.containsKey(entity.getClass().getName());
  167. }
  168. e.printStackTrace();
  169. return false;
  170. }
  171. }
  172. public CustomEntity getEntity(Entity entity) {
  173. CustomEntity customEntity = customEntityTypeMap.get(entity.getType().toString());
  174. if (customEntity == null) {
  175. try {
  176. customEntity = customEntityClassMap.get(((Class<?>) entity.getClass().getDeclaredField("entityClass").get(entity)).getName());
  177. }
  178. catch (Exception e) {
  179. if (e instanceof NoSuchFieldException || e instanceof IllegalArgumentException || e instanceof IllegalAccessException) {
  180. customEntity = customEntityClassMap.get(entity.getClass().getName());
  181. }
  182. else {
  183. e.printStackTrace();
  184. }
  185. }
  186. }
  187. return customEntity;
  188. }
  189. public void addCustomEntity(Entity entity) {
  190. if (!Config.getInstance().getEntityModsEnabled()) {
  191. return;
  192. }
  193. File entityFile = new File(mcMMO.p.getDataFolder(), "mods" + File.separator + "entities.default.yml");
  194. YamlConfiguration entitiesFile = YamlConfiguration.loadConfiguration(entityFile);
  195. String entityName = entity.getType().toString();
  196. String sanitizedEntityName = entityName.replace(".", "_");
  197. if (entitiesFile.getKeys(false).contains(sanitizedEntityName)) {
  198. return;
  199. }
  200. entitiesFile.set(sanitizedEntityName + ".XP_Multiplier", 1.0D);
  201. entitiesFile.set(sanitizedEntityName + ".Tameable", false);
  202. entitiesFile.set(sanitizedEntityName + ".Taming_XP", 0);
  203. entitiesFile.set(sanitizedEntityName + ".CanBeSummoned", false);
  204. entitiesFile.set(sanitizedEntityName + ".COTW_Material", "");
  205. entitiesFile.set(sanitizedEntityName + ".COTW_Material_Data", 0);
  206. entitiesFile.set(sanitizedEntityName + ".COTW_Material_Amount", 0);
  207. String className = "";
  208. try {
  209. className = ((Class<?>) entity.getClass().getDeclaredField("entityClass").get(entity)).getName();
  210. }
  211. catch (Exception e) {
  212. if (e instanceof NoSuchFieldException || e instanceof IllegalArgumentException || e instanceof IllegalAccessException) {
  213. className = entity.getClass().getName();
  214. }
  215. else {
  216. e.printStackTrace();
  217. }
  218. }
  219. CustomEntity customEntity = new CustomEntity(1.0D, false, 0, false, null, 0);
  220. customEntityTypeMap.put(entityName, customEntity);
  221. customEntityClassMap.put(className, customEntity);
  222. try {
  223. entitiesFile.save(entityFile);
  224. mcMMO.p.debug(entity.getType().toString() + " was added to the custom entities file!");
  225. }
  226. catch (Exception e) {
  227. e.printStackTrace();
  228. }
  229. }
  230. }