diff --git a/Velocity/src/main/java/dev/brighten/antivpn/velocity/VelocityPlugin.java b/Velocity/src/main/java/dev/brighten/antivpn/velocity/VelocityPlugin.java index f0494b8..c4e77e4 100644 --- a/Velocity/src/main/java/dev/brighten/antivpn/velocity/VelocityPlugin.java +++ b/Velocity/src/main/java/dev/brighten/antivpn/velocity/VelocityPlugin.java @@ -10,6 +10,8 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; import dev.brighten.antivpn.AntiVPN; import dev.brighten.antivpn.command.Command; +import dev.brighten.antivpn.velocity.command.VelocityCommand; +import dev.brighten.antivpn.velocity.command.VelocityCommandExecutor; import lombok.Getter; import lombok.val; import net.kyori.adventure.text.Component; @@ -28,7 +30,7 @@ public class VelocityPlugin { private final ProxyServer server; private final Logger logger; - private Metrics.Factory metricsFactory; + private final Metrics.Factory metricsFactory; public static VelocityPlugin INSTANCE; @@ -60,40 +62,7 @@ public class VelocityPlugin { logger.info("Registering commands..."); for (Command command : AntiVPN.getInstance().getCommands()) { server.getCommandManager().register(server.getCommandManager().metaBuilder(command.name()) - .aliases(command.aliases()).build(), (SimpleCommand) invocation -> { - CommandSource sender = invocation.source(); - if(!invocation.source().hasPermission("antivpn.command.*") - && !invocation.source().hasPermission(command.permission())) { - invocation.source().sendMessage(Component.text("No permission").toBuilder() - .color(TextColor.color(255,0,0)).build()); - return; - } - - val children = command.children(); - - String[] args = invocation.arguments(); - if(children.length > 0 && args.length > 0) { - for (Command child : children) { - if(child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases()) - .anyMatch(alias -> alias.equalsIgnoreCase(args[0]))) { - if(!sender.hasPermission("antivpn.command.*") - && !sender.hasPermission(child.permission())) { - invocation.source().sendMessage(Component.text("No permission") - .toBuilder().color(TextColor.color(255,0,0)).build()); - return; - } - sender.sendMessage(LegacyComponentSerializer.builder().character('&').build() - .deserialize(child.execute(new VelocityCommandExecutor(sender), IntStream - .range(0, args.length - 1) - .mapToObj(i -> args[i + 1]).toArray(String[]::new)))); - return; - } - } - } - - sender.sendMessage(LegacyComponentSerializer.builder().character('&').build() - .deserialize(command.execute(new VelocityCommandExecutor(sender), args))); - }); + .aliases(command.aliases()).build(), new VelocityCommand(command)); } } } diff --git a/Velocity/src/main/java/dev/brighten/antivpn/velocity/command/VelocityCommand.java b/Velocity/src/main/java/dev/brighten/antivpn/velocity/command/VelocityCommand.java new file mode 100644 index 0000000..f58a71e --- /dev/null +++ b/Velocity/src/main/java/dev/brighten/antivpn/velocity/command/VelocityCommand.java @@ -0,0 +1,89 @@ +package dev.brighten.antivpn.velocity.command; + +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import dev.brighten.antivpn.command.Command; +import lombok.val; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.stream.IntStream; + +public class VelocityCommand implements SimpleCommand { + + private final Command command; + + public VelocityCommand(Command command) { + this.command = command; + } + + @Override + public void execute(Invocation invocation) { + CommandSource sender = invocation.source(); + if(!invocation.source().hasPermission("antivpn.command.*") + && !invocation.source().hasPermission(command.permission())) { + invocation.source().sendMessage(Component.text("No permission").toBuilder() + .color(TextColor.color(255,0,0)).build()); + return; + } + + val children = command.children(); + + String[] args = invocation.arguments(); + if(children.length > 0 && args.length > 0) { + for (Command child : children) { + if(child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases()) + .anyMatch(alias -> alias.equalsIgnoreCase(args[0]))) { + if(!sender.hasPermission("antivpn.command.*") + && !sender.hasPermission(child.permission())) { + invocation.source().sendMessage(Component.text("No permission") + .toBuilder().color(TextColor.color(255,0,0)).build()); + return; + } + sender.sendMessage(LegacyComponentSerializer.builder().character('&').build() + .deserialize(child.execute(new VelocityCommandExecutor(sender), IntStream + .range(0, args.length - 1) + .mapToObj(i -> args[i + 1]).toArray(String[]::new)))); + return; + } + } + } + + sender.sendMessage(LegacyComponentSerializer.builder().character('&').build() + .deserialize(command.execute(new VelocityCommandExecutor(sender), args))); + } + + @Override + public List suggest(Invocation invocation) { + final CommandSource sender = invocation.source(); + final String[] args = invocation.arguments(); + + val children = command.children(); + + if(children.length > 0 && args.length > 0) { + for (dev.brighten.antivpn.command.Command child : children) { + if(child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases()) + .anyMatch(alias2 -> alias2.equalsIgnoreCase(args[0]))) { + return child.tabComplete(new VelocityCommandExecutor(sender), "alias", IntStream + .range(0, args.length - 1) + .mapToObj(i -> args[i + 1]).toArray(String[]::new)); + } + } + } + return command.tabComplete(new VelocityCommandExecutor(sender), "alias", args); + } + + @Override + public CompletableFuture> suggestAsync(Invocation invocation) { + return CompletableFuture.supplyAsync(() -> this.suggest(invocation)); + } + + @Override + public boolean hasPermission(Invocation invocation) { + return SimpleCommand.super.hasPermission(invocation); + } +} diff --git a/Velocity/src/main/java/dev/brighten/antivpn/velocity/VelocityCommandExecutor.java b/Velocity/src/main/java/dev/brighten/antivpn/velocity/command/VelocityCommandExecutor.java similarity index 95% rename from Velocity/src/main/java/dev/brighten/antivpn/velocity/VelocityCommandExecutor.java rename to Velocity/src/main/java/dev/brighten/antivpn/velocity/command/VelocityCommandExecutor.java index b1ee9bd..7603cb8 100644 --- a/Velocity/src/main/java/dev/brighten/antivpn/velocity/VelocityCommandExecutor.java +++ b/Velocity/src/main/java/dev/brighten/antivpn/velocity/command/VelocityCommandExecutor.java @@ -1,4 +1,4 @@ -package dev.brighten.antivpn.velocity; +package dev.brighten.antivpn.velocity.command; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.Player;