Util.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 java.util.List;
  21. import javolution.text.TextBuilder;
  22. import javolution.util.FastList;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.ThreadPoolManager;
  25. import com.l2jserver.gameserver.model.L2Object;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  29. import com.l2jserver.gameserver.network.serverpackets.ShowBoard;
  30. import com.l2jserver.util.file.filter.ExtFilter;
  31. /**
  32. * General Utility functions related to game server.
  33. */
  34. public final class Util
  35. {
  36. public static void handleIllegalPlayerAction(L2PcInstance actor, String message, int punishment)
  37. {
  38. ThreadPoolManager.getInstance().scheduleGeneral(new IllegalPlayerAction(actor, message, punishment), 5000);
  39. }
  40. public static String getRelativePath(File base, File file)
  41. {
  42. return file.toURI().getPath().substring(base.toURI().getPath().length());
  43. }
  44. /**
  45. * @param obj1
  46. * @param obj2
  47. * @return degree value of object 2 to the horizontal line with object 1 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. * @param obj1X
  55. * @param obj1Y
  56. * @param obj2X
  57. * @param obj2Y
  58. * @return degree value of object 2 to the horizontal line with object 1 being the origin
  59. */
  60. public static final double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  61. {
  62. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  63. if (angleTarget < 0)
  64. {
  65. angleTarget = 360 + angleTarget;
  66. }
  67. return angleTarget;
  68. }
  69. public static final double convertHeadingToDegree(int clientHeading)
  70. {
  71. double degree = clientHeading / 182.044444444;
  72. return degree;
  73. }
  74. public static final int convertDegreeToClientHeading(double degree)
  75. {
  76. if (degree < 0)
  77. {
  78. degree = 360 + degree;
  79. }
  80. return (int) (degree * 182.044444444);
  81. }
  82. public static final int calculateHeadingFrom(L2Object obj1, L2Object obj2)
  83. {
  84. return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  85. }
  86. public static final int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  87. {
  88. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  89. if (angleTarget < 0)
  90. {
  91. angleTarget = 360 + angleTarget;
  92. }
  93. return (int) (angleTarget * 182.044444444);
  94. }
  95. public static final int calculateHeadingFrom(double dx, double dy)
  96. {
  97. double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
  98. if (angleTarget < 0)
  99. {
  100. angleTarget = 360 + angleTarget;
  101. }
  102. return (int) (angleTarget * 182.044444444);
  103. }
  104. /**
  105. * @param x1
  106. * @param y1
  107. * @param x2
  108. * @param y2
  109. * @return the distance between the two coordinates in 2D plane
  110. */
  111. public static double calculateDistance(int x1, int y1, int x2, int y2)
  112. {
  113. return calculateDistance(x1, y1, 0, x2, y2, 0, false);
  114. }
  115. /**
  116. * @param x1
  117. * @param y1
  118. * @param z1
  119. * @param x2
  120. * @param y2
  121. * @param z2
  122. * @param includeZAxis - if true, includes also the Z axis in the calculation
  123. * @return the distance between the two coordinates
  124. */
  125. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis)
  126. {
  127. double dx = (double) x1 - x2;
  128. double dy = (double) y1 - y2;
  129. if (includeZAxis)
  130. {
  131. double dz = z1 - z2;
  132. return Math.sqrt((dx * dx) + (dy * dy) + (dz * dz));
  133. }
  134. return Math.sqrt((dx * dx) + (dy * dy));
  135. }
  136. /**
  137. * @param obj1
  138. * @param obj2
  139. * @param includeZAxis - if true, includes also the Z axis in the calculation
  140. * @return the distance between the two objects
  141. */
  142. public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
  143. {
  144. if ((obj1 == null) || (obj2 == null))
  145. {
  146. return 1000000;
  147. }
  148. return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
  149. }
  150. /**
  151. * @param str - the string whose first letter to capitalize
  152. * @return a string with the first letter of the {@code str} capitalized
  153. */
  154. public static String capitalizeFirst(String str)
  155. {
  156. if ((str == null) || str.isEmpty())
  157. {
  158. return str;
  159. }
  160. final char[] arr = str.toCharArray();
  161. final char c = arr[0];
  162. if (Character.isLetter(c))
  163. {
  164. arr[0] = Character.toUpperCase(c);
  165. }
  166. return new String(arr);
  167. }
  168. /**
  169. * (Based on ucwords() function of PHP)<br>
  170. * DrHouse: still functional but must be rewritten to avoid += to concat strings
  171. * @param str - the string to capitalize
  172. * @return a string with the first letter of every word in {@code str} capitalized
  173. */
  174. @Deprecated
  175. public static String capitalizeWords(String str)
  176. {
  177. if ((str == null) || str.isEmpty())
  178. {
  179. return str;
  180. }
  181. char[] charArray = str.toCharArray();
  182. StringBuilder result = new StringBuilder();
  183. // Capitalize the first letter in the given string!
  184. charArray[0] = Character.toUpperCase(charArray[0]);
  185. for (int i = 0; i < charArray.length; i++)
  186. {
  187. if (Character.isWhitespace(charArray[i]))
  188. {
  189. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  190. }
  191. result.append(charArray[i]);
  192. }
  193. return result.toString();
  194. }
  195. /**
  196. * @param range
  197. * @param obj1
  198. * @param obj2
  199. * @param includeZAxis
  200. * @return {@code true} if the two objects are within specified range between each other, {@code false} otherwise
  201. */
  202. public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
  203. {
  204. if ((obj1 == null) || (obj2 == null))
  205. {
  206. return false;
  207. }
  208. if (obj1.getInstanceId() != obj2.getInstanceId())
  209. {
  210. return false;
  211. }
  212. if (range == -1)
  213. {
  214. return true; // not limited
  215. }
  216. int rad = 0;
  217. if (obj1 instanceof L2Character)
  218. {
  219. rad += ((L2Character) obj1).getTemplate().getCollisionRadius();
  220. }
  221. if (obj2 instanceof L2Character)
  222. {
  223. rad += ((L2Character) obj2).getTemplate().getCollisionRadius();
  224. }
  225. double dx = obj1.getX() - obj2.getX();
  226. double dy = obj1.getY() - obj2.getY();
  227. if (includeZAxis)
  228. {
  229. double dz = obj1.getZ() - obj2.getZ();
  230. double d = (dx * dx) + (dy * dy) + (dz * dz);
  231. return d <= ((range * range) + (2 * range * rad) + (rad * rad));
  232. }
  233. double d = (dx * dx) + (dy * dy);
  234. return d <= ((range * range) + (2 * range * rad) + (rad * rad));
  235. }
  236. /**
  237. * 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).
  238. * @param radius
  239. * @param obj1
  240. * @param obj2
  241. * @param includeZAxis if true, check also Z axis (3-dimensional check), otherwise only 2D
  242. * @return {@code true} if objects are within specified range between each other, {@code false} otherwise
  243. */
  244. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  245. {
  246. if ((obj1 == null) || (obj2 == null))
  247. {
  248. return false;
  249. }
  250. if (radius == -1)
  251. {
  252. return true; // not limited
  253. }
  254. int dx = obj1.getX() - obj2.getX();
  255. int dy = obj1.getY() - obj2.getY();
  256. if (includeZAxis)
  257. {
  258. int dz = obj1.getZ() - obj2.getZ();
  259. return ((dx * dx) + (dy * dy) + (dz * dz)) <= (radius * radius);
  260. }
  261. return ((dx * dx) + (dy * dy)) <= (radius * radius);
  262. }
  263. /**
  264. * @param str - the String to count
  265. * @return the number of "words" in a given string.
  266. */
  267. public static int countWords(String str)
  268. {
  269. return str.trim().split("\\s+").length;
  270. }
  271. /**
  272. * (Based on implode() in PHP)
  273. * @param strArray - an array of strings to concatenate
  274. * @param strDelim - the delimiter to put between the strings
  275. * @return a delimited string for a given array of string elements.
  276. */
  277. public static String implodeString(Iterable<String> strArray, String strDelim)
  278. {
  279. final TextBuilder sbString = TextBuilder.newInstance();
  280. for (String strValue : strArray)
  281. {
  282. sbString.append(strValue);
  283. sbString.append(strDelim);
  284. }
  285. String result = sbString.toString();
  286. TextBuilder.recycle(sbString);
  287. return result;
  288. }
  289. /**
  290. * (Based on round() in PHP)
  291. * @param number - the number to round
  292. * @param numPlaces - how many digits after decimal point to leave intact
  293. * @return the value of {@code number} rounded to specified number of digits after the decimal point.
  294. */
  295. public static float roundTo(float number, int numPlaces)
  296. {
  297. if (numPlaces <= 1)
  298. {
  299. return Math.round(number);
  300. }
  301. float exponent = (float) Math.pow(10, numPlaces);
  302. return Math.round(number * exponent) / exponent;
  303. }
  304. /**
  305. * @param text - the text to check
  306. * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
  307. */
  308. public static boolean isDigit(String text)
  309. {
  310. if ((text == null) || text.isEmpty())
  311. {
  312. return false;
  313. }
  314. for (char c : text.toCharArray())
  315. {
  316. if (!Character.isDigit(c))
  317. {
  318. return false;
  319. }
  320. }
  321. return true;
  322. }
  323. /**
  324. * @param text - the text to check
  325. * @return {@code true} if {@code text} contains only letters and/or numbers, {@code false} otherwise
  326. */
  327. public static boolean isAlphaNumeric(String text)
  328. {
  329. if ((text == null) || text.isEmpty())
  330. {
  331. return false;
  332. }
  333. for (char c : text.toCharArray())
  334. {
  335. if (!Character.isLetterOrDigit(c))
  336. {
  337. return false;
  338. }
  339. }
  340. return true;
  341. }
  342. /**
  343. * Format the specified digit using the digit grouping symbol "," (comma).<br>
  344. * For example, 123456789 becomes 123,456,789.
  345. * @param amount - the amount of adena
  346. * @return the formatted adena amount
  347. */
  348. public static String formatAdena(long amount)
  349. {
  350. String s = "";
  351. long rem = amount % 1000;
  352. s = Long.toString(rem);
  353. amount = (amount - rem) / 1000;
  354. while (amount > 0)
  355. {
  356. if (rem < 99)
  357. {
  358. s = '0' + s;
  359. }
  360. if (rem < 9)
  361. {
  362. s = '0' + s;
  363. }
  364. rem = amount % 1000;
  365. s = Long.toString(rem) + "," + s;
  366. amount = (amount - rem) / 1000;
  367. }
  368. return s;
  369. }
  370. /**
  371. * Format the given date on the given format
  372. * @param date : the date to format.
  373. * @param format : the format to correct by.
  374. * @return a string representation of the formatted date.
  375. */
  376. public static String formatDate(Date date, String format)
  377. {
  378. if (date == null)
  379. {
  380. return null;
  381. }
  382. final DateFormat dateFormat = new SimpleDateFormat(format);
  383. return dateFormat.format(date);
  384. }
  385. /**
  386. * @param <T>
  387. * @param array - the array to look into
  388. * @param obj - the object to search for
  389. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise.
  390. */
  391. public static <T> boolean contains(T[] array, T obj)
  392. {
  393. for (T element : array)
  394. {
  395. if (element == obj)
  396. {
  397. return true;
  398. }
  399. }
  400. return false;
  401. }
  402. /**
  403. * @param array - the array to look into
  404. * @param obj - the integer to search for
  405. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  406. */
  407. public static boolean contains(int[] array, int obj)
  408. {
  409. for (int element : array)
  410. {
  411. if (element == obj)
  412. {
  413. return true;
  414. }
  415. }
  416. return false;
  417. }
  418. public static File[] getDatapackFiles(String dirname, String extention)
  419. {
  420. File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname);
  421. if (!dir.exists())
  422. {
  423. return null;
  424. }
  425. return dir.listFiles(new ExtFilter(extention));
  426. }
  427. public static String getDateString(Date date)
  428. {
  429. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  430. return dateFormat.format(date.getTime());
  431. }
  432. /**
  433. * Sends the given html to the player.
  434. * @param activeChar
  435. * @param html
  436. */
  437. public static void sendHtml(L2PcInstance activeChar, String html)
  438. {
  439. NpcHtmlMessage npcHtml = new NpcHtmlMessage(0);
  440. npcHtml.setHtml(html);
  441. activeChar.sendPacket(npcHtml);
  442. }
  443. /**
  444. * Sends the html using the community board window.
  445. * @param activeChar
  446. * @param html
  447. */
  448. public static void sendCBHtml(L2PcInstance activeChar, String html)
  449. {
  450. sendCBHtml(activeChar, html, "");
  451. }
  452. /**
  453. * Sends the html using the community board window.
  454. * @param activeChar
  455. * @param html
  456. * @param fillMultiEdit fills the multiedit window (if any) inside the community board.
  457. */
  458. public static void sendCBHtml(L2PcInstance activeChar, String html, String fillMultiEdit)
  459. {
  460. if (activeChar == null)
  461. return;
  462. if (html != null)
  463. {
  464. activeChar.clearBypass();
  465. int len = html.length();
  466. for (int i = 0; i < len; i++)
  467. {
  468. int start = html.indexOf("\"bypass ", i);
  469. int finish = html.indexOf("\"", start + 1);
  470. if (start < 0 || finish < 0)
  471. break;
  472. if (html.substring(start + 8, start + 10).equals("-h"))
  473. start += 11;
  474. else
  475. start += 8;
  476. i = finish;
  477. int finish2 = html.indexOf("$", start);
  478. if (finish2 < finish && finish2 > 0)
  479. activeChar.addBypass2(html.substring(start, finish2).trim());
  480. else
  481. activeChar.addBypass(html.substring(start, finish).trim());
  482. }
  483. }
  484. if (fillMultiEdit != null)
  485. {
  486. activeChar.sendPacket(new ShowBoard(html, "1001"));
  487. fillMultiEditContent(activeChar, fillMultiEdit);
  488. }
  489. else
  490. {
  491. activeChar.sendPacket(new ShowBoard(null, "101"));
  492. activeChar.sendPacket(new ShowBoard(html, "101"));
  493. activeChar.sendPacket(new ShowBoard(null, "102"));
  494. activeChar.sendPacket(new ShowBoard(null, "103"));
  495. }
  496. }
  497. /**
  498. *
  499. * Fills the community board's multiedit window with text. Must send after sendCBHtml
  500. * @param activeChar
  501. * @param text
  502. */
  503. public static void fillMultiEditContent(L2PcInstance activeChar, String text)
  504. {
  505. text = text.replaceAll("<br>", "\n");
  506. List<String> arg = new FastList<>();
  507. arg.add("0");
  508. arg.add("0");
  509. arg.add("0");
  510. arg.add("0");
  511. arg.add("0");
  512. arg.add("0");
  513. arg.add(activeChar.getName());
  514. arg.add(Integer.toString(activeChar.getObjectId()));
  515. arg.add(activeChar.getAccountName());
  516. arg.add("9");
  517. arg.add(" ");
  518. arg.add(" ");
  519. arg.add(text);
  520. arg.add("0");
  521. arg.add("0");
  522. arg.add("0");
  523. arg.add("0");
  524. activeChar.sendPacket(new ShowBoard(arg));
  525. }
  526. }