summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/EssentialsToggleCommand.java
blob: 6bec3afa6612caf30c7d8927fd7447d93d76e258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.earth2me.essentials.commands;

import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public abstract class EssentialsToggleCommand extends EssentialsCommand
{
	String othersPermission;

	public EssentialsToggleCommand(String command, String othersPermission)
	{
		super(command);
		this.othersPermission = othersPermission;
	}

	protected Boolean matchToggleArgument(final String arg)
	{
		if (arg.equalsIgnoreCase("on") || arg.startsWith("ena") || arg.equalsIgnoreCase("1"))
		{
			return true;
		}
		else if (arg.equalsIgnoreCase("off") || arg.startsWith("dis") || arg.equalsIgnoreCase("0"))
		{
			return false;
		}
		return null;
	}

	protected void toggleOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws PlayerNotFoundException, NotEnoughArgumentsException
	{
		if (args.length < 1 || args[0].trim().length() < 2)
		{
			throw new PlayerNotFoundException();
		}

		boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
		boolean foundUser = false;
		final List<Player> matchedPlayers = server.matchPlayer(args[0]);
		for (Player matchPlayer : matchedPlayers)
		{
			final User player = ess.getUser(matchPlayer);
			if (skipHidden && player.isHidden())
			{
				continue;
			}
			foundUser = true;
			if (args.length > 1)
			{
				Boolean toggle = matchToggleArgument(args[1]);
				if (toggle == true)
				{
					togglePlayer(sender, player, true);
				}
				else
				{
					togglePlayer(sender, player, false);
				}
			}
			else
			{
				togglePlayer(sender, player, null);
			}
		}
		if (!foundUser)
		{
			throw new PlayerNotFoundException();
		}
	}

	// Make sure when implementing this method that all 3 Boolean states are handled, 'null' should toggle the existing state.
	abstract void togglePlayer(CommandSender sender, User user, Boolean enabled) throws NotEnoughArgumentsException;
}