package org.royaldev.royalcommands.rcommands;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.royaldev.royalcommands.RUtils;
import org.royaldev.royalcommands.RoyalCommands;

public class CmdGamemode implements CommandExecutor {

    RoyalCommands plugin;

    public CmdGamemode(RoyalCommands plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("gamemode")) {
            if (!plugin.isAuthorized(cs, "rcmds.gamemode")) {
                RUtils.dispNoPerms(cs);
                return true;
            }
            if (!(cs instanceof Player) && args.length < 1) {
                cs.sendMessage(cmd.getDescription());
                return false;
            }
            if (args.length < 1) {
                Player p = (Player) cs;
                GameMode toSet = (p.getGameMode().equals(GameMode.CREATIVE)) ? GameMode.SURVIVAL : GameMode.CREATIVE;
                p.setGameMode(toSet);
                p.sendMessage(ChatColor.BLUE + "Your game mode has been set to " + ChatColor.GRAY + toSet.name().toLowerCase() + ChatColor.BLUE + ".");
                return true;
            }
            if (args.length > 0) {
                Player t = plugin.getServer().getPlayer(args[0].trim());
                if (t == null || plugin.isVanished(t, cs)) {
                    cs.sendMessage(ChatColor.RED + "That player does not exist!");
                    return true;
                }
                if (!t.equals(cs) && !plugin.isAuthorized(cs, "rcmds.others.gamemode")) {
                    cs.sendMessage(ChatColor.RED + "You can't change other players' gamemodes!");
                    return true;
                }
                if (!t.equals(cs) && plugin.isAuthorized(t, "rcmds.exempt.gamemode")) {
                    cs.sendMessage(ChatColor.RED + "You cannot change that player's gamemode.");
                    return true;
                }
                GameMode toSet = (t.getGameMode().equals(GameMode.CREATIVE)) ? GameMode.SURVIVAL : GameMode.CREATIVE;
                if (args.length > 1) {
                    String wantedMode = args[1];
                    try {
                        toSet = GameMode.valueOf(wantedMode.toUpperCase());
                    } catch (IllegalArgumentException e) {
                        cs.sendMessage(ChatColor.RED + "Invalid gamemode!");
                        cs.sendMessage(ChatColor.GRAY + "creative" + ChatColor.RESET + "," + ChatColor.GRAY + " survival" + ChatColor.RESET + ", " + ChatColor.GRAY + "adventure");
                        return true;
                    }
                }
                t.setGameMode(toSet);
                if (cs instanceof Player && !cs.equals(t))
                    cs.sendMessage(ChatColor.BLUE + "You have changed " + ChatColor.GRAY + t.getName() + "\'s" + ChatColor.BLUE + " game mode to " + ChatColor.GRAY + toSet.name().toLowerCase() + ChatColor.BLUE + ".");
                t.sendMessage(ChatColor.BLUE + "Your game mode has been changed to " + ChatColor.GRAY + toSet.name().toLowerCase() + ChatColor.BLUE + ".");
                return true;
            }
        }
        return false;
    }
}

@@ -507,16 +507,1116 @@
 %0A    %7D%0A%0A
+    /**%0A     * Gets a GameMode from a string. Can be in word format (%22creative%22) or number format (%221%22).%0A     *%0A     * @param s String representing GameMode%0A     * @return GameMode%0A     */%0A    public GameMode getGameMode(String s) %7B%0A        if (s == null) return null;%0A        GameMode toRet;%0A        try %7B%0A            toRet = GameMode.valueOf(s.toUpperCase());%0A        %7D catch (IllegalArgumentException e) %7B%0A            try %7B%0A                Integer i = Integer.valueOf(s);%0A                if (i %3E 2 %7C%7C i %3C 0) return null;%0A                switch (i) %7B%0A                    case 0:%0A                        toRet = GameMode.SURVIVAL;%0A                        break;%0A                    case 1:%0A                        toRet = GameMode.CREATIVE;%0A                        break;%0A                    case 2:%0A                        toRet = GameMode.ADVENTURE;%0A                        break;%0A                    default:%0A                        toRet = null;%0A                %7D%0A            %7D catch (NumberFormatException e2) %7B%0A                return null;%0A            %7D%0A        %7D%0A        return toRet;%0A    %7D%0A%0A
     @Ove
@@ -3460,194 +3460,86 @@
     
-String wantedMode = args%5B1%5D;%0A                    try %7B%0A                        toSet = GameMode.valueOf(wantedMode.toUpperCase());%0A                    %7D catch (IllegalArgumentException e
+GameMode result = getGameMode(args%5B1%5D);%0A                    if (result == null
 ) %7B%0A
@@ -3675,75 +3675,38 @@
 Y + 
-%22creative%22 + ChatColor.RESET + %22,%22 + ChatColor.GRAY + %22 survival%22 +
+RUtils.join(GameMode.values(),
  Cha
@@ -3745,22 +3745,9 @@
 GRAY
- + %22adventure%22
+)
 );%0A 
@@ -3796,32 +3796,68 @@
               %7D%0A
+                    toSet = result;%0A
                 

Use numbers in /gamemode