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); ExceptionHandlers.register(cloud);
// begin command registration // 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 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"));
registerBuiltInCommand(new OffhandCommand(geyser, "offhand", "geyser.commands.offhand.desc", "geyser.command.offhand")); 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 // Register help commands for all extensions with commands
String id = extension.description().id(); String id = extension.description().id();
registerExtensionCommand(extension, new HelpCommand( HelpCommand extensionHelp = new HelpCommand(
this.geyser, this.geyser,
"help", "help",
"geyser.commands.exthelp.desc", "geyser.commands.exthelp.desc",
"geyser.command.exthelp." + id, "geyser.command.exthelp." + id,
extension.rootCommand(), extension.rootCommand(),
extension.description().id() + ".command", entry.getValue()); // commands it provides help for
entry.getValue()));
registerExtensionCommand(extension, extensionHelp);
buildRootCommand(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
@ -187,10 +192,24 @@ public class CommandRegistry implements EventRegistrar {
if (!command.permission().isBlank() && command.permissionDefault() != null) { if (!command.permission().isBlank() && command.permissionDefault() != null) {
permissionDefaults.put(command.permission(), command.permissionDefault()); 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) { private void onRegisterPermissions(GeyserRegisterPermissionsEvent event) {

View file

@ -49,6 +49,8 @@ import java.util.function.BiConsumer;
*/ */
final class ExceptionHandlers { final class ExceptionHandlers {
final static String PERMISSION_FAIL_LANG_KEY = "geyser.command.permission_fail";
private final ExceptionController<GeyserCommandSource> controller; private final ExceptionController<GeyserCommandSource> controller;
private ExceptionHandlers(ExceptionController<GeyserCommandSource> controller) { private ExceptionHandlers(ExceptionController<GeyserCommandSource> controller) {
@ -117,7 +119,7 @@ final class ExceptionHandlers {
} }
// Result.NO_PERMISSION or generic permission failure // 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) { 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.command.GeyserCommandSource;
import org.geysermc.geyser.text.ChatColor; import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.GeyserLocale; import org.geysermc.geyser.text.GeyserLocale;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.context.CommandContext; import org.incendo.cloud.context.CommandContext;
import java.util.Collection; import java.util.Collection;
@ -42,14 +41,12 @@ import java.util.Map;
public class HelpCommand extends GeyserCommand { public class HelpCommand extends GeyserCommand {
private final String rootCommand; private final String rootCommand;
private final String rootCommandPermission;
private final Collection<Command> commands; private final Collection<Command> commands;
public HelpCommand(GeyserImpl geyser, String name, String description, String permission, 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); super(name, description, permission, TriState.TRUE);
this.rootCommand = rootCommand; this.rootCommand = rootCommand;
this.rootCommandPermission = rootCommandPermission;
this.commands = commands.values(); this.commands = commands.values();
this.aliases = Collections.singletonList("?"); this.aliases = Collections.singletonList("?");
} }
@ -60,21 +57,11 @@ public class HelpCommand extends GeyserCommand {
} }
@Override @Override
public void register(CommandManager<GeyserCommandSource> manager) { public void execute(CommandContext<GeyserCommandSource> context) {
super.register(manager); execute(context.sender());
// 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)));
} }
@Override public void execute(GeyserCommandSource source) {
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.sender();
boolean bedrockPlayer = source.connection() != null; boolean bedrockPlayer = source.connection() != null;
// todo: pagination // todo: pagination