Skip to content

Commit

Permalink
improve the structure of VelocityBrigadier
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Dec 20, 2024
1 parent a7a7d8d commit 913c4cc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
import revxrsal.commands.Lamp;
import revxrsal.commands.LampBuilderVisitor;
import revxrsal.commands.LampVisitor;
import revxrsal.commands.brigadier.types.ArgumentTypes;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.exception.CommandExceptionHandler;
import revxrsal.commands.parameter.ContextParameter;
import revxrsal.commands.velocity.actor.ActorFactory;
import revxrsal.commands.velocity.actor.VelocityCommandActor;
import revxrsal.commands.velocity.annotation.CommandPermission;
import revxrsal.commands.velocity.exception.VelocityExceptionHandler;
Expand Down Expand Up @@ -137,4 +139,48 @@ public final class VelocityVisitors {
public static <A extends VelocityCommandActor> @NotNull LampVisitor<A> brigadier(@NotNull VelocityLampConfig<A> config) {
return new VelocityBrigadier<>(config);
}

/**
* Registers the commands into Velocity as {@link BrigadierCommand brigadier commands}.
*
* @param server The server instance
* @param actorFactory The actor factory
* @param argumentTypes The argument type registry
* @param <A> The actor type
* @return The visitor
*/
public static <A extends VelocityCommandActor> @NotNull LampVisitor<A> brigadier(
@NotNull ProxyServer server,
@NotNull ActorFactory<A> actorFactory,
@NotNull ArgumentTypes<A> argumentTypes
) {
return new VelocityBrigadier<>(server, actorFactory, argumentTypes);
}

/**
* Registers the commands into Velocity as {@link BrigadierCommand brigadier commands}.
*
* @param server The server instance
* @param actorFactory The actor factory
* @param <A> The actor type
* @return The visitor
*/
public static <A extends VelocityCommandActor> @NotNull LampVisitor<A> brigadier(
@NotNull ProxyServer server,
@NotNull ActorFactory<A> actorFactory
) {
return new VelocityBrigadier<>(server, actorFactory, ArgumentTypes.<A>builder().build());
}

/**
* Registers the commands into Velocity as {@link BrigadierCommand brigadier commands}.
*
* @param server The server instance
* @return The visitor
*/
public static @NotNull LampVisitor<VelocityCommandActor> brigadier(
@NotNull ProxyServer server
) {
return new VelocityBrigadier<>(server, ActorFactory.defaultFactory(), ArgumentTypes.<VelocityCommandActor>builder().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.ProxyServer;
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.brigadier.types.ArgumentTypes;
import revxrsal.commands.command.ExecutableCommand;
import revxrsal.commands.node.ParameterNode;
import revxrsal.commands.velocity.VelocityLampConfig;
import revxrsal.commands.velocity.actor.ActorFactory;
import revxrsal.commands.velocity.actor.VelocityCommandActor;

/**
Expand All @@ -47,21 +50,31 @@
*/
public final class VelocityBrigadier<A extends VelocityCommandActor> implements LampVisitor<A>, BrigadierConverter<A, CommandSource> {

private final VelocityLampConfig<A> config;
private final ProxyServer server;
private final ActorFactory<A> actorFactory;
private final ArgumentTypes<A> argumentTypes;
private final BrigadierParser<CommandSource, A> parser = new BrigadierParser<>(this);

public VelocityBrigadier(VelocityLampConfig<A> config) {
this.config = config;
public VelocityBrigadier(@NotNull VelocityLampConfig<A> config) {
this.server = config.server();
this.actorFactory = config.actorFactory();
this.argumentTypes = config.argumentTypes();
}

public VelocityBrigadier(@NotNull ProxyServer server, @NotNull ActorFactory<A> actorFactory, @NotNull ArgumentTypes<A> argumentTypes) {
this.server = server;
this.actorFactory = actorFactory;
this.argumentTypes = argumentTypes;
}

@Override
public @NotNull ArgumentType<?> getArgumentType(@NotNull ParameterNode<A, ?> parameter) {
return config.argumentTypes().type(parameter);
return argumentTypes.type(parameter);
}

@Override
public @NotNull A createActor(@NotNull CommandSource sender, @NotNull Lamp<A> lamp) {
return config.actorFactory().create(sender, lamp);
return actorFactory.create(sender, lamp);
}

@Override
Expand All @@ -73,10 +86,8 @@ public void visit(@NotNull Lamp<A> lamp) {
}
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);
CommandMeta meta = server.getCommandManager().metaBuilder(node.getName()).build();
server.getCommandManager().register(meta, brigadierCommand);
}
}
}

0 comments on commit 913c4cc

Please sign in to comment.