Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
WeatherCommand.java
1 package org.bukkit.command.defaults;
2 
3 import com.google.common.collect.ImmutableList;
4 import org.apache.commons.lang.Validate;
5 import org.bukkit.Bukkit;
6 import org.bukkit.ChatColor;
7 import org.bukkit.World;
10 import org.bukkit.util.StringUtil;
11 
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Random;
15 
16 public class WeatherCommand extends VanillaCommand {
17  private static final List<String> WEATHER_TYPES = ImmutableList.of("clear", "rain", "thunder");
18 
19  public WeatherCommand() {
20  super("weather");
21  this.description = "Changes the weather";
22  this.usageMessage = "/weather <clear/rain/thunder> [duration in seconds]";
23  this.setPermission("bukkit.command.weather");
24  }
25 
26  @Override
27  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
28  if (!testPermission(sender)) return true;
29  if (args.length == 0) {
30  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
31  return false;
32  }
33 
34  int duration = (300 + new Random().nextInt(600)) * 20;
35  if (args.length >= 2) {
36  duration = getInteger(sender, args[1], 1, 1000000) * 20;
37  }
38 
39  World world = Bukkit.getWorlds().get(0);
40 
41  world.setWeatherDuration(duration);
42  world.setThunderDuration(duration);
43 
44  if ("clear".equalsIgnoreCase(args[0])) {
45  world.setStorm(false);
46  world.setThundering(false);
47  Command.broadcastCommandMessage(sender, "Changed weather to clear for " + (duration / 20) + " seconds.");
48  } else if ("rain".equalsIgnoreCase(args[0])) {
49  world.setStorm(true);
50  world.setThundering(false);
51  Command.broadcastCommandMessage(sender, "Changed weather to rainy for " + (duration / 20) + " seconds.");
52  } else if ("thunder".equalsIgnoreCase(args[0])) {
53  world.setStorm(true);
54  world.setThundering(true);
55  Command.broadcastCommandMessage(sender, "Changed weather to thundering " + (duration / 20) + " seconds.");
56  }
57 
58  return true;
59  }
60 
61  @Override
62  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
63  Validate.notNull(sender, "Sender cannot be null");
64  Validate.notNull(args, "Arguments cannot be null");
65  Validate.notNull(alias, "Alias cannot be null");
66 
67  if (args.length == 1) {
68  return StringUtil.copyPartialMatches(args[0], WEATHER_TYPES, new ArrayList<String>(WEATHER_TYPES.size()));
69  }
70 
71  return ImmutableList.of();
72  }
73 }
void setWeatherDuration(int duration)
boolean testPermission(CommandSender target)
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
void setThunderDuration(int duration)
void sendMessage(String message)
void setPermission(String permission)
void setStorm(boolean hasStorm)
static List< World > getWorlds()
Definition: Bukkit.java:229
void setThundering(boolean thundering)