summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/command/CommandHandler.java
blob: 075d64c73e6b8a611fc2c69eb794962b8bef84fc (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.earth2me.essentials.anticheat.command;

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.config.Permissions;
import java.util.*;
import java.util.Map.Entry;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;


/**
 * Handle all NoCheat related commands in a common place
 */
public class CommandHandler
{
	private final List<Permission> perms;

	public CommandHandler(NoCheat plugin)
	{
		// Make a copy to allow sorting
		perms = new LinkedList<Permission>(plugin.getDescription().getPermissions());

		// Sort NoCheats permission by name and parent-child relation with
		// a custom sorting method
		Collections.sort(perms, new Comparator<Permission>()
		{
			public int compare(Permission o1, Permission o2)
			{

				String name1 = o1.getName();
				String name2 = o2.getName();

				if (name1.equals(name2))
				{
					return 0;
				}

				if (name1.startsWith(name2))
				{
					return 1;
				}

				if (name2.startsWith(name1))
				{
					return -1;
				}

				return name1.compareTo(name2);
			}
		});
	}

	/**
	 * Handle a command that is directed at NoCheat
	 *
	 * @param plugin
	 * @param sender
	 * @param command
	 * @param label
	 * @param args
	 * @return
	 */
	public boolean handleCommand(NoCheat plugin, CommandSender sender, Command command, String label, String[] args)
	{

		boolean result = false;
		// Not our command, how did it get here?
		if (!command.getName().equalsIgnoreCase("nocheat") || args.length == 0)
		{
			result = false;
		}
		else if (args[0].equalsIgnoreCase("permlist") && args.length >= 2)
		{
			// permlist command was used
			result = handlePermlistCommand(plugin, sender, args);

		}
		else if (args[0].equalsIgnoreCase("reload"))
		{
			// reload command was used
			result = handleReloadCommand(plugin, sender);
		}
		else if (args[0].equalsIgnoreCase("playerinfo") && args.length >= 2)
		{
			// playerinfo command was used
			result = handlePlayerInfoCommand(plugin, sender, args);
		}

		return result;
	}

	private boolean handlePlayerInfoCommand(NoCheat plugin, CommandSender sender, String[] args)
	{

		Map<String, Object> map = plugin.getPlayerData(args[1]);
		String filter = "";

		if (args.length > 2)
		{
			filter = args[2];
		}

		sender.sendMessage("PlayerInfo for " + args[1]);
		for (Entry<String, Object> entry : map.entrySet())
		{
			if (entry.getKey().contains(filter))
			{
				sender.sendMessage(entry.getKey() + ": " + entry.getValue());
			}
		}
		return true;
	}

	private boolean handlePermlistCommand(NoCheat plugin, CommandSender sender, String[] args)
	{

		// Get the player by name
		Player player = plugin.getServer().getPlayerExact(args[1]);
		if (player == null)
		{
			sender.sendMessage("Unknown player: " + args[1]);
			return true;
		}

		// Should permissions be filtered by prefix?
		String prefix = "";
		if (args.length == 3)
		{
			prefix = args[2];
		}

		sender.sendMessage("Player " + player.getName() + " has the permission(s):");

		for (Permission permission : perms)
		{
			if (permission.getName().startsWith(prefix))
			{
				sender.sendMessage(permission.getName() + ": " + player.hasPermission(permission));
			}
		}
		return true;
	}

	private boolean handleReloadCommand(NoCheat plugin, CommandSender sender)
	{

		// Players need a special permission for this
		if (!(sender instanceof Player) || sender.hasPermission(Permissions.ADMIN_RELOAD))
		{
			sender.sendMessage("[NoCheat] Reloading configuration");
			plugin.reloadConfiguration();
			sender.sendMessage("[NoCheat] Configuration reloaded");
		}
		else
		{
			sender.sendMessage("You lack the " + Permissions.ADMIN_RELOAD + " permission to use 'reload'");
		}

		return true;
	}
}