usage = "[player] [gamemode]", desc = "Change a player's gamemode",
flags = "c", min = 0, max = 2)
public void gamemode(CommandContext args, CommandSender sender) throws CommandException {
Player player;
GameMode mode = null;
boolean change = false;
if (args.argsLength() == 0) { // check self
// check current player
player = PlayerUtil.checkPlayer(sender);
mode = player.getGameMode();
} else {
if (args.hasFlag('c')) { //check other player
player = InputUtil.PlayerParser.matchSinglePlayer(sender, args.getString(0));
mode = player.getGameMode();
} else {
change = true;
// we're going to assume that the first arg of one is mode, but the first of two is player
// if they want to check another player, they should use -c instead, since we can't guess
// reliably whether (with a single arg) they meant a player or a mode
String modeString;
if (args.argsLength() == 1) { // self mode
modeString = args.getString(0);
player = PlayerUtil.checkPlayer(sender);
} else { // 2 - first is player, second mode
// HERP DERP VANILLA COMMAND BLOCKS
try {
modeString = String.valueOf(args.getInteger(0));
player = InputUtil.PlayerParser.matchSinglePlayer(sender, args.getString(1));
} catch (NumberFormatException e) {
// NOPE NOT VANILLA COMMAND BLOCKS
player = InputUtil.PlayerParser.matchSinglePlayer(sender, args.getString(0));
modeString = args.getString(1);
}
}
CommandBook.inst().checkPermission(sender, "commandbook.gamemode."
+ (change ? "change" : "check")
+ (player != sender ? ".other" : ""));
try {
mode = GameMode.valueOf(modeString.toUpperCase());
} catch (IllegalArgumentException e) {
try {
mode = GameMode.getByValue(Integer.parseInt(modeString));
} catch (NumberFormatException ignored) {}
}
if (mode == null) {
throw new CommandException("Unrecognized gamemode: " + modeString + ".");
}
}
}
if (player == null || mode == null) {
throw new CommandException("Something went wrong, please try again.");
}
String message;
if (change) {
if (player.getGameMode() == mode) {
message = " already had gamemode " + mode.toString();
change = false;
} else {
message = " changed to gamemode " + mode.toString();
}
} else {
message = " currently has gamemode " + mode.toString();
}
if (change) {
player.setGameMode(mode);
}
sender.sendMessage(ChatColor.YELLOW + "Player " + (CommandBook.inst().useDisplayNames ? player.getDisplayName() : player.getName())