Util.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * $Header: Util.java, 21/10/2005 23:17:40 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 21/10/2005 23:17:40 $
  6. * $Revision: 1 $
  7. * $Log: Util.java,v $
  8. * Revision 1 21/10/2005 23:17:40 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package com.l2jserver.gameserver.util;
  26. import java.io.File;
  27. import java.util.Collection;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.model.L2Object;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. /**
  34. * General Utility functions related to Gameserver
  35. */
  36. public final class Util
  37. {
  38. public static void handleIllegalPlayerAction(L2PcInstance actor, String message, int punishment)
  39. {
  40. ThreadPoolManager.getInstance().scheduleGeneral(new IllegalPlayerAction(actor, message, punishment), 5000);
  41. }
  42. public static String getRelativePath(File base, File file)
  43. {
  44. return file.toURI().getPath().substring(base.toURI().getPath().length());
  45. }
  46. /**
  47. * Return degree value of object 2 to the horizontal line with object 1
  48. * being the origin
  49. */
  50. public static double calculateAngleFrom(L2Object obj1, L2Object obj2)
  51. {
  52. return calculateAngleFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  53. }
  54. /**
  55. * Return degree value of object 2 to the horizontal line with object 1
  56. * being the origin
  57. */
  58. public final static double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  59. {
  60. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  61. if (angleTarget < 0)
  62. angleTarget = 360 + angleTarget;
  63. return angleTarget;
  64. }
  65. public final static double convertHeadingToDegree(int clientHeading)
  66. {
  67. double degree = clientHeading / 182.044444444;
  68. return degree;
  69. }
  70. public final static int convertDegreeToClientHeading(double degree)
  71. {
  72. if (degree < 0)
  73. degree = 360 + degree;
  74. return (int) (degree * 182.044444444);
  75. }
  76. public final static int calculateHeadingFrom(L2Object obj1, L2Object obj2)
  77. {
  78. return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  79. }
  80. public final static int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  81. {
  82. double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
  83. if (angleTarget < 0)
  84. angleTarget = 360 + angleTarget;
  85. return (int) (angleTarget * 182.044444444);
  86. }
  87. public final static int calculateHeadingFrom(double dx, double dy)
  88. {
  89. double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
  90. if (angleTarget < 0)
  91. angleTarget = 360 + angleTarget;
  92. return (int) (angleTarget * 182.044444444);
  93. }
  94. /**
  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 includeZAxis - if true, includes also the Z axis in the calculation
  103. * @return the distance between the two coordinates
  104. */
  105. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis)
  106. {
  107. double dx = (double) x1 - x2;
  108. double dy = (double) y1 - y2;
  109. if (includeZAxis)
  110. {
  111. double dz = z1 - z2;
  112. return Math.sqrt(dx * dx + dy * dy + dz * dz);
  113. }
  114. else
  115. return Math.sqrt(dx * dx + dy * dy);
  116. }
  117. /**
  118. * @param includeZAxis - if true, includes also the Z axis in the calculation
  119. * @return the distance between the two objects
  120. */
  121. public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
  122. {
  123. if (obj1 == null || obj2 == null)
  124. return 1000000;
  125. return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
  126. }
  127. /**
  128. * (Based on ucfirst() function of PHP)
  129. *
  130. * @param str - the string whose first letter to capitalize
  131. * @return a string with the first letter of the {@code str} capitalized
  132. */
  133. public static String capitalizeFirst(String str)
  134. {
  135. str = str.trim();
  136. if (str.length() > 0 && Character.isLetter(str.charAt(0)))
  137. return str.substring(0, 1).toUpperCase() + str.substring(1);
  138. return str;
  139. }
  140. /**
  141. * (Based on ucwords() function of PHP)
  142. *
  143. * @param str - the string to capitalize
  144. * @return a string with the first letter of every word in {@code str} capitalized
  145. */
  146. public static String capitalizeWords(String str)
  147. {
  148. char[] charArray = str.toCharArray();
  149. StringBuilder result = new StringBuilder();
  150. // Capitalize the first letter in the given string!
  151. charArray[0] = Character.toUpperCase(charArray[0]);
  152. for (int i = 0; i < charArray.length; i++)
  153. {
  154. if (Character.isWhitespace(charArray[i]))
  155. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  156. result.append(charArray[i]);
  157. }
  158. return result.toString();
  159. }
  160. /**
  161. * @return {@code true} if the two objects are within specified range between each other, {@code false} otherwise
  162. */
  163. public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
  164. {
  165. if (obj1 == null || obj2 == null)
  166. return false;
  167. if (obj1.getInstanceId() != obj2.getInstanceId())
  168. return false;
  169. if (range == -1)
  170. return true; // not limited
  171. int rad = 0;
  172. if (obj1 instanceof L2Character)
  173. rad += ((L2Character) obj1).getTemplate().collisionRadius;
  174. if (obj2 instanceof L2Character)
  175. rad += ((L2Character) obj2).getTemplate().collisionRadius;
  176. double dx = obj1.getX() - obj2.getX();
  177. double dy = obj1.getY() - obj2.getY();
  178. if (includeZAxis)
  179. {
  180. double dz = obj1.getZ() - obj2.getZ();
  181. double d = dx * dx + dy * dy + dz * dz;
  182. return d <= range * range + 2 * range * rad + rad * rad;
  183. }
  184. else
  185. {
  186. double d = dx * dx + dy * dy;
  187. return d <= range * range + 2 * range * rad + rad * rad;
  188. }
  189. }
  190. /**
  191. * Checks if object is within short (sqrt(int.max_value)) radius, not using collisionRadius.
  192. * Faster calculation than checkIfInRange if distance is short and collisionRadius isn't needed.
  193. * Not for long distance checks (potential teleports, far away castles etc).
  194. * @param range - the maximum range between the two objects
  195. * @param includeZAxis - if true, check also Z axis (3-dimensional check), otherwise only 2D
  196. * @return {@code true} if objects are within specified range between each other, {@code false} otherwise
  197. */
  198. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  199. {
  200. if (obj1 == null || obj2 == null)
  201. return false;
  202. if (radius == -1)
  203. return true; // not limited
  204. int dx = obj1.getX() - obj2.getX();
  205. int dy = obj1.getY() - obj2.getY();
  206. if (includeZAxis)
  207. {
  208. int dz = obj1.getZ() - obj2.getZ();
  209. return dx * dx + dy * dy + dz * dz <= radius * radius;
  210. }
  211. else
  212. return dx * dx + dy * dy <= radius * radius;
  213. }
  214. /**
  215. * @param str - the String to count
  216. * @return the number of "words" in a given string.
  217. */
  218. public static int countWords(String str)
  219. {
  220. return str.trim().split("\\s+").length;
  221. }
  222. /**
  223. * (Based on implode() in PHP)
  224. * @param strArray - an array of strings to concatenate
  225. * @param strDelim - the delimiter to put between the strings
  226. * @return a delimited string for a given array of string elements.
  227. */
  228. public static String implodeString(String[] strArray, String strDelim)
  229. {
  230. StringBuilder result = new StringBuilder();
  231. for (String strValue : strArray)
  232. {
  233. result.append(strValue);
  234. result.append(strDelim);
  235. }
  236. return result.toString();
  237. }
  238. /**
  239. * @param strCollection - a collection of strings to concatenate
  240. * @param strDelim - the delimiter to put between the strings
  241. * @see #implodeString(String[] strArray, String strDelim)
  242. */
  243. public static String implodeString(Collection<String> strCollection, String strDelim)
  244. {
  245. return implodeString(strCollection.toArray(new String[strCollection.size()]), strDelim);
  246. }
  247. /**
  248. * (Based on round() in PHP)
  249. * @param number - the number to round
  250. * @param numPlaces - how many digits after decimal point to leave intact
  251. * @return the value of {@code number} rounded to specified number of digits after the decimal point.
  252. */
  253. public static float roundTo(float number, int numPlaces)
  254. {
  255. if (numPlaces <= 1)
  256. return Math.round(number);
  257. float exponent = (float) Math.pow(10, numPlaces);
  258. return Math.round(number * exponent) / exponent;
  259. }
  260. /**
  261. * @param text - the text to check
  262. * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
  263. */
  264. public static boolean isDigit(String text)
  265. {
  266. if (text == null)
  267. return false;
  268. return text.matches("[0-9]+");
  269. }
  270. /**
  271. * @param text - the text to check
  272. * @return {@code true} if {@code text} contains only letters and/or numbers, {@code false} otherwise
  273. */
  274. public static boolean isAlphaNumeric(String text)
  275. {
  276. if (text == null || text.isEmpty())
  277. return false;
  278. for (char c : text.toCharArray())
  279. if (!Character.isLetterOrDigit(c))
  280. return false;
  281. return true;
  282. }
  283. /**
  284. * Format the specified digit using the digit grouping symbol "," (comma).
  285. * For example, 123456789 becomes 123,456,789.
  286. * @param amount - the amount of adena
  287. * @return the formatted adena amount
  288. */
  289. public static String formatAdena(long amount)
  290. {
  291. String s = "";
  292. long rem = amount % 1000;
  293. s = Long.toString(rem);
  294. amount = (amount - rem) / 1000;
  295. while (amount > 0)
  296. {
  297. if (rem < 99)
  298. s = '0' + s;
  299. if (rem < 9)
  300. s = '0' + s;
  301. rem = amount % 1000;
  302. s = Long.toString(rem) + "," + s;
  303. amount = (amount - rem) / 1000;
  304. }
  305. return s;
  306. }
  307. /**
  308. * @param array - the array to look into
  309. * @param obj - the object to search for
  310. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  311. */
  312. public static <T> boolean contains(T[] array, T obj)
  313. {
  314. for (T element : array)
  315. if (element == obj)
  316. return true;
  317. return false;
  318. }
  319. /**
  320. * @param array - the array to look into
  321. * @param obj - the integer to search for
  322. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  323. */
  324. public static boolean contains(int[] array, int obj)
  325. {
  326. for (int element : array)
  327. if (element == obj)
  328. return true;
  329. return false;
  330. }
  331. public static File[] getDatapackFiles(String dirname, String extention)
  332. {
  333. File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname);
  334. if (!dir.exists())
  335. return null;
  336. CustomFileNameFilter filter = new CustomFileNameFilter(extention);
  337. return dir.listFiles(filter);
  338. }
  339. }