Procházet zdrojové kódy

BETA: Review and minor fixes for recent commits:
* checkSkillCastConditions JavaDoc fixes, comments and use of short circuit logic.
* L2VillageMasterInstance added own logger, cleanup for isValidName(..) method.
* Inventory own logger, JavaDoc fixes, use of ternary operators.
* PcInventory own logger.
* DecayTaskManager removed unnecessary try/catch, remove(..) doesn't throw NoSuchElementException, fixed NPE vulnerability, reused one method.
* gameserver/Util formatted and method formatDate changed, there is no need to instantiate the DateFormat if the date is null.

Zoey76 před 13 roky
rodič
revize
8f861b2ed4

+ 14 - 13
L2J_Server_BETA/java/com/l2jserver/gameserver/ai/L2AttackableAI.java

@@ -1917,26 +1917,27 @@ public class L2AttackableAI extends L2CharacterAI implements Runnable
 		}
 	}
 	
+	/**
+	 * @param skill the skill to check.
+	 * @return {@code true} if the skill is available for casting {@code false} otherwise.
+	 */
 	private boolean checkSkillCastConditions(L2Skill skill)
 	{
+		// Not enough MP.
 		if (skill.getMpConsume() >= getActiveChar().getCurrentMp())
+		{
 			return false;
-		else if (getActiveChar().isSkillDisabled(skill))
+		}
+		// Character is in "skill disabled" mode. 
+		if (getActiveChar().isSkillDisabled(skill))
+		{
 			return false;
-		else if (!skill.ignoreSkillMute())
+		}
+		// Is a magic skill and character is magically muted or is a physical skill and character is physically muted.
+		if (!skill.ignoreSkillMute() && ((skill.isMagic() && getActiveChar().isMuted()) || getActiveChar().isPhysicalMuted()))
 		{
-			if (skill.isMagic())
-			{
-				if (getActiveChar().isMuted())
-					return false;
-			}
-			else
-			{
-				if (getActiveChar().isPhysicalMuted())
-					return false;
-			}
+			return false;
 		}
-		
 		return true;
 	}
 	

+ 6 - 13
L2J_Server_BETA/java/com/l2jserver/gameserver/model/actor/instance/L2VillageMasterInstance.java

@@ -16,7 +16,7 @@ package com.l2jserver.gameserver.model.actor.instance;
 
 import java.util.Iterator;
 import java.util.Set;
