001    package org.bukkit.command.defaults;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.apache.commons.lang.Validate;
007    import org.bukkit.Bukkit;
008    import org.bukkit.GameMode;
009    import org.bukkit.command.Command;
010    import org.bukkit.command.CommandSender;
011    import org.bukkit.util.StringUtil;
012    
013    import com.google.common.collect.ImmutableList;
014    
015    public class DefaultGameModeCommand extends VanillaCommand {
016        private static final List<String> GAMEMODE_NAMES = ImmutableList.of("adventure", "creative", "survival");
017    
018        public DefaultGameModeCommand() {
019            super("defaultgamemode");
020            this.description = "Set the default gamemode";
021            this.usageMessage = "/defaultgamemode <mode>";
022            this.setPermission("bukkit.command.defaultgamemode");
023        }
024    
025        @Override
026        public boolean execute(CommandSender sender, String commandLabel, String[] args) {
027            if (!testPermission(sender)) return true;
028            if (args.length == 0) {
029                sender.sendMessage("Usage: " + usageMessage);
030                return false;
031            }
032    
033            String modeArg = args[0];
034            int value = -1;
035    
036            try {
037                value = Integer.parseInt(modeArg);
038            } catch (NumberFormatException ex) {}
039    
040            GameMode mode = GameMode.getByValue(value);
041    
042            if (mode == null) {
043                if (modeArg.equalsIgnoreCase("creative") || modeArg.equalsIgnoreCase("c")) {
044                    mode = GameMode.CREATIVE;
045                } else if (modeArg.equalsIgnoreCase("adventure") || modeArg.equalsIgnoreCase("a")) {
046                    mode = GameMode.ADVENTURE;
047                } else {
048                    mode = GameMode.SURVIVAL;
049                }
050            }
051    
052            Bukkit.getServer().setDefaultGameMode(mode);
053            Command.broadcastCommandMessage(sender, "Default game mode set to " + mode.toString().toLowerCase());
054    
055            return true;
056        }
057    
058        @Override
059        public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
060            Validate.notNull(sender, "Sender cannot be null");
061            Validate.notNull(args, "Arguments cannot be null");
062            Validate.notNull(alias, "Alias cannot be null");
063    
064            if (args.length == 1) {
065                return StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES, new ArrayList<String>(GAMEMODE_NAMES.size()));
066            }
067    
068            return ImmutableList.of();
069        }
070    }