Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
DefaultGameModeCommand.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.GameMode;
11 import org.bukkit.util.StringUtil;
12 
13 import com.google.common.collect.ImmutableList;
14 
15 public class DefaultGameModeCommand extends VanillaCommand {
16  private static final List<String> GAMEMODE_NAMES = ImmutableList.of("adventure", "creative", "survival");
17 
18  public DefaultGameModeCommand() {
19  super("defaultgamemode");
20  this.description = "Set the default gamemode";
21  this.usageMessage = "/defaultgamemode <mode>";
22  this.setPermission("bukkit.command.defaultgamemode");
23  }
24 
25  @Override
26  public boolean execute(CommandSender sender, String commandLabel, String[] args) {
27  if (!testPermission(sender)) return true;
28  if (args.length == 0) {
29  sender.sendMessage("Usage: " + usageMessage);
30  return false;
31  }
32 
33  String modeArg = args[0];
34  int value = -1;
35 
36  try {
37  value = Integer.parseInt(modeArg);
38  } catch (NumberFormatException ex) {}
39 
40  GameMode mode = GameMode.getByValue(value);
41 
42  if (mode == null) {
43  if (modeArg.equalsIgnoreCase("creative") || modeArg.equalsIgnoreCase("c")) {
44  mode = GameMode.CREATIVE;
45  } else if (modeArg.equalsIgnoreCase("adventure") || modeArg.equalsIgnoreCase("a")) {
46  mode = GameMode.ADVENTURE;
47  } else {
48  mode = GameMode.SURVIVAL;
49  }
50  }
51 
53  Command.broadcastCommandMessage(sender, "Default game mode set to " + mode.toString().toLowerCase());
54 
55  return true;
56  }
57 
58  @Override
59  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
60  Validate.notNull(sender, "Sender cannot be null");
61  Validate.notNull(args, "Arguments cannot be null");
62  Validate.notNull(alias, "Alias cannot be null");
63 
64  if (args.length == 1) {
65  return StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES, new ArrayList<String>(GAMEMODE_NAMES.size()));
66  }
67 
68  return ImmutableList.of();
69  }
70 }
void setDefaultGameMode(GameMode mode)
boolean testPermission(CommandSender target)
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 Server getServer()
Definition: Bukkit.java:50
static GameMode getByValue(final int value)
Definition: GameMode.java:57