001    package org.bukkit.command.defaults;
002    
003    import com.google.common.collect.ImmutableList;
004    import org.apache.commons.lang.Validate;
005    import org.bukkit.Bukkit;
006    import org.bukkit.ChatColor;
007    import org.bukkit.command.Command;
008    import org.bukkit.command.CommandSender;
009    import org.bukkit.util.StringUtil;
010    import org.bukkit.Difficulty;
011    
012    import java.util.ArrayList;
013    import java.util.List;
014    
015    public class DifficultyCommand extends VanillaCommand {
016        private static final List<String> DIFFICULTY_NAMES = ImmutableList.of("peaceful", "easy", "normal", "hard");
017    
018        public DifficultyCommand() {
019            super("difficulty");
020            this.description = "Sets the game difficulty";
021            this.usageMessage = "/difficulty <new difficulty> ";
022            this.setPermission("bukkit.command.difficulty");
023        }
024    
025        @Override
026        public boolean execute(CommandSender sender, String currentAlias, String[] args) {
027            if (!testPermission(sender)) return true;
028            if (args.length != 1 || args[0].length() == 0) {
029                sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
030                return false;
031            }
032    
033            Difficulty difficulty = Difficulty.getByValue(getDifficultyForString(sender, args[0]));
034    
035            if (Bukkit.isHardcore()) {
036                difficulty = Difficulty.HARD;
037            }
038    
039            Bukkit.getWorlds().get(0).setDifficulty(difficulty);
040    
041            int levelCount = 1;
042            if (Bukkit.getAllowNether()) {
043                Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty);
044                levelCount++;
045            }
046    
047            if (Bukkit.getAllowEnd()) {
048                Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty);
049            }
050    
051            Command.broadcastCommandMessage(sender, "Set difficulty to " + difficulty.toString());
052            return true;
053        }
054    
055        protected int getDifficultyForString(CommandSender sender, String name) {
056            if (name.equalsIgnoreCase("peaceful") || name.equalsIgnoreCase("p")) {
057                return 0;
058            } else if (name.equalsIgnoreCase("easy") || name.equalsIgnoreCase("e")) {
059                return 1;
060            } else if (name.equalsIgnoreCase("normal") || name.equalsIgnoreCase("n")) {
061                return 2;
062            } else if (name.equalsIgnoreCase("hard") || name.equalsIgnoreCase("h")) {
063                return 3;
064            } else {
065                return getInteger(sender, name, 0, 3);
066            }
067        }
068    
069        @Override
070        public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
071            Validate.notNull(sender, "Sender cannot be null");
072            Validate.notNull(args, "Arguments cannot be null");
073            Validate.notNull(alias, "Alias cannot be null");
074    
075            if (args.length == 1) {
076                return StringUtil.copyPartialMatches(args[0], DIFFICULTY_NAMES, new ArrayList<String>(DIFFICULTY_NAMES.size()));
077            }
078    
079            return ImmutableList.of();
080        }
081    }