From 4c30287de2a92af772f9d12178a7689cfe12a465 Mon Sep 17 00:00:00 2001 From: Revxrsal Date: Sun, 20 Oct 2024 13:46:35 +0300 Subject: [PATCH] fix brigadier commands for velocity --- .../commands/velocity/VelocityLampConfig.java | 1 - .../commands/velocity/VelocityVisitors.java | 30 ++++++------ ...mmandHooks.java => VelocityBrigadier.java} | 46 ++++++++----------- 3 files changed, 34 insertions(+), 43 deletions(-) rename velocity/src/main/java/revxrsal/commands/velocity/hooks/{VelocityCommandHooks.java => VelocityBrigadier.java} (73%) diff --git a/velocity/src/main/java/revxrsal/commands/velocity/VelocityLampConfig.java b/velocity/src/main/java/revxrsal/commands/velocity/VelocityLampConfig.java index 51ef0cc3..2b67437e 100644 --- a/velocity/src/main/java/revxrsal/commands/velocity/VelocityLampConfig.java +++ b/velocity/src/main/java/revxrsal/commands/velocity/VelocityLampConfig.java @@ -98,7 +98,6 @@ private VelocityLampConfig(ActorFactory actorFactory, ArgumentTypes argume .accept(velocityParameterTypes(server)) .accept(velocityExceptionHandler()) .accept(velocityPermissions()) - .accept(registrationHooks(this)) .accept(pluginContextParameters(plugin)); } diff --git a/velocity/src/main/java/revxrsal/commands/velocity/VelocityVisitors.java b/velocity/src/main/java/revxrsal/commands/velocity/VelocityVisitors.java index 3512403d..a95e8145 100644 --- a/velocity/src/main/java/revxrsal/commands/velocity/VelocityVisitors.java +++ b/velocity/src/main/java/revxrsal/commands/velocity/VelocityVisitors.java @@ -23,19 +23,21 @@ */ package revxrsal.commands.velocity; +import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import org.jetbrains.annotations.NotNull; import revxrsal.commands.Lamp; import revxrsal.commands.LampBuilderVisitor; +import revxrsal.commands.LampVisitor; import revxrsal.commands.command.CommandActor; import revxrsal.commands.exception.CommandExceptionHandler; import revxrsal.commands.parameter.ContextParameter; import revxrsal.commands.velocity.actor.VelocityCommandActor; import revxrsal.commands.velocity.annotation.CommandPermission; import revxrsal.commands.velocity.exception.VelocityExceptionHandler; -import revxrsal.commands.velocity.hooks.VelocityCommandHooks; +import revxrsal.commands.velocity.hooks.VelocityBrigadier; import revxrsal.commands.velocity.parameters.PlayerParameterType; import revxrsal.commands.velocity.sender.VelocityPermissionFactory; import revxrsal.commands.velocity.sender.VelocitySenderResolver; @@ -100,21 +102,6 @@ public final class VelocityVisitors { .addParameterTypeLast(Player.class, new PlayerParameterType(server)); } - /** - * Adds a registration hook that injects Lamp commands into Velocity. - * - * @param config The {@link VelocityLampConfig} instance - * @param The actor type - * @return The visitor - */ - public static @NotNull LampBuilderVisitor registrationHooks( - @NotNull VelocityLampConfig config - ) { - VelocityCommandHooks hooks = new VelocityCommandHooks<>(config); - return builder -> builder.hooks() - .onCommandRegistered(hooks); - } - /** * Adds dependencies and type resolvers for the given plugin object * @@ -139,4 +126,15 @@ public final class VelocityVisitors { public static @NotNull LampBuilderVisitor velocityPermissions() { return builder -> builder.permissionFactory(VelocityPermissionFactory.INSTANCE); } + + /** + * Registers the commands into Velocity as {@link BrigadierCommand brigadier commands}. + * + * @param config The {@link VelocityLampConfig} object + * @param The actor type + * @return The visitor + */ + public static @NotNull LampVisitor brigadier(@NotNull VelocityLampConfig config) { + return new VelocityBrigadier<>(config); + } } diff --git a/velocity/src/main/java/revxrsal/commands/velocity/hooks/VelocityCommandHooks.java b/velocity/src/main/java/revxrsal/commands/velocity/hooks/VelocityBrigadier.java similarity index 73% rename from velocity/src/main/java/revxrsal/commands/velocity/hooks/VelocityCommandHooks.java rename to velocity/src/main/java/revxrsal/commands/velocity/hooks/VelocityBrigadier.java index 2a976331..16a52ae4 100644 --- a/velocity/src/main/java/revxrsal/commands/velocity/hooks/VelocityCommandHooks.java +++ b/velocity/src/main/java/revxrsal/commands/velocity/hooks/VelocityBrigadier.java @@ -28,16 +28,14 @@ import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.RootCommandNode; import com.velocitypowered.api.command.BrigadierCommand; +import com.velocitypowered.api.command.CommandMeta; import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.event.Subscribe; -import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import org.jetbrains.annotations.NotNull; import revxrsal.commands.Lamp; +import revxrsal.commands.LampVisitor; import revxrsal.commands.brigadier.BrigadierConverter; import revxrsal.commands.brigadier.BrigadierParser; import revxrsal.commands.command.ExecutableCommand; -import revxrsal.commands.hook.CancelHandle; -import revxrsal.commands.hook.CommandRegisteredHook; import revxrsal.commands.node.ParameterNode; import revxrsal.commands.velocity.VelocityLampConfig; import revxrsal.commands.velocity.actor.VelocityCommandActor; @@ -47,33 +45,13 @@ * * @param The actor type */ -public final class VelocityCommandHooks implements CommandRegisteredHook, BrigadierConverter { - - /** - * Tracks the commands we registered - */ - private final RootCommandNode root = new RootCommandNode<>(); +public final class VelocityBrigadier implements LampVisitor, BrigadierConverter { private final VelocityLampConfig config; private final BrigadierParser parser = new BrigadierParser<>(this); - public VelocityCommandHooks(VelocityLampConfig config) { + public VelocityBrigadier(VelocityLampConfig config) { this.config = config; - config.server().getEventManager().register(config.plugin(), this); - } - - @Override - public void onRegistered(@NotNull ExecutableCommand command, @NotNull CancelHandle cancelHandle) { - LiteralCommandNode node = parser.createNode(command); - root.addChild(node); - } - - @Subscribe - public void onProxyInitialize(ProxyInitializeEvent event) { - for (CommandNode node : root.getChildren()) { - BrigadierCommand brigadierCommand = new BrigadierCommand((LiteralCommandNode) node); - config.server().getCommandManager().register(brigadierCommand); - } } @Override @@ -85,4 +63,20 @@ public void onProxyInitialize(ProxyInitializeEvent event) { public @NotNull A createActor(@NotNull CommandSource sender, @NotNull Lamp lamp) { return config.actorFactory().create(sender, lamp); } + + @Override + public void visit(@NotNull Lamp lamp) { + RootCommandNode root = new RootCommandNode<>(); + for (ExecutableCommand command : lamp.registry()) { + LiteralCommandNode node = parser.createNode(command); + root.addChild(node); + } + for (CommandNode node : root.getChildren()) { + BrigadierCommand brigadierCommand = new BrigadierCommand((LiteralCommandNode) node); + CommandMeta meta = config.server().getCommandManager().metaBuilder(node.getName()) +// .plugin(config.plugin()) + .build(); + config.server().getCommandManager().register(meta, brigadierCommand); + } + } }