2
0

Util.java 12 KB

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