Util.java 11 KB

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