Is there a better way I can write this?

Discussion in 'Plugin Development' started by k9rosie, Feb 26, 2015.

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

    k9rosie

    I have a YAML config that looks something like this:

    Code:
        worlds:
            - world:
                gamemode: 'deathmatch'
            - world_nether:
                gamemode: 'deathmatch'
            - world_the_end:
                gamemode: 'ctf'
    I want that worlds list to be appendable so anyone can add any amount of worlds they want.
    To search through the list and get the gamemode value, I have an algorithm set up like this:

    Code:
    public Gamemode getWorldGamemode(World world) {
            List<?> list = TeamPVP.getInstance().getConfig("core").getList("teampvp.worlds");
            Gamemode gamemode = null;
           
            for(Object item : list) { // for every object in the list of worlds
                HashMap<String, Object> worldItem = (HashMap<String, Object>) item; // particular world item
               
                if (worldItem.containsKey(world.getName())) { // if the world item is the one we're looking for
                    for (Entry<String, Object> propertiesList : worldItem.entrySet()) { // for every property in the worldItem
                        HashMap<String, Object> properties = (HashMap<String, Object>) propertiesList.getValue(); // we must convert the entry into a hashmap
                       
                        for (Entry<String, Object> value : properties.entrySet()) { // for every value the properties
                            if (value.getKey().equalsIgnoreCase("gamemode")) { // if the property name is the gamemode property
                                String gamemodeString = (String) value.getValue();
                               
                                for (Gamemode g : Gamemode.values()) { // for every gamemode in the enum Gamemode
                                    if (g.toString().equalsIgnoreCase(gamemodeString)) { // check if that gamemode matches the value
                                        gamemode = g;
                                    }
                                }
                            }
                        }
                    }
                }
            }
           
            return gamemode;
        }
    Basically it's just a bunch of for loops that searches through the worlds list until it finds the particular world it's looking for, then it searches through the list of properties until it finds the property it's looking for, then it checks to see if the property name is equal to 'gamemode', and if it is then it gets the value of the 'gamemode' property and iterates through the Gamemode enum to see if the 'gamemode' property matches with anything. If it does, then it returns the correct Gamemode enum it's looking for, otherwise it returns null.

    The code works, but I feel it is terribly inefficient and could be optimized and cleaned up to look way better. Any suggestions?
     
  2. Offline

    teej107

  3. Offline

    Ruptur

    @k9rosie

    I havn't tested what you post nor have i tested what i'm about to post but i get the gist of what it is you want
    I tried to neaten up your code abit here - i believe this does what you were looking for but neater
    Code:
        TeamPvp teampvp = TeamPvp.getInstance();
        public GameMode getWorldGamemode(World world) {
            // Some declarations
            String worldname;
            String gamemodename;
            GameMode gamemode = null;
    
            worldname = world.getName();
    
            for(String worlds : teampvp.getConfig().getConfigurationSection("worlds").getKeys(false)){
    
                gamemodename = teampvp.getConfig().getConfigurationSection(worlds).getString("gamemode");
    
                if(worlds.equalsIgnoreCase(worldname)){
                    for(GameMode g : GameMode.values()){
                        if(g.toString().equalsIgnoreCase(gamemodename)){
                            gamemode = g;
                        }
                    }
                }
            }
    
            return gamemode;
        }
     
    Last edited: Feb 26, 2015
Thread Status:
Not open for further replies.

Share This Page