How can I optimize this piece of code?

Discussion in 'Plugin Development' started by k9rosie, Aug 5, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    k9rosie

    It's a loop that searches through the list of teams in a config, and then creates those teams with the list's attributes. It works... but I feel like it could be better.

    Code:java
    1. List<?> teamsList = this.getConfig().getList("teams");
    2.  
    3. for(Object object : teamsList){
    4. HashMap<String, Object> map = (HashMap<String, Object>) object;
    5. String teamId = null;
    6. String teamName = null;
    7. ChatColor teamColor = null;
    8. boolean attackable = false;
    9. boolean damageable = false;
    10.  
    11. for (Entry<String, Object> entry : map.entrySet()) {
    12. String key = entry.getKey(); //team id
    13. HashMap<String, Object> value = (HashMap<String, Object>) entry.getValue(); //team attributes
    14. teamId = key;
    15. for (Entry<String, Object> e : value.entrySet()) {
    16. if(e.getKey().equalsIgnoreCase("name")) teamName = (String) e.getValue();
    17. if(e.getKey().equalsIgnoreCase("color")) teamColor = this.colorize((String) e.getValue());
    18. if(e.getKey().equalsIgnoreCase("attackable")) attackable = (boolean) e.getValue();
    19. if(e.getKey().equalsIgnoreCase("damageable")) damageable = (boolean) e.getValue();
    20. }
    21.  
    22. }
    23. gamestate.createTeam(teamId, teamName, teamColor);
    24. gamestate.getTeam(teamId).setAttackable(attackable);
    25. gamestate.getTeam(teamId).setDamageable(damageable);
    26. }


    Here is what the config that the loop searches through looks like:
    Code:
    # These teams are defined in the plugin
    # NOTE: The neutral team is always included by default! DO NOT define a neutral team in this config.
    teams:
      - red: #the identifier of the team
          name: Red #the name of the team
          color: red #team color (if you specify a color that doesn't exist, your color will default to white)
          attackable: true #can this team attack other players?
          damageable: true #can this team be attacked by other players?
      - blue:
          name: Blue
          color: blue
          attackable: true
          damageable: true
     
  2. Offline

    _LB

    "attackable" and "damageable" are synonyms.

    Not really sure what your specific concern is.
     
  3. You could increase preformance by a ton by not using bukkits default config system.
     
  4. Offline

    Not2EXceL

    Pretty sure these 2 lines sum it up.
    Code:java
    1.  
    2. HashMap<String, Object> value = (HashMap<String, Object>) entry.getValue();
    3. HashMap<String, Object> map = (HashMap<String, Object>) object;
    4.  
     
  5. Offline

    Rocoty

    And else if statements are a good thing.
     
    mine-care likes this.
  6. Offline

    Not2EXceL

    Rocoty
    Except for the part where else if statements doesnt fit his code.
     
  7. Offline

    Rocoty

    Not2EXceL Well, obviously. But there is no point in re-evaluating a variable once you know its value.
     
  8. Offline

    Not2EXceL

    Rocoty derp, this is why i shouldnt try to read sloppy formatted code after work
     
Thread Status:
Not open for further replies.

Share This Page