Misc.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.gmail.nossr50.util;
  2. import java.util.Random;
  3. import org.bukkit.Location;
  4. import org.bukkit.Material;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.entity.LivingEntity;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.entity.EntityDamageEvent;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.plugin.PluginManager;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.config.Config;
  13. import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
  14. import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
  15. import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
  16. import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
  17. public class Misc {
  18. private static Random random = new Random();
  19. /**
  20. * Gets a capitalized version of the target string.
  21. *
  22. * @param target String to capitalize
  23. * @return the capitalized string
  24. */
  25. public static String getCapitalized(String target) {
  26. String firstLetter = target.substring(0,1);
  27. String remainder = target.substring(1);
  28. String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
  29. return capitalized;
  30. }
  31. /**
  32. * Gets a nicely formatted string version of an item name from a given item ID.
  33. *
  34. * @param itemID The ID of the item to convert to string.
  35. * @return the nicely formatting string
  36. */
  37. public static String prettyItemString(int itemID) {
  38. String baseString = Material.getMaterial(itemID).toString();
  39. String[] substrings = baseString.split("_");
  40. String prettyString = "";
  41. int size = 1;
  42. for (String s : substrings) {
  43. prettyString = prettyString.concat(Misc.getCapitalized(s));
  44. if (size < substrings.length) {
  45. prettyString = prettyString.concat(" ");
  46. }
  47. size++;
  48. }
  49. return prettyString;
  50. }
  51. /**
  52. * Gets the int represented by this string.
  53. *
  54. * @param string The string to parse
  55. * @return the int represented by this string
  56. */
  57. public static int getInt(String string) {
  58. if (isInt(string)) {
  59. return Integer.parseInt(string);
  60. }
  61. else {
  62. return 0;
  63. }
  64. }
  65. /**
  66. * Checks to see if an entity is currently invincible.
  67. *
  68. * @param le The LivingEntity to check
  69. * @param event The event the entity is involved in
  70. * @return true if the entity is invincible, false otherwise
  71. */
  72. public static boolean isInvincible(LivingEntity le, EntityDamageEvent event) {
  73. /*
  74. * So apparently if you do more damage to a LivingEntity than its last damage int you bypass the invincibility.
  75. * So yeah, this is for that.
  76. */
  77. if (le.getNoDamageTicks() > le.getMaximumNoDamageTicks() / 2.0F && event.getDamage() <= le.getLastDamage()) {
  78. return true;
  79. }
  80. else {
  81. return false;
  82. }
  83. }
  84. /**
  85. * Simulate a block break event.
  86. *
  87. * @param block The block to break
  88. * @param player The player breaking the block
  89. * @param shouldArmSwing true if an armswing event should be fired, false otherwise
  90. * @return true if the event wasn't cancelled, false otherwise
  91. */
  92. public static boolean blockBreakSimulate(Block block, Player player, Boolean shouldArmSwing) {
  93. //Support for NoCheat
  94. if (shouldArmSwing) {
  95. FakePlayerAnimationEvent armswing = new FakePlayerAnimationEvent(player);
  96. mcMMO.p.getServer().getPluginManager().callEvent(armswing);
  97. }
  98. PluginManager pluginManger = mcMMO.p.getServer().getPluginManager();
  99. FakeBlockDamageEvent damageEvent = new FakeBlockDamageEvent(player, block, player.getItemInHand(), true);
  100. pluginManger.callEvent(damageEvent);
  101. FakeBlockBreakEvent breakEvent = new FakeBlockBreakEvent(block, player);
  102. pluginManger.callEvent(breakEvent);
  103. if (!damageEvent.isCancelled() || !breakEvent.isCancelled()) {
  104. return true;
  105. }
  106. else {
  107. return false;
  108. }
  109. }
  110. /**
  111. * Get the upgrade tier of the item in hand.
  112. *
  113. * @param inHand The item to check the tier of
  114. * @return the tier of the item
  115. */
  116. public static int getTier(ItemStack inHand) {
  117. int tier = 0;
  118. if (ItemChecks.isWoodTool(inHand)) {
  119. tier = 1;
  120. }
  121. else if (ItemChecks.isStoneTool(inHand)) {
  122. tier = 2;
  123. }
  124. else if (ItemChecks.isIronTool(inHand)) {
  125. tier = 3;
  126. }
  127. else if (ItemChecks.isGoldTool(inHand)) {
  128. tier = 1;
  129. }
  130. else if (ItemChecks.isDiamondTool(inHand)) {
  131. tier = 4;
  132. }
  133. else if (ModChecks.isCustomTool(inHand)) {
  134. tier = ModChecks.getToolFromItemStack(inHand).getTier();
  135. }
  136. return tier;
  137. }
  138. /**
  139. * Determine if two locations are near each other.
  140. *
  141. * @param first The first location
  142. * @param second The second location
  143. * @param maxDistance The max distance apart
  144. * @return true if the distance between <code>first</code> and <code>second</code> is less than <code>maxDistance</code>, false otherwise
  145. */
  146. public static boolean isNear(Location first, Location second, double maxDistance) {
  147. if (!first.getWorld().equals(second.getWorld())) {
  148. return false;
  149. }
  150. if (first.distanceSquared(second) < (maxDistance * maxDistance)) {
  151. return true;
  152. }
  153. else {
  154. return false;
  155. }
  156. }
  157. /**
  158. * Determine if a string represents an Integer
  159. *
  160. * @param string String to check
  161. * @return true if the string is an Integer, false otherwise
  162. */
  163. public static boolean isInt(String string) {
  164. try {
  165. Integer.parseInt(string);
  166. return true;
  167. }
  168. catch (NumberFormatException nFE) {
  169. return false;
  170. }
  171. }
  172. /**
  173. * Drop items at a given location.
  174. *
  175. * @param location The location to drop the items at
  176. * @param is The items to drop
  177. * @param quantity The amount of items to drop
  178. */
  179. public static void dropItems(Location location, ItemStack is, int quantity) {
  180. for (int i = 0; i < quantity; i++) {
  181. dropItem(location, is);
  182. }
  183. }
  184. /**
  185. * Randomly drop an item at a given location.
  186. *
  187. * @param location The location to drop the items at
  188. * @param is The item to drop
  189. * @param chance The percentage chance for the item to drop
  190. */
  191. public static void randomDropItem(Location location, ItemStack is, double chance) {
  192. if (random.nextInt(100) < chance) {
  193. dropItem(location, is);
  194. }
  195. }
  196. /**
  197. * Randomly drop items at a given location.
  198. *
  199. * @param location The location to drop the items at
  200. * @param is The item to drop
  201. * @param chance The percentage chance for the item to drop
  202. * @param quantity The amount of items to drop
  203. */
  204. public static void randomDropItems(Location location, ItemStack is, int chance, int quantity) {
  205. for(int i = 0; i < quantity; i++) {
  206. randomDropItem(location, is, chance);
  207. }
  208. }
  209. /**
  210. * Drop an item at a given location.
  211. *
  212. * @param location The location to drop the item at
  213. * @param itemStack The item to drop
  214. */
  215. public static void dropItem(Location location, ItemStack itemStack) {
  216. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  217. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  218. mcMMO.p.getServer().getPluginManager().callEvent(event);
  219. if (event.isCancelled()) {
  220. return;
  221. }
  222. location.getWorld().dropItemNaturally(location, itemStack);
  223. }
  224. /**
  225. * Check if a skill level is higher than the max bonus level of the ability.
  226. *
  227. * @param skillLevel Skill level to check
  228. * @param maxLevel Max level of the ability
  229. * @return whichever value is lower
  230. */
  231. public static int skillCheck(int skillLevel, int maxLevel) {
  232. if (skillLevel > maxLevel) {
  233. return maxLevel;
  234. }
  235. else {
  236. return skillLevel;
  237. }
  238. }
  239. public static int getPowerLevelCap() {
  240. int levelCap = Config.getInstance().getPowerLevelCap();
  241. if (levelCap > 0) {
  242. return levelCap;
  243. }
  244. else {
  245. return Integer.MAX_VALUE;
  246. }
  247. }
  248. }