Fix help command duplication

This commit is contained in:
Frank van der Heijden 2020-10-01 19:29:25 +02:00
parent 420dd9531f
commit 1723a9564f
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
2 changed files with 19 additions and 16 deletions

View file

@ -13,7 +13,7 @@ import net.frankheijden.serverutils.common.commands.Plugins;
import net.frankheijden.serverutils.common.config.Messenger; import net.frankheijden.serverutils.common.config.Messenger;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@CommandAlias("plugins|pl") @CommandAlias("pl|plugins")
public class CommandPlugins extends BaseCommand { public class CommandPlugins extends BaseCommand {
private static final BukkitPluginManager manager = BukkitPluginManager.get(); private static final BukkitPluginManager manager = BukkitPluginManager.get();

View file

@ -4,6 +4,8 @@ import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINO
import static net.frankheijden.serverutils.common.config.Messenger.sendMessage; import static net.frankheijden.serverutils.common.config.Messenger.sendMessage;
import co.aikar.commands.BaseCommand; import co.aikar.commands.BaseCommand;
import co.aikar.commands.RegisteredCommand;
import co.aikar.commands.RootCommand;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.CommandPermission;
@ -41,17 +43,12 @@ import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
@CommandAlias("serverutils|su") @CommandAlias("su|serverutils")
public class CommandServerUtils extends BaseCommand { public class CommandServerUtils extends BaseCommand {
private static final Set<String> ALIASES;
private static final Map<String, ReloadHandler> supportedConfigs; private static final Map<String, ReloadHandler> supportedConfigs;
static { static {
ALIASES = new HashSet<>();
ALIASES.add("serverutils");
ALIASES.add("plugins");
supportedConfigs = new HashMap<>(); supportedConfigs = new HashMap<>();
supportedConfigs.put("bukkit", RCraftServer::reloadBukkitConfiguration); supportedConfigs.put("bukkit", RCraftServer::reloadBukkitConfiguration);
supportedConfigs.put("commands.yml", RCraftServer::reloadCommandsConfiguration); supportedConfigs.put("commands.yml", RCraftServer::reloadCommandsConfiguration);
@ -81,15 +78,21 @@ public class CommandServerUtils extends BaseCommand {
FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format")) FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format"))
.orderedKeys("%command%", "%subcommand%", "%help%"); .orderedKeys("%command%", "%subcommand%", "%help%");
plugin.getCommandManager().getRegisteredRootCommands().stream()
.filter(c -> !ALIASES.contains(c.getCommandName().toLowerCase())) Set<String> rootCommands = new HashSet<>();
.forEach(rootCommand -> { for (RootCommand root : plugin.getCommandManager().getRegisteredRootCommands()) {
builder.add(rootCommand.getCommandName(), "", rootCommand.getDescription()); String rootName = root.getDefCommand().getName();
rootCommand.getSubCommands().values().forEach(cmd -> { if (!rootCommands.add(rootName)) continue;
if (cmd.getPrefSubCommand().isEmpty()) return; builder.add(rootName, "", root.getDescription());
builder.add(rootCommand.getCommandName(), " " + cmd.getPrefSubCommand(), cmd.getHelpText());
}); Set<String> subCommands = new HashSet<>();
}); for (RegisteredCommand<?> sub : root.getSubCommands().values()) {
String name = sub.getPrefSubCommand().toLowerCase();
if (name.isEmpty()) continue;
if (!subCommands.add(name)) continue;
builder.add(rootName, " " + name, sub.getHelpText());
}
}
builder.sendTo(sender); builder.sendTo(sender);
Messenger.sendMessage(sender, "serverutils.help.footer"); Messenger.sendMessage(sender, "serverutils.help.footer");
} }