add back root command permission optionally, to avoid Spigot regression

This commit is contained in:
Konicai 2024-06-06 22:50:40 -05:00
parent db399cb7a6
commit c672d6273b
2 changed files with 41 additions and 14 deletions

View file

@ -61,6 +61,11 @@ public class SpigotCommandRegistry extends CommandRegistry {
this.commandMap = commandMap; this.commandMap = commandMap;
} }
@Override
protected boolean applyRootPermission() {
return true; // because we have native permission defaults
}
@NonNull @NonNull
@Override @Override
public String description(@NonNull String command, @NonNull String locale) { public String description(@NonNull String command, @NonNull String locale) {

View file

@ -52,6 +52,7 @@ import org.geysermc.geyser.command.defaults.VersionCommand;
import org.geysermc.geyser.event.type.GeyserDefineCommandsEventImpl; import org.geysermc.geyser.event.type.GeyserDefineCommandsEventImpl;
import org.geysermc.geyser.extension.command.GeyserExtensionCommand; import org.geysermc.geyser.extension.command.GeyserExtensionCommand;
import org.geysermc.geyser.text.GeyserLocale; import org.geysermc.geyser.text.GeyserLocale;
import org.incendo.cloud.Command.Builder;
import org.incendo.cloud.CommandManager; import org.incendo.cloud.CommandManager;
import org.incendo.cloud.execution.ExecutionCoordinator; import org.incendo.cloud.execution.ExecutionCoordinator;
@ -70,6 +71,8 @@ import java.util.Map;
*/ */
public class CommandRegistry implements EventRegistrar { public class CommandRegistry implements EventRegistrar {
private static final String GEYSER_ROOT_PERMISSION = "geyser.command";
private final GeyserImpl geyser; private final GeyserImpl geyser;
private final CommandManager<GeyserCommandSource> cloud; private final CommandManager<GeyserCommandSource> cloud;
@ -103,7 +106,7 @@ public class CommandRegistry implements EventRegistrar {
// begin command registration // begin command registration
HelpCommand help = new HelpCommand(geyser, "help", "geyser.commands.help.desc", "geyser.command.help", GeyserCommand.DEFAULT_ROOT_COMMAND, this.commands); HelpCommand help = new HelpCommand(geyser, "help", "geyser.commands.help.desc", "geyser.command.help", GeyserCommand.DEFAULT_ROOT_COMMAND, this.commands);
registerBuiltInCommand(help); 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 ListCommand(geyser, "list", "geyser.commands.list.desc", "geyser.command.list"));
registerBuiltInCommand(new ReloadCommand(geyser, "reload", "geyser.commands.reload.desc", "geyser.command.reload")); 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 entry.getValue()); // commands it provides help for
registerExtensionCommand(extension, extensionHelp); registerExtensionCommand(extension, extensionHelp);
buildRootCommand(extensionHelp); buildRootCommand("geyser.extension." + id + ".command", extensionHelp);
} }
// wait for the right moment (depends on the platform) to register permissions // 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. * 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. * 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) { private void buildRootCommand(String permission, HelpCommand help) {
// We do the permission check inside the executor because we don't want to actually Builder<GeyserCommandSource> builder = cloud.commandBuilder(help.rootCommand());
// add a permission to the root yet, nor should it be the same as the help command.
cloud.command(cloud.commandBuilder(help.rootCommand()) if (applyRootPermission()) {
.handler(context -> { builder = builder.permission(permission);
GeyserCommandSource source = context.sender(); permissionDefaults.put(permission, TriState.TRUE);
if (!source.hasPermission(help.permission())) { }
source.sendLocaleString(ExceptionHandlers.PERMISSION_FAIL_LANG_KEY);
return; cloud.command(builder.handler(context -> {
} GeyserCommandSource source = context.sender();
help.execute(source); 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) { private void onRegisterPermissions(GeyserRegisterPermissionsEvent event) {