001 package org.bukkit.command.defaults; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import org.apache.commons.lang.Validate; 007 import org.bukkit.Bukkit; 008 import org.bukkit.ChatColor; 009 import org.bukkit.GameMode; 010 import org.bukkit.command.Command; 011 import org.bukkit.command.CommandSender; 012 import org.bukkit.entity.Player; 013 import org.bukkit.util.StringUtil; 014 015 import com.google.common.collect.ImmutableList; 016 017 public class GameModeCommand extends VanillaCommand { 018 private static final List<String> GAMEMODE_NAMES = ImmutableList.of("adventure", "creative", "survival"); 019 020 public GameModeCommand() { 021 super("gamemode"); 022 this.description = "Changes the player to a specific game mode"; 023 this.usageMessage = "/gamemode <mode> [player]"; 024 this.setPermission("bukkit.command.gamemode"); 025 } 026 027 @Override 028 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 029 if (!testPermission(sender)) return true; 030 if (args.length == 0) { 031 sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); 032 return false; 033 } 034 035 String modeArg = args[0]; 036 String playerArg = sender.getName(); 037 038 if (args.length == 2) { 039 playerArg = args[1]; 040 } 041 042 Player player = Bukkit.getPlayerExact(playerArg); 043 044 if (player != null) { 045 int value = -1; 046 047 try { 048 value = Integer.parseInt(modeArg); 049 } catch (NumberFormatException ex) {} 050 051 GameMode mode = GameMode.getByValue(value); 052 053 if (mode == null) { 054 if (modeArg.equalsIgnoreCase("creative") || modeArg.equalsIgnoreCase("c")) { 055 mode = GameMode.CREATIVE; 056 } else if (modeArg.equalsIgnoreCase("adventure") || modeArg.equalsIgnoreCase("a")) { 057 mode = GameMode.ADVENTURE; 058 } else { 059 mode = GameMode.SURVIVAL; 060 } 061 } 062 063 if (mode != player.getGameMode()) { 064 player.setGameMode(mode); 065 066 if (mode != player.getGameMode()) { 067 sender.sendMessage("Game mode change for " + player.getName() + " failed!"); 068 } else { 069 if (player == sender) { 070 Command.broadcastCommandMessage(sender, "Set own game mode to " + mode.toString() + " mode"); 071 } else { 072 Command.broadcastCommandMessage(sender, "Set " + player.getName() + "'s game mode to " + mode.toString() + " mode"); 073 } 074 } 075 } else { 076 sender.sendMessage(player.getName() + " already has game mode " + mode.getValue()); 077 } 078 } else { 079 sender.sendMessage("Can't find player " + playerArg); 080 } 081 082 return true; 083 } 084 085 @Override 086 public List<String> tabComplete(CommandSender sender, String alias, String[] args) { 087 Validate.notNull(sender, "Sender cannot be null"); 088 Validate.notNull(args, "Arguments cannot be null"); 089 Validate.notNull(alias, "Alias cannot be null"); 090 091 if (args.length == 1) { 092 return StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES, new ArrayList<String>(GAMEMODE_NAMES.size())); 093 } else if (args.length == 2) { 094 return super.tabComplete(sender, alias, args); 095 } 096 return ImmutableList.of(); 097 } 098 }