    001    package net.minecraft.command;
    002
    003    import java.util.List;
    004    import net.minecraft.entity.player.EntityPlayerMP;
    005    import net.minecraft.server.MinecraftServer;
    006    import net.minecraft.util.StatCollector;
    007    import net.minecraft.world.EnumGameType;
    008    import net.minecraft.world.WorldSettings;
    009
    010    public class CommandGameMode extends CommandBase
    011    {
    012        public String getCommandName()
    013        {
    014            return "gamemode";
    015        }
    016
    017        /**
    018         * Return the required permission level for this command.
    019         */
    020        public int getRequiredPermissionLevel()
    021        {
    022            return 2;
    023        }
    024
    025        public String getCommandUsage(ICommandSender par1ICommandSender)
    026        {
    027            return par1ICommandSender.translateString("commands.gamemode.usage", new Object[0]);
    028        }
    029
    030        public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    031        {
    032            if (par2ArrayOfStr.length > 0)
    033            {
    034                EnumGameType var3 = this.getGameModeFromCommand(par1ICommandSender, par2ArrayOfStr[0]);
    035                EntityPlayerMP var4 = par2ArrayOfStr.length >= 2 ? func_82359_c(par1ICommandSender, par2ArrayOfStr[1]) : getCommandSenderAsPlayer(par1ICommandSender);
    036                var4.sendGameTypeToPlayer(var3);
    037                var4.fallDistance = 0.0F;
    038                String var5 = StatCollector.translateToLocal("gameMode." + var3.getName());
    039
    040                if (var4 != par1ICommandSender)
    041                {
    042                    notifyAdmins(par1ICommandSender, 1, "commands.gamemode.success.other", new Object[] {var4.getEntityName(), var5});
    043                }
    044                else
    045                {
    046                    notifyAdmins(par1ICommandSender, 1, "commands.gamemode.success.self", new Object[] {var5});
    047                }
    048            }
    049            else
    050            {
    051                throw new WrongUsageException("commands.gamemode.usage", new Object[0]);
    052            }
    053        }
    054
    055        /**
    056         * Gets the Game Mode specified in the command.
    057         */
    058        protected EnumGameType getGameModeFromCommand(ICommandSender par1ICommandSender, String par2Str)
    059        {
    060            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;
    061        }
    062
    063        /**
    064         * Adds the strings available in this command to the given list of tab completion options.
    065         */
    066        public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    067        {
    068            return par2ArrayOfStr.length == 1 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, new String[] {"survival", "creative", "adventure"}): (par2ArrayOfStr.length == 2 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, this.getListOfPlayerUsernames()) : null);
    069        }
    070
    071        /**
    072         * Returns String array containing all player usernames in the server.
    073         */
    074        protected String[] getListOfPlayerUsernames()
    075        {
    076            return MinecraftServer.getServer().getAllUsernames();
    077        }
    078
    079        /**
    080         * Return whether the specified command parameter index is a username parameter.
    081         */
    082        public boolean isUsernameIndex(int par1)
    083        {
    084            return par1 == 1;
    085        }
    086    }