Skip to content

Commit

Permalink
Fix command handler not finding dynamically registered commands (Esse…
Browse files Browse the repository at this point in the history
  • Loading branch information
JRoy authored and DarkEyeDragon committed Dec 24, 2020
1 parent 49e3db6 commit f2735fb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.earth2me.essentials;

import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
Expand All @@ -16,7 +15,7 @@

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

Expand All @@ -33,58 +32,62 @@ public final void addPlugin(final Plugin plugin) {
if (plugin.getDescription().getMain().contains("com.earth2me.essentials")) {
return;
}
final List<Command> commands = PluginCommandYamlParser.parse(plugin);
final List<Command> commands = getPluginCommands(plugin);
final String pluginName = plugin.getDescription().getName().toLowerCase(Locale.ENGLISH);

for (final Command command : commands) {
final PluginCommand pc = (PluginCommand) command;
final List<String> labels = new ArrayList<>(pc.getAliases());
labels.add(pc.getName());
final List<String> labels = new ArrayList<>(command.getAliases());
labels.add(command.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 (final String label : labels) {
final List<PluginCommand> plugincommands = altcommands.computeIfAbsent(label.toLowerCase(Locale.ENGLISH), k -> new ArrayList<>());
final List<Command> plugincommands = altcommands.computeIfAbsent(label.toLowerCase(Locale.ENGLISH), k -> new ArrayList<>());
boolean found = false;
for (final PluginCommand pc2 : plugincommands) {
if (pc2.getPlugin().equals(plugin)) {
found = true;
break;
for (final Command pc2 : plugincommands) {
if (pc2 instanceof PluginIdentifiableCommand) {
if (((PluginIdentifiableCommand) pc2).getPlugin().equals(plugin)) {
found = true;
break;
}
}
}
if (!found) {
plugincommands.add(reg);
plugincommands.add(command);
}
}
}
}

private List<Command> getPluginCommands(Plugin plugin) {
final List<Command> commands = new ArrayList<>();
for (Command cmd : ess.getKnownCommandsProvider().getKnownCommands().values()) {
if (cmd instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand) cmd).getPlugin().getName().equals(plugin.getName())) {
commands.add(cmd);
}
}
return commands;
}

public void removePlugin(final Plugin plugin) {
final Iterator<Map.Entry<String, List<PluginCommand>>> iterator = altcommands.entrySet().iterator();
final Iterator<Map.Entry<String, List<Command>>> 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));
final Map.Entry<String, List<Command>> entry = iterator.next();
entry.getValue().removeIf(pc -> !(pc instanceof PluginIdentifiableCommand) || ((PluginIdentifiableCommand) pc).getPlugin().equals(plugin));
if (entry.getValue().isEmpty()) {
iterator.remove();
}
}
}

public PluginCommand getAlternative(final String label) {
final List<PluginCommand> commands = altcommands.get(label);
public Command getAlternative(final String label) {
final List<Command> 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 (final PluginCommand command : commands) {
for (final Command command : commands) {
if (command.getName().equalsIgnoreCase(label)) {
return command;
}
Expand All @@ -93,12 +96,14 @@ public PluginCommand getAlternative(final String label) {
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);
public void executed(final String label, final Command pc) {
if (pc instanceof PluginIdentifiableCommand) {
final String altString = ((PluginIdentifiableCommand) pc).getPlugin().getName() + ":" + pc.getLabel();
if (ess.getSettings().isDebug()) {
LOGGER.log(Level.INFO, "Essentials: Alternative command " + label + " found, using " + altString);
}
disabledList.put(label, altString);
}
disabledList.put(label, altString);
}

public Map<String, String> disabledCommands() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,10 @@ public List<String> onTabCompleteEssentials(final CommandSender cSender, final C
final ClassLoader classLoader, final String commandPath, final String permissionPrefix,
final IEssentialsModule module) {
if (!getSettings().isCommandOverridden(command.getName()) && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) {
final PluginCommand pc = alternativeCommandsHandler.getAlternative(commandLabel);
if (pc != null) {
final Command pc = alternativeCommandsHandler.getAlternative(commandLabel);
if (pc instanceof PluginCommand) {
try {
final TabCompleter completer = pc.getTabCompleter();
final TabCompleter completer = ((PluginCommand) pc).getTabCompleter();
if (completer != null) {
return completer.onTabComplete(cSender, command, commandLabel, args);
}
Expand Down Expand Up @@ -574,7 +574,7 @@ public boolean onCommand(final CommandSender sender, final Command command, fina
public boolean onCommandEssentials(final CommandSender cSender, final Command command, final String commandLabel, final String[] args, final ClassLoader classLoader, final String commandPath, final String permissionPrefix, final IEssentialsModule module) {
// Allow plugins to override the command via onCommand
if (!getSettings().isCommandOverridden(command.getName()) && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) {
final PluginCommand pc = alternativeCommandsHandler.getAlternative(commandLabel);
final Command pc = alternativeCommandsHandler.getAlternative(commandLabel);
if (pc != null) {
alternativeCommandsHandler.executed(commandLabel, pc);
try {
Expand Down

0 comments on commit f2735fb

Please # to comment.