summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/AlternativeCommandsHandler.java
blob: 3016ffe594f6ec00f6ddca748b4ab46ebb16bcc0 (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;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.ess3.api.IEssentials;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.plugin.Plugin;


public class AlternativeCommandsHandler
{
	private static final Logger LOGGER = Logger.getLogger("Essentials");
	private final transient Map<String, List<PluginCommand>> altcommands = new HashMap<String, List<PluginCommand>>();
	private final transient Map<String, String> disabledList = new HashMap<String, String>();
	private final transient IEssentials ess;

	public AlternativeCommandsHandler(final IEssentials ess)
	{
		this.ess = ess;
		for (Plugin plugin : ess.getServer().getPluginManager().getPlugins())
		{
			if (plugin.isEnabled())
			{
				addPlugin(plugin);
			}
		}
	}

	public final void addPlugin(final Plugin plugin)
	{
		if (plugin.getDescription().getMain().contains("com.earth2me.essentials"))
		{
			return;
		}
		final List<Command> commands = PluginCommandYamlParser.parse(plugin);
		final String pluginName = plugin.getDescription().getName().toLowerCase(Locale.ENGLISH);

		for (Command command : commands)
		{
			final PluginCommand pc = (PluginCommand)command;
			final List<String> labels = new ArrayList<String>(pc.getAliases());
			labels.add(pc.getName());

			PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase(Locale.ENGLISH));
			if (reg == null)
			{
				reg = ess.getServer().getPluginCommand(pc.getName().toLowerCase(Locale.ENGLISH));
			}
			if (reg == null || !reg.getPlugin().equals(plugin))
			{
				continue;
			}
			for (String label : labels)
			{
				List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH));
				if (plugincommands == null)
				{
					plugincommands = new ArrayList<PluginCommand>();
					altcommands.put(label.toLowerCase(Locale.ENGLISH), plugincommands);
				}
				boolean found = false;
				for (PluginCommand pc2 : plugincommands)
				{
					if (pc2.getPlugin().equals(plugin))
					{
						found = true;
					}
				}
				if (!found)
				{
					plugincommands.add(reg);
				}
			}
		}
	}

	public void removePlugin(final Plugin plugin)
	{
		final Iterator<Map.Entry<String, List<PluginCommand>>> iterator = altcommands.entrySet().iterator();
		while (iterator.hasNext())
		{
			final Map.Entry<String, List<PluginCommand>> entry = iterator.next();
			final Iterator<PluginCommand> pcIterator = entry.getValue().iterator();
			while (pcIterator.hasNext())
			{
				final PluginCommand pc = pcIterator.next();
				if (pc.getPlugin() == null || pc.getPlugin().equals(plugin))
				{
					pcIterator.remove();
				}
			}
			if (entry.getValue().isEmpty())
			{
				iterator.remove();
			}
		}
	}

	public PluginCommand getAlternative(final String label)
	{
		final List<PluginCommand> commands = altcommands.get(label);
		if (commands == null || commands.isEmpty())
		{
			return null;
		}
		if (commands.size() == 1)
		{
			return commands.get(0);
		}
		// return the first command that is not an alias
		for (PluginCommand command : commands)
		{
			if (command.getName().equalsIgnoreCase(label))
			{
				return command;
			}
		}
		// return the first alias
		return commands.get(0);
	}

	public void executed(final String label, final PluginCommand pc)
	{
		final String altString = pc.getPlugin().getName() + ":" + pc.getLabel();
		if (ess.getSettings().isDebug())
		{
			LOGGER.log(Level.INFO, "Essentials: Alternative command " + label + " found, using " + altString);
		}
		disabledList.put(label, altString);
	}

	public Map<String, String> disabledCommands()
	{
		return disabledList;
	}
}