Changeset 5156


Ignore:
Timestamp:
Jan 27, 2012 11:18:01 PM (4 months ago)
Author:
Zoey76
Message:

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.
Location:
branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/ai/L2AttackableAI.java

    r5146 r5156  
    19181918        } 
    19191919         
     1920        /** 
     1921         * @param skill the skill to check. 
     1922         * @return {@code true} if the skill is available for casting {@code false} otherwise. 
     1923         */ 
    19201924        private boolean checkSkillCastConditions(L2Skill skill) 
    19211925        { 
     1926                // Not enough MP. 
    19221927                if (skill.getMpConsume() >= getActiveChar().getCurrentMp()) 
     1928                { 
    19231929                        return false; 
    1924                 else if (getActiveChar().isSkillDisabled(skill)) 
     1930                } 
     1931                // Character is in "skill disabled" mode.  
     1932                if (getActiveChar().isSkillDisabled(skill)) 
     1933                { 
    19251934                        return false; 
    1926                 else if (!skill.ignoreSkillMute()) 
    1927                 { 
    1928                         if (skill.isMagic()) 
    1929                         { 
    1930                                 if (getActiveChar().isMuted()) 
    1931                                         return false; 
    1932                         } 
    1933                         else 
    1934                         { 
    1935                                 if (getActiveChar().isPhysicalMuted()) 
    1936                                         return false; 
    1937                         } 
    1938                 } 
    1939                  
     1935                } 
     1936                // Is a magic skill and character is magically muted or is a physical skill and character is physically muted. 
     1937                if (!skill.ignoreSkillMute() && ((skill.isMagic() && getActiveChar().isMuted()) || getActiveChar().isPhysicalMuted())) 
     1938                { 
     1939                        return false; 
     1940                } 
    19401941                return true; 
    19411942        } 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/actor/instance/L2VillageMasterInstance.java

    r5146 r5156  
    1717import java.util.Iterator; 
    1818import java.util.Set; 
    19 import java.util.regex.Matcher; 
     19import java.util.logging.Logger; 
    2020import java.util.regex.Pattern; 
    2121import java.util.regex.PatternSyntaxException; 
     
    6363public class L2VillageMasterInstance extends L2NpcInstance 
    6464{ 
    65         //private static Logger _log = Logger.getLogger(L2VillageMasterInstance.class.getName()); 
     65        private static Logger _log = Logger.getLogger(L2VillageMasterInstance.class.getName()); 
    6666         
    6767        /** 
     
    11461146        } 
    11471147         
    1148         private static boolean isValidName(String text) 
    1149         { 
    1150                 boolean result = true; 
    1151                 String test = text; 
     1148        private static boolean isValidName(String name) 
     1149        { 
    11521150                Pattern pattern; 
    11531151                try 
     
    11551153                        pattern = Pattern.compile(Config.CLAN_NAME_TEMPLATE); 
    11561154                } 
    1157                 catch (PatternSyntaxException e) // case of illegal pattern 
    1158                 { 
    1159                         _log.warning("ERROR : Clan name pattern of config is wrong!"); 
     1155                catch (PatternSyntaxException e) 
     1156                { 
     1157                        _log.warning("ERROR: Wrong pattern for clan name!"); 
    11601158                        pattern = Pattern.compile(".*"); 
    11611159                } 
    1162                 Matcher regexp = pattern.matcher(test); 
    1163                 if (!regexp.matches()) 
    1164                 { 
    1165                         result = false; 
    1166                 } 
    1167                 return result; 
     1160                return pattern.matcher(name).matches(); 
    11681161        } 
    11691162} 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/itemcontainer/Inventory.java

    r5146 r5156  
    2121import java.util.List; 
    2222import java.util.logging.Level; 
     23import java.util.logging.Logger; 
    2324 
    2425import javolution.util.FastList; 
     
    5354public abstract class Inventory extends ItemContainer 
    5455{ 
    55         //protected static final Logger _log = Logger.getLogger(Inventory.class.getName()); 
     56        private static final Logger _log = Logger.getLogger(Inventory.class.getName()); 
    5657         
    5758        public interface PaperdollListener 
     
    933934         
    934935        /** 
    935          * Returns the ID of the item in the paperdol slot 
     936         * Returns the ID of the item in the paperdoll slot 
    936937         * @param slot : int designating the slot 
    937938         * @return int designating the ID of the item 
     
    939940        public int getPaperdollItemDisplayId(int slot) 
    940941        { 
    941                 L2ItemInstance item = _paperdoll[slot]; 
    942                 if (item != null) 
    943                         return item.getDisplayId(); 
    944                 return 0; 
     942                final L2ItemInstance item = _paperdoll[slot]; 
     943                return (item != null) ? item.getDisplayId() : 0; 
    945944        } 
    946945         
     
    948947        { 
    949948                final L2ItemInstance item = _paperdoll[slot]; 
    950                 if ((item != null) && (item.getAugmentation() != null)) 
    951                 { 
    952                         return item.getAugmentation().getAugmentationId(); 
    953                 } 
    954                 return 0; 
     949                return ((item != null) && (item.getAugmentation() != null)) ? item.getAugmentation().getAugmentationId() : 0; 
    955950        } 
    956951         
     
    962957        public int getPaperdollObjectId(int slot) 
    963958        { 
    964                 L2ItemInstance item = _paperdoll[slot]; 
    965                 if (item != null) 
    966                         return item.getObjectId(); 
    967                 return 0; 
     959                final L2ItemInstance item = _paperdoll[slot]; 
     960                return (item != null) ? item.getObjectId() : 0; 
    968961        } 
    969962         
     
    10451038         
    10461039        /** 
    1047          * Return the mask of weared item 
    1048          * @return int 
     1040         * @return the mask of wore item 
    10491041         */ 
    10501042        public int getWearedMask() 
     
    11601152         
    11611153        /** 
    1162          * Unepquips item in slot and returns alterations<BR> 
     1154         * Unequips item in slot and returns alterations<BR> 
    11631155         * <B>If you dont need return value use {@link Inventory#unEquipItemInSlot(int)} instead</B> 
    11641156         * @param slot : int designating the slot 
     
    12821274        /** 
    12831275         * Equips item and returns list of alterations<BR> 
    1284          * <B>If you dont need return value use {@link Inventory#equipItem(L2ItemInstance)} instead</B> 
     1276         * <B>If you don't need return value use {@link Inventory#equipItem(L2ItemInstance)} instead</B> 
    12851277         * @param item : L2ItemInstance corresponding to the item 
    12861278         * @return L2ItemInstance[] : list of alterations 
     
    13621354                        case L2Item.SLOT_R_HAND: 
    13631355                        { 
    1364                                 // dont care about arrows, listener will unequip them (hopefully) 
     1356                                // don't care about arrows, listener will unequip them (hopefully) 
    13651357                                setPaperdollItem(PAPERDOLL_RHAND, item); 
    13661358                                break; 
     
    15011493         
    15021494        /** 
    1503          * Returns the totalWeight. 
    1504          * @return int 
     1495         * @return the totalWeight. 
    15051496         */ 
    15061497        public int getTotalWeight() 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/itemcontainer/PcInventory.java

    r5146 r5156  
    1919import java.sql.ResultSet; 
    2020import java.util.logging.Level; 
     21import java.util.logging.Logger; 
    2122 
    2223import javolution.util.FastList; 
     
    4041public class PcInventory extends Inventory 
    4142{ 
     43        private static final Logger _log = Logger.getLogger(PcInventory.class.getName()); 
     44         
    4245        public static final int ADENA_ID = 57; 
    4346        public static final int ANCIENT_ADENA_ID = 5575; 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/taskmanager/DecayTaskManager.java

    r4981 r5156  
    1818import java.util.Map; 
    1919import java.util.Map.Entry; 
    20 import java.util.NoSuchElementException; 
    2120import java.util.logging.Level; 
    2221import java.util.logging.Logger; 
     
    5049        public void addDecayTask(L2Character actor) 
    5150        { 
    52                 _decayTasks.put(actor, System.currentTimeMillis()); 
     51                addDecayTask(actor, 0);  
    5352        } 
    5453         
     
    6059        public void cancelDecayTask(L2Character actor) 
    6160        { 
    62                 try 
    63                 { 
    64                         _decayTasks.remove(actor); 
    65                 } 
    66                 catch (NoSuchElementException e) 
    67                 { 
    68                 } 
     61                _decayTasks.remove(actor); 
    6962        } 
    7063         
     
    9285                                        actor = e.getKey(); 
    9386                                        next = e.getValue(); 
    94                                         if (next == null) 
     87                                        if (actor == null || next == null) 
    9588                                                continue; 
    9689                                        if (actor.isRaid() && !actor.isRaidMinion()) 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/util/Util.java

    r5130 r5156  
    4343         
    4444        /** 
    45          * @param obj1  
    46          * @param obj2  
     45         * @param obj1 
     46         * @param obj2 
    4747         * @return degree value of object 2 to the horizontal line with object 1 being the origin. 
    4848         */ 
     
    5353         
    5454        /** 
    55          * @param obj1X  
    56          * @param obj1Y  
    57          * @param obj2X  
    58          * @param obj2Y  
     55         * @param obj1X 
     56         * @param obj1Y 
     57         * @param obj2X 
     58         * @param obj2Y 
    5959         * @return degree value of object 2 to the horizontal line with object 1 being the origin 
    6060         */ 
     
    6363                double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X)); 
    6464                if (angleTarget < 0) 
     65                { 
    6566                        angleTarget = 360 + angleTarget; 
     67                } 
    6668                return angleTarget; 
    6769        } 
     
    7678        { 
    7779                if (degree < 0) 
     80                { 
    7881                        degree = 360 + degree; 
     82                } 
    7983                return (int) (degree * 182.044444444); 
    8084        } 
     
    8993                double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X)); 
    9094                if (angleTarget < 0) 
     95                { 
    9196                        angleTarget = 360 + angleTarget; 
     97                } 
    9298                return (int) (angleTarget * 182.044444444); 
    9399        } 
     
    97103                double angleTarget = Math.toDegrees(Math.atan2(dy, dx)); 
    98104                if (angleTarget < 0) 
     105                { 
    99106                        angleTarget = 360 + angleTarget; 
     107                } 
    100108                return (int) (angleTarget * 182.044444444); 
    101109        } 
    102110         
    103111        /** 
    104          * @param x1  
    105          * @param y1  
    106          * @param x2  
    107          * @param y2  
     112         * @param x1 
     113         * @param y1 
     114         * @param x2 
     115         * @param y2 
    108116         * @return the distance between the two coordinates in 2D plane 
    109117         */ 
     
    114122         
    115123        /** 
    116          * @param x1  
    117          * @param y1  
    118          * @param z1  
    119          * @param x2  
    120          * @param y2  
    121          * @param z2  
     124         * @param x1 
     125         * @param y1 
     126         * @param z1 
     127         * @param x2 
     128         * @param y2 
     129         * @param z2 
    122130         * @param includeZAxis - if true, includes also the Z axis in the calculation 
    123131         * @return the distance between the two coordinates 
     
    131139                { 
    132140                        double dz = z1 - z2; 
    133                         return Math.sqrt(dx * dx + dy * dy + dz * dz); 
    134                 } 
    135                 return Math.sqrt(dx * dx + dy * dy); 
    136         } 
    137          
    138         /** 
    139          * @param obj1  
    140          * @param obj2  
     141                        return Math.sqrt((dx * dx) + (dy * dy) + (dz * dz)); 
     142                } 
     143                return Math.sqrt((dx * dx) + (dy * dy)); 
     144        } 
     145         
     146        /** 
     147         * @param obj1 
     148         * @param obj2 
    141149         * @param includeZAxis - if true, includes also the Z axis in the calculation 
    142150         * @return the distance between the two objects 
     
    144152        public static double calculateDistance(L2Object obj1, L2Object obj2, boolean includeZAxis) 
    145153        { 
    146                 if (obj1 == null || obj2 == null) 
     154                if ((obj1 == null) || (obj2 == null)) 
     155                { 
    147156                        return 1000000; 
     157                } 
    148158                 
    149159                return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj1.getPosition().getZ(), obj2.getPosition().getX(), obj2.getPosition().getY(), obj2.getPosition().getZ(), includeZAxis); 
     
    151161         
    152162        /** 
    153          *  
    154163         * @param str - the string whose first letter to capitalize 
    155164         * @return a string with the first letter of the {@code str} capitalized 
     
    157166        public static String capitalizeFirst(String str) 
    158167        { 
    159                 if (str == null || str.isEmpty()) 
     168                if ((str == null) || str.isEmpty()) 
    160169                { 
    161170                        return str; 
     
    172181         
    173182        /** 
    174          * (Based on ucwords() function of PHP) 
    175          *  
     183         * (Based on ucwords() function of PHP)<br> 
    176184         * DrHouse: still functional but must be rewritten to avoid += to concat strings 
    177          * 
    178185         * @param str - the string to capitalize 
    179186         * @return a string with the first letter of every word in {@code str} capitalized 
     
    182189        public static String capitalizeWords(String str) 
    183190        { 
    184                 if (str == null || str.isEmpty()) 
     191                if ((str == null) || str.isEmpty()) 
     192                { 
    185193                        return str; 
     194                } 
    186195                 
    187196                char[] charArray = str.toCharArray(); 
     
    194203                { 
    195204                        if (Character.isWhitespace(charArray[i])) 
     205                        { 
    196206                                charArray[i + 1] = Character.toUpperCase(charArray[i + 1]); 
     207                        } 
    197208                         
    198209                        result.append(charArray[i]); 
     
    203214         
    204215        /** 
    205          * @param range  
    206          * @param obj1  
    207          * @param obj2  
    208          * @param includeZAxis  
     216         * @param range 
     217         * @param obj1 
     218         * @param obj2 
     219         * @param includeZAxis 
    209220         * @return {@code true} if the two objects are within specified range between each other, {@code false} otherwise 
    210221         */ 
    211222        public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2, boolean includeZAxis) 
    212223        { 
    213                 if (obj1 == null || obj2 == null) 
     224                if ((obj1 == null) || (obj2 == null)) 
     225                { 
    214226                        return false; 
     227                } 
    215228                if (obj1.getInstanceId() != obj2.getInstanceId()) 
     229                { 
    216230                        return false; 
     231                } 
    217232                if (range == -1) 
     233                { 
    218234                        return true; // not limited 
    219                          
     235                } 
     236                 
    220237                int rad = 0; 
    221238                if (obj1 instanceof L2Character) 
     239                { 
    222240                        rad += ((L2Character) obj1).getTemplate().getCollisionRadius(); 
     241                } 
    223242                if (obj2 instanceof L2Character) 
     243                { 
    224244                        rad += ((L2Character) obj2).getTemplate().getCollisionRadius(); 
     245                } 
    225246                 
    226247                double dx = obj1.getX() - obj2.getX(); 
     
    230251                { 
    231252                        double dz = obj1.getZ() - obj2.getZ(); 
    232                         double d = dx * dx + dy * dy + dz * dz; 
     253                        double d = (dx * dx) + (dy * dy) + (dz * dz); 
    233254                         
    234                         return d <= range * range + 2 * range * rad + rad * rad; 
    235                 } 
    236                 double d = dx * dx + dy * dy; 
    237                 return d <= range * range + 2 * range * rad + rad * rad; 
    238         } 
    239          
    240         /** 
    241          *  Checks if object is within short (sqrt(int.max_value)) radius, not using collisionRadius. 
    242          *  Faster calculation than checkIfInRange if distance is short and collisionRadius isn't needed. 
    243          *  Not for long distance checks (potential teleports, far away castles etc). 
     255                        return d <= ((range * range) + (2 * range * rad) + (rad * rad)); 
     256                } 
     257                double d = (dx * dx) + (dy * dy); 
     258                return d <= ((range * range) + (2 * range * rad) + (rad * rad)); 
     259        } 
     260         
     261        /** 
     262         * 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). 
    244263         * @param radius 
    245264         * @param obj1 
     
    250269        public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2, boolean includeZAxis) 
    251270        { 
    252                 if (obj1 == null || obj2 == null) 
     271                if ((obj1 == null) || (obj2 == null)) 
     272                { 
    253273                        return false; 
     274                } 
    254275                if (radius == -1) 
     276                { 
    255277                        return true; // not limited 
    256                          
     278                } 
     279                 
    257280                int dx = obj1.getX() - obj2.getX(); 
    258281                int dy = obj1.getY() - obj2.getY(); 
     
    261284                { 
    262285                        int dz = obj1.getZ() - obj2.getZ(); 
    263                         return dx * dx + dy * dy + dz * dz <= radius * radius; 
    264                 } 
    265                 return dx * dx + dy * dy <= radius * radius; 
     286                        return ((dx * dx) + (dy * dy) + (dz * dz)) <= (radius * radius); 
     287                } 
     288                return ((dx * dx) + (dy * dy)) <= (radius * radius); 
    266289        } 
    267290         
     
    305328        { 
    306329                if (numPlaces <= 1) 
     330                { 
    307331                        return Math.round(number); 
     332                } 
    308333                 
    309334                float exponent = (float) Math.pow(10, numPlaces); 
    310                  
    311335                return Math.round(number * exponent) / exponent; 
    312336        } 
    313337         
    314338        /** 
    315      * @param text - the text to check 
    316      * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise 
    317      */ 
    318     public static boolean isDigit(String text) 
    319     { 
    320         if (text == null || text.isEmpty()) 
    321             return false; 
    322         for (char c : text.toCharArray()) 
    323             if (!Character.isDigit(c)) 
    324                 return false; 
    325         return true; 
    326     } 
     339         * @param text - the text to check 
     340         * @return {@code true} if {@code text} contains only numbers, {@code false} otherwise 
     341         */ 
     342        public static boolean isDigit(String text) 
     343        { 
     344                if ((text == null) || text.isEmpty()) 
     345                { 
     346                        return false; 
     347                } 
     348                for (char c : text.toCharArray()) 
     349                { 
     350                        if (!Character.isDigit(c)) 
     351                        { 
     352                                return false; 
     353                        } 
     354                } 
     355                return true; 
     356        } 
    327357         
    328358        /** 
     
    332362        public static boolean isAlphaNumeric(String text) 
    333363        { 
    334                 if (text == null || text.isEmpty()) 
     364                if ((text == null) || text.isEmpty()) 
     365                { 
    335366                        return false; 
     367                } 
    336368                for (char c : text.toCharArray()) 
     369                { 
    337370                        if (!Character.isLetterOrDigit(c)) 
     371                        { 
    338372                                return false; 
     373                        } 
     374                } 
    339375                return true; 
    340376        } 
    341377         
    342378        /** 
    343          * Format the specified digit using the digit grouping symbol "," (comma). 
     379         * Format the specified digit using the digit grouping symbol "," (comma).<br> 
    344380         * For example, 123456789 becomes 123,456,789. 
    345381         * @param amount - the amount of adena 
     
    355391                { 
    356392                        if (rem < 99) 
     393                        { 
    357394                                s = '0' + s; 
     395                        } 
    358396                        if (rem < 9) 
     397                        { 
    359398                                s = '0' + s; 
     399                        } 
    360400                        rem = amount % 1000; 
    361401                        s = Long.toString(rem) + "," + s; 
     
    365405        } 
    366406         
    367     /** 
    368      * Format the given date on the given format 
    369      * @param date : the date to format. 
    370      * @param format : the format to correct by. 
    371      * @return a string representation of the formatted date. 
    372      */ 
    373     public static String formatDate(Date date, String format) 
    374     { 
    375         final DateFormat dateFormat = new SimpleDateFormat(format); 
    376         if (date != null) 
    377             return dateFormat.format(date); 
    378         
    379         return null; 
    380     } 
    381          
    382         /** 
    383          * @param <T>  
     407        /** 
     408         * Format the given date on the given format 
     409         * @param date : the date to format. 
     410         * @param format : the format to correct by. 
     411         * @return a string representation of the formatted date. 
     412         */ 
     413        public static String formatDate(Date date, String format) 
     414        { 
     415                if (date != null) 
     416                { 
     417                        return null; 
     418                } 
     419                final DateFormat dateFormat = new SimpleDateFormat(format); 
     420                return dateFormat.format(date); 
     421        } 
     422         
     423        /** 
     424         * @param <T> 
    384425         * @param array - the array to look into 
    385426         * @param obj - the object to search for 
    386          * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise 
     427         * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise. 
    387428         */ 
    388429        public static <T> boolean contains(T[] array, T obj) 
    389430        { 
    390431                for (T element : array) 
     432                { 
    391433                        if (element == obj) 
     434                        { 
    392435                                return true; 
     436                        } 
     437                } 
    393438                return false; 
    394439        } 
     
    402447        { 
    403448                for (int element : array) 
     449                { 
    404450                        if (element == obj) 
     451                        { 
    405452                                return true; 
     453                        } 
     454                } 
    406455                return false; 
    407456        } 
    408  
     457         
    409458        public static File[] getDatapackFiles(String dirname, String extention) 
    410459        { 
    411460                File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname); 
    412461                if (!dir.exists()) 
     462                { 
    413463                        return null; 
    414  
     464                } 
    415465                return dir.listFiles(new ExtFilter(extention)); 
    416466        } 
Note: See TracChangeset for help on using the changeset viewer.