Guest User

Untitled

a guest
Jan 1st, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  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. * L2jFrozen Project - www.l2jfrozen.com
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2, or (at your option)
  17. * any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * http://www.gnu.org/copyleft/gpl.html
  30. */
  31. package com.l2jfrozen.gameserver.util;
  32.  
  33. import java.io.File;
  34. import java.util.Collection;
  35.  
  36. import com.l2jfrozen.gameserver.model.L2Character;
  37. import com.l2jfrozen.gameserver.model.L2Object;
  38. import com.l2jfrozen.gameserver.model.actor.instance.L2NpcInstance;
  39. import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  40. import com.l2jfrozen.gameserver.thread.ThreadPoolManager;
  41.  
  42. /**
  43. * General Utility functions related to Gameserver
  44. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  45. */
  46. public final class Util
  47. {
  48.  
  49. public static void handleIllegalPlayerAction(final L2PcInstance actor, final String message, final int punishment)
  50. {
  51. ThreadPoolManager.getInstance().scheduleGeneral(new IllegalPlayerAction(actor, message, punishment), 5000);
  52. }
  53.  
  54. public static String getRelativePath(final File base, final File file)
  55. {
  56. return file.toURI().getPath().substring(base.toURI().getPath().length());
  57. }
  58.  
  59. /**
  60. * @param obj1
  61. * @param obj2
  62. * @return degree value of object 2 to the horizontal line with object 1 being the origin
  63. */
  64. public static double calculateAngleFrom(final L2Object obj1, final L2Object obj2)
  65. {
  66. return calculateAngleFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  67. }
  68.  
  69. /**
  70. * @param obj1X
  71. * @param obj1Y
  72. * @param obj2X
  73. * @param obj2Y
  74. * @return degree value of object 2 to the horizontal line with object 1 being the origin
  75. */
  76. public static double calculateAngleFrom(final int obj1X, final int obj1Y, final int obj2X, final int obj2Y)
  77. {
  78. double angleTarget = Math.toDegrees(Math.atan2(obj1Y - obj2Y, obj1X - obj2X));
  79. if (angleTarget <= 0)
  80. {
  81. angleTarget += 360;
  82. }
  83. return angleTarget;
  84. }
  85.  
  86. public static double calculateDistance(final int x1, final int y1, final int z1, final int x2, final int y2)
  87. {
  88. return calculateDistance(x1, y1, 0, x2, y2, 0, false);
  89. }
  90.  
  91. public static double calculateDistance(final int x1, final int y1, final int z1, final int x2, final int y2, final int z2, final boolean includeZAxis)
  92. {
  93. final double dx = (double) x1 - x2;
  94. final double dy = (double) y1 - y2;
  95.  
  96. if (includeZAxis)
  97. {
  98. final double dz = z1 - z2;
  99. return Math.sqrt(dx * dx + dy * dy + dz * dz);
  100. }
  101. return Math.sqrt(dx * dx + dy * dy);
  102. }
  103.  
  104. public static double calculateDistance(final L2Object obj1, final L2Object obj2, final boolean includeZAxis)
  105. {
  106. if (obj1 == null || obj2 == null)
  107. return 1000000;
  108. return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
  109. }
  110.  
  111. /**
  112. * Capitalizes the first letter of a string, and returns the result.<BR>
  113. * (Based on ucfirst() function of PHP)
  114. * @param str
  115. * @return String containing the modified string.
  116. */
  117. public static String capitalizeFirst(String str)
  118. {
  119. str = str.trim();
  120.  
  121. if (str.length() > 0 && Character.isLetter(str.charAt(0)))
  122. return str.substring(0, 1).toUpperCase() + str.substring(1);
  123.  
  124. return str;
  125. }
  126.  
  127. /**
  128. * Capitalizes the first letter of every "word" in a string.<BR>
  129. * (Based on ucwords() function of PHP)
  130. * @param str
  131. * @return String containing the modified string.
  132. */
  133. public static String capitalizeWords(final String str)
  134. {
  135. final char[] charArray = str.toCharArray();
  136. String result = "";
  137.  
  138. // Capitalize the first letter in the given string!
  139. charArray[0] = Character.toUpperCase(charArray[0]);
  140.  
  141. for (int i = 0; i < charArray.length; i++)
  142. {
  143. if (Character.isWhitespace(charArray[i]))
  144. {
  145. charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
  146. }
  147.  
  148. result += Character.toString(charArray[i]);
  149. }
  150.  
  151. return result;
  152. }
  153.  
  154. // Micht: Removed this because UNUSED
  155. /*
  156. * public static boolean checkIfInRange(int range, int x1, int y1, int x2, int y2) { return checkIfInRange(range, x1, y1, 0, x2, y2, 0, false); } public static boolean checkIfInRange(int range, int x1, int y1, int z1, int x2, int y2, int z2, boolean includeZAxis) { if (includeZAxis) { return
  157. * ((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2)) <= range * range; } else { return ((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)) <= range * range; } } public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis) { if (obj1 == null || obj2
  158. * == null) return false; return checkIfInRange(range, obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis); }
  159. */
  160. public static boolean checkIfInRange(final int range, final L2Object obj1, final L2Object obj2, final boolean includeZAxis)
  161. {
  162. if (obj1 == null || obj2 == null)
  163. return false;
  164. if (range == -1)
  165. return true; // not limited
  166.  
  167. int rad = 0;
  168. if (obj1 instanceof L2Character)
  169. {
  170. rad += ((L2Character) obj1).getTemplate().collisionRadius;
  171. }
  172. if (obj2 instanceof L2Character)
  173. {
  174. rad += ((L2Character) obj2).getTemplate().collisionRadius;
  175. }
  176.  
  177. final double dx = obj1.getX() - obj2.getX();
  178. final double dy = obj1.getY() - obj2.getY();
  179.  
  180. if (includeZAxis)
  181. {
  182. final double dz = obj1.getZ() - obj2.getZ();
  183. final double d = dx * dx + dy * dy + dz * dz;
  184.  
  185. return d <= range * range + 2 * range * rad + rad * rad;
  186. }
  187. final double d = dx * dx + dy * dy;
  188.  
  189. return d <= range * range + 2 * range * rad + rad * rad;
  190. }
  191.  
  192. public static double convertHeadingToDegree(final int heading)
  193. {
  194. if (heading == 0)
  195. return 360D;
  196. return 9.0D * heading / 1610.0D; // = 360.0 * (heading / 64400.0)
  197. }
  198.  
  199. /**
  200. * Returns the number of "words" in a given string.
  201. * @param str
  202. * @return int numWords
  203. */
  204. public static int countWords(final String str)
  205. {
  206. return str.trim().split(" ").length;
  207. }
  208.  
  209. /**
  210. * Returns a delimited string for an given array of string elements.<BR>
  211. * (Based on implode() in PHP)
  212. * @param strArray
  213. * @param strDelim
  214. * @return String implodedString
  215. */
  216. public static String implodeString(final String[] strArray, final String strDelim)
  217. {
  218. String result = "";
  219.  
  220. for (final String strValue : strArray)
  221. {
  222. result += strValue + strDelim;
  223. }
  224.  
  225. return result;
  226. }
  227.  
  228. /**
  229. * Returns a delimited string for an given collection of string elements.<BR>
  230. * (Based on implode() in PHP)
  231. * @param strCollection
  232. * @param strDelim
  233. * @return String implodedString
  234. */
  235. public static String implodeString(final Collection<String> strCollection, final String strDelim)
  236. {
  237. return implodeString(strCollection.toArray(new String[strCollection.size()]), strDelim);
  238. }
  239.  
  240. /**
  241. * Returns the rounded value of val to specified number of digits after the decimal point.<BR>
  242. * (Based on round() in PHP)
  243. * @param val
  244. * @param numPlaces
  245. * @return float roundedVal
  246. */
  247. public static float roundTo(final float val, final int numPlaces)
  248. {
  249. if (numPlaces <= 1)
  250. return Math.round(val);
  251.  
  252. final float exponent = (float) Math.pow(10, numPlaces);
  253.  
  254. return Math.round(val * exponent) / exponent;
  255. }
  256.  
  257. public static boolean isAlphaNumeric(final String text)
  258. {
  259. boolean result = true;
  260. final char[] chars = text.toCharArray();
  261. for (final char aChar : chars)
  262. {
  263. if (!Character.isLetterOrDigit(aChar))
  264. {
  265. result = false;
  266. break;
  267. }
  268. }
  269. return result;
  270. }
  271.  
  272. /**
  273. * Return amount of adena formatted with "," delimiter
  274. * @param amount
  275. * @return String formatted adena amount
  276. */
  277. public static String formatAdena(int amount)
  278. {
  279. String s = "";
  280. int rem = amount % 1000;
  281. s = Integer.toString(rem);
  282. amount = (amount - rem) / 1000;
  283. while (amount > 0)
  284. {
  285. if (rem < 99)
  286. {
  287. s = '0' + s;
  288. }
  289. if (rem < 9)
  290. {
  291. s = '0' + s;
  292. }
  293. rem = amount % 1000;
  294. s = Integer.toString(rem) + "," + s;
  295. amount = (amount - rem) / 1000;
  296. }
  297. return s;
  298. }
  299.  
  300. public static String reverseColor(final String color)
  301. {
  302. final char[] ch1 = color.toCharArray();
  303. final char[] ch2 = new char[6];
  304. ch2[0] = ch1[4];
  305. ch2[1] = ch1[5];
  306. ch2[2] = ch1[2];
  307. ch2[3] = ch1[3];
  308. ch2[4] = ch1[0];
  309. ch2[5] = ch1[1];
  310. return new String(ch2);
  311. }
  312.  
  313. /**
  314. * converts a given time from minutes -> miliseconds
  315. * @param minutesToConvert
  316. * @return
  317. */
  318. public static int convertMinutesToMiliseconds(final int minutesToConvert)
  319. {
  320. return minutesToConvert * 60000;
  321. }
  322.  
  323. public static int calculateHeadingFrom(final L2Object obj1, final L2Object obj2)
  324. {
  325. return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  326. }
  327.  
  328. public static int calculateHeadingFrom(final int obj1X, final int obj1Y, final int obj2X, final int obj2Y)
  329. {
  330. return (int) (Math.atan2(obj1Y - obj2Y, obj1X - obj2X) * 10430.379999999999D + 32768.0D);
  331. }
  332.  
  333. public static final int calculateHeadingFrom(final double dx, final double dy)
  334. {
  335. double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
  336. if (angleTarget < 0.0D)
  337. angleTarget = 360.0D + angleTarget;
  338. return (int) (angleTarget * 182.04444444399999D);
  339. }
  340.  
  341. public static int calcCameraAngle(final int heading)
  342. {
  343. int angle;
  344. // int angle;
  345. if (heading == 0)
  346. angle = 360;
  347. else
  348. {
  349. angle = (int) (heading / 182.03999999999999D);
  350. }
  351. if (angle <= 90)
  352. angle += 90;
  353. else if ((angle > 90) && (angle <= 180))
  354. angle -= 90;
  355. else if ((angle > 180) && (angle <= 270))
  356. angle += 90;
  357. else if ((angle > 270) && (angle <= 360))
  358. {
  359. angle -= 90;
  360. }
  361. return angle;
  362. }
  363.  
  364. public static int calcCameraAngle(final L2NpcInstance target)
  365. {
  366. return calcCameraAngle(target.getHeading());
  367. }
  368.  
  369. public static boolean contains(final int[] array, final int obj)
  370. {
  371. for (final int anArray : array)
  372. {
  373. if (anArray == obj)
  374. {
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380.  
  381. }
Advertisement
Add Comment
Please, Sign In to add comment