summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandlist.java
blob: bb54757217ab030626ddd0247ddccf94daf2c4da (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
package com.earth2me.essentials.commands;

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.PlayerList;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.NumberUtil;
import java.util.*;
import org.bukkit.Server;


public class Commandlist extends EssentialsCommand
{
	public Commandlist()
	{
		super("list");
	}

	@Override
	public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		boolean showHidden = true;
		User user = null;
		if (sender.isPlayer())
		{
			user = ess.getUser(sender.getPlayer());
			showHidden = user.isAuthorized("essentials.list.hidden") || user.canInteractVanished();
		}
		sender.sendMessage(PlayerList.listSummary(ess, user, showHidden));
		final Map<String, List<User>> playerList = PlayerList.getPlayerLists(ess, user, showHidden);

		if (args.length > 0)
		{
			sender.sendMessage(PlayerList.listGroupUsers(ess, playerList, args[0].toLowerCase()));
		}
		else
		{
			sendGroupedList(sender, commandLabel, playerList);
		}
	}

	// Output the standard /list output, when no group is specified
	private void sendGroupedList(CommandSource sender, String commandLabel, Map<String, List<User>> playerList)
	{
		final Set<String> configGroups = ess.getSettings().getListGroupConfig().keySet();
		final List<String> asterisk = new ArrayList<String>();

		// Loop through the custom defined groups and display them
		for (String oConfigGroup : configGroups)
		{
			String groupValue = ess.getSettings().getListGroupConfig().get(oConfigGroup).toString().trim();
			String configGroup = oConfigGroup.toLowerCase();

			// If the group value is an asterisk, then skip it, and handle it later
			if (groupValue.equals("*"))
			{
				asterisk.add(oConfigGroup);
				continue;
			}

			// If the group value is hidden, we don't need to display it
			if (groupValue.equalsIgnoreCase("hidden"))
			{
				playerList.remove(configGroup);
				continue;
			}

			List<User> outputUserList = new ArrayList<User>();
			final List<User> matchedList = playerList.get(configGroup);

			// If the group value is an int, then we might need to truncate it
			if (NumberUtil.isInt(groupValue))
			{
				if (matchedList != null && !matchedList.isEmpty())
				{
					playerList.remove(configGroup);
					outputUserList.addAll(matchedList);
					int limit = Integer.parseInt(groupValue);
					if (matchedList.size() > limit)
					{
						sender.sendMessage(PlayerList.outputFormat(oConfigGroup, tl("groupNumber", matchedList.size(), commandLabel, FormatUtil.stripFormat(configGroup))));
					}
					else
					{
						sender.sendMessage(PlayerList.outputFormat(oConfigGroup, PlayerList.listUsers(ess, outputUserList, ", ")));
					}
					continue;
				}
			}

			outputUserList = PlayerList.getMergedList(ess, playerList, configGroup);

			// If we have no users, than we don't need to continue parsing this group
			if (outputUserList == null || outputUserList.isEmpty())
			{
				continue;
			}

			sender.sendMessage(PlayerList.outputFormat(oConfigGroup, PlayerList.listUsers(ess, outputUserList, ", ")));
		}

		String[] onlineGroups = playerList.keySet().toArray(new String[0]);
		Arrays.sort(onlineGroups, String.CASE_INSENSITIVE_ORDER);

		// If we have an asterisk group, then merge all remaining groups
		if (!asterisk.isEmpty())
		{
			List<User> asteriskUsers = new ArrayList<User>();
			for (String onlineGroup : onlineGroups)
			{
				asteriskUsers.addAll(playerList.get(onlineGroup));
			}
			for (String key : asterisk)
			{
				playerList.put(key, asteriskUsers);
			}
			onlineGroups = asterisk.toArray(new String[0]);
		}

		// If we have any groups remaining after the custom groups loop through and display them
		for (String onlineGroup : onlineGroups)
		{
			List<User> users = playerList.get(onlineGroup);
			String groupName = asterisk.isEmpty() ? users.get(0).getGroup() : onlineGroup;

			if (ess.getPermissionsHandler().getName().equals("ConfigPermissions"))
			{
				groupName = tl("connectedPlayers");
			}
			if (users == null || users.isEmpty())
			{
				continue;
			}

			sender.sendMessage(PlayerList.outputFormat(groupName, PlayerList.listUsers(ess, users, ", ")));
		}
	}
}