[Simple][Tool] ParseGameMode

Status
This thread has been locked.

subbotted

Contact on Discord, subbotted#5560
Supreme
Feedback score
17
Posts
524
Reactions
407
Resources
0
Well, I was just helping my friend with his GameMode command, and I made him a tool for parsing the GameMode instead of doing if(args[0].equalsIgnoreCase("creative") || args[0].equalsIgnoreCase("c") etc...

This tool is simple, first of all, here is the code for it:
Code:
    public GameMode parseGameMode(String string) {
        HashMap<GameMode, Collection<?extends String>> gamemodes = new HashMap<>();
        gamemodes.put(GameMode.CREATIVE, Arrays.asList("creative", "c", "1"));
        gamemodes.put(GameMode.ADVENTURE, Arrays.asList("adventure", "a", "2"));
        gamemodes.put(GameMode.SURVIVAL, Arrays.asList("survival", "s", "0"));
        for(GameMode gamemode : gamemodes.keySet()) { // for each gamemode
            for(String alias : gamemodes.get(gamemode)) { // go through the aliases
                if(alias.equalsIgnoreCase(string)) { // if an alias matches the string provided, return the gamemode the alias belongs to
                    return gamemode;
                }
            }
        }
        return null; // if no gamemode is found, return null, then send a message to the player saying invalid gamemode or some shit like that
    }

This is how it would be used in a command:

Code:
GameMode gamemode = parseGameMode(args[0]);
if(gamemode != null) {
    player.setGameMode(gamemode);
    player.sendMessage(translateColor("&eYour GameMode is now &a&l" + gamemode.name()));
} else {
    player.sendMessage(translateColor("&cPlease enter a valid gamemode."));
}

Just a simple but useful little tool. Not saying its amazing or the best way to do it.

Thanks,
subbotted
 
Type
Offering
Last edited:
Status
This thread has been locked.
Top