|
@@ -42,8 +42,8 @@ public final class Util
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param obj1
|
|
|
- * @param obj2
|
|
|
+ * @param obj1
|
|
|
+ * @param obj2
|
|
|
* @return degree value of object 2 to the horizontal line with object 1 being the origin.
|
|
|
*/
|
|
|
public static double calculateAngleFrom(L2Object obj1, L2Object obj2)
|
|
@@ -52,17 +52,19 @@ public final class Util
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param obj1X
|
|
|
- * @param obj1Y
|
|
|
- * @param obj2X
|
|
|
- * @param obj2Y
|
|
|
+ * @param obj1X
|
|
|
+ * @param obj1Y
|
|
|
+ * @param obj2X
|
|
|
+ * @param obj2Y
|
|
|
* @return degree value of object 2 to the horizontal line with object 1 being the origin
|
|
|
*/
|
|
|
public final static double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
|
|
|
{
|
|
|
double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
|
|
|
if (angleTarget < 0)
|
|
|
+ {
|
|
|
angleTarget = 360 + angleTarget;
|
|
|
+ }
|
|
|
return angleTarget;
|
|
|
}
|
|
|
|
|
@@ -75,7 +77,9 @@ public final class Util
|
|
|
public final static int convertDegreeToClientHeading(double degree)
|
|
|
{
|
|
|
if (degree < 0)
|
|
|
+ {
|
|
|
degree = 360 + degree;
|
|
|
+ }
|
|
|
return (int) (degree * 182.044444444);
|
|
|
}
|
|
|
|
|
@@ -88,7 +92,9 @@ public final class Util
|
|
|
{
|
|
|
double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
|
|
|
if (angleTarget < 0)
|
|
|
+ {
|
|
|
angleTarget = 360 + angleTarget;
|
|
|
+ }
|
|
|
return (int) (angleTarget * 182.044444444);
|
|
|
}
|
|
|
|
|
@@ -96,15 +102,17 @@ public final class Util
|
|
|
{
|
|
|
double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
|
|
|
if (angleTarget < 0)
|
|
|
+ {
|
|
|
angleTarget = 360 + angleTarget;
|
|
|
+ }
|
|
|
return (int) (angleTarget * 182.044444444);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param x1
|
|
|
- * @param y1
|
|
|
- * @param x2
|
|
|
- * @param y2
|
|
|
+ * @param x1
|
|
|
+ * @param y1
|
|
|
+ * @param x2
|
|
|
+ * @param y2
|
|
|
* @return the distance between the two coordinates in 2D plane
|
|
|
*/
|
|
|
public static double calculateDistance(int x1, int y1, int x2, int y2)
|
|
@@ -113,12 +121,12 @@ public final class Util
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param x1
|
|
|
- * @param y1
|
|
|
- * @param z1
|
|
|
- * @param x2
|
|
|
- * @param y2
|
|
|
- * @param z2
|
|
|
+ * @param x1
|
|
|
+ * @param y1
|
|
|
+ * @param z1
|
|
|
+ * @param x2
|
|
|
+ * @param y2
|
|
|
+ * @param z2
|
|
|
* @param includeZAxis - if true, includes also the Z axis in the calculation
|
|
|
* @return the distance between the two coordinates
|
|
|
*/
|
|
@@ -130,33 +138,34 @@ public final class Util
|
|
|
if (includeZAxis)
|
|
|
{
|
|
|
double dz = z1 - z2;
|
|
|
- return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
|
+ return Math.sqrt((dx * dx) + (dy * dy) + (dz * dz));
|
|
|
}
|
|
|
- return Math.sqrt(dx * dx + dy * dy);
|
|
|
+ return Math.sqrt((dx * dx) + (dy * dy));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param obj1
|
|
|
- * @param obj2
|
|
|
+ * @param obj1
|
|
|
+ * @param obj2
|
|
|
* @param includeZAxis - if true, includes also the Z axis in the calculation
|
|
|
* @return the distance between the two objects
|
|
|
*/
|
|
|
public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis)
|
|
|
{
|
|
|
- if (obj1 == null || obj2 == null)
|
|
|
+ if ((obj1 == null) || (obj2 == null))
|
|
|
+ {
|
|
|
return 1000000;
|
|
|
+ }
|
|
|
|
|
|
return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* @param str - the string whose first letter to capitalize
|
|
|
* @return a string with the first letter of the {@code str} capitalized
|
|
|
*/
|
|
|
public static String capitalizeFirst(String str)
|
|
|
{
|
|
|
- if (str == null || str.isEmpty())
|
|
|
+ if ((str == null) || str.isEmpty())
|
|
|
{
|
|
|
return str;
|
|
|
}
|
|
@@ -171,18 +180,18 @@ public final class Util
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * (Based on ucwords() function of PHP)
|
|
|
- *
|
|
|
+ * (Based on ucwords() function of PHP)<br>
|
|
|
* DrHouse: still functional but must be rewritten to avoid += to concat strings
|
|
|
- *
|
|
|
* @param str - the string to capitalize
|
|
|
* @return a string with the first letter of every word in {@code str} capitalized
|
|
|
*/
|
|
|
@Deprecated
|
|
|
public static String capitalizeWords(String str)
|
|
|
{
|
|
|
- if (str == null || str.isEmpty())
|
|
|
+ if ((str == null) || str.isEmpty())
|
|
|
+ {
|
|
|
return str;
|
|
|
+ }
|
|
|
|
|
|
char[] charArray = str.toCharArray();
|
|
|
StringBuilder result = new StringBuilder();
|
|
@@ -193,7 +202,9 @@ public final class Util
|
|
|
for (int i = 0; i < charArray.length; i++)
|
|
|
{
|
|
|
if (Character.isWhitespace(charArray[i]))
|
|
|
+ {
|
|
|
charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
|
|
|
+ }
|
|
|
|
|
|
result.append(charArray[i]);
|
|
|
}
|
|
@@ -202,26 +213,36 @@ public final class Util
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param range
|
|
|
- * @param obj1
|
|
|
- * @param obj2
|
|
|
- * @param includeZAxis
|
|
|
+ * @param range
|
|
|
+ * @param obj1
|
|
|
+ * @param obj2
|
|
|
+ * @param includeZAxis
|
|
|
* @return {@code true} if the two objects are within specified range between each other, {@code false} otherwise
|
|
|
*/
|
|
|
public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis)
|
|
|
{
|
|
|
- if (obj1 == null || obj2 == null)
|
|
|
+ if ((obj1 == null) || (obj2 == null))
|
|
|
+ {
|
|
|
return false;
|
|
|
+ }
|
|
|
if (obj1.getInstanceId() != obj2.getInstanceId())
|
|
|
+ {
|
|
|
return false;
|
|
|
+ }
|
|
|
if (range == -1)
|
|
|
+ {
|
|
|
return true; // not limited
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
int rad = 0;
|
|
|
if (obj1 instanceof L2Character)
|
|
|
+ {
|
|
|
rad += ((L2Character) obj1).getTemplate().getCollisionRadius();
|
|
|
+ }
|
|
|
if (obj2 instanceof L2Character)
|
|
|
+ {
|
|
|
rad += ((L2Character) obj2).getTemplate().getCollisionRadius();
|
|
|
+ }
|
|
|
|
|
|
double dx = obj1.getX() - obj2.getX();
|
|
|
double dy = obj1.getY() - obj2.getY();
|
|
@@ -229,18 +250,16 @@ public final class Util
|
|
|
if (includeZAxis)
|
|
|
{
|
|
|
double dz = obj1.getZ() - obj2.getZ();
|
|
|
- double d = dx * dx + dy * dy + dz * dz;
|
|
|
+ double d = (dx * dx) + (dy * dy) + (dz * dz);
|
|
|
|
|
|
- return d <= range * range + 2 * range * rad + rad * rad;
|
|
|
+ return d <= ((range * range) + (2 * range * rad) + (rad * rad));
|
|
|
}
|
|
|
- double d = dx * dx + dy * dy;
|
|
|
- return d <= range * range + 2 * range * rad + rad * rad;
|
|
|
+ double d = (dx * dx) + (dy * dy);
|
|
|
+ return d <= ((range * range) + (2 * range * rad) + (rad * rad));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Checks if object is within short (sqrt(int.max_value)) radius, not using collisionRadius.
|
|
|
- * Faster calculation than checkIfInRange if distance is short and collisionRadius isn't needed.
|
|
|
- * Not for long distance checks (potential teleports, far away castles etc).
|
|
|
+ * Checks if object is within short (sqrt(int.max_value)) radius, not using collisionRadius. Faster calculation than checkIfInRange if distance is short and collisionRadius isn't needed. Not for long distance checks (potential teleports, far away castles etc).
|
|
|
* @param radius
|
|
|
* @param obj1
|
|
|
* @param obj2
|
|
@@ -249,20 +268,24 @@ public final class Util
|
|
|
*/
|
|
|
public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis)
|
|
|
{
|
|
|
- if (obj1 == null || obj2 == null)
|
|
|
+ if ((obj1 == null) || (obj2 == null))
|
|
|
+ {
|
|
|
return false;
|
|
|
+ }
|
|
|
if (radius == -1)
|
|
|
+ {
|
|
|
return true; // not limited
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
int dx = obj1.getX() - obj2.getX();
|
|
|
int dy = obj1.getY() - obj2.getY();
|
|
|
|
|
|
if (includeZAxis)
|
|
|
{
|
|
|
int dz = obj1.getZ() - obj2.getZ();
|
|
|
- return dx * dx + dy * dy + dz * dz <= radius * radius;
|
|
|
+ return ((dx * dx) + (dy * dy) + (dz * dz)) <= (radius * radius);
|
|
|
}
|
|
|
- return dx * dx + dy * dy <= radius * radius;
|
|
|
+ return ((dx * dx) + (dy * dy)) <= (radius * radius);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -304,26 +327,33 @@ public final class Util
|
|
|
public static float roundTo(float number, int numPlaces)
|
|
|
{
|
|
|
if (numPlaces <= 1)
|
|
|
+ {
|
|
|
return Math.round(number);
|
|
|
+ }
|
|
|
|
|
|
float exponent = (float) Math.pow(10, numPlaces);
|
|
|
-
|
|
|
return Math.round(number * exponent) / exponent;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param text - the text to check
|
|
|
- * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
|
|
|
- */
|
|
|
- public static boolean isDigit(String text)
|
|
|
- {
|
|
|
- if (text == null || text.isEmpty())
|
|
|
- return false;
|
|
|
- for (char c : text.toCharArray())
|
|
|
- if (!Character.isDigit(c))
|
|
|
- return false;
|
|
|
- return true;
|
|
|
- }
|
|
|
+ * @param text - the text to check
|
|
|
+ * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise
|
|
|
+ */
|
|
|
+ public static boolean isDigit(String text)
|
|
|
+ {
|
|
|
+ if ((text == null) || text.isEmpty())
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (char c : text.toCharArray())
|
|
|
+ {
|
|
|
+ if (!Character.isDigit(c))
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @param text - the text to check
|
|
@@ -331,16 +361,22 @@ public final class Util
|
|
|
*/
|
|
|
public static boolean isAlphaNumeric(String text)
|
|
|
{
|
|
|
- if (text == null || text.isEmpty())
|
|
|
+ if ((text == null) || text.isEmpty())
|
|
|
+ {
|
|
|
return false;
|
|
|
+ }
|
|
|
for (char c : text.toCharArray())
|
|
|
+ {
|
|
|
if (!Character.isLetterOrDigit(c))
|
|
|
+ {
|
|
|
return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Format the specified digit using the digit grouping symbol "," (comma).
|
|
|
+ * Format the specified digit using the digit grouping symbol "," (comma).<br>
|
|
|
* For example, 123456789 becomes 123,456,789.
|
|
|
* @param amount - the amount of adena
|
|
|
* @return the formatted adena amount
|
|
@@ -354,9 +390,13 @@ public final class Util
|
|
|
while (amount > 0)
|
|
|
{
|
|
|
if (rem < 99)
|
|
|
+ {
|
|
|
s = '0' + s;
|
|
|
+ }
|
|
|
if (rem < 9)
|
|
|
+ {
|
|
|
s = '0' + s;
|
|
|
+ }
|
|
|
rem = amount % 1000;
|
|
|
s = Long.toString(rem) + "," + s;
|
|
|
amount = (amount - rem) / 1000;
|
|
@@ -364,32 +404,37 @@ public final class Util
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Format the given date on the given format
|
|
|
- * @param date : the date to format.
|
|
|
- * @param format : the format to correct by.
|
|
|
- * @return a string representation of the formatted date.
|
|
|
- */
|
|
|
- public static String formatDate(Date date, String format)
|
|
|
- {
|
|
|
- final DateFormat dateFormat = new SimpleDateFormat(format);
|
|
|
- if (date != null)
|
|
|
- return dateFormat.format(date);
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Format the given date on the given format
|
|
|
+ * @param date : the date to format.
|
|
|
+ * @param format : the format to correct by.
|
|
|
+ * @return a string representation of the formatted date.
|
|
|
+ */
|
|
|
+ public static String formatDate(Date date, String format)
|
|
|
+ {
|
|
|
+ if (date != null)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ final DateFormat dateFormat = new SimpleDateFormat(format);
|
|
|
+ return dateFormat.format(date);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- * @param <T>
|
|
|
+ * @param <T>
|
|
|
* @param array - the array to look into
|
|
|
* @param obj - the object to search for
|
|
|
- * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise
|
|
|
+ * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise.
|
|
|
*/
|
|
|
public static <T> boolean contains(T[] array, T obj)
|
|
|
{
|
|
|
for (T element : array)
|
|
|
+ {
|
|
|
if (element == obj)
|
|
|
+ {
|
|
|
return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -401,17 +446,22 @@ public final class Util
|
|
|
public static boolean contains(int[] array, int obj)
|
|
|
{
|
|
|
for (int element : array)
|
|
|
+ {
|
|
|
if (element == obj)
|
|
|
+ {
|
|
|
return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static File[] getDatapackFiles(String dirname, String extention)
|
|
|
{
|
|
|
File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname);
|
|
|
if (!dir.exists())
|
|
|
+ {
|
|
|
return null;
|
|
|
-
|
|
|
+ }
|
|
|
return dir.listFiles(new ExtFilter(extention));
|
|
|
}
|
|
|
|