Util.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * $Header: Util.java, 21/10/2005 23:17:40 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 21/10/2005 23:17:40 $
  6. * $Revision: 1 $
  7. * $Log: Util.java,v $
  8. * Revision 1 21/10/2005 23:17:40 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package com.l2jserver.gameserver.util;
  26. import java.io.File;
  27. import java.util.Collection;
  28. import com.l2jserver.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. /**
  33. * General Utility functions related to Gameserver
  34. */
  35. public final class Util
  36. {
  37. public static void handleIllegalPlayerAction(L2PcInstance actor, String message, int punishment)
  38. {
  39. ThreadPoolManager.getInstance().scheduleGeneral(new IllegalPlayerAction(actor, message, punishment), 5000);
  40. }
  41. public static String getRelativePath(File base, File file)
  42. {
  43. return file.toURI().getPath().substring(base.toURI().getPath().length());
  44. }
  45. /**
  46. * Return degree value of object 2 to the horizontal line with object 1
  47. * being the origin
  48. */
  49. public static double calculateAngleFrom(L2Object obj1, L2Object obj2)
  50. {
  51. return calculateAngleFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  52. }
  53. /**
  54. * Return degree value of object 2 to the horizontal line with object 1
  55. * being the origin
  56. */
  57. public final static double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  58. {
  59. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  60. if (angleTarget < 0)
  61. angleTarget = 360 + angleTarget;
  62. return angleTarget;
  63. }
  64. public final static double convertHeadingToDegree(int clientHeading)
  65. {
  66. double degree = clientHeading / 182.044444444;
  67. return degree;
  68. }
  69. public final static int convertDegreeToClientHeading(double degree)
  70. {
  71. if (degree < 0)
  72. degree = 360 + degree;
  73. return (int) (degree * 182.044444444);
  74. }
  75. public final static int calculateHeadingFrom(L2Object obj1, L2Object obj2)
  76. {
  77. return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  78. }
  79. public final static int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  80. {
  81. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  82. if (angleTarget < 0)
  83. angleTarget = 360 + angleTarget;
  84. return (int) (angleTarget * 182.044444444);
  85. }
  86. public final static int calculateHeadingFrom(double dx, double dy)
  87. {
  88. double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
  89. if (angleTarget < 0)
  90. angleTarget = 360 + angleTarget;
  91. return (int) (angleTarget * 182.044444444);
  92. }
  93. /**
  94. * @return the distance between the two coordinates in 2D plane
  95. */
  96. public static double calculateDistance(int x1, int y1, int x2, int y2)
  97. {
  98. return calculateDistance(x1, y1, 0, x2, y2, 0, false);
  99. }
  100. /**
  101. * @param includeZAxis - if true, includes also the Z axis in the calculation
  102. * @return the distance between the two coordinates
  103. */
  104. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis)
  105. {
  106. double dx = (double) x1 - x2;
  107. double dy = (double) y1 - y2;
  108. if (includeZAxis)
  109. {
  110. double dz = z1 - z2;
  111. return Math.sqrt(dx * dx + dy * dy + dz * dz);
  112. }
  113. else
  114. return Math.sqrt(dx * dx + dy * dy);
  115. }
  116. /**
  117. * @param includeZAxis - if true, includes also the Z axis in the calculation
  118. * @return the distance between the two objects
  119. */
  120. public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
  121. {
  122. if (obj1 == null || obj2 == null)
  123. return 1000000;
  124. return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
  125. }
  126. /**
  127. * (Based on ucfirst() function of PHP)
  128. *
  129. * @param str - the string whose first letter to capitalize
  130. * @return a string with the first letter of the {@code str} capitalized
  131. */
  132. public static String capitalizeFirst(String str)
  133. {
  134. str = str.trim();
  135. if (str.length() > 0 && Character.isLetter(str.charAt(0)))
  136. return str.substring(0, 1).toUpperCase() + str.substring(1);
  137. return str;
  138. }
  139. /**
  140. * (Based on ucwords() function of PHP)
  141. *
  142. * @param str - the string to capitalize
  143. * @return a string with the first letter of every word in {@code str} capitalized
  144. */
  145. public static String capitalizeWords(String str)
  146. {
  147. char[] charArray = str.toCharArray();
  148. StringBuilder result = new StringBuilder();
  149. // Capitalize the first letter in the given string!
  150. charArray[0] = Character.toUpperCase(charArray[0]);
  151. for (int i = 0; i < charArray.length; i++)
  152. {
  153. if (Character.isWhitespace(charArray[i]))
  154. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  155. result.append(charArray[i]);
  156. }
  157. return result.toString();
  158. }
  159. /**
  160. * @return {@code true} if the two objects are within specified range between each other, {@code false} otherwise
  161. */
  162. public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
  163. {
  164. if (obj1 == null || obj2 == null)
  165. return false;
  166. if (obj1.getInstanceId() != obj2.getInstanceId())
  167. return false;
  168. if (range == -1)
  169. return true; // not limited
  170. int rad = 0;
  171. if (obj1 instanceof L2Character)
  172. rad += ((L2Character) obj1).getTemplate().collisionRadius;
  173. if (obj2 instanceof L2Character)
  174. rad += ((L2Character) obj2).getTemplate().collisionRadius;
  175. double dx = obj1.getX() - obj2.getX();
  176. double dy = obj1.getY() - obj2.getY();
  177. if (includeZAxis)
  178. {
  179. double dz = obj1.getZ() - obj2.getZ();
  180. double d = dx * dx + dy * dy + dz * dz;
  181. return d <= range * range + 2 * range * rad + rad * rad;
  182. }
  183. else
  184. {
  185. double d = dx * dx + dy * dy;
  186. return d <= range * range + 2 * range * rad + rad * rad;
  187. }
  188. }
  189. /**
  190. * Checks if object is within short (sqrt(int.max_value)) radius, not using collisionRadius.
  191. * Faster calculation than checkIfInRange if distance is short and collisionRadius isn't needed.
  192. * Not for long distance checks (potential teleports, far away castles etc).
  193. * @param range - the maximum range between the two objects
  194. * @param includeZAxis - if true, check also Z axis (3-dimensional check), otherwise only 2D
  195. * @return {@code true} if objects are within specified range between each other, {@code false} otherwise
  196. */
  197. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  198. {
  199. if (obj1 == null || obj2 == null)
  200. return false;
  201. if (radius == -1)
  202. return true; // not limited
  203. int dx = obj1.getX() - obj2.getX();
  204. int dy = obj1.getY() - obj2.getY();
  205. if (includeZAxis)
  206. {
  207. int dz = obj1.getZ() - obj2.getZ();
  208. return dx * dx + dy * dy + dz * dz <= radius * radius;
  209. }
  210. else
  211. return dx * dx + dy * dy <= radius * radius;
  212. }
  213. /**
  214. * @param str - the String to count
  215. * @return the number of "words" in a given string.
  216. */
  217. public static int countWords(String str)
  218. {
  219. return str.trim().split("\\s+").length;
  220. }
  221. /**
  222. * (Based on implode() in PHP)
  223. * @param strArray - an array of strings to concatenate
  224. * @param strDelim - the delimiter to put between the strings
  225. * @return a delimited string for a given array of string elements.
  226. */
  227. public static String implodeString(String[] strArray, String strDelim)
  228. {
  229. StringBuilder result = new StringBuilder();
  230. for (String strValue : strArray)
  231. {
  232. result.append(strValue);
  233. result.append(strDelim);
  234. }
  235. return result.toString();
  236. }
  237. /**
  238. * @param strCollection - a collection of strings to concatenate
  239. * @param strDelim - the delimiter to put between the strings
  240. * @see #implodeString(String[] strArray, String strDelim)
  241. */
  242. public static String implodeString(Collection<String> strCollection, String strDelim)
  243. {
  244. return implodeString(strCollection.toArray(new String[strCollection.size()]), strDelim);
  245. }
  246. /**
  247. * (Based on round() in PHP)
  248. * @param number - the number to round
  249. * @param numPlaces - how many digits after decimal point to leave intact
  250. * @return the value of {@code number} rounded to specified number of digits after the decimal point.
  251. */
  252. public static float roundTo(float number, int numPlaces)
  253. {
  254. if (numPlaces <= 1)
  255. return Math.round(number);
  256. float exponent = (float) Math.pow(10, numPlaces);
  257. return Math.round(number * exponent) / exponent;
  258. }
  259. /**
  260. * @param text - the text to check
  261. * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
  262. */
  263. public static boolean isDigit(String text)
  264. {
  265. if (text == null)
  266. return false;
  267. return text.matches("[0-9]+");
  268. }
  269. /**
  270. * @param text - the text to check
  271. * @return {@code true} if {@code text} contains only letters and/or numbers, {@code false} otherwise
  272. */
  273. public static boolean isAlphaNumeric(String text)
  274. {
  275. if (text == null || text.isEmpty())
  276. return false;
  277. for (char c : text.toCharArray())
  278. if (!Character.isLetterOrDigit(c))
  279. return false;
  280. return true;
  281. }
  282. /**
  283. * Format the specified digit using the digit grouping symbol "," (comma).
  284. * For example, 123456789 becomes 123,456,789.
  285. * @param amount - the amount of adena
  286. * @return the formatted adena amount
  287. */
  288. public static String formatAdena(long amount)
  289. {
  290. String s = "";
  291. long rem = amount % 1000;
  292. s = Long.toString(rem);
  293. amount = (amount - rem) / 1000;
  294. while (amount > 0)
  295. {
  296. if (rem < 99)
  297. s = '0' + s;
  298. if (rem < 9)
  299. s = '0' + s;
  300. rem = amount % 1000;
  301. s = Long.toString(rem) + "," + s;
  302. amount = (amount - rem) / 1000;
  303. }
  304. return s;
  305. }
  306. /**
  307. * @param array - the array to look into
  308. * @param obj - the object to search for
  309. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  310. */
  311. public static <T> boolean contains(T[] array, T obj)
  312. {
  313. for (T element : array)
  314. if (element == obj)
  315. return true;
  316. return false;
  317. }
  318. /**
  319. * @param array - the array to look into
  320. * @param obj - the integer to search for
  321. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  322. */
  323. public static boolean contains(int[] array, int obj)
  324. {
  325. for (int element : array)
  326. if (element == obj)
  327. return true;
  328. return false;
  329. }
  330. }