Misc.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.AnimalTamer;
  7. import org.bukkit.entity.Entity;
  8. import org.bukkit.entity.Item;
  9. import org.bukkit.entity.LivingEntity;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.entity.Tameable;
  12. import org.bukkit.event.entity.EntityDamageEvent;
  13. import org.bukkit.inventory.ItemStack;
  14. import org.bukkit.plugin.PluginManager;
  15. import com.gmail.nossr50.mcMMO;
  16. import com.gmail.nossr50.config.AdvancedConfig;
  17. import com.gmail.nossr50.config.Config;
  18. import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
  19. import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
  20. import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
  21. import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
  22. import com.gmail.nossr50.mods.ModChecks;
  23. import com.gmail.nossr50.party.PartyManager;
  24. public final class Misc {
  25. private static Random random = new Random();
  26. public static int toolDurabilityLoss = Config.getInstance().getAbilityToolDamage();
  27. public static int abilityLengthIncreaseLevel = AdvancedConfig.getInstance().getAbilityLength();
  28. public static boolean isSpawnerXPEnabled = Config.getInstance().getExperienceGainsMobspawnersEnabled();
  29. public static final int PLAYER_RESPAWN_COOLDOWN_SECONDS = 5;
  30. public static final int TIME_CONVERSION_FACTOR = 1000;
  31. public static final double SKILL_MESSAGE_MAX_SENDING_DISTANCE = 10.0;
  32. //Sound Pitches & Volumes from CB
  33. public static final float ANVIL_USE_PITCH = 0.3F; // Not in CB directly, I went off the place sound values
  34. public static final float ANVIL_USE_VOLUME = 1.0F; // Not in CB directly, I went off the place sound values
  35. public static final float FIZZ_PITCH = 2.6F + (Misc.getRandom().nextFloat() - Misc.getRandom().nextFloat()) * 0.8F;
  36. public static final float FIZZ_VOLUME = 0.5F;
  37. public static final float POP_PITCH = ((getRandom().nextFloat() - getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F;
  38. public static final float POP_VOLUME = 0.2F;
  39. private Misc() {};
  40. public static boolean isFriendlyPet(Player attacker, Tameable pet) {
  41. if (pet.isTamed()) {
  42. AnimalTamer tamer = pet.getOwner();
  43. if (tamer instanceof Player) {
  44. Player owner = (Player) tamer;
  45. if (owner == attacker || PartyManager.inSameParty(attacker, owner)) {
  46. return true;
  47. }
  48. }
  49. }
  50. return false;
  51. }
  52. public static boolean isNPCEntity(Entity entity) {
  53. if (entity == null || entity.hasMetadata("NPC")) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. public static boolean isNPCPlayer(Player player) {
  59. if (player == null || player.hasMetadata("NPC")) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * Checks to see if an entity is currently invincible.
  66. *
  67. * @param le The LivingEntity to check
  68. * @param event The event the entity is involved in
  69. * @return true if the entity is invincible, false otherwise
  70. */
  71. public static boolean isInvincible(LivingEntity le, EntityDamageEvent event) {
  72. /*
  73. * So apparently if you do more damage to a LivingEntity than its last damage int you bypass the invincibility.
  74. * So yeah, this is for that.
  75. */
  76. if (le.getNoDamageTicks() > le.getMaximumNoDamageTicks() / 2.0F && event.getDamage() <= le.getLastDamage()) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. /**
  82. * Simulate a block break event.
  83. *
  84. * @param block The block to break
  85. * @param player The player breaking the block
  86. * @param shouldArmSwing true if an armswing event should be fired, false otherwise
  87. * @return true if the event wasn't cancelled, false otherwise
  88. */
  89. public static boolean blockBreakSimulate(Block block, Player player, Boolean shouldArmSwing) {
  90. //Support for NoCheat
  91. if (shouldArmSwing) {
  92. FakePlayerAnimationEvent armswing = new FakePlayerAnimationEvent(player);
  93. mcMMO.p.getServer().getPluginManager().callEvent(armswing);
  94. }
  95. PluginManager pluginManger = mcMMO.p.getServer().getPluginManager();
  96. FakeBlockDamageEvent damageEvent = new FakeBlockDamageEvent(player, block, player.getItemInHand(), true);
  97. pluginManger.callEvent(damageEvent);
  98. FakeBlockBreakEvent breakEvent = new FakeBlockBreakEvent(block, player);
  99. pluginManger.callEvent(breakEvent);
  100. if (!damageEvent.isCancelled() && !breakEvent.isCancelled()) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * Get the upgrade tier of the item in hand.
  107. *
  108. * @param inHand The item to check the tier of
  109. * @return the tier of the item
  110. */
  111. public static int getTier(ItemStack inHand) {
  112. int tier = 0;
  113. if (ItemChecks.isWoodTool(inHand)) {
  114. tier = 1;
  115. }
  116. else if (ItemChecks.isStoneTool(inHand)) {
  117. tier = 2;
  118. }
  119. else if (ItemChecks.isIronTool(inHand)) {
  120. tier = 3;
  121. }
  122. else if (ItemChecks.isGoldTool(inHand)) {
  123. tier = 1;
  124. }
  125. else if (ItemChecks.isDiamondTool(inHand)) {
  126. tier = 4;
  127. }
  128. else if (ModChecks.isCustomTool(inHand)) {
  129. tier = ModChecks.getToolFromItemStack(inHand).getTier();
  130. }
  131. return tier;
  132. }
  133. /**
  134. * Determine if two locations are near each other.
  135. *
  136. * @param first The first location
  137. * @param second The second location
  138. * @param maxDistance The max distance apart
  139. * @return true if the distance between <code>first</code> and <code>second</code> is less than <code>maxDistance</code>, false otherwise
  140. */
  141. public static boolean isNear(Location first, Location second, double maxDistance) {
  142. if (!first.getWorld().equals(second.getWorld())) {
  143. return false;
  144. }
  145. if (first.distanceSquared(second) < (maxDistance * maxDistance)) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Drop items at a given location.
  152. *
  153. * @param location The location to drop the items at
  154. * @param is The items to drop
  155. * @param quantity The amount of items to drop
  156. */
  157. public static void dropItems(Location location, ItemStack is, int quantity) {
  158. for (int i = 0; i < quantity; i++) {
  159. dropItem(location, is);
  160. }
  161. }
  162. /**
  163. * Randomly drop an item at a given location.
  164. *
  165. * @param location The location to drop the items at
  166. * @param is The item to drop
  167. * @param chance The percentage chance for the item to drop
  168. */
  169. public static void randomDropItem(Location location, ItemStack is, int chance) {
  170. if (random.nextInt(100) < chance) {
  171. dropItem(location, is);
  172. }
  173. }
  174. /**
  175. * Randomly drop items at a given location.
  176. *
  177. * @param location The location to drop the items at
  178. * @param is The item to drop
  179. * @param chance The percentage chance for the item to drop
  180. * @param quantity The amount of items to drop
  181. */
  182. public static void randomDropItems(Location location, ItemStack is, int chance, int quantity) {
  183. int dropCount = random.nextInt(quantity);
  184. //I could just have the itemstacks quantity value changed but I think this will make it look more natural
  185. for (int i = 0; i < dropCount; i++) {
  186. dropItem(location, is);
  187. }
  188. }
  189. /**
  190. * Drop an item at a given location.
  191. *
  192. * @param location The location to drop the item at
  193. * @param itemStack The item to drop
  194. */
  195. public static void dropItem(Location location, ItemStack itemStack) {
  196. if (itemStack.getType() == Material.AIR) {
  197. return;
  198. }
  199. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  200. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  201. mcMMO.p.getServer().getPluginManager().callEvent(event);
  202. if (event.isCancelled())
  203. return;
  204. Item newItem = location.getWorld().dropItemNaturally(location, itemStack);
  205. ItemStack cloned = itemStack.clone();
  206. cloned.setAmount(newItem.getItemStack().getAmount());
  207. newItem.setItemStack(cloned);
  208. }
  209. /**
  210. * Get the max power level for a player.
  211. *
  212. * @return the maximum power level for a player
  213. */
  214. public static int getPowerLevelCap() {
  215. int levelCap = Config.getInstance().getPowerLevelCap();
  216. if (levelCap > 0) {
  217. return levelCap;
  218. }
  219. return Integer.MAX_VALUE;
  220. }
  221. public static Random getRandom() {
  222. return random;
  223. }
  224. }