Skip to content

Commit

Permalink
fix brigadier commands for velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Oct 20, 2024
1 parent e468037 commit 4c30287
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ private VelocityLampConfig(ActorFactory<A> actorFactory, ArgumentTypes<A> argume
.accept(velocityParameterTypes(server))
.accept(velocityExceptionHandler())
.accept(velocityPermissions())
.accept(registrationHooks(this))
.accept(pluginContextParameters(plugin));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <A> The actor type
* @return The visitor
*/
public static <A extends VelocityCommandActor> @NotNull LampBuilderVisitor<A> registrationHooks(
@NotNull VelocityLampConfig<A> config
) {
VelocityCommandHooks<A> hooks = new VelocityCommandHooks<>(config);
return builder -> builder.hooks()
.onCommandRegistered(hooks);
}

/**
* Adds dependencies and type resolvers for the given plugin object
*
Expand All @@ -139,4 +126,15 @@ public final class VelocityVisitors {
public static <A extends VelocityCommandActor> @NotNull LampBuilderVisitor<A> velocityPermissions() {
return builder -> builder.permissionFactory(VelocityPermissionFactory.INSTANCE);
}

/**
* Registers the commands into Velocity as {@link BrigadierCommand brigadier commands}.
*
* @param config The {@link VelocityLampConfig} object
* @param <A> The actor type
* @return The visitor
*/
public static <A extends VelocityCommandActor> @NotNull LampVisitor<A> brigadier(@NotNull VelocityLampConfig<A> config) {
return new VelocityBrigadier<>(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,33 +45,13 @@
*
* @param <A> The actor type
*/
public final class VelocityCommandHooks<A extends VelocityCommandActor> implements CommandRegisteredHook<A>, BrigadierConverter<A, CommandSource> {

/**
* Tracks the commands we registered
*/
private final RootCommandNode<CommandSource> root = new RootCommandNode<>();
public final class VelocityBrigadier<A extends VelocityCommandActor> implements LampVisitor<A>, BrigadierConverter<A, CommandSource> {

private final VelocityLampConfig<A> config;
private final BrigadierParser<CommandSource, A> parser = new BrigadierParser<>(this);

public VelocityCommandHooks(VelocityLampConfig<A> config) {
public VelocityBrigadier(VelocityLampConfig<A> config) {
this.config = config;
config.server().getEventManager().register(config.plugin(), this);
}

@Override
public void onRegistered(@NotNull ExecutableCommand<A> command, @NotNull CancelHandle cancelHandle) {
LiteralCommandNode<CommandSource> node = parser.createNode(command);
root.addChild(node);
}

@Subscribe
public void onProxyInitialize(ProxyInitializeEvent event) {
for (CommandNode<CommandSource> node : root.getChildren()) {
BrigadierCommand brigadierCommand = new BrigadierCommand((LiteralCommandNode<CommandSource>) node);
config.server().getCommandManager().register(brigadierCommand);
}
}

@Override
Expand All @@ -85,4 +63,20 @@ public void onProxyInitialize(ProxyInitializeEvent event) {
public @NotNull A createActor(@NotNull CommandSource sender, @NotNull Lamp<A> lamp) {
return config.actorFactory().create(sender, lamp);
}

@Override
public void visit(@NotNull Lamp<A> lamp) {
RootCommandNode<CommandSource> root = new RootCommandNode<>();
for (ExecutableCommand<A> command : lamp.registry()) {
LiteralCommandNode<CommandSource> node = parser.createNode(command);
root.addChild(node);
}
for (CommandNode<CommandSource> node : root.getChildren()) {
BrigadierCommand brigadierCommand = new BrigadierCommand((LiteralCommandNode<CommandSource>) node);
CommandMeta meta = config.server().getCommandManager().metaBuilder(node.getName())
// .plugin(config.plugin())
.build();
config.server().getCommandManager().register(meta, brigadierCommand);
}
}
}

0 comments on commit 4c30287

Please sign in to comment.