    001    package net.minecraft.src;
    002
    003    import java.util.List;
    004    import net.minecraft.server.MinecraftServer;
    005
    006    public class CommandGameMode extends CommandBase
    007    {
    008        public String getCommandName()
    009        {
    010            return "gamemode";
    011        }
    012
    013        public String getCommandUsage(ICommandSender par1ICommandSender)
    014        {
    015            return par1ICommandSender.translateString("commands.gamemode.usage", new Object[0]);
    016        }
    017
    018        public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    019        {
    020            if (par2ArrayOfStr.length > 0)
    021            {
    022                EnumGameType var3 = this.getGameModeFromCommand(par1ICommandSender, par2ArrayOfStr[0]);
    023                EntityPlayer var4 = par2ArrayOfStr.length >= 2 ? this.getGameModeTargetPlayer(par2ArrayOfStr[1]) : getCommandSenderAsPlayer(par1ICommandSender);
    024                var4.sendGameTypeToPlayer(var3);
    025                String var5 = StatCollector.translateToLocal("gameMode." + var3.getName());
    026
    027                if (var4 != par1ICommandSender)
    028                {
    029                    notifyAdmins(par1ICommandSender, 1, "commands.gamemode.success.other", new Object[] {var4.getEntityName(), var5});
    030                }
    031                else
    032                {
    033                    notifyAdmins(par1ICommandSender, 1, "commands.gamemode.success.self", new Object[] {var5});
    034                }
    035            }
    036            else
    037            {
    038                throw new WrongUsageException("commands.gamemode.usage", new Object[0]);
    039            }
    040        }
    041
    042        /**
    043         * Gets the Game Mode specified in the command.
    044         */
    045        protected EnumGameType getGameModeFromCommand(ICommandSender par1ICommandSender, String par2Str)
    046        {
    047            return !par2Str.equalsIgnoreCase(EnumGameType.SURVIVAL.getName()) && !par2Str.equalsIgnoreCase("s") ? (!par2Str.equalsIgnoreCase(EnumGameType.CREATIVE.getName()) && !par2Str.equalsIgnoreCase("c") ? (!par2Str.equalsIgnoreCase(EnumGameType.ADVENTURE.getName()) && !par2Str.equalsIgnoreCase("a") ? WorldSettings.getGameTypeById(parseIntBounded(par1ICommandSender, par2Str, 0, EnumGameType.values().length - 2)) : EnumGameType.ADVENTURE) : EnumGameType.CREATIVE) : EnumGameType.SURVIVAL;
    048        }
    049
    050        /**
    051         * Adds the strings available in this command to the given list of tab completion options.
    052         */
    053        public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    054        {
    055            return par2ArrayOfStr.length == 1 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, new String[] {"survival", "creative", "adventure"}): (par2ArrayOfStr.length == 2 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, this.getListOfPlayerUsernames()) : null);
    056        }
    057
    058        /**
    059         * Returns the target player for whom the gamemode should be changed.
    060         */
    061        protected EntityPlayer getGameModeTargetPlayer(String par1Str)
    062        {
    063            EntityPlayerMP var2 = MinecraftServer.getServer().getConfigurationManager().getPlayerForUsername(par1Str);
    064
    065            if (var2 == null)
    066            {
    067                throw new PlayerNotFoundException();
    068            }
    069            else
    070            {
    071                return var2;
    072            }
    073        }
    074
    075        /**
    076         * Returns String array containing all player usernames in the server.
    077         */
    078        protected String[] getListOfPlayerUsernames()
    079        {
    080            return MinecraftServer.getServer().getAllUsernames();
    081        }
    082    }