From 6fdd81c9330c36a9821d4027a771b9bb69df4e5b Mon Sep 17 00:00:00 2001 From: Adrian Date: Sun, 29 Jan 2023 13:41:56 -0500 Subject: [PATCH] Implement tab complete and permissions check on commands --- .../feusalamander/vmessage/ReloadCommand.java | 34 ++++++++++++------- .../feusalamander/vmessage/SendCommand.java | 18 +++++----- .../me/feusalamander/vmessage/VMessage.java | 2 +- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/main/java/me/feusalamander/vmessage/ReloadCommand.java b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java index 47bff7b..28ccab2 100644 --- a/src/main/java/me/feusalamander/vmessage/ReloadCommand.java +++ b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java @@ -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 suggestion = List.of("reload"); + + @Override + public List 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(); + } } diff --git a/src/main/java/me/feusalamander/vmessage/SendCommand.java b/src/main/java/me/feusalamander/vmessage/SendCommand.java index 7d66fbd..b4ff4f1 100644 --- a/src/main/java/me/feusalamander/vmessage/SendCommand.java +++ b/src/main/java/me/feusalamander/vmessage/SendCommand.java @@ -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; + } } diff --git a/src/main/java/me/feusalamander/vmessage/VMessage.java b/src/main/java/me/feusalamander/vmessage/VMessage.java index 51b5e10..4f4657b 100644 --- a/src/main/java/me/feusalamander/vmessage/VMessage.java +++ b/src/main/java/me/feusalamander/vmessage/VMessage.java @@ -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)