Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
TimeCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import org.apache.commons.lang.Validate;
7 import org.bukkit.Bukkit;
8 import org.bukkit.ChatColor;
9 import org.bukkit.World;
10 import org.bukkit.command.Command;
12 import org.bukkit.util.StringUtil;
13 
14 import com.google.common.collect.ImmutableList;
15 
16 public class TimeCommand extends VanillaCommand {
17  private static final List<String> TABCOMPLETE_ADD_SET = ImmutableList.of("add", "set");
18  private static final List<String> TABCOMPLETE_DAY_NIGHT = ImmutableList.of("day", "night");
19 
20  public TimeCommand() {
21  super("time");
22  this.description = "Changes the time on each world";
23  this.usageMessage = "/time set <value>\n/time add <value>";
24  this.setPermission("bukkit.command.time.add;bukkit.command.time.set");
25  }
26 
27  @Override
28  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
29  if (args.length < 2) {
30  sender.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage:\n" + usageMessage);
31  return false;
32  }
33 
34  int value;
35 
36  if (args[0].equals("set")) {
37  if (!sender.hasPermission("bukkit.command.time.set")) {
38  sender.sendMessage(ChatColor.RED + "You don't have permission to set the time");
39  return true;
40  }
41 
42  if (args[1].equals("day")) {
43  value = 0;
44  } else if (args[1].equals("night")) {
45  value = 12500;
46  } else {
47  value = getInteger(sender, args[1], 0);
48  }
49 
50  for (World world : Bukkit.getWorlds()) {
51  world.setTime(value);
52  }
53 
54  Command.broadcastCommandMessage(sender, "Set time to " + value);
55  } else if (args[0].equals("add")) {
56  if (!sender.hasPermission("bukkit.command.time.add")) {
57  sender.sendMessage(ChatColor.RED + "You don't have permission to set the time");
58  return true;
59  }
60 
61  value = getInteger(sender, args[1], 0);
62 
63  for (World world : Bukkit.getWorlds()) {
64  world.setFullTime(world.getFullTime() + value);
65  }
66 
67  Command.broadcastCommandMessage(sender, "Added " + value + " to time");
68  } else {
69  sender.sendMessage("Unknown method. Usage: " + usageMessage);
70  }
71 
72  return true;
73  }
74 
75  @Override
76  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
77  Validate.notNull(sender, "Sender cannot be null");
78  Validate.notNull(args, "Arguments cannot be null");
79  Validate.notNull(alias, "Alias cannot be null");
80 
81  if (args.length == 1) {
82  return StringUtil.copyPartialMatches(args[0], TABCOMPLETE_ADD_SET, new ArrayList<String>(TABCOMPLETE_ADD_SET.size()));
83  } else if (args.length == 2 && args[0].equalsIgnoreCase("set")) {
84  return StringUtil.copyPartialMatches(args[1], TABCOMPLETE_DAY_NIGHT, new ArrayList<String>(TABCOMPLETE_DAY_NIGHT.size()));
85  }
86  return ImmutableList.of();
87  }
88 }
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
boolean hasPermission(String name)
void sendMessage(String message)
void setPermission(String permission)
static List< World > getWorlds()
Definition: Bukkit.java:229