Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
GameModeCommand.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.GameMode;
10 import org.bukkit.command.Command;
12 import org.bukkit.entity.Player;
13 import org.bukkit.util.StringUtil;
14 
15 import com.google.common.collect.ImmutableList;
16 
17 public class GameModeCommand extends VanillaCommand {
18  private static final List<String> GAMEMODE_NAMES = ImmutableList.of("adventure", "creative", "survival");
19 
20  public GameModeCommand() {
21  super("gamemode");
22  this.description = "Changes the player to a specific game mode";
23  this.usageMessage = "/gamemode <mode> [player]";
24  this.setPermission("bukkit.command.gamemode");
25  }
26 
27  @Override
28  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
29  if (!testPermission(sender)) return true;
30  if (args.length == 0) {
31  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
32  return false;
33  }
34 
35  String modeArg = args[0];
36  String playerArg = sender.getName();
37 
38  if (args.length == 2) {
39  playerArg = args[1];
40  }
41 
42  Player player = Bukkit.getPlayerExact(playerArg);
43 
44  if (player != null) {
45  int value = -1;
46 
47  try {
48  value = Integer.parseInt(modeArg);
49  } catch (NumberFormatException ex) {}
50 
51  GameMode mode = GameMode.getByValue(value);
52 
53  if (mode == null) {
54  if (modeArg.equalsIgnoreCase("creative") || modeArg.equalsIgnoreCase("c")) {
55  mode = GameMode.CREATIVE;
56  } else if (modeArg.equalsIgnoreCase("adventure") || modeArg.equalsIgnoreCase("a")) {
57  mode = GameMode.ADVENTURE;
58  } else {
59  mode = GameMode.SURVIVAL;
60  }
61  }
62 
63  if (mode != player.getGameMode()) {
64  player.setGameMode(mode);
65 
66  if (mode != player.getGameMode()) {
67  sender.sendMessage("Game mode change for " + player.getName() + " failed!");
68  } else {
69  if (player == sender) {
70  Command.broadcastCommandMessage(sender, "Set own game mode to " + mode.toString() + " mode");
71  } else {
72  Command.broadcastCommandMessage(sender, "Set " + player.getName() + "'s game mode to " + mode.toString() + " mode");
73  }
74  }
75  } else {
76  sender.sendMessage(player.getName() + " already has game mode " + mode.getValue());
77  }
78  } else {
79  sender.sendMessage("Can't find player " + playerArg);
80  }
81 
82  return true;
83  }
84 
85  @Override
86  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
87  Validate.notNull(sender, "Sender cannot be null");
88  Validate.notNull(args, "Arguments cannot be null");
89  Validate.notNull(alias, "Alias cannot be null");
90 
91  if (args.length == 1) {
92  return StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES, new ArrayList<String>(GAMEMODE_NAMES.size()));
93  } else if (args.length == 2) {
94  return super.tabComplete(sender, alias, args);
95  }
96  return ImmutableList.of();
97  }
98 }
boolean testPermission(CommandSender target)
void setGameMode(GameMode mode)
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 GameMode getByValue(final int value)
Definition: GameMode.java:57
static Player getPlayerExact(String name)
Definition: Bukkit.java:436