TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandgamemode.java

87 lines
2.5 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import java.util.Locale;
import org.bukkit.GameMode;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commandgamemode extends EssentialsCommand
{
public Commandgamemode()
{
super("gamemode");
}
@Override
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
if (args.length < 2)
{
throw new NotEnoughArgumentsException();
}
gamemodeOtherPlayers(server, sender, args);
}
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
2012-08-03 08:39:45 +00:00
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
2012-08-03 08:39:45 +00:00
2012-08-04 11:17:41 +00:00
if (args.length > 1 && args[1].trim().length() > 2 && user.isAuthorized("essentials.gamemode.others"))
{
gamemodeOtherPlayers(server, user, args);
return;
2012-08-03 08:39:45 +00:00
}
2012-08-03 08:41:23 +00:00
performSetMode(args[0].toLowerCase(Locale.ENGLISH), user);
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
{
//TODO: TL this
if (args[1].trim().length() < 2)
{
throw new NotEnoughArgumentsException("You need to specify a valid player/mode.");
}
boolean foundUser = false;
for (Player matchPlayer : server.matchPlayer(args[1]))
{
final User player = ess.getUser(matchPlayer);
if (player.isHidden())
{
continue;
2012-08-03 08:39:45 +00:00
}
2012-08-03 08:41:23 +00:00
performSetMode(args[0].toLowerCase(Locale.ENGLISH), player);
sender.sendMessage(_("gameMode", _(player.getGameMode().toString().toLowerCase(Locale.ENGLISH)), player.getDisplayName()));
foundUser = true;
}
if (!foundUser)
{
throw new NotEnoughArgumentsException(_("playerNotFound"));
}
}
2012-08-03 08:39:45 +00:00
private void performSetMode(String mode, Player player)
{
2012-08-03 08:39:45 +00:00
if (mode.contains("survi") || mode.equalsIgnoreCase("0") || mode.equalsIgnoreCase("s"))
{
player.setGameMode(GameMode.SURVIVAL);
}
else if (mode.contains("creat") || mode.equalsIgnoreCase("1") || mode.equalsIgnoreCase("c"))
{
player.setGameMode(GameMode.CREATIVE);
}
else if (mode.contains("advent") || mode.equalsIgnoreCase("2") || mode.equalsIgnoreCase("a"))
{
player.setGameMode(GameMode.ADVENTURE);
}
}
}