Better solution for the alternative commands

This commit is contained in:
snowleo 2011-11-18 01:43:58 +01:00
parent 603d23659b
commit 2a98734d22
4 changed files with 133 additions and 61 deletions

View file

@ -0,0 +1,117 @@
package com.earth2me.essentials;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.plugin.Plugin;
public class AlternativeCommandsHandler
{
private final transient Map<String, List<PluginCommand>> altcommands = new HashMap<String, List<PluginCommand>>();
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();
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());
if (reg == null)
{
reg = Bukkit.getServer().getPluginCommand(pc.getName().toLowerCase());
}
for (String label : labels)
{
List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase());
if (plugincommands == null)
{
plugincommands = new ArrayList<PluginCommand>();
altcommands.put(label.toLowerCase(), 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().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);
}
}

View file

@ -60,6 +60,7 @@ public class Essentials extends JavaPlugin implements IEssentials
private transient ItemDb itemDb;
private transient final Methods paymentMethod = new Methods();
private transient PermissionsHandler permissionsHandler;
private transient AlternativeCommandsHandler alternativeCommandsHandler;
private transient UserMap userMap;
private transient ExecuteTimer execTimer;
@ -149,6 +150,7 @@ public class Essentials extends JavaPlugin implements IEssentials
}
permissionsHandler = new PermissionsHandler(this, settings.useBukkitPermissions());
alternativeCommandsHandler = new AlternativeCommandsHandler(this);
final EssentialsPluginListener serverListener = new EssentialsPluginListener(this);
pm.registerEvent(Type.PLUGIN_ENABLE, serverListener, Priority.Low, this);
pm.registerEvent(Type.PLUGIN_DISABLE, serverListener, Priority.Low, this);
@ -366,68 +368,11 @@ public class Essentials extends JavaPlugin implements IEssentials
// Allow plugins to override the command via onCommand
if (!getSettings().isCommandOverridden(command.getName()) && !commandLabel.startsWith("e"))
{
for (Plugin p : getServer().getPluginManager().getPlugins())
final PluginCommand pc = alternativeCommandsHandler.getAlternative(commandLabel);
if (pc != null)
{
if (p.getDescription().getMain().contains("com.earth2me.essentials"))
{
continue;
}
final PluginDescriptionFile desc = p.getDescription();
if (desc == null)
{
continue;
}
if (desc.getName() == null)
{
continue;
}
final PluginCommand pc = getServer().getPluginCommand(desc.getName().toLowerCase() + ":" + commandLabel);
if (pc != null)
{
LOGGER.info("Essentials: Alternative command " + commandLabel + " found, using " + desc.getName().toLowerCase() + ":" + commandLabel);
return pc.execute(sender, commandLabel, args);
}
}
for (Plugin p : getServer().getPluginManager().getPlugins())
{
if (p.getDescription().getMain().contains("com.earth2me.essentials"))
{
continue;
}
final List<Command> commands = PluginCommandYamlParser.parse(p);
for (Command c : commands)
{
if (c.getAliases().contains(commandLabel))
{
final PluginDescriptionFile desc = p.getDescription();
if (desc == null)
{
continue;
}
if (desc.getName() == null)
{
continue;
}
final PluginCommand pc = getServer().getPluginCommand(desc.getName().toLowerCase() + ":" + c.getName().toLowerCase());
if (pc != null)
{
LOGGER.info("Essentials: Alternative alias " + commandLabel + " found, using " + desc.getName().toLowerCase() + ":" + c.getName().toLowerCase());
return pc.execute(sender, commandLabel, args);
}
final PluginCommand pc2 = getServer().getPluginCommand(c.getName().toLowerCase());
if (pc2 != null)
{
LOGGER.info("Essentials: Alternative alias " + commandLabel + " found, using " + c.getName().toLowerCase());
return pc2.execute(sender, commandLabel, args);
}
}
}
LOGGER.info("Essentials: Alternative command " + commandLabel + " found, using " + pc.getLabel());
return pc.execute(sender, commandLabel, args);
}
}
@ -703,6 +648,12 @@ public class Essentials extends JavaPlugin implements IEssentials
return permissionsHandler;
}
@Override
public AlternativeCommandsHandler getAlternativeCommandsHandler()
{
return alternativeCommandsHandler;
}
@Override
public ItemDb getItemDb()
{

View file

@ -21,6 +21,7 @@ public class EssentialsPluginListener extends ServerListener implements IConf
public void onPluginEnable(final PluginEnableEvent event)
{
ess.getPermissionsHandler().checkPermissions();
ess.getAlternativeCommandsHandler().addPlugin(event.getPlugin());
if (!ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().setMethod(ess.getServer().getPluginManager()))
{
LOGGER.log(Level.INFO, "[Essentials] Payment method found (" + ess.getPaymentMethod().getMethod().getName() + " version: " + ess.getPaymentMethod().getMethod().getVersion() + ")");
@ -31,6 +32,7 @@ public class EssentialsPluginListener extends ServerListener implements IConf
public void onPluginDisable(final PluginDisableEvent event)
{
ess.getPermissionsHandler().checkPermissions();
ess.getAlternativeCommandsHandler().removePlugin(event.getPlugin());
// Check to see if the plugin thats being disabled is the one we are using
if (ess.getPaymentMethod() != null && ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().checkDisabled(event.getPlugin()))
{

View file

@ -56,6 +56,8 @@ public interface IEssentials extends Plugin
TNTExplodeListener getTNTListener();
PermissionsHandler getPermissionsHandler();
AlternativeCommandsHandler getAlternativeCommandsHandler();
void showError(final CommandSender sender, final Throwable exception, final String commandLabel);