Essentials/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler....

108 lines
4.0 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.plugin.Plugin;
2020-10-03 19:46:05 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
2015-04-15 06:06:16 +02:00
import java.util.logging.Level;
import java.util.logging.Logger;
public class AlternativeCommandsHandler {
private static final Logger LOGGER = Logger.getLogger("Essentials");
2015-06-03 22:11:56 +02:00
private final transient Map<String, List<PluginCommand>> altcommands = new HashMap<>();
private final transient Map<String, String> disabledList = new HashMap<>();
2015-04-15 06:06:16 +02:00
private final transient IEssentials ess;
2011-11-29 18:48:52 +01:00
2015-04-15 06:06:16 +02:00
public AlternativeCommandsHandler(final IEssentials ess) {
this.ess = ess;
2020-10-03 19:46:05 +02:00
for (final Plugin plugin : ess.getServer().getPluginManager().getPlugins()) {
2015-04-15 06:06:16 +02:00
if (plugin.isEnabled()) {
addPlugin(plugin);
}
}
}
2011-11-29 18:48:52 +01:00
2015-04-15 06:06:16 +02:00
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);
2020-10-03 19:46:05 +02:00
for (final Command command : commands) {
2015-04-15 06:06:16 +02:00
final PluginCommand pc = (PluginCommand) command;
2015-06-03 22:11:56 +02:00
final List<String> labels = new ArrayList<>(pc.getAliases());
2015-04-15 06:06:16 +02:00
labels.add(pc.getName());
2015-04-15 06:06:16 +02:00
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;
}
2020-10-03 19:46:05 +02:00
for (final String label : labels) {
final List<PluginCommand> plugincommands = altcommands.computeIfAbsent(label.toLowerCase(Locale.ENGLISH), k -> new ArrayList<>());
2015-04-15 06:06:16 +02:00
boolean found = false;
2020-10-03 19:46:05 +02:00
for (final PluginCommand pc2 : plugincommands) {
2015-04-15 06:06:16 +02:00
if (pc2.getPlugin().equals(plugin)) {
found = true;
break;
2015-04-15 06:06:16 +02:00
}
}
if (!found) {
plugincommands.add(reg);
}
}
}
}
2015-04-15 06:06:16 +02:00
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();
entry.getValue().removeIf(pc -> pc.getPlugin() == null || pc.getPlugin().equals(plugin));
2015-04-15 06:06:16 +02:00
if (entry.getValue().isEmpty()) {
iterator.remove();
}
}
}
2015-04-15 06:06:16 +02:00
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
2020-10-03 19:46:05 +02:00
for (final PluginCommand command : commands) {
2015-04-15 06:06:16 +02:00
if (command.getName().equalsIgnoreCase(label)) {
return command;
}
}
// return the first alias
return commands.get(0);
}
2011-11-29 18:48:52 +01:00
2015-04-15 06:06:16 +02:00
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);
}
2011-11-29 18:48:52 +01:00
2015-04-15 06:06:16 +02:00
public Map<String, String> disabledCommands() {
return disabledList;
}
}