m.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package com.gmail.nossr50;
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.Location;
  4. import org.bukkit.block.Block;
  5. import org.bukkit.entity.LivingEntity;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.event.entity.EntityDamageEvent;
  8. import org.bukkit.event.player.PlayerAnimationEvent;
  9. import org.bukkit.inventory.ItemStack;
  10. import com.gmail.nossr50.config.LoadProperties;
  11. import com.gmail.nossr50.datatypes.PlayerProfile;
  12. import com.gmail.nossr50.datatypes.SkillType;
  13. import com.gmail.nossr50.events.FakeBlockBreakEvent;
  14. import com.gmail.nossr50.events.McMMOItemSpawnEvent;
  15. import com.gmail.nossr50.runnables.SQLConversionTask;
  16. public class m {
  17. /**
  18. * Gets a capitalized version of the target string.
  19. *
  20. * @param target String to capitalize
  21. * @return the capitalized string
  22. */
  23. public static String getCapitalized(String target) {
  24. String firstLetter = target.substring(0,1);
  25. String remainder = target.substring(1);
  26. String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
  27. return capitalized;
  28. }
  29. /**
  30. * Gets the int represented by this string.
  31. *
  32. * @param string The string to parse
  33. * @return the int represented by this string
  34. */
  35. public static int getInt(String string) {
  36. if (isInt(string)) {
  37. return Integer.parseInt(string);
  38. }
  39. else {
  40. return 0;
  41. }
  42. }
  43. /**
  44. * Checks to see if an entity is currently invincible.
  45. *
  46. * @param le The LivingEntity to check
  47. * @param event The event the entity is involved in
  48. * @return true if the entity is invincible, false otherwise
  49. */
  50. public static boolean isInvincible(LivingEntity le, EntityDamageEvent event) {
  51. /*
  52. * So apparently if you do more damage to a LivingEntity than its last damage int you bypass the invincibility.
  53. * So yeah, this is for that.
  54. */
  55. if (le.getNoDamageTicks() > le.getMaximumNoDamageTicks() / 2.0F && event.getDamage() <= le.getLastDamage()) {
  56. return true;
  57. }
  58. else {
  59. return false;
  60. }
  61. }
  62. /**
  63. * Gets the power level of a player.
  64. *
  65. * @param player The player to get the power level of
  66. * @param PP The profile of the player
  67. * @return the power level of the player
  68. */
  69. public static int getPowerLevel(Player player, PlayerProfile PP) {
  70. int powerLevel = 0;
  71. for (SkillType type : SkillType.values()) {
  72. if (type.getPermissions(player)) {
  73. powerLevel += PP.getSkillLevel(type);
  74. }
  75. }
  76. return powerLevel;
  77. }
  78. /**
  79. * Simulate a block break event.
  80. *
  81. * @param block The block to break
  82. * @param player The player breaking the block
  83. * @param shouldArmSwing true if an armswing event should be fired, false otherwise
  84. * @return true if the event wasn't cancelled, false otherwise
  85. */
  86. public static boolean blockBreakSimulate(Block block, Player player, Boolean shouldArmSwing) {
  87. //Support for NoCheat
  88. if (shouldArmSwing) {
  89. PlayerAnimationEvent armswing = new PlayerAnimationEvent(player);
  90. Bukkit.getPluginManager().callEvent(armswing);
  91. }
  92. FakeBlockBreakEvent event = new FakeBlockBreakEvent(block, player);
  93. Bukkit.getPluginManager().callEvent(event);
  94. if (!event.isCancelled()) {
  95. return true;
  96. }
  97. else {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Get the upgrade tier of the item in hand.
  103. *
  104. * @param inHand The item to check the tier of
  105. * @return the tier of the item
  106. */
  107. public static Integer getTier(ItemStack inHand) {
  108. int tier = 0;
  109. if (ItemChecks.isWoodTool(inHand)) {
  110. tier = 1;
  111. }
  112. else if (ItemChecks.isStoneTool(inHand)) {
  113. tier = 2;
  114. }
  115. else if (ItemChecks.isIronTool(inHand)) {
  116. tier = 3;
  117. }
  118. else if(ItemChecks.isGoldTool(inHand)) {
  119. tier = 1;
  120. }
  121. else if(ItemChecks.isDiamondTool(inHand))
  122. tier = 4;
  123. return tier;
  124. }
  125. /**
  126. * Determine if two locations are near each other.
  127. *
  128. * @param first The first location
  129. * @param second The second location
  130. * @param maxDistance The max distance apart
  131. * @return true if the distance between <code>first</code> and <code>second</code> is less than <code>maxDistance</code>, false otherwise
  132. */
  133. public static boolean isNear(Location first, Location second, int maxDistance) {
  134. double relX = first.getX() - second.getX();
  135. double relY = first.getY() - second.getY();
  136. double relZ = first.getZ() - second.getZ();
  137. double dist = (relX * relX) + (relY * relY) + (relZ * relZ);
  138. if (dist < maxDistance * maxDistance) {
  139. return true;
  140. }
  141. else {
  142. return false;
  143. }
  144. }
  145. /**
  146. * Determine if a string represents an Integer
  147. *
  148. * @param string String to check
  149. * @return true if the string is an Integer, false otherwise
  150. */
  151. public static boolean isInt(String string) {
  152. try {
  153. Integer.parseInt(string);
  154. return true;
  155. }
  156. catch (NumberFormatException nFE) {
  157. return false;
  158. }
  159. }
  160. /**
  161. * Drop items at a given location.
  162. *
  163. * @param location The location to drop the items at
  164. * @param is The items to drop
  165. * @param quantity The amount of items to drop
  166. */
  167. public static void mcDropItems(Location location, ItemStack is, int quantity) {
  168. for (int i = 0; i < quantity; i++) {
  169. mcDropItem(location, is);
  170. }
  171. }
  172. /**
  173. * Randomly drop an item at a given location.
  174. *
  175. * @param location The location to drop the items at
  176. * @param is The item to drop
  177. * @param chance The percentage chance for the item to drop
  178. */
  179. public static void mcRandomDropItem(Location location, ItemStack is, double chance) {
  180. if (Math.random() * 100 < chance) {
  181. mcDropItem(location, is);
  182. }
  183. }
  184. /**
  185. * Randomly drop items 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. * @param quantity The amount of items to drop
  191. */
  192. public static void mcRandomDropItems(Location location, ItemStack is, int chance, int quantity) {
  193. for(int i = 0; i < quantity; i++) {
  194. mcRandomDropItem(location, is, chance);
  195. }
  196. }
  197. /**
  198. * Drop an item at a given location.
  199. *
  200. * @param location The location to drop the item at
  201. * @param itemStack The item to drop
  202. */
  203. public static void mcDropItem(Location location, ItemStack itemStack) {
  204. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  205. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  206. Bukkit.getPluginManager().callEvent(event);
  207. if (event.isCancelled()) {
  208. return;
  209. }
  210. else {
  211. location.getWorld().dropItemNaturally(location, itemStack);
  212. }
  213. }
  214. /**
  215. * Convert FlatFile data to MySQL data.
  216. */
  217. public static void convertToMySQL() {
  218. if (!LoadProperties.useMySQL) {
  219. return;
  220. }
  221. Bukkit.getScheduler().scheduleAsyncDelayedTask(Bukkit.getPluginManager().getPlugin("mcMMO"), new SQLConversionTask(), 1);
  222. }
  223. public static int skillCheck(int skillLevel, int maxLevel) {
  224. if (skillLevel > maxLevel) {
  225. return maxLevel;
  226. }
  227. else {
  228. return skillLevel;
  229. }
  230. }
  231. }