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

View File

@ -1,24 +1,25 @@
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.SimpleCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.william278.desertwell.AboutMenu;
import net.william278.desertwell.Version;
import net.william278.velocitab.Velocitab;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class VelocitabCommand implements SimpleCommand {
public final class VelocitabCommand {
private final AboutMenu aboutMenu;
private final Velocitab plugin;
public VelocitabCommand(Velocitab plugin) {
public VelocitabCommand(final @NotNull Velocitab plugin) {
this.plugin = plugin;
this.aboutMenu = AboutMenu.create("Velocitab")
.withDescription(plugin.getDescription().getDescription().get())
.withVersion(Version.fromString(plugin.getDescription().getVersion().get(), "-"))
.withDescription(plugin.getDescription().getDescription().orElseThrow())
.withVersion(Version.fromString(plugin.getDescription().getVersion().orElseThrow(), "-"))
.addAttribution("Author",
AboutMenu.Credit.of("William278").withDescription("Click to visit website").withUrl("https://william278.net"))
.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"));
}
@Override
public void execute(Invocation invocation) {
if (invocation.arguments().length >= 1) {
if (invocation.arguments()[0].equalsIgnoreCase("reload")) {
reloadSettings(invocation.source());
return;
}
}
public BrigadierCommand command() {
final LiteralArgumentBuilder<CommandSource> builder = LiteralArgumentBuilder
.<CommandSource>literal("velocitab")
.executes(ctx -> {
sendAboutInfo(ctx.getSource());
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());
}
@Override
public List<String> suggest(Invocation invocation) {
if (invocation.source().hasPermission("velocitab.command.reload")) {
return List.of("about", "reload");
} else {
return List.of("about");
}
return new BrigadierCommand(builder);
}
private void sendAboutInfo(CommandSource source) {
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"));
}
}
}