2011-11-18 00:43:58 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-11-18 17:42:26 +00:00
|
|
|
import java.util.*;
|
2011-12-03 23:38:28 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2011-11-18 00:43:58 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.PluginCommand;
|
|
|
|
import org.bukkit.command.PluginCommandYamlParser;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
|
|
|
|
|
|
|
public class AlternativeCommandsHandler
|
|
|
|
{
|
2011-12-03 23:38:28 +00:00
|
|
|
private static final Logger LOGGER = Logger.getLogger("Minecraft");
|
2011-11-18 00:43:58 +00:00
|
|
|
private final transient Map<String, List<PluginCommand>> altcommands = new HashMap<String, List<PluginCommand>>();
|
2011-12-03 23:38:28 +00:00
|
|
|
private final transient Map<String, String> disabledList = new HashMap<String, String>();
|
2011-11-18 00:43:58 +00:00
|
|
|
private final transient IEssentials ess;
|
2011-11-29 17:48:52 +00:00
|
|
|
|
2011-11-18 00:43:58 +00:00
|
|
|
public AlternativeCommandsHandler(final IEssentials ess)
|
|
|
|
{
|
|
|
|
this.ess = ess;
|
|
|
|
for (Plugin plugin : ess.getServer().getPluginManager().getPlugins())
|
|
|
|
{
|
2011-11-29 17:48:52 +00:00
|
|
|
if (plugin.isEnabled())
|
|
|
|
{
|
2011-11-18 00:43:58 +00:00
|
|
|
addPlugin(plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-29 17:48:52 +00:00
|
|
|
|
2011-11-18 00:43:58 +00:00
|
|
|
public final void addPlugin(final Plugin plugin)
|
|
|
|
{
|
|
|
|
if (plugin.getDescription().getMain().contains("com.earth2me.essentials"))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final List<Command> commands = PluginCommandYamlParser.parse(plugin);
|
2011-11-21 01:55:26 +00:00
|
|
|
final String pluginName = plugin.getDescription().getName().toLowerCase(Locale.ENGLISH);
|
2011-11-18 00:43:58 +00:00
|
|
|
|
|
|
|
for (Command command : commands)
|
|
|
|
{
|
|
|
|
final PluginCommand pc = (PluginCommand)command;
|
|
|
|
final List<String> labels = new ArrayList<String>(pc.getAliases());
|
|
|
|
labels.add(pc.getName());
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase(Locale.ENGLISH));
|
2011-11-18 00:43:58 +00:00
|
|
|
if (reg == null)
|
|
|
|
{
|
2011-11-25 12:55:09 +00:00
|
|
|
reg = ess.getServer().getPluginCommand(pc.getName().toLowerCase(Locale.ENGLISH));
|
|
|
|
}
|
2011-11-25 13:02:40 +00:00
|
|
|
if (reg == null || !reg.getPlugin().equals(plugin))
|
2011-11-25 12:55:09 +00:00
|
|
|
{
|
|
|
|
continue;
|
2011-11-18 00:43:58 +00:00
|
|
|
}
|
|
|
|
for (String label : labels)
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH));
|
2011-11-18 00:43:58 +00:00
|
|
|
if (plugincommands == null)
|
|
|
|
{
|
|
|
|
plugincommands = new ArrayList<PluginCommand>();
|
2011-11-21 01:55:26 +00:00
|
|
|
altcommands.put(label.toLowerCase(Locale.ENGLISH), plugincommands);
|
2011-11-18 00:43:58 +00:00
|
|
|
}
|
|
|
|
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();
|
2011-11-24 23:22:57 +00:00
|
|
|
if (pc.getPlugin() == null || pc.getPlugin().equals(plugin))
|
2011-11-18 00:43:58 +00:00
|
|
|
{
|
|
|
|
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;
|
2011-11-29 17:48:52 +00:00
|
|
|
}
|
2011-11-18 00:43:58 +00:00
|
|
|
if (commands.size() == 1)
|
|
|
|
{
|
|
|
|
return commands.get(0);
|
|
|
|
}
|
|
|
|
// return the first command that is not an alias
|
2011-11-29 17:48:52 +00:00
|
|
|
for (PluginCommand command : commands)
|
|
|
|
{
|
|
|
|
if (command.getName().equalsIgnoreCase(label))
|
|
|
|
{
|
2011-11-18 00:43:58 +00:00
|
|
|
return command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// return the first alias
|
|
|
|
return commands.get(0);
|
|
|
|
}
|
2011-11-29 17:48:52 +00:00
|
|
|
|
2012-12-21 06:38:51 +00:00
|
|
|
public void executed(final String label, final PluginCommand pc)
|
2011-11-29 17:21:18 +00:00
|
|
|
{
|
2012-12-21 06:38:51 +00:00
|
|
|
final String altString = pc.getPlugin().getName() + ":" + pc.getLabel();
|
2011-12-03 23:38:28 +00:00
|
|
|
if (ess.getSettings().isDebug())
|
|
|
|
{
|
2012-12-21 06:38:51 +00:00
|
|
|
LOGGER.log(Level.INFO, "Essentials: Alternative command " + label + " found, using " + altString);
|
2011-12-03 23:38:28 +00:00
|
|
|
}
|
2012-12-21 06:38:51 +00:00
|
|
|
disabledList.put(label, altString);
|
2011-11-29 17:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, String> disabledCommands()
|
2011-11-29 17:21:18 +00:00
|
|
|
{
|
2011-12-03 23:38:28 +00:00
|
|
|
return disabledList;
|
2011-11-29 17:48:52 +00:00
|
|
|
}
|
2011-11-18 00:43:58 +00:00
|
|
|
}
|