discard root command permission for now, move root->help delegate out of Help

This commit is contained in:
Konicai 2024-06-02 22:34:07 -05:00
parent c92ae75331
commit db399cb7a6
3 changed files with 33 additions and 25 deletions

View file

@ -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) {

View file

@ -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<GeyserCommandSource> controller;
private ExceptionHandlers(ExceptionController<GeyserCommandSource> 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) {

View file

@ -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<Command> commands;
public HelpCommand(GeyserImpl geyser, String name, String description, String permission,
String rootCommand, String rootCommandPermission, Map<String, Command> commands) {
String rootCommand, Map<String, Command> 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<GeyserCommandSource> 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<GeyserCommandSource> context) {
execute(context.sender());
}
@Override
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.sender();
public void execute(GeyserCommandSource source) {
boolean bedrockPlayer = source.connection() != null;
// todo: pagination