Util.java 12 KB

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