Improved Velocitab command using native Brigadier (#34)

This commit is contained in:
Adrian 2023-04-01 18:40:22 -05:00 committed by GitHub
parent b653d568f5
commit 82ffd265a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 38 deletions

View File

@ -1,9 +1,11 @@
package net.william278.velocitab; package net.william278.velocitab;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
@ -36,6 +38,8 @@ public class Velocitab {
private final ProxyServer server; private final ProxyServer server;
private final Logger logger; private final Logger logger;
private final Path dataDirectory; private final Path dataDirectory;
@Inject
private PluginContainer pluginContainer;
private PlayerTabList tabList; private PlayerTabList tabList;
private List<Hook> hooks; private List<Hook> hooks;
private ScoreboardManager scoreboardManager; private ScoreboardManager scoreboardManager;
@ -144,12 +148,14 @@ public class Velocitab {
} }
public PluginDescription getDescription() { public PluginDescription getDescription() {
return server.getPluginManager().getPlugin("velocitab").get().getDescription(); return pluginContainer.getDescription();
} }
private void registerCommands() { private void registerCommands() {
final BrigadierCommand command = new VelocitabCommand(this).command();
server.getCommandManager().register( server.getCommandManager().register(
server.getCommandManager().metaBuilder("velocitab").build(), server.getCommandManager().metaBuilder(command).plugin(this).build(),
new VelocitabCommand(this)); command
);
} }
} }

View File

@ -1,24 +1,25 @@
package net.william278.velocitab.commands; package net.william278.velocitab.commands;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.william278.desertwell.AboutMenu; import net.william278.desertwell.AboutMenu;
import net.william278.desertwell.Version; import net.william278.desertwell.Version;
import net.william278.velocitab.Velocitab; import net.william278.velocitab.Velocitab;
import org.jetbrains.annotations.NotNull;
import java.util.List; public final class VelocitabCommand {
public class VelocitabCommand implements SimpleCommand {
private final AboutMenu aboutMenu; private final AboutMenu aboutMenu;
private final Velocitab plugin; private final Velocitab plugin;
public VelocitabCommand(Velocitab plugin) { public VelocitabCommand(final @NotNull Velocitab plugin) {
this.plugin = plugin; this.plugin = plugin;
this.aboutMenu = AboutMenu.create("Velocitab") this.aboutMenu = AboutMenu.create("Velocitab")
.withDescription(plugin.getDescription().getDescription().get()) .withDescription(plugin.getDescription().getDescription().orElseThrow())
.withVersion(Version.fromString(plugin.getDescription().getVersion().get(), "-")) .withVersion(Version.fromString(plugin.getDescription().getVersion().orElseThrow(), "-"))
.addAttribution("Author", .addAttribution("Author",
AboutMenu.Credit.of("William278").withDescription("Click to visit website").withUrl("https://william278.net")) AboutMenu.Credit.of("William278").withDescription("Click to visit website").withUrl("https://william278.net"))
.addAttribution("Contributors", .addAttribution("Contributors",
@ -30,38 +31,35 @@ public class VelocitabCommand implements SimpleCommand {
AboutMenu.Link.of("https://modrinth.com/plugin/velocitab").withText("Modrinth").withIcon("X").withColor("#589143")); AboutMenu.Link.of("https://modrinth.com/plugin/velocitab").withText("Modrinth").withIcon("X").withColor("#589143"));
} }
@Override public BrigadierCommand command() {
public void execute(Invocation invocation) { final LiteralArgumentBuilder<CommandSource> builder = LiteralArgumentBuilder
if (invocation.arguments().length >= 1) { .<CommandSource>literal("velocitab")
if (invocation.arguments()[0].equalsIgnoreCase("reload")) { .executes(ctx -> {
reloadSettings(invocation.source()); sendAboutInfo(ctx.getSource());
return; return Command.SINGLE_SUCCESS;
} })
} .then(LiteralArgumentBuilder.<CommandSource>literal("about")
.executes(ctx -> {
sendAboutInfo(ctx.getSource());
return Command.SINGLE_SUCCESS;
})
)
.then(LiteralArgumentBuilder.<CommandSource>literal("reload")
.requires(src -> src.hasPermission("velocitab.command.reload"))
.executes(ctx -> {
plugin.loadSettings();
plugin.getTabList().reloadUpdate();
ctx.getSource().sendMessage(Component.text(
"Velocitab has been reloaded!",
TextColor.color(255, 199, 31)));
return Command.SINGLE_SUCCESS;
})
);
sendAboutInfo(invocation.source()); return new BrigadierCommand(builder);
}
@Override
public List<String> suggest(Invocation invocation) {
if (invocation.source().hasPermission("velocitab.command.reload")) {
return List.of("about", "reload");
} else {
return List.of("about");
}
} }
private void sendAboutInfo(CommandSource source) { private void sendAboutInfo(CommandSource source) {
source.sendMessage(aboutMenu.toMineDown().toComponent()); source.sendMessage(aboutMenu.toMineDown().toComponent());
} }
private void reloadSettings(CommandSource source) {
if (source.hasPermission("velocitab.command.reload")) {
plugin.loadSettings();
plugin.getTabList().reloadUpdate();
source.sendMessage(Component.text("Velocitab has been reloaded!").color(TextColor.color(255, 199, 31)));
} else {
source.sendMessage(Component.text("You do not have permission to use this command"));
}
}
} }