PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/zmanww/bukkit/util/LocationUtil.java

https://github.com/zwollner/zMod
Java | 286 lines | 159 code | 23 blank | 104 comment | 85 complexity | e007abe9abd1de3c1d6b52c548932109 MD5 | raw file
  1. package com.zmanww.bukkit.util;
  2. import java.util.List;
  3. import org.bukkit.Location;
  4. import org.bukkit.Material;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.block.BlockFace;
  7. import org.bukkit.entity.LivingEntity;
  8. import org.bukkit.entity.Entity;
  9. /**
  10. * @author Zeb
  11. *
  12. */
  13. public class LocationUtil {
  14. /**
  15. * Evaluates whether or not the passed material is within the given distance
  16. * of the location passed.
  17. *
  18. * @param loc
  19. * @param mat
  20. * @param blocks
  21. * @return
  22. */
  23. public static boolean isWithinBlocksOf(Location loc, Material mat, int blocks) {
  24. boolean retVal = false;
  25. Block baseBlock = loc.getBlock();
  26. for (int dist = 1; dist <= blocks; dist++) {
  27. for (BlockFace face : BlockFace.values()) {
  28. if (baseBlock.getFace(face, dist).getType() == mat) {
  29. return true;
  30. }
  31. }
  32. }
  33. return retVal;
  34. }
  35. /**
  36. * Returns the location of the nearest material passes, within the given block radius.
  37. *
  38. * @param loc
  39. * @param mat
  40. * @param blocks
  41. * @return
  42. */
  43. public static Location locateNearest(Location loc, Material mat, int blockRadius) {
  44. Location retVal = loc;
  45. Block baseBlock = loc.getBlock();
  46. // this search is very expensive, the amount of processing it takes
  47. // grows exponentially the further it has to search.
  48. try {
  49. for (int i = 1; i <= blockRadius; i++) {
  50. // checks the entire 3x3, 5x5, etc grid only at
  51. // "top and bottom of the imaginary box"
  52. for (int y = -i; y <= i; y++) {
  53. if (y == -i || y == i) {
  54. for (int x = -i; x <= i; x++) {
  55. for (int z = -i; z <= i; z++) {
  56. if (baseBlock.getRelative(x, y, z).getType().getId()==mat.getId()) {
  57. return baseBlock.getRelative(x, y, z).getLocation();
  58. }
  59. }
  60. }
  61. }
  62. // checks the other "sides" of the imaginary box
  63. if (y != -i && y != i) {
  64. for (int x = -i; x <= i; x++) {
  65. // checks the x sides of the box
  66. if (x == -i || x == i) {
  67. for (int z = -i; z <= i; z++) {
  68. if (baseBlock.getRelative(x, y, z).getType().getId()==mat.getId()) {
  69. return baseBlock.getRelative(x, y, z).getLocation();
  70. }
  71. }
  72. }
  73. // checks the z sides of the box
  74. if (x != -i || x != i) {
  75. int z = -i;
  76. if (baseBlock.getRelative(x, y, z).getType().getId()==mat.getId()) {
  77. return baseBlock.getRelative(x, y, z).getLocation();
  78. }
  79. z = i;
  80. if (baseBlock.getRelative(x, y, z).getType().getId()==mat.getId()) {
  81. return baseBlock.getRelative(x, y, z).getLocation();
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. } catch (Exception e) {
  89. retVal = loc;
  90. }
  91. // try {
  92. // for (int y = 0; y <= blockRadius; y++) {
  93. // for (int x = 0; x <= y; x++) {
  94. // for (int z = 0; z <= x; z++) {
  95. // if (baseBlock.getRelative(x, y, z).getType().equals(mat)) {
  96. // return baseBlock.getRelative(x, y, z).getLocation();
  97. // }
  98. // }
  99. // }
  100. // for (int x = -y; x >= -y; x--) {
  101. // for (int z = -x; z >= -x; z--) {
  102. // if (baseBlock.getRelative(x, -y, z).getType().equals(mat)) {
  103. // return baseBlock.getRelative(x, -y, z).getLocation();
  104. // }
  105. // }
  106. // }
  107. // }
  108. // } catch (Exception e) {
  109. // retVal = loc;
  110. // }
  111. // for (int i = 1; i <= blockRadius; i++) {
  112. // for (int z = i-1; z <= i; z++) {
  113. // for (int x = i-1; x <= i; x++) {
  114. // for (int y = i-1; y <= i; y++) {
  115. // if (baseBlock.getRelative(x, y, z).getType().equals(mat)) {
  116. // return baseBlock.getRelative(x, y, z).getLocation();
  117. // }
  118. // }
  119. // }
  120. // }
  121. // for (int z = i+1; z >= 0-i; z--) {
  122. // for (int x = i+1; x >= 0-i; x--) {
  123. // for (int y = i+1; y >= 0-i; y--) {
  124. // if (baseBlock.getRelative(x, y, z).getType().equals(mat)) {
  125. // return baseBlock.getRelative(x, y, z).getLocation();
  126. // }
  127. // }
  128. // }
  129. // }
  130. // }
  131. // for (int dist = 1; dist <= blockRadius; dist++) {
  132. // for (BlockFace face : BlockFace.values()) {
  133. // if (baseBlock.getFace(face, dist).getType() == mat) {
  134. // return baseBlock.getFace(face, dist).getLocation();
  135. // }
  136. // }
  137. // }
  138. return retVal;
  139. }
  140. /**
  141. * Returns the location of the closest entity to the given location, that is
  142. * not in a cave!
  143. *
  144. * @param loc
  145. * Location to get closest to;
  146. * @param mobName
  147. * Name of Entity must match (ignoring case)
  148. * @return
  149. */
  150. public static Location getClosestMob(Location loc, String mobName) {
  151. return getClosestMob(loc, mobName, false);
  152. }
  153. /**
  154. * Returns the location of the closest entity to the given location,
  155. * including or excluding caves.
  156. *
  157. * @param loc
  158. * Location to get closest to;
  159. * @param mobName
  160. * Name of Entity must match (ignoring case)
  161. * @param includeCaves
  162. * Specifies whether or not to includes mobs in caves.
  163. * @return
  164. */
  165. public static Location getClosestMob(Location loc, String mobName, boolean includeCaves) {
  166. Location retVal = loc;
  167. List<LivingEntity> mobs = loc.getWorld().getLivingEntities();
  168. double closest = 1000.00;
  169. for (LivingEntity mob : mobs) {
  170. if (isEntityOfType(mob, mobName)) {
  171. double tmp = distanceBetween(loc, mob.getLocation());
  172. if (tmp < closest && (includeCaves ? true : !isInCave(mob.getLocation()))) {
  173. closest = tmp;
  174. retVal = mob.getLocation();
  175. }
  176. }
  177. }
  178. return retVal;
  179. }
  180. public static boolean isInCave(Location loc) {
  181. boolean retVal = false;
  182. int yCord = (int) loc.getY();
  183. int i = 1;
  184. while (yCord < 100) {// no caves gonna be above this
  185. if (loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.DIRT
  186. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.GRASS
  187. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.IRON_BLOCK
  188. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.COBBLESTONE
  189. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.MOSSY_COBBLESTONE
  190. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.SAND
  191. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.GRAVEL
  192. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.STONE
  193. || loc.getBlock().getFace(BlockFace.UP, i).getType() == Material.SANDSTONE) {
  194. retVal = true;
  195. yCord = 100;// will stop the loop
  196. }
  197. i++;
  198. yCord++;
  199. }
  200. return retVal;
  201. }
  202. /**
  203. * Returns the distance between two Locations.
  204. *
  205. * @param loc1
  206. * @param loc2
  207. * @return
  208. */
  209. public static double distanceBetween(Location loc1, Location loc2) {
  210. return Math.sqrt(Math.pow((loc2.getX() - loc1.getBlockX()), 2)
  211. + Math.pow((loc2.getY() - loc1.getBlockY()), 2)
  212. + Math.pow((loc2.getZ() - loc1.getBlockZ()), 2));
  213. }
  214. private static boolean isEntityOfType(Entity e, String mobName) {
  215. boolean retVal = false;
  216. if (mobName.equalsIgnoreCase("Chicken")) {
  217. if (e instanceof org.bukkit.entity.Chicken) {
  218. retVal = true;
  219. }
  220. } else if (mobName.equalsIgnoreCase("Cow")) {
  221. if (e instanceof org.bukkit.entity.Cow) {
  222. retVal = true;
  223. }
  224. } else if (mobName.equalsIgnoreCase("Creeper")) {
  225. if (e instanceof org.bukkit.entity.Creeper) {
  226. retVal = true;
  227. }
  228. } else if (mobName.equalsIgnoreCase("Ghast")) {
  229. if (e instanceof org.bukkit.entity.Ghast) {
  230. retVal = true;
  231. }
  232. } else if (mobName.equalsIgnoreCase("Pig")) {
  233. if (e instanceof org.bukkit.entity.Pig) {
  234. retVal = true;
  235. }
  236. } else if (mobName.equalsIgnoreCase("Sheep")) {
  237. if (e instanceof org.bukkit.entity.Sheep) {
  238. retVal = true;
  239. }
  240. } else if (mobName.equalsIgnoreCase("Skeleton")) {
  241. if (e instanceof org.bukkit.entity.Skeleton) {
  242. retVal = true;
  243. }
  244. } else if (mobName.equalsIgnoreCase("Spider")) {
  245. if (e instanceof org.bukkit.entity.Spider) {
  246. retVal = true;
  247. }
  248. } else if (mobName.equalsIgnoreCase("Zombie")) {
  249. if (e instanceof org.bukkit.entity.Zombie) {
  250. retVal = true;
  251. }
  252. } else if (mobName.equalsIgnoreCase("Squid")) {
  253. if (e instanceof org.bukkit.entity.Squid) {
  254. retVal = true;
  255. }
  256. } else if (mobName.equalsIgnoreCase("Slime")) {
  257. if (e instanceof org.bukkit.entity.Slime) {
  258. retVal = true;
  259. }
  260. }
  261. return retVal;
  262. }
  263. }