Search in sources :

Example 1 with SimpleCommand

use of com.velocitypowered.api.command.SimpleCommand in project AntiVPN by funkemunky.

the class VelocityPlugin method onInit.

@Subscribe
public void onInit(ProxyInitializeEvent event) {
    INSTANCE = this;
    logger.info("Loading config...");
    config = new Config();
    // Loading plugin
    logger.info("Starting AntiVPN services...");
    AntiVPN.start(new VelocityConfig(), new VelocityListener(), new VelocityPlayerExecutor(), configDir.toFile());
    if (AntiVPN.getInstance().getConfig().metrics()) {
        logger.info("Starting metrics...");
        Metrics metrics = metricsFactory.make(this, 12791);
    }
    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)));
        });
    }
}
Also used : IntStream(java.util.stream.IntStream) Plugin(com.velocitypowered.api.plugin.Plugin) Arrays(java.util.Arrays) SimpleCommand(com.velocitypowered.api.command.SimpleCommand) Getter(lombok.Getter) TextColor(net.kyori.adventure.text.format.TextColor) Command(dev.brighten.antivpn.command.Command) Inject(com.google.inject.Inject) lombok.val(lombok.val) Logger(java.util.logging.Logger) DataDirectory(com.velocitypowered.api.plugin.annotation.DataDirectory) Config(dev.brighten.antivpn.velocity.util.Config) AntiVPN(dev.brighten.antivpn.AntiVPN) Metrics(org.bstats.velocity.Metrics) Component(net.kyori.adventure.text.Component) ProxyServer(com.velocitypowered.api.proxy.ProxyServer) Subscribe(com.velocitypowered.api.event.Subscribe) ProxyInitializeEvent(com.velocitypowered.api.event.proxy.ProxyInitializeEvent) CommandSource(com.velocitypowered.api.command.CommandSource) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) Path(java.nio.file.Path) lombok.val(lombok.val) Metrics(org.bstats.velocity.Metrics) SimpleCommand(com.velocitypowered.api.command.SimpleCommand) Command(dev.brighten.antivpn.command.Command) Config(dev.brighten.antivpn.velocity.util.Config) CommandSource(com.velocitypowered.api.command.CommandSource) Subscribe(com.velocitypowered.api.event.Subscribe)

Aggregations

Inject (com.google.inject.Inject)1 CommandSource (com.velocitypowered.api.command.CommandSource)1 SimpleCommand (com.velocitypowered.api.command.SimpleCommand)1 Subscribe (com.velocitypowered.api.event.Subscribe)1 ProxyInitializeEvent (com.velocitypowered.api.event.proxy.ProxyInitializeEvent)1 Plugin (com.velocitypowered.api.plugin.Plugin)1 DataDirectory (com.velocitypowered.api.plugin.annotation.DataDirectory)1 ProxyServer (com.velocitypowered.api.proxy.ProxyServer)1 AntiVPN (dev.brighten.antivpn.AntiVPN)1 Command (dev.brighten.antivpn.command.Command)1 Config (dev.brighten.antivpn.velocity.util.Config)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Logger (java.util.logging.Logger)1 IntStream (java.util.stream.IntStream)1 Getter (lombok.Getter)1 lombok.val (lombok.val)1 Component (net.kyori.adventure.text.Component)1 TextColor (net.kyori.adventure.text.format.TextColor)1 LegacyComponentSerializer (net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)1