Util.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. /**
  33. * General Utility functions related to Gameserver
  34. *
  35. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  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. public static double calculateDistance(int x1, int y1, int x2, int y2)
  96. {
  97. return calculateDistance(x1, y1, 0, x2, y2, 0, false);
  98. }
  99. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis)
  100. {
  101. double dx = (double) x1 - x2;
  102. double dy = (double) y1 - y2;
  103. if (includeZAxis)
  104. {
  105. double dz = z1 - z2;
  106. return Math.sqrt((dx * dx) + (dy * dy) + (dz * dz));
  107. }
  108. else
  109. return Math.sqrt((dx * dx) + (dy * dy));
  110. }
  111. public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
  112. {
  113. if (obj1 == null || obj2 == null)
  114. return 1000000;
  115. return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
  116. }
  117. /**
  118. * Capitalizes the first letter of a string, and returns the result.<BR>
  119. * (Based on ucfirst() function of PHP)
  120. *
  121. * @param String str
  122. * @return String containing the modified string.
  123. */
  124. public static String capitalizeFirst(String str)
  125. {
  126. str = str.trim();
  127. if (str.length() > 0 && Character.isLetter(str.charAt(0)))
  128. return str.substring(0, 1).toUpperCase() + str.substring(1);
  129. return str;
  130. }
  131. /**
  132. * Capitalizes the first letter of every "word" in a string.<BR>
  133. * (Based on ucwords() function of PHP)
  134. *
  135. * @param String str
  136. * @return String containing the modified string.
  137. */
  138. public static String capitalizeWords(String str)
  139. {
  140. char[] charArray = str.toCharArray();
  141. String result = "";
  142. // Capitalize the first letter in the given string!
  143. charArray[0] = Character.toUpperCase(charArray[0]);
  144. for (int i = 0; i < charArray.length; i++)
  145. {
  146. if (Character.isWhitespace(charArray[i]))
  147. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  148. result += Character.toString(charArray[i]);
  149. }
  150. return result;
  151. }
  152. /*
  153. * Checks if object is within range, adding collisionRadius
  154. */
  155. public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
  156. {
  157. if (obj1 == null || obj2 == null)
  158. return false;
  159. if (obj1.getInstanceId() != obj2.getInstanceId())
  160. return false;
  161. if (range == -1)
  162. return true; // not limited
  163. int rad = 0;
  164. if (obj1 instanceof L2Character)
  165. rad += ((L2Character) obj1).getTemplate().collisionRadius;
  166. if (obj2 instanceof L2Character)
  167. rad += ((L2Character) obj2).getTemplate().collisionRadius;
  168. double dx = obj1.getX() - obj2.getX();
  169. double dy = obj1.getY() - obj2.getY();
  170. if (includeZAxis)
  171. {
  172. double dz = obj1.getZ() - obj2.getZ();
  173. double d = dx * dx + dy * dy + dz * dz;
  174. return d <= range * range + 2 * range * rad + rad * rad;
  175. }
  176. else
  177. {
  178. double d = dx * dx + dy * dy;
  179. return d <= range * range + 2 * range * rad + rad * rad;
  180. }
  181. }
  182. /*
  183. * Checks if object is within short (sqrt(int.max_value)) radius,
  184. * not using collisionRadius. Faster calculation than checkIfInRange
  185. * if distance is short and collisionRadius isn't needed.
  186. * Not for long distance checks (potential teleports, far away castles etc)
  187. */
  188. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  189. {
  190. if (obj1 == null || obj2 == null)
  191. return false;
  192. if (radius == -1)
  193. return true; // not limited
  194. int dx = obj1.getX() - obj2.getX();
  195. int dy = obj1.getY() - obj2.getY();
  196. if (includeZAxis)
  197. {
  198. int dz = obj1.getZ() - obj2.getZ();
  199. return dx * dx + dy * dy + dz * dz <= radius * radius;
  200. }
  201. else
  202. {
  203. return dx * dx + dy * dy <= radius * radius;
  204. }
  205. }
  206. /**
  207. * Returns the number of "words" in a given string.
  208. *
  209. * @param String str
  210. * @return int numWords
  211. */
  212. public static int countWords(String str)
  213. {
  214. return str.trim().split(" ").length;
  215. }
  216. /**
  217. * Returns a delimited string for an given array of string elements.<BR>
  218. * (Based on implode() in PHP)
  219. *
  220. * @param String[] strArray
  221. * @param String strDelim
  222. * @return String implodedString
  223. */
  224. public static String implodeString(String[] strArray, String strDelim)
  225. {
  226. String result = "";
  227. for (String strValue : strArray)
  228. result += strValue + strDelim;
  229. return result;
  230. }
  231. /**
  232. * Returns a delimited string for an given collection of string elements.<BR>
  233. * (Based on implode() in PHP)
  234. *
  235. * @param Collection&lt;String&gt; strCollection
  236. * @param String strDelim
  237. * @return String implodedString
  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. * Returns the rounded value of val to specified number of digits
  245. * after the decimal point.<BR>
  246. * (Based on round() in PHP)
  247. *
  248. * @param float val
  249. * @param int numPlaces
  250. * @return float roundedVal
  251. */
  252. public static float roundTo(float val, int numPlaces)
  253. {
  254. if (numPlaces <= 1)
  255. return Math.round(val);
  256. float exponent = (float) Math.pow(10, numPlaces);
  257. return (Math.round(val * exponent) / exponent);
  258. }
  259. public static boolean isDigit(String text)
  260. {
  261. if (text == null)
  262. return false;
  263. boolean result = true;
  264. char[] chars = text.toCharArray();
  265. for (int i = 0; i < chars.length; i++)
  266. {
  267. if (!Character.isDigit(chars[i]))
  268. {
  269. result = false;
  270. break;
  271. }
  272. }
  273. return result;
  274. }
  275. public static boolean isAlphaNumeric(String text)
  276. {
  277. if (text == null)
  278. return false;
  279. boolean result = true;
  280. char[] chars = text.toCharArray();
  281. for (int i = 0; i < chars.length; i++)
  282. {
  283. if (!Character.isLetterOrDigit(chars[i]))
  284. {
  285. result = false;
  286. break;
  287. }
  288. }
  289. return result;
  290. }
  291. /**
  292. * Return amount of adena formatted with "," delimiter
  293. * @param amount
  294. * @return String formatted adena amount
  295. */
  296. public static String formatAdena(long amount)
  297. {
  298. String s = "";
  299. long rem = amount % 1000;
  300. s = Long.toString(rem);
  301. amount = (amount - rem) / 1000;
  302. while (amount > 0)
  303. {
  304. if (rem < 99)
  305. s = '0' + s;
  306. if (rem < 9)
  307. s = '0' + s;
  308. rem = amount % 1000;
  309. s = Long.toString(rem) + "," + s;
  310. amount = (amount - rem) / 1000;
  311. }
  312. return s;
  313. }
  314. public static <T> boolean contains(T[] array, T obj)
  315. {
  316. for (int i = 0; i < array.length; i++)
  317. {
  318. if (array[i] == obj)
  319. {
  320. return true;
  321. }
  322. }
  323. return false;
  324. }
  325. public static boolean contains(int[] array, int obj)
  326. {
  327. for (int i = 0; i < array.length; i++)
  328. {
  329. if (array[i] == obj)
  330. {
  331. return true;
  332. }
  333. }
  334. return false;
  335. }
  336. }