Util.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 net.sf.l2j.gameserver.util;
  26. import java.io.File;
  27. import java.util.Collection;
  28. import net.sf.l2j.gameserver.ThreadPoolManager;
  29. import net.sf.l2j.gameserver.model.L2Character;
  30. import net.sf.l2j.gameserver.model.L2Object;
  31. import net.sf.l2j.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. /** Return degree value of object 2 to the horizontal line with object 1 being the origin */
  48. public static double calculateAngleFrom(L2Object obj1, L2Object obj2) { return calculateAngleFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY()); }
  49. /** Return degree value of object 2 to the horizontal line with object 1 being the origin */
  50. public static double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  51. {
  52. double angleTarget = Math.toDegrees(Math.atan2(obj1Y - obj2Y, obj1X - obj2X));
  53. if (angleTarget < 0) angleTarget += 360;
  54. return angleTarget;
  55. }
  56. public static double convertHeadingToDegree(int clientHeading)
  57. {
  58. double degree = (clientHeading / 182.04) - 180;
  59. if (degree < 0) degree += 360;
  60. return degree;
  61. }
  62. public static int convertDegreeToClientHeading(double degree)
  63. {
  64. if (degree > 180) degree -= 360;
  65. return (int)((degree + 180)*182.04);
  66. }
  67. public static int calculateHeadingFrom(L2Object obj1, L2Object obj2) { return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY()); }
  68. public static int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
  69. {
  70. return (int)(Math.atan2(obj1Y - obj2Y, obj1X - obj2X)*10430.38 + 32768);
  71. }
  72. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2) { return calculateDistance(x1, y1, 0, x2, y2, 0, false); }
  73. public static double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis)
  74. {
  75. double dx = (double)x1 - x2;
  76. double dy = (double)y1 - y2;
  77. if (includeZAxis)
  78. {
  79. double dz = z1 - z2;
  80. return Math.sqrt((dx*dx) + (dy*dy) + (dz*dz));
  81. }
  82. else
  83. return Math.sqrt((dx*dx) + (dy*dy));
  84. }
  85. public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
  86. {
  87. if (obj1 == null || obj2 == null) return 1000000;
  88. return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
  89. }
  90. /**
  91. * Capitalizes the first letter of a string, and returns the result.<BR>
  92. * (Based on ucfirst() function of PHP)
  93. *
  94. * @param String str
  95. * @return String containing the modified string.
  96. */
  97. public static String capitalizeFirst(String str)
  98. {
  99. str = str.trim();
  100. if (str.length() > 0 && Character.isLetter(str.charAt(0)))
  101. return str.substring(0, 1).toUpperCase() + str.substring(1);
  102. return str;
  103. }
  104. /**
  105. * Capitalizes the first letter of every "word" in a string.<BR>
  106. * (Based on ucwords() function of PHP)
  107. *
  108. * @param String str
  109. * @return String containing the modified string.
  110. */
  111. public static String capitalizeWords(String str)
  112. {
  113. char[] charArray = str.toCharArray();
  114. String result = "";
  115. // Capitalize the first letter in the given string!
  116. charArray[0] = Character.toUpperCase(charArray[0]);
  117. for (int i = 0; i < charArray.length; i++)
  118. {
  119. if (Character.isWhitespace(charArray[i]))
  120. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  121. result += Character.toString(charArray[i]);
  122. }
  123. return result;
  124. }
  125. /*
  126. * Checks if object is within range, adding collisionRadius
  127. */
  128. public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
  129. {
  130. if (obj1 == null || obj2 == null) return false;
  131. if (range == -1) return true; // not limited
  132. int rad = 0;
  133. if(obj1 instanceof L2Character)
  134. rad += ((L2Character)obj1).getTemplate().collisionRadius;
  135. if(obj2 instanceof L2Character)
  136. rad += ((L2Character)obj2).getTemplate().collisionRadius;
  137. double dx = obj1.getX() - obj2.getX();
  138. double dy = obj1.getY() - obj2.getY();
  139. if (includeZAxis)
  140. {
  141. double dz = obj1.getZ() - obj2.getZ();
  142. double d = dx*dx + dy*dy +dz*dz;
  143. return d <= range*range + 2*range*rad + rad*rad;
  144. }
  145. else
  146. {
  147. double d = dx*dx + dy*dy;
  148. return d <= range*range + 2*range*rad +rad*rad;
  149. }
  150. }
  151. /*
  152. * Checks if object is within short (sqrt(int.max_value)) radius,
  153. * not using collisionRadius. Faster calculation than checkIfInRange
  154. * if distance is short and collisionRadius isn't needed.
  155. * Not for long distance checks (potential teleports, far away castles etc)
  156. */
  157. public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
  158. {
  159. if (obj1 == null || obj2 == null) return false;
  160. if (radius == -1) return true; // not limited
  161. int dx = obj1.getX() - obj2.getX();
  162. int dy = obj1.getY() - obj2.getY();
  163. if (includeZAxis)
  164. {
  165. int dz = obj1.getZ() - obj2.getZ();
  166. return dx*dx + dy*dy + dz*dz <= radius*radius;
  167. }
  168. else
  169. {
  170. return dx*dx + dy*dy <= radius*radius;
  171. }
  172. }
  173. /**
  174. * Returns the number of "words" in a given string.
  175. *
  176. * @param String str
  177. * @return int numWords
  178. */
  179. public static int countWords(String str)
  180. {
  181. return str.trim().split(" ").length;
  182. }
  183. /**
  184. * Returns a delimited string for an given array of string elements.<BR>
  185. * (Based on implode() in PHP)
  186. *
  187. * @param String[] strArray
  188. * @param String strDelim
  189. * @return String implodedString
  190. */
  191. public static String implodeString(String[] strArray, String strDelim)
  192. {
  193. String result = "";
  194. for (String strValue : strArray)
  195. result += strValue + strDelim;
  196. return result;
  197. }
  198. /**
  199. * Returns a delimited string for an given collection of string elements.<BR>
  200. * (Based on implode() in PHP)
  201. *
  202. * @param Collection&lt;String&gt; strCollection
  203. * @param String strDelim
  204. * @return String implodedString
  205. */
  206. public static String implodeString(Collection<String> strCollection, String strDelim)
  207. {
  208. return implodeString(strCollection.toArray(new String[strCollection.size()]), strDelim);
  209. }
  210. /**
  211. * Returns the rounded value of val to specified number of digits
  212. * after the decimal point.<BR>
  213. * (Based on round() in PHP)
  214. *
  215. * @param float val
  216. * @param int numPlaces
  217. * @return float roundedVal
  218. */
  219. public static float roundTo(float val, int numPlaces)
  220. {
  221. if (numPlaces <= 1)
  222. return Math.round(val);
  223. float exponent = (float) Math.pow(10, numPlaces);
  224. return (Math.round(val * exponent) / exponent);
  225. }
  226. public static boolean isAlphaNumeric(String text)
  227. {
  228. if (text == null) return false;
  229. boolean result = true;
  230. char[] chars = text.toCharArray();
  231. for (int i = 0; i < chars.length; i++)
  232. {
  233. if (!Character.isLetterOrDigit(chars[i]))
  234. {
  235. result = false;
  236. break;
  237. }
  238. }
  239. return result;
  240. }
  241. /**
  242. * Return amount of adena formatted with "," delimiter
  243. * @param amount
  244. * @return String formatted adena amount
  245. */
  246. public static String formatAdena(int amount) {
  247. String s = "";
  248. int rem = amount % 1000;
  249. s = Integer.toString(rem);
  250. amount = (amount - rem)/1000;
  251. while (amount > 0) {
  252. if (rem < 99)
  253. s = '0' + s;
  254. if (rem < 9)
  255. s = '0' + s;
  256. rem = amount % 1000;
  257. s = Integer.toString(rem) + "," + s;
  258. amount = (amount - rem)/1000;
  259. }
  260. return s;
  261. }
  262. }