Util.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.util;
  20. import java.io.File;
  21. import java.text.DateFormat;
  22. import java.text.DecimalFormat;
  23. import java.text.DecimalFormatSymbols;
  24. import java.text.NumberFormat;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Collection;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Locale;
  30. import java.util.logging.Logger;
  31. import javolution.text.TextBuilder;
  32. import javolution.util.FastList;
  33. import com.l2jserver.Config;
  34. import com.l2jserver.gameserver.GeoData;
  35. import com.l2jserver.gameserver.ThreadPoolManager;
  36. import com.l2jserver.gameserver.enums.HtmlActionScope;
  37. import com.l2jserver.gameserver.enums.IllegalActionPunishmentType;
  38. import com.l2jserver.gameserver.model.L2Object;
  39. import com.l2jserver.gameserver.model.actor.L2Character;
  40. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  41. import com.l2jserver.gameserver.model.actor.tasks.player.IllegalPlayerActionTask;
  42. import com.l2jserver.gameserver.network.serverpackets.AbstractHtmlPacket;
  43. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  44. import com.l2jserver.gameserver.network.serverpackets.ShowBoard;
  45. import com.l2jserver.util.file.filter.ExtFilter;
  46. /**
  47. * General Utility functions related to game server.
  48. */
  49. public final class Util
  50. {
  51. private static final Logger LOGGER = Logger.getLogger(Util.class.getName());
  52. private static final NumberFormat ADENA_FORMATTER = NumberFormat.getIntegerInstance(Locale.ENGLISH);
  53. public static void handleIllegalPlayerAction(L2PcInstance actor, String message, IllegalActionPunishmentType punishment)
  54. {
  55. ThreadPoolManager.getInstance().scheduleGeneral(new IllegalPlayerActionTask(actor, message, punishment), 5000);
  56. }
  57. public static String getRelativePath(File base, File file)
  58. {
  59. return file.toURI().getPath().substring(base.toURI().getPath().length());
  60. }
  61. /**
  62. * @param obj1
  63. * @param obj2
  64. * @return degree value of object 2 to the horizontal line with object 1 being the origin.
  65. */
  66. public static double calculateAngleFrom(L2Object obj1, L2Object obj2)
  67. {
  68. return calculateAngleFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  69. }
  70. /**
  71. * @param obj1X
  72. * @param obj1Y
  73. * @param obj2X
  74. * @param obj2Y
  75. * @return degree value of object 2 to the horizontal line with object 1 being the origin
  76. */
  77. public static final double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  78. {
  79. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  80. if (angleTarget < 0)
  81. {
  82. angleTarget = 360 + angleTarget;
  83. }
  84. return angleTarget;
  85. }
  86. public static final double convertHeadingToDegree(int clientHeading)
  87. {
  88. double degree = clientHeading / 182.044444444;
  89. return degree;
  90. }
  91. public static final int convertDegreeToClientHeading(double degree)
  92. {
  93. if (degree < 0)
  94. {
  95. degree = 360 + degree;
  96. }
  97. return (int) (degree * 182.044444444);
  98. }
  99. public static final int calculateHeadingFrom(L2Object obj1, L2Object obj2)
  100. {
  101. return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  102. }
  103. public static final int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  104. {
  105. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  106. if (angleTarget < 0)
  107. {
  108. angleTarget = 360 + angleTarget;
  109. }
  110. return (int) (angleTarget * 182.044444444);
  111. }
  112. public static final int calculateHeadingFrom(double dx, double dy)
  113. {
  114. double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
  115. if (angleTarget < 0)
  116. {
  117. angleTarget = 360 + angleTarget;
  118. }
  119. return (int) (angleTarget * 182.044444444);
  120. }
  121. /**
  122. * @param x1
  123. * @param y1
  124. * @param x2
  125. * @param y2
  126. * @return the distance between the two coordinates in 2D plane
  127. */
  128. public static double calculateDistance(int x1, int y1, int x2, int y2)
  129. {
  130. return calculateDistance(x1, y1, 0, x2, y2, 0, false);
  131. }
  132. /**
  133. * @param x1
  134. * @param y1
  135. * @param z1
  136. * @param x2
  137. * @param y2
  138. * @param z2
  139. * @param includeZAxis - if true, includes also the Z axis in the calculation
  140. * @return the distance between the two coordinates
  141. */
  142. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis)
  143. {
  144. double dx = (double) x1 - x2;
  145. double dy = (double) y1 - y2;
  146. if (includeZAxis)
  147. {
  148. double dz = z1 - z2;
  149. return Math.sqrt((dx * dx) + (dy * dy) + (dz * dz));
  150. }
  151. return Math.sqrt((dx * dx) + (dy * dy));
  152. }
  153. /**
  154. * @param obj1
  155. * @param obj2
  156. * @param includeZAxis - if true, includes also the Z axis in the calculation
  157. * @return the distance between the two objects
  158. */
  159. public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
  160. {
  161. if ((obj1 == null) || (obj2 == null))
  162. {
  163. return 1000000;
  164. }
  165. return calculateDistance(obj1.getX(), obj1.getY(), obj1.getZ(), obj2.getX(), obj2.getY(), obj2.getZ(), includeZAxis);
  166. }
  167. /**
  168. * @param str - the string whose first letter to capitalize
  169. * @return a string with the first letter of the {@code str} capitalized
  170. */
  171. public static String capitalizeFirst(String str)
  172. {
  173. if ((str == null) || str.isEmpty())
  174. {
  175. return str;
  176. }
  177. final char[] arr = str.toCharArray();
  178. final char c = arr[0];
  179. if (Character.isLetter(c))
  180. {
  181. arr[0] = Character.toUpperCase(c);
  182. }
  183. return new String(arr);
  184. }
  185. /**
  186. * (Based on ucwords() function of PHP)<br>
  187. * DrHouse: still functional but must be rewritten to avoid += to concat strings
  188. * @param str - the string to capitalize
  189. * @return a string with the first letter of every word in {@code str} capitalized
  190. */
  191. @Deprecated
  192. public static String capitalizeWords(String str)
  193. {
  194. if ((str == null) || str.isEmpty())
  195. {
  196. return str;
  197. }
  198. char[] charArray = str.toCharArray();
  199. StringBuilder result = new StringBuilder();
  200. // Capitalize the first letter in the given string!
  201. charArray[0] = Character.toUpperCase(charArray[0]);
  202. for (int i = 0; i < charArray.length; i++)
  203. {
  204. if (Character.isWhitespace(charArray[i]))
  205. {
  206. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  207. }
  208. result.append(charArray[i]);
  209. }
  210. return result.toString();
  211. }
  212. /**
  213. * @param range
  214. * @param obj1
  215. * @param obj2
  216. * @param includeZAxis
  217. * @return {@code true} if the two objects are within specified range between each other, {@code false} otherwise
  218. */
  219. public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
  220. {
  221. if ((obj1 == null) || (obj2 == null))
  222. {
  223. return false;
  224. }
  225. if (obj1.getInstanceId() != obj2.getInstanceId())
  226. {
  227. return false;
  228. }
  229. if (range == -1)
  230. {
  231. return true; // not limited
  232. }
  233. int rad = 0;
  234. if (obj1 instanceof L2Character)
  235. {
  236. rad += ((L2Character) obj1).getTemplate().getCollisionRadius();
  237. }
  238. if (obj2 instanceof L2Character)
  239. {
  240. rad += ((L2Character) obj2).getTemplate().getCollisionRadius();
  241. }
  242. double dx = obj1.getX() - obj2.getX();
  243. double dy = obj1.getY() - obj2.getY();
  244. double d = (dx * dx) + (dy * dy);
  245. if (includeZAxis)
  246. {
  247. double dz = obj1.getZ() - obj2.getZ();
  248. d += (dz * dz);
  249. }
  250. return d <= ((range * range) + (2 * range * rad) + (rad * rad));
  251. }
  252. /**
  253. * 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).
  254. * @param radius
  255. * @param obj1
  256. * @param obj2
  257. * @param includeZAxis if true, check also Z axis (3-dimensional check), otherwise only 2D
  258. * @return {@code true} if objects are within specified range between each other, {@code false} otherwise
  259. */
  260. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  261. {
  262. if ((obj1 == null) || (obj2 == null))
  263. {
  264. return false;
  265. }
  266. if (radius == -1)
  267. {
  268. return true; // not limited
  269. }
  270. int dx = obj1.getX() - obj2.getX();
  271. int dy = obj1.getY() - obj2.getY();
  272. if (includeZAxis)
  273. {
  274. int dz = obj1.getZ() - obj2.getZ();
  275. return ((dx * dx) + (dy * dy) + (dz * dz)) <= (radius * radius);
  276. }
  277. return ((dx * dx) + (dy * dy)) <= (radius * radius);
  278. }
  279. /**
  280. * @param str - the String to count
  281. * @return the number of "words" in a given string.
  282. */
  283. public static int countWords(String str)
  284. {
  285. return str.trim().split("\\s+").length;
  286. }
  287. /**
  288. * (Based on implode() in PHP)
  289. * @param strArray - an array of strings to concatenate
  290. * @param strDelim - the delimiter to put between the strings
  291. * @return a delimited string for a given array of string elements.
  292. */
  293. public static String implodeString(Iterable<String> strArray, String strDelim)
  294. {
  295. final TextBuilder sbString = TextBuilder.newInstance();
  296. for (String strValue : strArray)
  297. {
  298. sbString.append(strValue);
  299. sbString.append(strDelim);
  300. }
  301. String result = sbString.toString();
  302. TextBuilder.recycle(sbString);
  303. return result;
  304. }
  305. /**
  306. * Based on implode() in PHP
  307. * @param <T>
  308. * @param array
  309. * @param delim
  310. * @return a delimited string for a given array of string elements.
  311. */
  312. public static <T> String implode(T[] array, String delim)
  313. {
  314. String result = "";
  315. for (T val : array)
  316. {
  317. result += val.toString() + delim;
  318. }
  319. if (!result.isEmpty())
  320. {
  321. result = result.substring(0, result.length() - 1);
  322. }
  323. return result;
  324. }
  325. /**
  326. * (Based on round() in PHP)
  327. * @param number - the number to round
  328. * @param numPlaces - how many digits after decimal point to leave intact
  329. * @return the value of {@code number} rounded to specified number of digits after the decimal point.
  330. */
  331. public static float roundTo(float number, int numPlaces)
  332. {
  333. if (numPlaces <= 1)
  334. {
  335. return Math.round(number);
  336. }
  337. float exponent = (float) Math.pow(10, numPlaces);
  338. return Math.round(number * exponent) / exponent;
  339. }
  340. /**
  341. * @param text - the text to check
  342. * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
  343. */
  344. public static boolean isDigit(String text)
  345. {
  346. if ((text == null) || text.isEmpty())
  347. {
  348. return false;
  349. }
  350. for (char c : text.toCharArray())
  351. {
  352. if (!Character.isDigit(c))
  353. {
  354. return false;
  355. }
  356. }
  357. return true;
  358. }
  359. /**
  360. * @param text - the text to check
  361. * @return {@code true} if {@code text} contains only letters and/or numbers, {@code false} otherwise
  362. */
  363. public static boolean isAlphaNumeric(String text)
  364. {
  365. if ((text == null) || text.isEmpty())
  366. {
  367. return false;
  368. }
  369. for (char c : text.toCharArray())
  370. {
  371. if (!Character.isLetterOrDigit(c))
  372. {
  373. return false;
  374. }
  375. }
  376. return true;
  377. }
  378. /**
  379. * Format the specified digit using the digit grouping symbol "," (comma).<br>
  380. * For example, 123456789 becomes 123,456,789.
  381. * @param amount - the amount of adena
  382. * @return the formatted adena amount
  383. */
  384. public static String formatAdena(long amount)
  385. {
  386. synchronized (ADENA_FORMATTER)
  387. {
  388. return ADENA_FORMATTER.format(amount);
  389. }
  390. }
  391. /**
  392. * @param val
  393. * @param format
  394. * @return formatted double value by specified format.
  395. */
  396. public static String formatDouble(double val, String format)
  397. {
  398. final DecimalFormat formatter = new DecimalFormat(format, new DecimalFormatSymbols(Locale.ENGLISH));
  399. return formatter.format(val);
  400. }
  401. /**
  402. * Format the given date on the given format
  403. * @param date : the date to format.
  404. * @param format : the format to correct by.
  405. * @return a string representation of the formatted date.
  406. */
  407. public static String formatDate(Date date, String format)
  408. {
  409. if (date == null)
  410. {
  411. return null;
  412. }
  413. final DateFormat dateFormat = new SimpleDateFormat(format);
  414. return dateFormat.format(date);
  415. }
  416. /**
  417. * @param <T>
  418. * @param array - the array to look into
  419. * @param obj - the object to search for
  420. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise.
  421. */
  422. public static <T> boolean contains(T[] array, T obj)
  423. {
  424. for (T element : array)
  425. {
  426. if (element == obj)
  427. {
  428. return true;
  429. }
  430. }
  431. return false;
  432. }
  433. /**
  434. * @param array - the array to look into
  435. * @param obj - the integer to search for
  436. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  437. */
  438. public static boolean contains(int[] array, int obj)
  439. {
  440. for (int element : array)
  441. {
  442. if (element == obj)
  443. {
  444. return true;
  445. }
  446. }
  447. return false;
  448. }
  449. public static File[] getDatapackFiles(String dirname, String extention)
  450. {
  451. File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname);
  452. if (!dir.exists())
  453. {
  454. return null;
  455. }
  456. return dir.listFiles(new ExtFilter(extention));
  457. }
  458. public static String getDateString(Date date)
  459. {
  460. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  461. return dateFormat.format(date.getTime());
  462. }
  463. private static final void buildHtmlBypassCache(L2PcInstance player, HtmlActionScope scope, String html)
  464. {
  465. String htmlLower = html.toLowerCase(Locale.ENGLISH);
  466. int bypassEnd = 0;
  467. int bypassStart = htmlLower.indexOf("=\"bypass ", bypassEnd);
  468. int bypassStartEnd;
  469. while (bypassStart != -1)
  470. {
  471. bypassStartEnd = bypassStart + 9;
  472. bypassEnd = htmlLower.indexOf("\"", bypassStartEnd);
  473. if (bypassEnd == -1)
  474. {
  475. break;
  476. }
  477. int hParamPos = htmlLower.indexOf("-h ", bypassStartEnd);
  478. String bypass;
  479. if ((hParamPos != -1) && (hParamPos < bypassEnd))
  480. {
  481. bypass = html.substring(hParamPos + 3, bypassEnd).trim();
  482. }
  483. else
  484. {
  485. bypass = html.substring(bypassStartEnd, bypassEnd).trim();
  486. }
  487. int firstParameterStart = bypass.indexOf(AbstractHtmlPacket.VAR_PARAM_START_CHAR);
  488. if (firstParameterStart != -1)
  489. {
  490. bypass = bypass.substring(0, firstParameterStart + 1);
  491. }
  492. if (Config.HTML_ACTION_CACHE_DEBUG)
  493. {
  494. LOGGER.info("Cached html bypass(" + scope.toString() + "): '" + bypass + "'");
  495. }
  496. player.addHtmlAction(scope, bypass);
  497. bypassStart = htmlLower.indexOf("=\"bypass ", bypassEnd);
  498. }
  499. }
  500. private static final void buildHtmlLinkCache(L2PcInstance player, HtmlActionScope scope, String html)
  501. {
  502. String htmlLower = html.toLowerCase(Locale.ENGLISH);
  503. int linkEnd = 0;
  504. int linkStart = htmlLower.indexOf("=\"link ", linkEnd);
  505. int linkStartEnd;
  506. while (linkStart != -1)
  507. {
  508. linkStartEnd = linkStart + 7;
  509. linkEnd = htmlLower.indexOf("\"", linkStartEnd);
  510. if (linkEnd == -1)
  511. {
  512. break;
  513. }
  514. String htmlLink = html.substring(linkStartEnd, linkEnd).trim();
  515. if (htmlLink.isEmpty())
  516. {
  517. LOGGER.warning("Html link path is empty!");
  518. continue;
  519. }
  520. if (htmlLink.contains(".."))
  521. {
  522. LOGGER.warning("Html link path is invalid: " + htmlLink);
  523. continue;
  524. }
  525. if (Config.HTML_ACTION_CACHE_DEBUG)
  526. {
  527. LOGGER.info("Cached html link(" + scope.toString() + "): '" + htmlLink + "'");
  528. }
  529. // let's keep an action cache with "link " lowercase literal kept
  530. player.addHtmlAction(scope, "link " + htmlLink);
  531. linkStart = htmlLower.indexOf("=\"link ", linkEnd);
  532. }
  533. }
  534. /**
  535. * Builds the html action cache for the specified scope.<br>
  536. * An {@code npcObjId} of 0 means, the cached actions can be clicked<br>
  537. * without beeing near an npc which is spawned in the world.
  538. * @param player the player to build the html action cache for
  539. * @param scope the scope to build the html action cache for
  540. * @param npcObjId the npc object id the html actions are cached for
  541. * @param html the html code to parse
  542. */
  543. public static void buildHtmlActionCache(L2PcInstance player, HtmlActionScope scope, int npcObjId, String html)
  544. {
  545. if ((player == null) || (scope == null) || (npcObjId < 0) || (html == null))
  546. {
  547. throw new IllegalArgumentException();
  548. }
  549. if (Config.HTML_ACTION_CACHE_DEBUG)
  550. {
  551. LOGGER.info("Set html action npc(" + scope.toString() + "): " + npcObjId);
  552. }
  553. player.setHtmlActionOriginObjectId(scope, npcObjId);
  554. buildHtmlBypassCache(player, scope, html);
  555. buildHtmlLinkCache(player, scope, html);
  556. }
  557. /**
  558. * Sends the given html to the player.
  559. * @param activeChar
  560. * @param html
  561. */
  562. public static void sendHtml(L2PcInstance activeChar, String html)
  563. {
  564. final NpcHtmlMessage npcHtml = new NpcHtmlMessage();
  565. npcHtml.setHtml(html);
  566. activeChar.sendPacket(npcHtml);
  567. }
  568. public static void sendCBHtml(L2PcInstance activeChar, String html)
  569. {
  570. sendCBHtml(activeChar, html, true);
  571. }
  572. public static void sendCBHtml(L2PcInstance activeChar, String html, boolean buildActionCache)
  573. {
  574. sendCBHtml(activeChar, html, null, buildActionCache);
  575. }
  576. public static void sendCBHtml(L2PcInstance activeChar, String html, String fillMultiEdit)
  577. {
  578. sendCBHtml(activeChar, html, fillMultiEdit, true);
  579. }
  580. /**
  581. * Sends the html using the community board window.
  582. * @param activeChar
  583. * @param html
  584. * @param fillMultiEdit fills the multiedit window (if any) inside the community board.
  585. * @param buildActionCache if false, action cache will not be built and players won't be able to click links in the HTML
  586. */
  587. public static void sendCBHtml(L2PcInstance activeChar, String html, String fillMultiEdit, boolean buildActionCache)
  588. {
  589. if (activeChar == null)
  590. {
  591. return;
  592. }
  593. activeChar.clearHtmlActions(HtmlActionScope.COMM_BOARD_HTML);
  594. if (buildActionCache)
  595. {
  596. buildHtmlActionCache(activeChar, HtmlActionScope.COMM_BOARD_HTML, 0, html);
  597. }
  598. if (fillMultiEdit != null)
  599. {
  600. activeChar.sendPacket(new ShowBoard(html, "1001"));
  601. fillMultiEditContent(activeChar, fillMultiEdit);
  602. }
  603. else
  604. {
  605. if (html.length() < 16250)
  606. {
  607. activeChar.sendPacket(new ShowBoard(html, "101"));
  608. activeChar.sendPacket(new ShowBoard(null, "102"));
  609. activeChar.sendPacket(new ShowBoard(null, "103"));
  610. }
  611. else if (html.length() < (16250 * 2))
  612. {
  613. activeChar.sendPacket(new ShowBoard(html.substring(0, 16250), "101"));
  614. activeChar.sendPacket(new ShowBoard(html.substring(16250), "102"));
  615. activeChar.sendPacket(new ShowBoard(null, "103"));
  616. }
  617. else if (html.length() < (16250 * 3))
  618. {
  619. activeChar.sendPacket(new ShowBoard(html.substring(0, 16250), "101"));
  620. activeChar.sendPacket(new ShowBoard(html.substring(16250, 16250 * 2), "102"));
  621. activeChar.sendPacket(new ShowBoard(html.substring(16250 * 2), "103"));
  622. }
  623. else
  624. {
  625. activeChar.sendPacket(new ShowBoard("<html><body><br><center>Error: HTML was too long!</center></body></html>", "101"));
  626. activeChar.sendPacket(new ShowBoard(null, "102"));
  627. activeChar.sendPacket(new ShowBoard(null, "103"));
  628. }
  629. }
  630. }
  631. /**
  632. * Fills the community board's multiedit window with text. Must send after sendCBHtml
  633. * @param activeChar
  634. * @param text
  635. */
  636. public static void fillMultiEditContent(L2PcInstance activeChar, String text)
  637. {
  638. text = text.replaceAll("<br>", Config.EOL);
  639. List<String> arg = new FastList<>();
  640. arg.add("0");
  641. arg.add("0");
  642. arg.add("0");
  643. arg.add("0");
  644. arg.add("0");
  645. arg.add("0");
  646. arg.add(activeChar.getName());
  647. arg.add(Integer.toString(activeChar.getObjectId()));
  648. arg.add(activeChar.getAccountName());
  649. arg.add("9");
  650. arg.add(" ");
  651. arg.add(" ");
  652. arg.add(text);
  653. arg.add("0");
  654. arg.add("0");
  655. arg.add("0");
  656. arg.add("0");
  657. activeChar.sendPacket(new ShowBoard(arg));
  658. }
  659. /**
  660. * Return the number of playable characters in a defined radius around the specified object.
  661. * @param range : the radius in which to look for players
  662. * @param npc : the object whose knownlist to check
  663. * @param playable : if {@code true}, count summons and pets aswell
  664. * @param invisible : if {@code true}, count invisible characters aswell
  665. * @return the number of targets found
  666. */
  667. public static int getPlayersCountInRadius(int range, L2Object npc, boolean playable, boolean invisible)
  668. {
  669. int count = 0;
  670. final Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  671. for (L2Object obj : objs)
  672. {
  673. if ((obj != null) && ((obj.isPlayable() && playable) || obj.isPet()))
  674. {
  675. if (obj.isPlayer() && !invisible && obj.getActingPlayer().getAppearance().getInvisible())
  676. {
  677. continue;
  678. }
  679. final L2Character cha = (L2Character) obj;
  680. if (((cha.getZ() < (npc.getZ() - 100)) && (cha.getZ() > (npc.getZ() + 100))) || !(GeoData.getInstance().canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), npc.getX(), npc.getY(), npc.getZ())))
  681. {
  682. continue;
  683. }
  684. if (Util.checkIfInRange(range, npc, obj, true) && !cha.isDead())
  685. {
  686. count++;
  687. }
  688. }
  689. }
  690. return count;
  691. }
  692. public static boolean isInsideRangeOfObjectId(L2Object obj, int targetObjId, int radius)
  693. {
  694. L2Object target = obj.getKnownList().getKnownObjects().get(targetObjId);
  695. if (target == null)
  696. {
  697. return false;
  698. }
  699. if (Util.calculateDistance(obj, target, false) > radius)
  700. {
  701. return false;
  702. }
  703. return true;
  704. }
  705. }