Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
GameRuleCommand.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.ChatColor;
10 
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14 import org.bukkit.Bukkit;
15 import org.bukkit.World;
17 
18 public class GameRuleCommand extends VanillaCommand {
19  private static final List<String> GAMERULE_STATES = ImmutableList.of("true", "false");
20 
21  public GameRuleCommand() {
22  super("gamerule");
23  this.description = "Sets a server's game rules";
24  this.usageMessage = "/gamerule <rule name> <value> OR /gamerule <rule name>";
25  this.setPermission("bukkit.command.gamerule");
26  }
27 
28  @Override
29  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
30  if (!testPermission(sender)) return true;
31 
32  if (args.length > 0) {
33  String rule = args[0];
34  World world = getGameWorld(sender);
35 
36  if (world.isGameRule(rule)) {
37  if (args.length > 1) {
38  String value = args[1];
39 
40  world.setGameRuleValue(rule, value);
41  Command.broadcastCommandMessage(sender, "Game rule " + rule + " has been set to: " + value);
42  } else {
43  String value = world.getGameRuleValue(rule);
44  sender.sendMessage(rule + " = " + value);
45  }
46  } else {
47  sender.sendMessage(ChatColor.RED + "No game rule called " + rule + " is available");
48  }
49 
50  return true;
51  } else {
52  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
53  sender.sendMessage("Rules: " + this.createString(getGameWorld(sender).getGameRules(), 0, ", "));
54 
55  return true;
56  }
57  }
58 
59  private World getGameWorld(CommandSender sender) {
60  if (sender instanceof HumanEntity) {
61  World world = ((HumanEntity) sender).getWorld();
62  if (world != null) {
63  return world;
64  }
65  } else if (sender instanceof BlockCommandSender) {
66  return ((BlockCommandSender) sender).getBlock().getWorld();
67  }
68 
69  return Bukkit.getWorlds().get(0);
70  }
71 
72  @Override
73  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
74  Validate.notNull(sender, "Sender cannot be null");
75  Validate.notNull(args, "Arguments cannot be null");
76  Validate.notNull(alias, "Alias cannot be null");
77 
78  if (args.length == 1) {
79  return StringUtil.copyPartialMatches(args[0], Arrays.asList(getGameWorld(sender).getGameRules()), new ArrayList<String>());
80  }
81 
82  if (args.length == 2) {
83  return StringUtil.copyPartialMatches(args[1], GAMERULE_STATES, new ArrayList<String>(GAMERULE_STATES.size()));
84  }
85 
86  return ImmutableList.of();
87  }
88 }
boolean isGameRule(String rule)
boolean testPermission(CommandSender target)
String getGameRuleValue(String rule)
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
void sendMessage(String message)
void setPermission(String permission)
static List< World > getWorlds()
Definition: Bukkit.java:229
boolean setGameRuleValue(String rule, String value)