From c672d6273b23aa06f8efd0a54086de0807e001d3 Mon Sep 17 00:00:00 2001 From: Konicai <71294714+Konicai@users.noreply.github.com> Date: Thu, 6 Jun 2024 22:50:40 -0500 Subject: [PATCH] add back root command permission optionally, to avoid Spigot regression --- .../spigot/command/SpigotCommandRegistry.java | 5 ++ .../geyser/command/CommandRegistry.java | 50 +++++++++++++------ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/command/SpigotCommandRegistry.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/command/SpigotCommandRegistry.java index 39496d2c6..b61dc6f8d 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/command/SpigotCommandRegistry.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/command/SpigotCommandRegistry.java @@ -61,6 +61,11 @@ public class SpigotCommandRegistry extends CommandRegistry { this.commandMap = commandMap; } + @Override + protected boolean applyRootPermission() { + return true; // because we have native permission defaults + } + @NonNull @Override public String description(@NonNull String command, @NonNull String locale) { 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 abc25c2f2..220bb0e8c 100644 --- a/core/src/main/java/org/geysermc/geyser/command/CommandRegistry.java +++ b/core/src/main/java/org/geysermc/geyser/command/CommandRegistry.java @@ -52,6 +52,7 @@ import org.geysermc.geyser.command.defaults.VersionCommand; import org.geysermc.geyser.event.type.GeyserDefineCommandsEventImpl; import org.geysermc.geyser.extension.command.GeyserExtensionCommand; import org.geysermc.geyser.text.GeyserLocale; +import org.incendo.cloud.Command.Builder; import org.incendo.cloud.CommandManager; import org.incendo.cloud.execution.ExecutionCoordinator; @@ -70,6 +71,8 @@ import java.util.Map; */ public class CommandRegistry implements EventRegistrar { + private static final String GEYSER_ROOT_PERMISSION = "geyser.command"; + private final GeyserImpl geyser; private final CommandManager cloud; @@ -103,7 +106,7 @@ public class CommandRegistry implements EventRegistrar { // begin command registration 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 + buildRootCommand(GEYSER_ROOT_PERMISSION, 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")); @@ -153,7 +156,7 @@ public class CommandRegistry implements EventRegistrar { entry.getValue()); // commands it provides help for registerExtensionCommand(extension, extensionHelp); - buildRootCommand(extensionHelp); + buildRootCommand("geyser.extension." + id + ".command", extensionHelp); } // wait for the right moment (depends on the platform) to register permissions @@ -197,19 +200,38 @@ public class CommandRegistry implements EventRegistrar { /** * 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. + * + * @param permission the permission of the root command. currently, it may or may not be + * applied depending on the platform. see below. + * @param help the help command to delegate to */ - 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 buildRootCommand(String permission, HelpCommand help) { + Builder builder = cloud.commandBuilder(help.rootCommand()); + + if (applyRootPermission()) { + builder = builder.permission(permission); + permissionDefaults.put(permission, TriState.TRUE); + } + + cloud.command(builder.handler(context -> { + GeyserCommandSource source = context.sender(); + if (!source.hasPermission(help.permission())) { + // delegate if possible - otherwise we have nothing else to offer the user. + source.sendLocaleString(ExceptionHandlers.PERMISSION_FAIL_LANG_KEY); + return; + } + help.execute(source); + })); + } + + /** + * Returns true if the registry should apply a permission to Geyser and Extension root commands. + * This currently exists because we want to retain the root command permission for Spigot, but don't want to add + * it yet to platforms like Velocity where we cannot natively specify a permission default. Doing so will + * break setups as players would suddenly not have the required permission to execute any Geyser commands. + */ + protected boolean applyRootPermission() { + return false; } private void onRegisterPermissions(GeyserRegisterPermissionsEvent event) {