-import java.util.regex.Matcher;
+import java.util.logging.Logger;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 
@@ -62,7 +62,7 @@ import com.l2jserver.util.StringUtil;
  */
 public class L2VillageMasterInstance extends L2NpcInstance
 {
-	//private static Logger _log = Logger.getLogger(L2VillageMasterInstance.class.getName());
+	private static Logger _log = Logger.getLogger(L2VillageMasterInstance.class.getName());
 	
 	/**
 	 * @param objectId
@@ -1145,25 +1145,18 @@ public class L2VillageMasterInstance extends L2NpcInstance
 		player.sendPacket(ActionFailed.STATIC_PACKET);
 	}
 	
-	private static boolean isValidName(String text)
+	private static boolean isValidName(String name)
 	{
-		boolean result = true;
-		String test = text;
 		Pattern pattern;
 		try
 		{
 			pattern = Pattern.compile(Config.CLAN_NAME_TEMPLATE);
 		}
-		catch (PatternSyntaxException e) // case of illegal pattern
+		catch (PatternSyntaxException e)
 		{
-			_log.warning("ERROR : Clan name pattern of config is wrong!");
+			_log.warning("ERROR: Wrong pattern for clan name!");
 			pattern = Pattern.compile(".*");
 		}
-		Matcher regexp = pattern.matcher(test);
-		if (!regexp.matches())
-		{
-			result = false;
-		}
-		return result;
+		return pattern.matcher(name).matches();
 	}
 }

+ 13 - 22
L2J_Server_BETA/java/com/l2jserver/gameserver/model/itemcontainer/Inventory.java

@@ -20,6 +20,7 @@ import java.sql.ResultSet;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javolution.util.FastList;
 
@@ -52,7 +53,7 @@ import com.l2jserver.util.StringUtil;
  */
 public abstract class Inventory extends ItemContainer
 {
-	//protected static final Logger _log = Logger.getLogger(Inventory.class.getName());
+	private static final Logger _log = Logger.getLogger(Inventory.class.getName());
 	
 	public interface PaperdollListener
 	{
@@ -932,26 +933,20 @@ public abstract class Inventory extends ItemContainer
 	}
 	
 	/**
-	 * Returns the ID of the item in the paperdol slot
+	 * Returns the ID of the item in the paperdoll slot
 	 * @param slot : int designating the slot
 	 * @return int designating the ID of the item
 	 */
 	public int getPaperdollItemDisplayId(int slot)
 	{
-		L2ItemInstance item = _paperdoll[slot];
-		if (item != null)
-			return item.getDisplayId();
-		return 0;
+		final L2ItemInstance item = _paperdoll[slot];
+		return (item != null) ? item.getDisplayId() : 0;
 	}
 	
 	public int getPaperdollAugmentationId(int slot)
 	{
 		final L2ItemInstance item = _paperdoll[slot];
-		if ((item != null) && (item.getAugmentation() != null))
-		{
-			return item.getAugmentation().getAugmentationId();
-		}
-		return 0;
+		return ((item != null) && (item.getAugmentation() != null)) ? item.getAugmentation().getAugmentationId() : 0;
 	}
 	
 	/**
@@ -961,10 +956,8 @@ public abstract class Inventory extends ItemContainer
 	 */
 	public int getPaperdollObjectId(int slot)
 	{
-		L2ItemInstance item = _paperdoll[slot];
-		if (item != null)
-			return item.getObjectId();
-		return 0;
+		final L2ItemInstance item = _paperdoll[slot];
+		return (item != null) ? item.getObjectId() : 0;
 	}
 	
 	/**
@@ -1044,8 +1037,7 @@ public abstract class Inventory extends ItemContainer
 	}
 	
 	/**
-	 * Return the mask of weared item
-	 * @return int
+	 * @return the mask of wore item
 	 */
 	public int getWearedMask()
 	{
@@ -1159,7 +1151,7 @@ public abstract class Inventory extends ItemContainer
 	}
 	
 	/**
-	 * Unepquips item in slot and returns alterations<BR>
+	 * Unequips item in slot and returns alterations<BR>
 	 * <B>If you dont need return value use {@link Inventory#unEquipItemInSlot(int)} instead</B>
 	 * @param slot : int designating the slot
 	 * @return L2ItemInstance[] : list of items altered
@@ -1281,7 +1273,7 @@ public abstract class Inventory extends ItemContainer
 	
 	/**
 	 * Equips item and returns list of alterations<BR>
-	 * <B>If you dont need return value use {@link Inventory#equipItem(L2ItemInstance)} instead</B>
+	 * <B>If you don't need return value use {@link Inventory#equipItem(L2ItemInstance)} instead</B>
 	 * @param item : L2ItemInstance corresponding to the item
 	 * @return L2ItemInstance[] : list of alterations
 	 */
@@ -1361,7 +1353,7 @@ public abstract class Inventory extends ItemContainer
 			}
 			case L2Item.SLOT_R_HAND:
 			{
-				// dont care about arrows, listener will unequip them (hopefully)
+				// don't care about arrows, listener will unequip them (hopefully)
 				setPaperdollItem(PAPERDOLL_RHAND, item);
 				break;
 			}
@@ -1500,8 +1492,7 @@ public abstract class Inventory extends ItemContainer
 	}
 	
 	/**
-	 * Returns the totalWeight.
-	 * @return int
+	 * @return the totalWeight.
 	 */
 	public int getTotalWeight()
 	{

+ 3 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/itemcontainer/PcInventory.java

@@ -18,6 +18,7 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javolution.util.FastList;
 
@@ -39,6 +40,8 @@ import com.l2jserver.gameserver.util.Util;
 
 public class PcInventory extends Inventory
 {
+	private static final Logger _log = Logger.getLogger(PcInventory.class.getName());
+	
 	public static final int ADENA_ID = 57;
 	public static final int ANCIENT_ADENA_ID = 5575;
 	public static final long MAX_ADENA = 99900000000L;

+ 3 - 10
L2J_Server_BETA/java/com/l2jserver/gameserver/taskmanager/DecayTaskManager.java

@@ -17,7 +17,6 @@ package com.l2jserver.gameserver.taskmanager;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.NoSuchElementException;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -49,7 +48,7 @@ public class DecayTaskManager
 	
 	public void addDecayTask(L2Character actor)
 	{
-		_decayTasks.put(actor, System.currentTimeMillis());
+		addDecayTask(actor, 0); 
 	}
 	
 	public void addDecayTask(L2Character actor, int interval)
@@ -59,13 +58,7 @@ public class DecayTaskManager
 	
 	public void cancelDecayTask(L2Character actor)
 	{
-		try
-		{
-			_decayTasks.remove(actor);
-		}
-		catch (NoSuchElementException e)
-		{
-		}
+		_decayTasks.remove(actor);
 	}
 	
 	private class DecayScheduler implements Runnable
@@ -91,7 +84,7 @@ public class DecayTaskManager
 					e = it.next();
 					actor = e.getKey();
 					next = e.getValue();
-					if (next == null)
+					if (actor == null || next == null)
 						continue;
 					if (actor.isRaid() && !actor.isRaidMinion())
 						delay = Config.RAID_BOSS_DECAY_TIME;

+ 127 - 77
L2J_Server_BETA/java/com/l2jserver/gameserver/util/Util.java

@@ -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));
 	}