Refactor command visibility methods for clarity and consistency in help listings

This commit is contained in:
Xujiayao 2026-02-26 20:59:59 +08:00
parent 679d446810
commit d874eccd38
5 changed files with 30 additions and 8 deletions

View file

@ -41,7 +41,7 @@ public interface Command {
}
/**
* Whether this command should be visible to the given sender in help listings.
* Whether this command should be visible in help listings for the given sender.
* <p>
* Commands can override this to hide themselves from certain sender types.
* This does NOT affect whether the command can be executed only its visibility in help.
@ -49,7 +49,20 @@ public interface Command {
* @param sender The command sender requesting the help listing.
* @return true if this command should be shown to the sender, false otherwise.
*/
default boolean isVisibleTo(CommandSender sender) {
default boolean isVisibleInHelp(CommandSender sender) {
return true;
}
/**
* Whether this command should be visible in help when the request originates
* from a local Minecraft sender context.
* <p>
* This is evaluated only in Minecraft environment for local senders.
* It does NOT affect whether the command can be executed only its visibility in help.
*
* @return true if this command should be shown in Minecraft local help, false otherwise.
*/
default boolean isVisibleFromMinecraft() {
return true;
}

View file

@ -115,6 +115,11 @@ public class ConsoleCommand implements Command {
return I18nManager.getDmccTranslation("commands.console.description");
}
@Override
public boolean isVisibleFromMinecraft() {
return false;
}
@Override
public void execute(CommandSender sender, String... args) {
if ("standalone".equals(ModeManager.getMode())) {

View file

@ -41,13 +41,12 @@ public class HelpCommand implements Command {
StringBuilder builder = new StringBuilder();
builder.append("========== ").append(I18nManager.getDmccTranslation("commands.help.help")).append(" ==========\n");
String mcPrefix = (IS_MINECRAFT_ENV && sender instanceof LocalCommandSender) ? "dmcc " : "";
boolean isFromMinecraft = (IS_MINECRAFT_ENV && sender instanceof LocalCommandSender);
String mcPrefix = isFromMinecraft ? "dmcc " : "";
CommandManager.getCommands().stream()
// 1. Check if the command is structurally visible to this sender type
// (e.g. log command hides itself from LocalCommandSender because it needs file upload)
.filter(cmd -> cmd.isVisibleTo(sender))
// 2. Check if the sender has the required OP level to execute this command
.filter(cmd -> cmd.isVisibleInHelp(sender))
.filter(cmd -> !isFromMinecraft || cmd.isVisibleFromMinecraft())
.filter(cmd -> sender.getOpLevel() >= ConfigManager.getInt("command_permission_levels." + cmd.name(), 4))
.sorted(Comparator.comparing(Command::name))
.forEach(cmd -> {

View file

@ -43,7 +43,7 @@ public class LogCommand implements Command {
}
@Override
public boolean isVisibleTo(CommandSender sender) {
public boolean isVisibleInHelp(CommandSender sender) {
// The log command is only meaningful when the sender supports file attachments,
// i.e., Discord slash commands (JdaCommandSender) or remote execute (capture sender).
// It should not appear in help for local senders (terminal or Minecraft in-game).

View file

@ -57,6 +57,11 @@ public class WhitelistCommand implements Command {
return I18nManager.getDmccTranslation("commands.whitelist.description");
}
@Override
public boolean isVisibleFromMinecraft() {
return false;
}
@Override
public void execute(CommandSender sender, String... args) {
String player = args[0];