Add command name matching to /gamemode

This allows aliases such as /gmc and /creative [player] to be used without stating gamemode as a parameter.
This commit is contained in:
KHobbits 2012-08-12 01:13:52 +01:00
parent f17a790d78
commit c40b23d80e

View file

@ -23,44 +23,57 @@ public class Commandgamemode extends EssentialsCommand
{ {
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
gamemodeOtherPlayers(server, sender, args); GameMode gameMode = matchGameMode(args[0].toLowerCase(Locale.ENGLISH));
gamemodeOtherPlayers(server, sender, gameMode, args[1]);
} }
@Override @Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{ {
if (args.length < 1) GameMode gameMode;
if (args.length == 0)
{ {
throw new NotEnoughArgumentsException(); gameMode = matchGameMode(commandLabel);
} }
else if (args.length > 1 && args[1].trim().length() > 2 && user.isAuthorized("essentials.gamemode.others"))
if (args.length > 1 && args[1].trim().length() > 2 && user.isAuthorized("essentials.gamemode.others"))
{ {
gamemodeOtherPlayers(server, user, args); gameMode = matchGameMode(args[0].toLowerCase(Locale.ENGLISH));
gamemodeOtherPlayers(server, user, gameMode, args[1]);
return; return;
} }
performSetMode(args[0].toLowerCase(Locale.ENGLISH), user); else
{
try {
gameMode = matchGameMode(args[0].toLowerCase(Locale.ENGLISH));
}
catch (NotEnoughArgumentsException e) {
gameMode = matchGameMode(commandLabel);
gamemodeOtherPlayers(server, user, gameMode, args[0]);
return;
}
}
user.setGameMode(gameMode);
user.sendMessage(_("gameMode", _(user.getGameMode().toString().toLowerCase(Locale.ENGLISH)), user.getDisplayName())); user.sendMessage(_("gameMode", _(user.getGameMode().toString().toLowerCase(Locale.ENGLISH)), user.getDisplayName()));
} }
private void gamemodeOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException private void gamemodeOtherPlayers(final Server server, final CommandSender sender, final GameMode gameMode, final String player) throws NotEnoughArgumentsException
{ {
//TODO: TL this //TODO: TL this
if (args[1].trim().length() < 2) if (player.trim().length() < 2)
{ {
throw new NotEnoughArgumentsException("You need to specify a valid player/mode."); throw new NotEnoughArgumentsException("You need to specify a valid player/mode.");
} }
boolean foundUser = false; boolean foundUser = false;
for (Player matchPlayer : server.matchPlayer(args[1])) for (Player matchPlayer : server.matchPlayer(player))
{ {
final User player = ess.getUser(matchPlayer); final User user = ess.getUser(matchPlayer);
if (player.isHidden()) if (user.isHidden())
{ {
continue; continue;
} }
performSetMode(args[0].toLowerCase(Locale.ENGLISH), player); user.setGameMode(gameMode);
sender.sendMessage(_("gameMode", _(player.getGameMode().toString().toLowerCase(Locale.ENGLISH)), player.getDisplayName())); sender.sendMessage(_("gameMode", _(user.getGameMode().toString().toLowerCase(Locale.ENGLISH)), user.getDisplayName()));
foundUser = true; foundUser = true;
} }
if (!foundUser) if (!foundUser)
@ -69,19 +82,27 @@ public class Commandgamemode extends EssentialsCommand
} }
} }
private void performSetMode(String mode, Player player) private GameMode matchGameMode(String modeString) throws NotEnoughArgumentsException
{ {
if (mode.contains("survi") || mode.equalsIgnoreCase("0") || mode.equalsIgnoreCase("s")) GameMode mode = null;
if (modeString.equalsIgnoreCase("gmc") || modeString.equalsIgnoreCase("egmc")
|| modeString.contains("creat") || modeString.equalsIgnoreCase("1") || modeString.equalsIgnoreCase("c"))
{ {
player.setGameMode(GameMode.SURVIVAL); mode = GameMode.CREATIVE;
} }
else if (mode.contains("creat") || mode.equalsIgnoreCase("1") || mode.equalsIgnoreCase("c")) else if (modeString.equalsIgnoreCase("gms") || modeString.equalsIgnoreCase("egms")
|| modeString.contains("survi") || modeString.equalsIgnoreCase("0") || modeString.equalsIgnoreCase("s"))
{ {
player.setGameMode(GameMode.CREATIVE); mode = GameMode.SURVIVAL;
} }
else if (mode.contains("advent") || mode.equalsIgnoreCase("2") || mode.equalsIgnoreCase("a")) else if (modeString.equalsIgnoreCase("gma") || modeString.equalsIgnoreCase("egma")
|| modeString.contains("advent") || modeString.equalsIgnoreCase("2") || modeString.equalsIgnoreCase("a"))
{ {
player.setGameMode(GameMode.ADVENTURE); mode = GameMode.ADVENTURE;
} }
else {
throw new NotEnoughArgumentsException();
}
return mode;
} }
} }