From db399cb7a6435107c3dbf428973c74ffc23542b0 Mon Sep 17 00:00:00 2001 From: Konicai <71294714+Konicai@users.noreply.github.com> Date: Sun, 2 Jun 2024 22:34:07 -0500 Subject: [PATCH] discard root command permission for now, move root->help delegate out of Help --- .../geyser/command/CommandRegistry.java | 33 +++++++++++++++---- .../geyser/command/ExceptionHandlers.java | 4 ++- .../geyser/command/defaults/HelpCommand.java | 21 +++--------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/command/CommandRegistry.java b/core/src/main/java/org/geysermc/geyser/command/CommandRegistry.java index 6cb7fde20..abc25c2f2 100644 --- a/core/src/main/java/org/geysermc/geyser/command/CommandRegistry.java +++ b/core/src/main/java/org/geysermc/geyser/command/CommandRegistry.java @@ -101,7 +101,10 @@ public class CommandRegistry implements EventRegistrar { ExceptionHandlers.register(cloud); // begin command registration - registerBuiltInCommand(new HelpCommand(geyser, "help", "geyser.commands.help.desc", "geyser.command.help", "geyser", "geyser.command", this.commands)); + HelpCommand help = new HelpCommand(geyser, "help", "geyser.commands.help.desc", "geyser.command.help", GeyserCommand.DEFAULT_ROOT_COMMAND, this.commands); + registerBuiltInCommand(help); + buildRootCommand(help); // build root and delegate to help + registerBuiltInCommand(new ListCommand(geyser, "list", "geyser.commands.list.desc", "geyser.command.list")); registerBuiltInCommand(new ReloadCommand(geyser, "reload", "geyser.commands.reload.desc", "geyser.command.reload")); registerBuiltInCommand(new OffhandCommand(geyser, "offhand", "geyser.commands.offhand.desc", "geyser.command.offhand")); @@ -141,14 +144,16 @@ public class CommandRegistry implements EventRegistrar { // Register help commands for all extensions with commands String id = extension.description().id(); - registerExtensionCommand(extension, new HelpCommand( + HelpCommand extensionHelp = new HelpCommand( this.geyser, "help", "geyser.commands.exthelp.desc", "geyser.command.exthelp." + id, extension.rootCommand(), - extension.description().id() + ".command", - entry.getValue())); + entry.getValue()); // commands it provides help for + + registerExtensionCommand(extension, extensionHelp); + buildRootCommand(extensionHelp); } // wait for the right moment (depends on the platform) to register permissions @@ -187,10 +192,24 @@ public class CommandRegistry implements EventRegistrar { if (!command.permission().isBlank() && command.permissionDefault() != null) { permissionDefaults.put(command.permission(), command.permissionDefault()); } + } - if (command instanceof HelpCommand helpCommand) { - permissionDefaults.put(helpCommand.rootCommand(), helpCommand.permissionDefault()); - } + /** + * Registers a root command to cloud that delegates to the given help command. + * The name of this root command is the root of the given help command. + */ + private void buildRootCommand(HelpCommand help) { + // We do the permission check inside the executor because we don't want to actually + // add a permission to the root yet, nor should it be the same as the help command. + cloud.command(cloud.commandBuilder(help.rootCommand()) + .handler(context -> { + GeyserCommandSource source = context.sender(); + if (!source.hasPermission(help.permission())) { + source.sendLocaleString(ExceptionHandlers.PERMISSION_FAIL_LANG_KEY); + return; + } + help.execute(source); + })); } private void onRegisterPermissions(GeyserRegisterPermissionsEvent event) { diff --git a/core/src/main/java/org/geysermc/geyser/command/ExceptionHandlers.java b/core/src/main/java/org/geysermc/geyser/command/ExceptionHandlers.java index 1b85b2aa6..45657a596 100644 --- a/core/src/main/java/org/geysermc/geyser/command/ExceptionHandlers.java +++ b/core/src/main/java/org/geysermc/geyser/command/ExceptionHandlers.java @@ -49,6 +49,8 @@ import java.util.function.BiConsumer; */ final class ExceptionHandlers { + final static String PERMISSION_FAIL_LANG_KEY = "geyser.command.permission_fail"; + private final ExceptionController controller; private ExceptionHandlers(ExceptionController controller) { @@ -117,7 +119,7 @@ final class ExceptionHandlers { } // Result.NO_PERMISSION or generic permission failure - source.sendLocaleString("geyser.command.permission_fail"); + source.sendLocaleString(PERMISSION_FAIL_LANG_KEY); } private static void handleUnexpectedThrowable(GeyserCommandSource source, Throwable throwable) { diff --git a/core/src/main/java/org/geysermc/geyser/command/defaults/HelpCommand.java b/core/src/main/java/org/geysermc/geyser/command/defaults/HelpCommand.java index 85334cc17..c5adb50fb 100644 --- a/core/src/main/java/org/geysermc/geyser/command/defaults/HelpCommand.java +++ b/core/src/main/java/org/geysermc/geyser/command/defaults/HelpCommand.java @@ -32,7 +32,6 @@ import org.geysermc.geyser.command.GeyserCommand; import org.geysermc.geyser.command.GeyserCommandSource; import org.geysermc.geyser.text.ChatColor; import org.geysermc.geyser.text.GeyserLocale; -import org.incendo.cloud.CommandManager; import org.incendo.cloud.context.CommandContext; import java.util.Collection; @@ -42,14 +41,12 @@ import java.util.Map; public class HelpCommand extends GeyserCommand { private final String rootCommand; - private final String rootCommandPermission; private final Collection commands; public HelpCommand(GeyserImpl geyser, String name, String description, String permission, - String rootCommand, String rootCommandPermission, Map commands) { + String rootCommand, Map commands) { super(name, description, permission, TriState.TRUE); this.rootCommand = rootCommand; - this.rootCommandPermission = rootCommandPermission; this.commands = commands.values(); this.aliases = Collections.singletonList("?"); } @@ -60,21 +57,11 @@ public class HelpCommand extends GeyserCommand { } @Override - public void register(CommandManager manager) { - super.register(manager); - - // Also register just the root (ie `/geyser` or `/extensionId`) - // note: this doesn't do the other permission checks that GeyserCommand#baseBuilder does, - // but it's fine because the help command can be executed by non-bedrock players and by the console. - manager.command(manager.commandBuilder(rootCommand) - .apply(meta()) // shouldn't be necessary - just for consistency - .permission(rootCommandPermission) - .handler((this::execute))); + public void execute(CommandContext context) { + execute(context.sender()); } - @Override - public void execute(CommandContext context) { - GeyserCommandSource source = context.sender(); + public void execute(GeyserCommandSource source) { boolean bedrockPlayer = source.connection() != null; // todo: pagination