    001package net.minecraft.command;
    002
    003import java.util.List;
    004import net.minecraft.entity.player.EntityPlayerMP;
    005import net.minecraft.server.MinecraftServer;
    006import net.minecraft.util.StatCollector;
    007import net.minecraft.world.EnumGameType;
    008import net.minecraft.world.WorldSettings;
    009
    010public 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 enumgametype = this.getGameModeFromCommand(par1ICommandSender, par2ArrayOfStr[0]);
    035            EntityPlayerMP entityplayermp = par2ArrayOfStr.length >= 2 ? func_82359_c(par1ICommandSender, par2ArrayOfStr[1]) : getCommandSenderAsPlayer(par1ICommandSender);
    036            entityplayermp.setGameType(enumgametype);
    037            entityplayermp.fallDistance = 0.0F;
    038            String s = StatCollector.translateToLocal("gameMode." + enumgametype.getName());
    039
    040            if (entityplayermp != par1ICommandSender)
    041            {
    042                notifyAdmins(par1ICommandSender, 1, "commands.gamemode.success.other", new Object[] {entityplayermp.getEntityName(), s});
    043            }
    044            else
    045            {
    046                notifyAdmins(par1ICommandSender, 1, "commands.gamemode.success.self", new Object[] {s});
    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(String[] par1ArrayOfStr, int par2)
    083    {
    084        return par2 == 1;
    085    }
    086}