Implement tab complete and permissions check on commands

This commit is contained in:
Adrian 2023-01-29 13:41:56 -05:00
parent 25d0fdaa29
commit 6fdd81c933
3 changed files with 33 additions and 21 deletions

View File

@ -5,33 +5,43 @@ import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.nio.file.Path;
import java.util.List;
public final class ReloadCommand implements SimpleCommand {
final Path dataDirectory;
final Configuration config;
private final Configuration config;
ReloadCommand(Path dataDirectory, Configuration config) {
this.dataDirectory = dataDirectory;
ReloadCommand(Configuration config) {
this.config = config;
}
@Override
public void execute(final Invocation invocation) {
CommandSource source = invocation.source();
String[] args = invocation.arguments();
final CommandSource source = invocation.source();
final String[] args = invocation.arguments();
if (args.length == 0) {
source.sendMessage(Component.text("Usage: /vmessage reload", NamedTextColor.RED));
return;
}
String s = args[0];
final String s = args[0];
if (s.equalsIgnoreCase("reload")) {
if (!source.hasPermission("*")) {
source.sendMessage(Component.text("You don't have the permission to do that", NamedTextColor.RED));
return;
}
config.reload();
source.sendMessage(Component.text("The Vmessage's config has been succefully reloaded"));
}
}
@Override
public boolean hasPermission(final Invocation invocation) {
return invocation.source().hasPermission("vmessage.command");
}
private static final List<String> suggestion = List.of("reload");
@Override
public List<String> suggest(final Invocation invocation) {
final String[] args = invocation.arguments();
if (args.length == 0 || (args.length == 1 && "reload".startsWith(args[0]))) {
return suggestion;
}
return List.of();
}
}

View File

@ -7,7 +7,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public final class SendCommand implements SimpleCommand {
VMessage main;
private final VMessage main;
SendCommand(VMessage main) {
this.main = main;
@ -15,17 +15,19 @@ public final class SendCommand implements SimpleCommand {
@Override
public void execute(final Invocation invocation) {
CommandSource source = invocation.source();
String[] args = invocation.arguments();
if (!(source instanceof Player)) {
return;
}
final CommandSource source = invocation.source();
final String[] args = invocation.arguments();
if (args.length == 0) {
source.sendMessage(Component.text("Usage: /sendall *your message*", NamedTextColor.RED));
return;
}
Player p = (Player) source;
String s = args[0];
final Player p = (Player) source;
final String s = args[0];
main.listeners.message(p, s);
}
@Override
public boolean hasPermission(final Invocation invocation) {
return invocation.source() instanceof Player;
}
}

View File

@ -50,7 +50,7 @@ public class VMessage {
CommandMeta commandMeta = commandManager.metaBuilder("Vmessage")
.plugin(this)
.build();
SimpleCommand command = new ReloadCommand(dataDirectory, configuration);
SimpleCommand command = new ReloadCommand(configuration);
commandManager.register(commandMeta, command);
CommandMeta sendmeta = commandManager.metaBuilder("sendall")
.plugin(this)