Util.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. angleTarget = 360 + angleTarget;
  59. return angleTarget;
  60. }
  61. public final static double convertHeadingToDegree(int clientHeading)
  62. {
  63. double degree = clientHeading / 182.044444444;
  64. return degree;
  65. }
  66. public final static int convertDegreeToClientHeading(double degree)
  67. {
  68. if (degree < 0)
  69. degree = 360 + degree;
  70. return (int) (degree * 182.044444444);
  71. }
  72. public final static int calculateHeadingFrom(L2Object obj1, L2Object obj2)
  73. {
  74. return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  75. }
  76. public final static int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  77. {
  78. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  79. if (angleTarget < 0)
  80. angleTarget = 360 + angleTarget;
  81. return (int) (angleTarget * 182.044444444);
  82. }
  83. public final static int calculateHeadingFrom(double dx, double dy)
  84. {
  85. double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
  86. if (angleTarget < 0)
  87. angleTarget = 360 + angleTarget;
  88. return (int) (angleTarget * 182.044444444);
  89. }
  90. /**
  91. * @param x1
  92. * @param y1
  93. * @param x2
  94. * @param y2
  95. * @return the distance between the two coordinates in 2D plane
  96. */
  97. public static double calculateDistance(int x1, int y1, int x2, int y2)
  98. {
  99. return calculateDistance(x1, y1, 0, x2, y2, 0, false);
  100. }
  101. /**
  102. * @param x1
  103. * @param y1
  104. * @param z1
  105. * @param x2
  106. * @param y2
  107. * @param z2
  108. * @param includeZAxis - if true, includes also the Z axis in the calculation
  109. * @return the distance between the two coordinates
  110. */
  111. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis)
  112. {
  113. double dx = (double) x1 - x2;
  114. double dy = (double) y1 - y2;
  115. if (includeZAxis)
  116. {
  117. double dz = z1 - z2;
  118. return Math.sqrt(dx * dx + dy * dy + dz * dz);
  119. }
  120. return Math.sqrt(dx * dx + dy * dy);
  121. }
  122. /**
  123. * @param obj1
  124. * @param obj2
  125. * @param includeZAxis - if true, includes also the Z axis in the calculation
  126. * @return the distance between the two objects
  127. */
  128. public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
  129. {
  130. if (obj1 == null || obj2 == null)
  131. return 1000000;
  132. return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
  133. }
  134. /**
  135. *
  136. * @param str - the string whose first letter to capitalize
  137. * @return a string with the first letter of the {@code str} capitalized
  138. */
  139. public static String capitalizeFirst(String str)
  140. {
  141. if (str == null || str.isEmpty())
  142. return str;
  143. final char[] arr = str.toCharArray();
  144. final char c = arr[0];
  145. if (Character.isLetter(c))
  146. arr[0] = Character.toUpperCase(c);
  147. return arr.toString();
  148. }
  149. /**
  150. * (Based on ucwords() function of PHP)
  151. *
  152. * DrHouse: still functional but must be rewritten to avoid += to concat strings
  153. *
  154. * @param str - the string to capitalize
  155. * @return a string with the first letter of every word in {@code str} capitalized
  156. */
  157. @Deprecated
  158. public static String capitalizeWords(String str)
  159. {
  160. if (str == null || str.isEmpty())
  161. return str;
  162. char[] charArray = str.toCharArray();
  163. StringBuilder result = new StringBuilder();
  164. // Capitalize the first letter in the given string!
  165. charArray[0] = Character.toUpperCase(charArray[0]);
  166. for (int i = 0; i < charArray.length; i++)
  167. {
  168. if (Character.isWhitespace(charArray[i]))
  169. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  170. result.append(charArray[i]);
  171. }
  172. return result.toString();
  173. }
  174. /**
  175. * @param range
  176. * @param obj1
  177. * @param obj2
  178. * @param includeZAxis
  179. * @return {@code true} if the two objects are within specified range between each other, {@code false} otherwise
  180. */
  181. public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
  182. {
  183. if (obj1 == null || obj2 == null)
  184. return false;
  185. if (obj1.getInstanceId() != obj2.getInstanceId())
  186. return false;
  187. if (range == -1)
  188. return true; // not limited
  189. int rad = 0;
  190. if (obj1 instanceof L2Character)
  191. rad += ((L2Character) obj1).getTemplate().getCollisionRadius();
  192. if (obj2 instanceof L2Character)
  193. rad += ((L2Character) obj2).getTemplate().getCollisionRadius();
  194. double dx = obj1.getX() - obj2.getX();
  195. double dy = obj1.getY() - obj2.getY();
  196. if (includeZAxis)
  197. {
  198. double dz = obj1.getZ() - obj2.getZ();
  199. double d = dx * dx + dy * dy + dz * dz;
  200. return d <= range * range + 2 * range * rad + rad * rad;
  201. }
  202. double d = dx * dx + dy * dy;
  203. return d <= range * range + 2 * range * rad + rad * rad;
  204. }
  205. /**
  206. * Checks if object is within short (sqrt(int.max_value)) radius, not using collisionRadius.
  207. * Faster calculation than checkIfInRange if distance is short and collisionRadius isn't needed.
  208. * Not for long distance checks (potential teleports, far away castles etc).
  209. * @param radius
  210. * @param obj1
  211. * @param obj2
  212. * @param includeZAxis if true, check also Z axis (3-dimensional check), otherwise only 2D
  213. * @return {@code true} if objects are within specified range between each other, {@code false} otherwise
  214. */
  215. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  216. {
  217. if (obj1 == null || obj2 == null)
  218. return false;
  219. if (radius == -1)
  220. return true; // not limited
  221. int dx = obj1.getX() - obj2.getX();
  222. int dy = obj1.getY() - obj2.getY();
  223. if (includeZAxis)
  224. {
  225. int dz = obj1.getZ() - obj2.getZ();
  226. return dx * dx + dy * dy + dz * dz <= radius * radius;
  227. }
  228. return dx * dx + dy * dy <= radius * radius;
  229. }
  230. /**
  231. * @param str - the String to count
  232. * @return the number of "words" in a given string.
  233. */
  234. public static int countWords(String str)
  235. {
  236. return str.trim().split("\\s+").length;
  237. }
  238. /**
  239. * (Based on implode() in PHP)
  240. * @param strArray - an array of strings to concatenate
  241. * @param strDelim - the delimiter to put between the strings
  242. * @return a delimited string for a given array of string elements.
  243. */
  244. public static String implodeString(Iterable<String> strArray, String strDelim)
  245. {
  246. final TextBuilder sbString = TextBuilder.newInstance();
  247. for (String strValue : strArray)
  248. {
  249. sbString.append(strValue);
  250. sbString.append(strDelim);
  251. }
  252. String result = sbString.toString();
  253. TextBuilder.recycle(sbString);
  254. return result;
  255. }
  256. /**
  257. * (Based on round() in PHP)
  258. * @param number - the number to round
  259. * @param numPlaces - how many digits after decimal point to leave intact
  260. * @return the value of {@code number} rounded to specified number of digits after the decimal point.
  261. */
  262. public static float roundTo(float number, int numPlaces)
  263. {
  264. if (numPlaces <= 1)
  265. return Math.round(number);
  266. float exponent = (float) Math.pow(10, numPlaces);
  267. return Math.round(number * exponent) / exponent;
  268. }
  269. /**
  270. * @param text - the text to check
  271. * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
  272. */
  273. public static boolean isDigit(String text)
  274. {
  275. if (text == null || text.isEmpty())
  276. return false;
  277. for (char c : text.toCharArray())
  278. if (!Character.isDigit(c))
  279. return false;
  280. return true;
  281. }
  282. /**
  283. * @param text - the text to check
  284. * @return {@code true} if {@code text} contains only letters and/or numbers, {@code false} otherwise
  285. */
  286. public static boolean isAlphaNumeric(String text)
  287. {
  288. if (text == null || text.isEmpty())
  289. return false;
  290. for (char c : text.toCharArray())
  291. if (!Character.isLetterOrDigit(c))
  292. return false;
  293. return true;
  294. }
  295. /**
  296. * Format the specified digit using the digit grouping symbol "," (comma).
  297. * For example, 123456789 becomes 123,456,789.
  298. * @param amount - the amount of adena
  299. * @return the formatted adena amount
  300. */
  301. public static String formatAdena(long amount)
  302. {
  303. String s = "";
  304. long rem = amount % 1000;
  305. s = Long.toString(rem);
  306. amount = (amount - rem) / 1000;
  307. while (amount > 0)
  308. {
  309. if (rem < 99)
  310. s = '0' + s;
  311. if (rem < 9)
  312. s = '0' + s;
  313. rem = amount % 1000;
  314. s = Long.toString(rem) + "," + s;
  315. amount = (amount - rem) / 1000;
  316. }
  317. return s;
  318. }
  319. /**
  320. * Format the given date on the given format
  321. * @param date : the date to format.
  322. * @param format : the format to correct by.
  323. * @return a string representation of the formatted date.
  324. */
  325. public static String formatDate(Date date, String format)
  326. {
  327. final DateFormat dateFormat = new SimpleDateFormat(format);
  328. if (date != null)
  329. return dateFormat.format(date);
  330. return null;
  331. }
  332. /**
  333. * @param <T>
  334. * @param array - the array to look into
  335. * @param obj - the object to search for
  336. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  337. */
  338. public static <T> boolean contains(T[] array, T obj)
  339. {
  340. for (T element : array)
  341. if (element == obj)
  342. return true;
  343. return false;
  344. }
  345. /**
  346. * @param array - the array to look into
  347. * @param obj - the integer to search for
  348. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  349. */
  350. public static boolean contains(int[] array, int obj)
  351. {
  352. for (int element : array)
  353. if (element == obj)
  354. return true;
  355. return false;
  356. }
  357. public static File[] getDatapackFiles(String dirname, String extention)
  358. {
  359. File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname);
  360. if (!dir.exists())
  361. return null;
  362. return dir.listFiles(new ExtFilter(extention));
  363. }
  364. public static String getDateString(Date date)
  365. {
  366. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  367. return dateFormat.format(date.getTime());
  368. }
  369. }