| 1 | package net.minecraft.command;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.entity.player.EntityPlayer;
|
| 5 | import net.minecraft.server.MinecraftServer;
|
| 6 | import net.minecraft.util.BlockPos;
|
| 7 | import net.minecraft.util.ChatComponentTranslation;
|
| 8 | import net.minecraft.util.IChatComponent;
|
| 9 | import net.minecraft.world.WorldSettings;
|
| 10 |
|
| 11 | public class CommandGameMode extends CommandBase
|
| 12 | {
|
| 13 | /**
|
| 14 | * Gets the name of the command
|
| 15 | */
|
| 16 | public String getCommandName()
|
| 17 | {
|
| 18 | return "gamemode";
|
| 19 | }
|
| 20 |
|
| 21 | /**
|
| 22 | * Return the required permission level for this command.
|
| 23 | */
|
| 24 | public int getRequiredPermissionLevel()
|
| 25 | {
|
| 26 | return 2;
|
| 27 | }
|
| 28 |
|
| 29 | /**
|
| 30 | * Gets the usage string for the command.
|
| 31 | */
|
| 32 | public String getCommandUsage(ICommandSender sender)
|
| 33 | {
|
| 34 | return "commands.gamemode.usage";
|
| 35 | }
|
| 36 |
|
| 37 | /**
|
| 38 | * Callback when the command is invoked
|
| 39 | */
|
| 40 | public void processCommand(ICommandSender sender, String[] args) throws CommandException
|
| 41 | {
|
| 42 | if (args.length <= 0)
|
| 43 | {
|
| 44 | throw new WrongUsageException("commands.gamemode.usage", new Object[0]);
|
| 45 | }
|
| 46 | else
|
| 47 | {
|
| 48 | WorldSettings.GameType worldsettings$gametype = this.getGameModeFromCommand(sender, args[0]);
|
| 49 | EntityPlayer entityplayer = args.length >= 2 ? getPlayer(sender, args[1]) : getCommandSenderAsPlayer(sender);
|
| 50 | entityplayer.setGameType(worldsettings$gametype);
|
| 51 | entityplayer.fallDistance = 0.0F;
|
| 52 |
|
| 53 | if (sender.getEntityWorld().getGameRules().getBoolean("sendCommandFeedback"))
|
| 54 | {
|
| 55 | entityplayer.addChatMessage(new ChatComponentTranslation("gameMode.changed", new Object[0]));
|
| 56 | }
|
| 57 |
|
| 58 | IChatComponent ichatcomponent = new ChatComponentTranslation("gameMode." + worldsettings$gametype.getName(), new Object[0]);
|
| 59 |
|
| 60 | if (entityplayer != sender)
|
| 61 | {
|
| 62 | notifyOperators(sender, this, 1, "commands.gamemode.success.other", new Object[] {entityplayer.getName(), ichatcomponent});
|
| 63 | }
|
| 64 | else
|
| 65 | {
|
| 66 | notifyOperators(sender, this, 1, "commands.gamemode.success.self", new Object[] {ichatcomponent});
|
| 67 | }
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | /**
|
| 72 | * Gets the Game Mode specified in the command.
|
| 73 | */
|
| 74 | protected WorldSettings.GameType getGameModeFromCommand(ICommandSender p_71539_1_, String p_71539_2_) throws CommandException, NumberInvalidException
|
| 75 | {
|
| 76 | return !p_71539_2_.equalsIgnoreCase(WorldSettings.GameType.SURVIVAL.getName()) && !p_71539_2_.equalsIgnoreCase("s") ? (!p_71539_2_.equalsIgnoreCase(WorldSettings.GameType.CREATIVE.getName()) && !p_71539_2_.equalsIgnoreCase("c") ? (!p_71539_2_.equalsIgnoreCase(WorldSettings.GameType.ADVENTURE.getName()) && !p_71539_2_.equalsIgnoreCase("a") ? (!p_71539_2_.equalsIgnoreCase(WorldSettings.GameType.SPECTATOR.getName()) && !p_71539_2_.equalsIgnoreCase("sp") ? WorldSettings.getGameTypeById(parseInt(p_71539_2_, 0, WorldSettings.GameType.values().length - 2)) : WorldSettings.GameType.SPECTATOR) : WorldSettings.GameType.ADVENTURE) : WorldSettings.GameType.CREATIVE) : WorldSettings.GameType.SURVIVAL;
|
| 77 | }
|
| 78 |
|
| 79 | public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
|
| 80 | {
|
| 81 | return args.length == 1 ? getListOfStringsMatchingLastWord(args, new String[] {"survival", "creative", "adventure", "spectator"}): (args.length == 2 ? getListOfStringsMatchingLastWord(args, this.getListOfPlayerUsernames()) : null);
|
| 82 | }
|
| 83 |
|
| 84 | /**
|
| 85 | * Returns String array containing all player usernames in the server.
|
| 86 | */
|
| 87 | protected String[] getListOfPlayerUsernames()
|
| 88 | {
|
| 89 | return MinecraftServer.getServer().getAllUsernames();
|
| 90 | }
|
| 91 |
|
| 92 | /**
|
| 93 | * Return whether the specified command parameter index is a username parameter.
|
| 94 | */
|
| 95 | public boolean isUsernameIndex(String[] args, int index)
|
| 96 | {
|
| 97 | return index == 1;
|
| 98 | }
|
| 99 | }
|
| 100 |
|