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.World;
008    import org.bukkit.command.Command;
009    import org.bukkit.command.CommandSender;
010    import org.bukkit.util.StringUtil;
011    
012    import java.util.ArrayList;
013    import java.util.List;
014    import java.util.Random;
015    
016    public class WeatherCommand extends VanillaCommand {
017        private static final List<String> WEATHER_TYPES = ImmutableList.of("clear", "rain", "thunder");
018    
019        public WeatherCommand() {
020            super("weather");
021            this.description = "Changes the weather";
022            this.usageMessage = "/weather <clear/rain/thunder> [duration in seconds]";
023            this.setPermission("bukkit.command.weather");
024        }
025    
026        @Override
027        public boolean execute(CommandSender sender, String currentAlias, String[] args) {
028            if (!testPermission(sender)) return true;
029            if (args.length == 0) {
030                sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
031                return false;
032            }
033    
034            int duration = (300 + new Random().nextInt(600)) * 20;
035            if (args.length >= 2) {
036                duration = getInteger(sender, args[1], 1, 1000000) * 20;
037            }
038    
039            World world = Bukkit.getWorlds().get(0);
040    
041            world.setWeatherDuration(duration);
042            world.setThunderDuration(duration);
043    
044            if ("clear".equalsIgnoreCase(args[0])) {
045                world.setStorm(false);
046                world.setThundering(false);
047                Command.broadcastCommandMessage(sender, "Changed weather to clear for " + (duration / 20) + " seconds.");
048            } else if ("rain".equalsIgnoreCase(args[0])) {
049                world.setStorm(true);
050                world.setThundering(false);
051                Command.broadcastCommandMessage(sender, "Changed weather to rainy for " + (duration / 20) + " seconds.");
052            } else if ("thunder".equalsIgnoreCase(args[0])) {
053                world.setStorm(true);
054                world.setThundering(true);
055                Command.broadcastCommandMessage(sender, "Changed weather to thundering " + (duration / 20) + " seconds.");
056            }
057    
058            return true;
059        }
060    
061        @Override
062        public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
063            Validate.notNull(sender, "Sender cannot be null");
064            Validate.notNull(args, "Arguments cannot be null");
065            Validate.notNull(alias, "Alias cannot be null");
066    
067            if (args.length == 1) {
068                return StringUtil.copyPartialMatches(args[0], WEATHER_TYPES, new ArrayList<String>(WEATHER_TYPES.size()));
069            }
070    
071            return ImmutableList.of();
072        }
073    }