Util.java 22 KB

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