Skip to content

Commit ef12029

Browse files
authored
Fix command handler not finding dynamically registered commands (#3816)
Fixes #3815.
1 parent e1e98f5 commit ef12029

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

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

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.earth2me.essentials;
22

33
import org.bukkit.command.Command;
4-
import org.bukkit.command.PluginCommand;
5-
import org.bukkit.command.PluginCommandYamlParser;
4+
import org.bukkit.command.PluginIdentifiableCommand;
65
import org.bukkit.plugin.Plugin;
76

87
import java.util.ArrayList;
@@ -16,7 +15,7 @@
1615

1716
public class AlternativeCommandsHandler {
1817
private static final Logger LOGGER = Logger.getLogger("Essentials");
19-
private final transient Map<String, List<PluginCommand>> altcommands = new HashMap<>();
18+
private final transient Map<String, List<Command>> altcommands = new HashMap<>();
2019
private final transient Map<String, String> disabledList = new HashMap<>();
2120
private final transient IEssentials ess;
2221

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

3938
for (final Command command : commands) {
40-
final PluginCommand pc = (PluginCommand) command;
41-
final List<String> labels = new ArrayList<>(pc.getAliases());
42-
labels.add(pc.getName());
39+
final List<String> labels = new ArrayList<>(command.getAliases());
40+
labels.add(command.getName());
4341

44-
PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase(Locale.ENGLISH));
45-
if (reg == null) {
46-
reg = ess.getServer().getPluginCommand(pc.getName().toLowerCase(Locale.ENGLISH));
47-
}
48-
if (reg == null || !reg.getPlugin().equals(plugin)) {
49-
continue;
50-
}
5142
for (final String label : labels) {
52-
final List<PluginCommand> plugincommands = altcommands.computeIfAbsent(label.toLowerCase(Locale.ENGLISH), k -> new ArrayList<>());
43+
final List<Command> plugincommands = altcommands.computeIfAbsent(label.toLowerCase(Locale.ENGLISH), k -> new ArrayList<>());
5344
boolean found = false;
54-
for (final PluginCommand pc2 : plugincommands) {
55-
if (pc2.getPlugin().equals(plugin)) {
56-
found = true;
57-
break;
45+
for (final Command pc2 : plugincommands) {
46+
if (pc2 instanceof PluginIdentifiableCommand) {
47+
if (((PluginIdentifiableCommand) pc2).getPlugin().equals(plugin)) {
48+
found = true;
49+
break;
50+
}
5851
}
5952
}
6053
if (!found) {
61-
plugincommands.add(reg);
54+
plugincommands.add(command);
6255
}
6356
}
6457
}
6558
}
6659

60+
private List<Command> getPluginCommands(Plugin plugin) {
61+
final List<Command> commands = new ArrayList<>();
62+
for (Command cmd : ess.getKnownCommandsProvider().getKnownCommands().values()) {
63+
if (cmd instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand) cmd).getPlugin().getName().equals(plugin.getName())) {
64+
commands.add(cmd);
65+
}
66+
}
67+
return commands;
68+
}
69+
6770
public void removePlugin(final Plugin plugin) {
68-
final Iterator<Map.Entry<String, List<PluginCommand>>> iterator = altcommands.entrySet().iterator();
71+
final Iterator<Map.Entry<String, List<Command>>> iterator = altcommands.entrySet().iterator();
6972
while (iterator.hasNext()) {
70-
final Map.Entry<String, List<PluginCommand>> entry = iterator.next();
71-
entry.getValue().removeIf(pc -> pc.getPlugin() == null || pc.getPlugin().equals(plugin));
73+
final Map.Entry<String, List<Command>> entry = iterator.next();
74+
entry.getValue().removeIf(pc -> !(pc instanceof PluginIdentifiableCommand) || ((PluginIdentifiableCommand) pc).getPlugin().equals(plugin));
7275
if (entry.getValue().isEmpty()) {
7376
iterator.remove();
7477
}
7578
}
7679
}
7780

78-
public PluginCommand getAlternative(final String label) {
79-
final List<PluginCommand> commands = altcommands.get(label);
81+
public Command getAlternative(final String label) {
82+
final List<Command> commands = altcommands.get(label);
8083
if (commands == null || commands.isEmpty()) {
8184
return null;
8285
}
8386
if (commands.size() == 1) {
8487
return commands.get(0);
8588
}
8689
// return the first command that is not an alias
87-
for (final PluginCommand command : commands) {
90+
for (final Command command : commands) {
8891
if (command.getName().equalsIgnoreCase(label)) {
8992
return command;
9093
}
@@ -93,12 +96,14 @@ public PluginCommand getAlternative(final String label) {
9396
return commands.get(0);
9497
}
9598

96-
public void executed(final String label, final PluginCommand pc) {
97-
final String altString = pc.getPlugin().getName() + ":" + pc.getLabel();
98-
if (ess.getSettings().isDebug()) {
99-
LOGGER.log(Level.INFO, "Essentials: Alternative command " + label + " found, using " + altString);
99+
public void executed(final String label, final Command pc) {
100+
if (pc instanceof PluginIdentifiableCommand) {
101+
final String altString = ((PluginIdentifiableCommand) pc).getPlugin().getName() + ":" + pc.getLabel();
102+
if (ess.getSettings().isDebug()) {
103+
LOGGER.log(Level.INFO, "Essentials: Alternative command " + label + " found, using " + altString);
104+
}
105+
disabledList.put(label, altString);
100106
}
101-
disabledList.put(label, altString);
102107
}
103108

104109
public Map<String, String> disabledCommands() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,10 @@ public List<String> onTabCompleteEssentials(final CommandSender cSender, final C
498498
final ClassLoader classLoader, final String commandPath, final String permissionPrefix,
499499
final IEssentialsModule module) {
500500
if (!getSettings().isCommandOverridden(command.getName()) && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) {
501-
final PluginCommand pc = alternativeCommandsHandler.getAlternative(commandLabel);
502-
if (pc != null) {
501+
final Command pc = alternativeCommandsHandler.getAlternative(commandLabel);
502+
if (pc instanceof PluginCommand) {
503503
try {
504-
final TabCompleter completer = pc.getTabCompleter();
504+
final TabCompleter completer = ((PluginCommand) pc).getTabCompleter();
505505
if (completer != null) {
506506
return completer.onTabComplete(cSender, command, commandLabel, args);
507507
}
@@ -574,7 +574,7 @@ public boolean onCommand(final CommandSender sender, final Command command, fina
574574
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) {
575575
// Allow plugins to override the command via onCommand
576576
if (!getSettings().isCommandOverridden(command.getName()) && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) {
577-
final PluginCommand pc = alternativeCommandsHandler.getAlternative(commandLabel);
577+
final Command pc = alternativeCommandsHandler.getAlternative(commandLabel);
578578
if (pc != null) {
579579
alternativeCommandsHandler.executed(commandLabel, pc);
580580
try {

0 commit comments

Comments
 (0)