Util.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. 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. double d = dx * dx + dy * dy;
  185. return d <= range * range + 2 * range * rad + rad * rad;
  186. }
  187. /**
  188. * Checks if object is within short (sqrt(int.max_value)) radius, not using collisionRadius.
  189. * Faster calculation than checkIfInRange if distance is short and collisionRadius isn't needed.
  190. * Not for long distance checks (potential teleports, far away castles etc).
  191. * @param range - the maximum range between the two objects
  192. * @param includeZAxis - if true, check also Z axis (3-dimensional check), otherwise only 2D
  193. * @return {@code true} if objects are within specified range between each other, {@code false} otherwise
  194. */
  195. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  196. {
  197. if (obj1 == null || obj2 == null)
  198. return false;
  199. if (radius == -1)
  200. return true; // not limited
  201. int dx = obj1.getX() - obj2.getX();
  202. int dy = obj1.getY() - obj2.getY();
  203. if (includeZAxis)
  204. {
  205. int dz = obj1.getZ() - obj2.getZ();
  206. return dx * dx + dy * dy + dz * dz <= radius * radius;
  207. }
  208. return dx * dx + dy * dy <= radius * radius;
  209. }
  210. /**
  211. * @param str - the String to count
  212. * @return the number of "words" in a given string.
  213. */
  214. public static int countWords(String str)
  215. {
  216. return str.trim().split("\\s+").length;
  217. }
  218. /**
  219. * (Based on implode() in PHP)
  220. * @param strArray - an array of strings to concatenate
  221. * @param strDelim - the delimiter to put between the strings
  222. * @return a delimited string for a given array of string elements.
  223. */
  224. public static String implodeString(String[] strArray, String strDelim)
  225. {
  226. StringBuilder result = new StringBuilder();
  227. for (String strValue : strArray)
  228. {
  229. result.append(strValue);
  230. result.append(strDelim);
  231. }
  232. return result.toString();
  233. }
  234. /**
  235. * @param strCollection - a collection of strings to concatenate
  236. * @param strDelim - the delimiter to put between the strings
  237. * @see #implodeString(String[] strArray, String strDelim)
  238. */
  239. public static String implodeString(Collection<String> strCollection, String strDelim)
  240. {
  241. return implodeString(strCollection.toArray(new String[strCollection.size()]), strDelim);
  242. }
  243. /**
  244. * (Based on round() in PHP)
  245. * @param number - the number to round
  246. * @param numPlaces - how many digits after decimal point to leave intact
  247. * @return the value of {@code number} rounded to specified number of digits after the decimal point.
  248. */
  249. public static float roundTo(float number, int numPlaces)
  250. {
  251. if (numPlaces <= 1)
  252. return Math.round(number);
  253. float exponent = (float) Math.pow(10, numPlaces);
  254. return Math.round(number * exponent) / exponent;
  255. }
  256. /**
  257. * @param text - the text to check
  258. * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
  259. */
  260. public static boolean isDigit(String text)
  261. {
  262. if (text == null)
  263. return false;
  264. return text.matches("[0-9]+");
  265. }
  266. /**
  267. * @param text - the text to check
  268. * @return {@code true} if {@code text} contains only letters and/or numbers, {@code false} otherwise
  269. */
  270. public static boolean isAlphaNumeric(String text)
  271. {
  272. if (text == null || text.isEmpty())
  273. return false;
  274. for (char c : text.toCharArray())
  275. if (!Character.isLetterOrDigit(c))
  276. return false;
  277. return true;
  278. }
  279. /**
  280. * Format the specified digit using the digit grouping symbol "," (comma).
  281. * For example, 123456789 becomes 123,456,789.
  282. * @param amount - the amount of adena
  283. * @return the formatted adena amount
  284. */
  285. public static String formatAdena(long amount)
  286. {
  287. String s = "";
  288. long rem = amount % 1000;
  289. s = Long.toString(rem);
  290. amount = (amount - rem) / 1000;
  291. while (amount > 0)
  292. {
  293. if (rem < 99)
  294. s = '0' + s;
  295. if (rem < 9)
  296. s = '0' + s;
  297. rem = amount % 1000;
  298. s = Long.toString(rem) + "," + s;
  299. amount = (amount - rem) / 1000;
  300. }
  301. return s;
  302. }
  303. /**
  304. * @param array - the array to look into
  305. * @param obj - the object to search for
  306. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  307. */
  308. public static <T> boolean contains(T[] array, T obj)
  309. {
  310. for (T element : array)
  311. if (element == obj)
  312. return true;
  313. return false;
  314. }
  315. /**
  316. * @param array - the array to look into
  317. * @param obj - the integer to search for
  318. * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
  319. */
  320. public static boolean contains(int[] array, int obj)
  321. {
  322. for (int element : array)
  323. if (element == obj)
  324. return true;
  325. return false;
  326. }
  327. public static File[] getDatapackFiles(String dirname, String extention)
  328. {
  329. File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname);
  330. if (!dir.exists())
  331. return null;
  332. return dir.listFiles(new ExtFilter(extention));
  333. }
  334. }