Created
April 3, 2022 23:34
-
-
Save 4drian3d/8b8db7bd01d2c7b4f8a7e4744620afbc to your computer and use it in GitHub Desktop.
Velocity Brigadier Example Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.dreamerzero.example.commands; | |
import java.util.Locale; | |
import com.mojang.brigadier.arguments.StringArgumentType; | |
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | |
import com.mojang.brigadier.builder.RequiredArgumentBuilder; | |
import com.mojang.brigadier.tree.LiteralCommandNode; | |
import com.velocitypowered.api.command.BrigadierCommand; | |
import com.velocitypowered.api.command.CommandManager; | |
import com.velocitypowered.api.command.CommandMeta; | |
import com.velocitypowered.api.command.CommandSource; | |
import com.velocitypowered.api.command.VelocityBrigadierMessage; | |
import com.velocitypowered.api.proxy.Player; | |
import com.velocitypowered.api.proxy.ProxyServer; | |
import org.jetbrains.annotations.NotNull; | |
import net.kyori.adventure.text.Component; | |
import net.kyori.adventure.text.format.NamedTextColor; | |
public final class BrigadierExample { | |
private BrigadierExample(){} | |
// This method must be invoked in the listener of the ProxyInitializeEvent | |
// in which the plugin functions will be initialized | |
public static final void command(@NotNull final ProxyServer proxy){ | |
// Here you create the command "/command" | |
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder.<CommandSource>literal("command") | |
// Here you can filter the subjects that can execute the command. | |
// This is the ideal place to do "hasPermission" checks | |
.requires(source -> source.hasPermission("plugin.permission")) | |
// The "executes" method allows you to execute an action at the time of executing the command | |
// This will be executed in the command "/command", without any argument | |
.executes(context -> { | |
// Here you get the subject that executed the command | |
CommandSource source = context.getSource(); | |
// Here you can check the type of instance that has executed the command, | |
// it can be a player or the console | |
if(source instanceof Player){ | |
Player player = (Player)source; | |
player.sendActionBar(Component.text("You are a Player")); | |
} else { | |
source.sendMessage(Component.text("You are not a Player")); | |
} | |
// In this method you must return an integer | |
// 1 means that the execution was successful | |
// Returning BrigadierCommand.FORWARD will send the command to the server | |
return 1; | |
}) | |
// This is an argument of the "command" command | |
// This will be executed in the command "/command literalArgument" | |
.then(LiteralArgumentBuilder.<CommandSource>literal("literalArgument") | |
// Permission requeriment to execute this command | |
.requires(source -> source.hasPermission("plugin.permission.argument")) | |
// Execution of this command | |
.executes(context -> { | |
// Here you get the subject that executed the command | |
CommandSource source = context.getSource(); | |
source.sendMessage(Component.text("Hello from the literalArgument argument")); | |
return 1; | |
}) | |
// TODO: Document better the RequiredArgumentBuilder | |
// This will be executed in the command "/command literalArgument <some argument>" | |
.then(RequiredArgumentBuilder.<CommandSource, String>argument("requiredArgument", StringArgumentType.word()) | |
// Permission required to execute this command | |
.requires(source -> source.hasPermission("plugin.permission.requiredargument")) | |
// If the argument type does not offer suggestions, you can implement them as required | |
// | |
// This method offers you the context to give the | |
// suggestions (the actual command, the CommandSource, specific arguments, etc) | |
// and the suggestion builder, in which you can add the suggestions you want | |
.suggests((context, builder) -> { | |
// Adding simple text suggestions | |
builder.suggest("example"); | |
// Adding text suggestions with component tooltips | |
builder.suggest("exampleWithTooltip", VelocityBrigadierMessage.tooltip( | |
Component.text("example", NamedTextColor.AQUA) | |
)); | |
builder.suggest("example2"); | |
// Adding numeric suggestions | |
builder.suggest(2); | |
// Adding numeric suggestions with component tooltips | |
builder.suggest(5, VelocityBrigadierMessage.tooltip( | |
Component.text("example numeric suggestion") | |
)); | |
return builder.buildFuture(); | |
}) | |
// Execution of this command | |
.executes(context -> { | |
// Here you get the subject that executed the command | |
CommandSource source = context.getSource(); | |
// Here you can get the argument delivered by the command executor. | |
// You must enter the exact name as you have named the RequiredArgumentBuilder | |
// You must also enter the type of object which is the argument, | |
// in this case as it was specified a StringArgumentType, | |
// you must put String as the type of object, | |
// but you could also receive Float, Double, | |
// Integer, etc depending on the ArgumentType specified. | |
String argument = context.getArgument("requiredArgument", String.class); | |
switch(argument.toLowerCase(Locale.ROOT)){ | |
case "example": | |
source.sendMessage(Component.text("You has introduced the example argument")); | |
break; | |
case "exampleWithTooltip": | |
source.sendMessage(Component.text("You has introduced the exampleWithTooltip argument")); | |
break; | |
case "example2": | |
source.sendMessage(Component.text("You has introduced the example2 argument")); | |
break; | |
default: | |
source.sendMessage(Component.text("You has introduced an unknown argument")); | |
} | |
return 1; | |
}) | |
) | |
) | |
// This will be executed in the command "/command someOtherArgument" | |
.then(LiteralArgumentBuilder.literal("someOtherArgument") | |
// etc, etc | |
) | |
.build(); | |
//Here you wrap the LiteralCommandNode into a BrigadierCommand which implements Command | |
BrigadierCommand brigadierCommand = new BrigadierCommand(command); | |
// Obtain the velocity's command manager | |
CommandManager commandManager = proxy.getCommandManager(); | |
// Here you can add meta for the command, as aliases and the plugin to which it belongs(RECOMMENDED) | |
CommandMeta commandMeta = commandManager.metaBuilder(brigadierCommand) | |
// This will create a new command "/examplee" | |
// with the same arguments and functionality as the command "/command" | |
.aliases("examplee") | |
.build(); | |
// Here you can register the command | |
commandManager.register(commandMeta, brigadierCommand); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment