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.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import java.nio.file.Path; import java.util.List;
public final class ReloadCommand implements SimpleCommand { public final class ReloadCommand implements SimpleCommand {
final Path dataDirectory; private final Configuration config;
final Configuration config;
ReloadCommand(Path dataDirectory, Configuration config) { ReloadCommand(Configuration config) {
this.dataDirectory = dataDirectory;
this.config = config; this.config = config;
} }
@Override @Override
public void execute(final Invocation invocation) { public void execute(final Invocation invocation) {
CommandSource source = invocation.source(); final CommandSource source = invocation.source();
String[] args = invocation.arguments(); final String[] args = invocation.arguments();
if (args.length == 0) { if (args.length == 0) {
source.sendMessage(Component.text("Usage: /vmessage reload", NamedTextColor.RED)); source.sendMessage(Component.text("Usage: /vmessage reload", NamedTextColor.RED));
return; return;
} }
String s = args[0]; final String s = args[0];
if (s.equalsIgnoreCase("reload")) { 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(); config.reload();
source.sendMessage(Component.text("The Vmessage's config has been succefully reloaded")); 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; import net.kyori.adventure.text.format.NamedTextColor;
public final class SendCommand implements SimpleCommand { public final class SendCommand implements SimpleCommand {
VMessage main; private final VMessage main;
SendCommand(VMessage main) { SendCommand(VMessage main) {
this.main = main; this.main = main;
@ -15,17 +15,19 @@ public final class SendCommand implements SimpleCommand {
@Override @Override
public void execute(final Invocation invocation) { public void execute(final Invocation invocation) {
CommandSource source = invocation.source(); final CommandSource source = invocation.source();
String[] args = invocation.arguments(); final String[] args = invocation.arguments();
if (!(source instanceof Player)) {
return;
}
if (args.length == 0) { if (args.length == 0) {
source.sendMessage(Component.text("Usage: /sendall *your message*", NamedTextColor.RED)); source.sendMessage(Component.text("Usage: /sendall *your message*", NamedTextColor.RED));
return; return;
} }
Player p = (Player) source; final Player p = (Player) source;
String s = args[0]; final String s = args[0];
main.listeners.message(p, s); 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") CommandMeta commandMeta = commandManager.metaBuilder("Vmessage")
.plugin(this) .plugin(this)
.build(); .build();
SimpleCommand command = new ReloadCommand(dataDirectory, configuration); SimpleCommand command = new ReloadCommand(configuration);
commandManager.register(commandMeta, command); commandManager.register(commandMeta, command);
CommandMeta sendmeta = commandManager.metaBuilder("sendall") CommandMeta sendmeta = commandManager.metaBuilder("sendall")
.plugin(this) .plugin(this)