Misc.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package com.gmail.nossr50.util;
  2. import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
  3. import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
  4. import com.google.common.collect.ImmutableSet;
  5. import org.bukkit.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.block.BlockState;
  8. import org.bukkit.entity.*;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.util.Vector;
  11. import java.util.Collection;
  12. import java.util.Random;
  13. import java.util.Set;
  14. public final class Misc {
  15. public static final int TIME_CONVERSION_FACTOR = 1000;
  16. public static final int TICK_CONVERSION_FACTOR = 20;
  17. public static final int PLAYER_RESPAWN_COOLDOWN_SECONDS = 5;
  18. public static final double SKILL_MESSAGE_MAX_SENDING_DISTANCE = 10.0;
  19. public static final Set<String> modNames = ImmutableSet.of("LOTR", "BUILDCRAFT", "ENDERIO", "ENHANCEDBIOMES", "IC2", "METALLURGY", "FORESTRY", "GALACTICRAFT", "RAILCRAFT", "TWILIGHTFOREST", "THAUMCRAFT", "GRAVESTONEMOD", "GROWTHCRAFT", "ARCTICMOBS", "DEMONMOBS", "INFERNOMOBS", "SWAMPMOBS", "MARICULTURE", "MINESTRAPPOLATION");
  20. // Sound Pitches & Volumes from CB
  21. /* public static final float ANVIL_USE_PITCH = 0.3F; // Not in CB directly, I went off the place sound values
  22. public static final float ANVIL_USE_VOLUME = 1.0F * MainConfig.getInstance().getMasterVolume(); // Not in CB directly, I went off the place sound values
  23. public static final float FIZZ_VOLUME = 0.5F * MainConfig.getInstance().getMasterVolume();
  24. public static final float POP_VOLUME = 0.2F * MainConfig.getInstance().getMasterVolume();
  25. public static final float BAT_VOLUME = 1.0F * MainConfig.getInstance().getMasterVolume();
  26. public static final float BAT_PITCH = 0.6F;
  27. public static final float GHAST_VOLUME = 1.0F * MainConfig.getInstance().getMasterVolume();
  28. public static final float LEVELUP_PITCH = 0.5F; // Reduced to differentiate between vanilla level-up
  29. public static final float LEVELUP_VOLUME = 0.75F * MainConfig.getInstance().getMasterVolume(); // Use max volume always*/
  30. private static Random random = new Random();
  31. private Misc() {
  32. }
  33. public static boolean isNPCEntityExcludingVillagers(Entity entity) {
  34. return (entity == null
  35. || (entity.hasMetadata("NPC") && !(entity instanceof Villager))
  36. || (entity instanceof NPC && !(entity instanceof Villager))
  37. || entity.getClass().getName().equalsIgnoreCase("cofh.entity.PlayerFake"));
  38. }
  39. public static boolean isNPCIncludingVillagers(Player entity) {
  40. return (entity == null
  41. || (entity.hasMetadata("NPC"))
  42. || (entity instanceof NPC)
  43. || entity.getClass().getName().equalsIgnoreCase("cofh.entity.PlayerFake"));
  44. }
  45. /**
  46. * Determine if two locations are near each other.
  47. *
  48. * @param first The first location
  49. * @param second The second location
  50. * @param maxDistance The max distance apart
  51. * @return true if the distance between {@code first} and {@code second} is less than {@code maxDistance}, false otherwise
  52. */
  53. public static boolean isNear(Location first, Location second, double maxDistance) {
  54. return (first.getWorld() == second.getWorld()) && (first.distanceSquared(second) < (maxDistance * maxDistance) || maxDistance == 0);
  55. }
  56. /**
  57. * Get the center of the given block.
  58. *
  59. * @param blockState The {@link BlockState} of the block
  60. * @return A {@link Location} lying at the center of the block
  61. */
  62. public static Location getBlockCenter(BlockState blockState) {
  63. return blockState.getLocation().add(0.5, 0.5, 0.5);
  64. }
  65. public static void dropItems(Location location, Collection<ItemStack> drops) {
  66. for (ItemStack drop : drops) {
  67. dropItem(location, drop);
  68. }
  69. }
  70. /**
  71. * Drop items at a given location.
  72. *
  73. * @param location The location to drop the items at
  74. * @param is The items to drop
  75. * @param quantity The amount of items to drop
  76. */
  77. public static void dropItems(Location location, ItemStack is, int quantity) {
  78. for (int i = 0; i < quantity; i++) {
  79. dropItem(location, is);
  80. }
  81. }
  82. /**
  83. * Drop an item at a given location.
  84. *
  85. * @param location The location to drop the item at
  86. * @param itemStack The item to drop
  87. * @return Dropped Item entity or null if invalid or cancelled
  88. */
  89. public static Item dropItem(Location location, ItemStack itemStack) {
  90. if (itemStack.getType() == Material.AIR) {
  91. return null;
  92. }
  93. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  94. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  95. pluginRef.getServer().getPluginManager().callEvent(event);
  96. if (event.isCancelled()) {
  97. return null;
  98. }
  99. return location.getWorld().dropItemNaturally(location, itemStack);
  100. }
  101. /**
  102. * Drop items at a given location.
  103. *
  104. * @param fromLocation The location to drop the items at
  105. * @param is The items to drop
  106. * @param speed the speed that the item should travel
  107. * @param quantity The amount of items to drop
  108. */
  109. public static void spawnItemsTowardsLocation(Location fromLocation, Location toLocation, ItemStack is, int quantity, double speed) {
  110. for (int i = 0; i < quantity; i++) {
  111. spawnItemTowardsLocation(fromLocation, toLocation, is, speed);
  112. }
  113. }
  114. /**
  115. * Drop an item at a given location.
  116. * This method is fairly expensive as it creates clones of everything passed to itself since they are mutable objects
  117. *
  118. * @param fromLocation The location to drop the item at
  119. * @param toLocation The location the item will travel towards
  120. * @param itemToSpawn The item to spawn
  121. * @param speed the speed that the item should travel
  122. * @return Dropped Item entity or null if invalid or cancelled
  123. */
  124. public static Item spawnItemTowardsLocation(Location fromLocation, Location toLocation, ItemStack itemToSpawn, double speed) {
  125. if (itemToSpawn.getType() == Material.AIR) {
  126. return null;
  127. }
  128. //Work with fresh copies of everything
  129. ItemStack clonedItem = itemToSpawn.clone();
  130. Location spawnLocation = fromLocation.clone();
  131. Location targetLocation = toLocation.clone();
  132. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  133. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(spawnLocation, clonedItem);
  134. pluginRef.getServer().getPluginManager().callEvent(event);
  135. //Something cancelled the event so back out
  136. if (event.isCancelled() || event.getItemStack() == null) {
  137. return null;
  138. }
  139. //Use the item from the event
  140. Item spawnedItem = spawnLocation.getWorld().dropItem(spawnLocation, clonedItem);
  141. Vector vecFrom = spawnLocation.clone().toVector().clone();
  142. Vector vecTo = targetLocation.clone().toVector().clone();
  143. //Vector which is pointing towards out target location
  144. Vector direction = vecTo.subtract(vecFrom).normalize();
  145. //Modify the speed of the vector
  146. direction = direction.multiply(speed);
  147. spawnedItem.setVelocity(direction);
  148. return spawnedItem;
  149. }
  150. public static void profileCleanup(String playerName) {
  151. Player player = pluginRef.getServer().getPlayerExact(playerName);
  152. if (player != null) {
  153. pluginRef.getUserManager().remove(player);
  154. new PlayerProfileLoadingTask(player).runTaskLaterAsynchronously(pluginRef, 1); // 1 Tick delay to ensure the player is marked as online before we begin loading
  155. }
  156. }
  157. public static String getModName(String materialName) {
  158. for (String mod : modNames) {
  159. if (materialName.contains(mod)) {
  160. return mod;
  161. }
  162. }
  163. String[] materialSplit = materialName.split("_");
  164. if (materialSplit.length > 1) {
  165. return materialSplit[0].toLowerCase();
  166. }
  167. return "UnknownMods";
  168. }
  169. /**
  170. * Gets a random location near the specified location
  171. */
  172. public static Location getLocationOffset(Location location, double strength) {
  173. double blockX = location.getBlockX();
  174. double blockZ = location.getBlockZ();
  175. double distance;
  176. distance = strength * random.nextDouble();
  177. blockX = (random.nextBoolean()) ? blockX + (distance) : blockX - (distance);
  178. distance = strength * random.nextDouble();
  179. blockZ = (random.nextBoolean()) ? blockZ + (distance) : blockZ - (distance);
  180. return new Location(location.getWorld(), blockX, location.getY(), blockZ);
  181. }
  182. public static Random getRandom() {
  183. return random;
  184. }
  185. }