ModChecks.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package com.gmail.nossr50.mods;
  2. import org.bukkit.block.Block;
  3. import org.bukkit.entity.Entity;
  4. import org.bukkit.inventory.ItemStack;
  5. import org.bukkit.material.MaterialData;
  6. import com.gmail.nossr50.config.Config;
  7. import com.gmail.nossr50.mods.config.CustomArmorConfig;
  8. import com.gmail.nossr50.mods.config.CustomBlocksConfig;
  9. import com.gmail.nossr50.mods.config.CustomEntityConfig;
  10. import com.gmail.nossr50.mods.config.CustomToolsConfig;
  11. import com.gmail.nossr50.mods.datatypes.CustomBlock;
  12. import com.gmail.nossr50.mods.datatypes.CustomEntity;
  13. import com.gmail.nossr50.mods.datatypes.CustomItem;
  14. import com.gmail.nossr50.mods.datatypes.CustomTool;
  15. public final class ModChecks {
  16. private static Config configInstance = Config.getInstance();
  17. private static boolean customToolsEnabled = configInstance.getToolModsEnabled();
  18. private static boolean customArmorEnabled = configInstance.getArmorModsEnabled();
  19. private static boolean customBlocksEnabled = configInstance.getBlockModsEnabled();
  20. private static boolean customEntitiesEnabled = configInstance.getEntityModsEnabled();
  21. private ModChecks() {}
  22. /**
  23. * Get the custom armor associated with an item.
  24. *
  25. * @param item The item to check
  26. * @return the armor if it exists, null otherwise
  27. */
  28. public static CustomItem getArmorFromItemStack(ItemStack item) {
  29. return CustomArmorConfig.getInstance().customArmor.get(item.getTypeId());
  30. }
  31. /**
  32. * Get the custom tool associated with an item.
  33. *
  34. * @param item The item to check
  35. * @return the tool if it exists, null otherwise
  36. */
  37. public static CustomTool getToolFromItemStack(ItemStack item) {
  38. return CustomToolsConfig.getInstance().customTools.get(item.getTypeId());
  39. }
  40. /**
  41. * Get the custom block associated with an block.
  42. *
  43. * @param block The block to check
  44. * @return the block if it exists, null otherwise
  45. */
  46. public static CustomBlock getCustomBlock(Block block) {
  47. if (!Config.getInstance().getBlockModsEnabled()) {
  48. return null;
  49. }
  50. ItemStack item = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
  51. if (!CustomBlocksConfig.getInstance().customItems.contains(item)) {
  52. return null;
  53. }
  54. for (CustomBlock b : CustomBlocksConfig.getInstance().customBlocks) {
  55. if ((b.getItemID() == block.getTypeId()) && (b.getDataValue() == block.getData())) {
  56. return b;
  57. }
  58. }
  59. return null;
  60. }
  61. public static CustomEntity getCustomEntity(Entity entity) {
  62. if (!CustomEntityConfig.getInstance().customEntityIds.contains(entity.getEntityId()) && !CustomEntityConfig.getInstance().customEntityTypes.contains(entity.getType())) {
  63. return null;
  64. }
  65. for (CustomEntity customEntity : CustomEntityConfig.getInstance().customEntities) {
  66. if ((customEntity.getEntityID() == entity.getEntityId()) && (customEntity.getEntityType() == entity.getType())) {
  67. return customEntity;
  68. }
  69. }
  70. return null;
  71. }
  72. /**
  73. * Check if a custom block is a mining block.
  74. *
  75. * @param block The block to check
  76. * @return true if the block is custom, false otherwise
  77. */
  78. public static boolean isCustomMiningBlock(Block block) {
  79. ItemStack item = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
  80. if (customBlocksEnabled && CustomBlocksConfig.getInstance().customMiningBlocks.contains(item)) {
  81. for (CustomBlock b : CustomBlocksConfig.getInstance().customBlocks) {
  82. if ((b.getItemID() == block.getTypeId()) && (b.getDataValue() == block.getData())) {
  83. return true;
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. /**
  90. * Check if a custom block is a mining block.
  91. *
  92. * @param block The block to check
  93. * @return true if the block is custom, false otherwise
  94. */
  95. public static boolean isCustomExcavationBlock(Block block) {
  96. ItemStack item = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
  97. if (customBlocksEnabled && CustomBlocksConfig.getInstance().customExcavationBlocks.contains(item)) {
  98. for (CustomBlock b : CustomBlocksConfig.getInstance().customBlocks) {
  99. if ((b.getItemID() == block.getTypeId()) && (b.getDataValue() == block.getData())) {
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Check if a custom block is a leaf block.
  108. *
  109. * @param block The block to check
  110. * @return true if the block represents leaves, false otherwise
  111. */
  112. public static boolean isCustomLeafBlock(Block block) {
  113. ItemStack item = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
  114. if (CustomBlocksConfig.getInstance().customLeaves.contains(item)) {
  115. for (CustomBlock b : CustomBlocksConfig.getInstance().customBlocks) {
  116. if ((b.getItemID() == block.getTypeId()) && (b.getDataValue() == block.getData())) {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Check if a custom block is a log block.
  125. *
  126. * @param block The block to check
  127. * @return true if the block represents a log, false otherwise
  128. */
  129. public static boolean isCustomLogBlock(Block block) {
  130. ItemStack item = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
  131. if (CustomBlocksConfig.getInstance().customLogs.contains(item)) {
  132. for (CustomBlock b : CustomBlocksConfig.getInstance().customBlocks) {
  133. if ((b.getItemID() == block.getTypeId()) && (b.getDataValue() == block.getData())) {
  134. return true;
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * Check if a custom block is an ore block.
  142. *
  143. * @param block The block to check
  144. * @return true if the block represents an ore, false otherwise
  145. */
  146. public static boolean isCustomOreBlock(Block block) {
  147. ItemStack item = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
  148. if (CustomBlocksConfig.getInstance().customOres.contains(item)) {
  149. for (CustomBlock b : CustomBlocksConfig.getInstance().customBlocks) {
  150. if ((b.getItemID() == block.getTypeId()) && (b.getDataValue() == block.getData())) {
  151. return true;
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * Checks to see if an item is a custom tool.
  159. *
  160. * @param item Item to check
  161. * @return true if the item is a custom tool, false otherwise
  162. */
  163. public static boolean isCustomTool(ItemStack item) {
  164. if (customToolsEnabled && CustomToolsConfig.getInstance().customTools.containsKey(item.getTypeId())) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * Checks to see if an item is custom armor.
  171. *
  172. * @param item Item to check
  173. * @return true if the item is custom armor, false otherwise
  174. */
  175. public static boolean isCustomArmor(ItemStack item) {
  176. if (customArmorEnabled && CustomArmorConfig.getInstance().customArmor.containsKey(item.getTypeId())) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. public static boolean isCustomEntity(Entity entity) {
  182. if (customEntitiesEnabled && CustomEntityConfig.getInstance().customEntityIds.contains(entity.getEntityId())) {
  183. return true;
  184. }
  185. return false;
  186. }
  187. }