Search in sources :

Example 1 with ProxyInitializeEvent

use of com.velocitypowered.api.event.proxy.ProxyInitializeEvent 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)

Example 2 with ProxyInitializeEvent

use of com.velocitypowered.api.event.proxy.ProxyInitializeEvent in project InteractiveChat by LOOHP.

the class InteractiveChatVelocity method onProxyInitialization.

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
    plugin = this;
    try {
        JSONObject json = (JSONObject) new JSONParser().parse(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("velocity-plugin.json"), StandardCharsets.UTF_8));
        description = new VelocityPluginDescription(json);
    } catch (IOException | ParseException e1) {
        e1.printStackTrace();
    }
    if (!getDataFolder().exists()) {
        getDataFolder().mkdir();
    }
    try {
        Config.loadConfig(CONFIG_ID, new File(getDataFolder(), "bungeeconfig.yml"), getClass().getClassLoader().getResourceAsStream("config_proxy.yml"), getClass().getClassLoader().getResourceAsStream("config_proxy.yml"), true);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    loadConfig();
    CommandsVelocity.createBrigadierCommand();
    proxyServer.getChannelRegistrar().register(ICChannelIdentifier.INSTANCE);
    getLogger().info(TextColor.GREEN + "[InteractiveChat] Registered Plugin Messaging Channels!");
    Metrics metrics = metricsFactory.make(this, BSTATS_PLUGIN_ID);
    Charts.setup(metrics);
    playerCooldownManager = new ProxyPlayerCooldownManager(placeholderList.values().stream().flatMap(each -> each.stream()).distinct().collect(Collectors.toList()));
    messageForwardingHandler = new ProxyMessageForwardingHandler((info, component) -> {
        Player player = proxyServer.getPlayer(info.getPlayer()).get();
        ServerConnection server = player.getCurrentServer().get();
        proxyServer.getScheduler().buildTask(plugin, () -> {
            try {
                if (player != null && server != null) {
                    PluginMessageSendingVelocity.requestMessageProcess(player, server.getServer(), component, info.getId());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).delay(delay + 50, TimeUnit.MILLISECONDS).schedule();
    }, (info, component) -> {
        Chat chatPacket = new Chat(component + "<QUxSRUFEWVBST0NFU1NFRA==>", info.getPosition(), null);
        Optional<Player> optplayer = getServer().getPlayer(info.getPlayer());
        if (optplayer.isPresent()) {
            ConnectedPlayer userConnection = (ConnectedPlayer) optplayer.get();
            userConnection.getConnection().getChannel().write(chatPacket);
        }
    }, uuid -> {
        return proxyServer.getPlayer(uuid).isPresent();
    }, uuid -> {
        Optional<ServerConnection> optCurrentServer = proxyServer.getPlayer(uuid).get().getCurrentServer();
        return optCurrentServer.isPresent() && hasInteractiveChat(optCurrentServer.get().getServer());
    }, () -> (long) delay + 2000);
    ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("InteractiveChatProxy Async PluginMessage Processing Thread #%d").build();
    pluginMessageHandlingExecutor = new ThreadPoolExecutor(8, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(true), factory);
    getLogger().info(TextColor.GREEN + "[InteractiveChat] InteractiveChat (Velocity) has been enabled!");
    run();
}
Also used : Arrays(java.util.Arrays) Inject(com.google.inject.Inject) ConnectedPlayer(com.velocitypowered.proxy.connection.client.ConnectedPlayer) Random(java.util.Random) ProxyShutdownEvent(com.velocitypowered.api.event.proxy.ProxyShutdownEvent) ParsePlayer(com.loohp.interactivechat.objectholders.CustomPlaceholder.ParsePlayer) ByteBuffer(java.nio.ByteBuffer) Matcher(java.util.regex.Matcher) ChannelPromise(io.netty.channel.ChannelPromise) PluginMessageEvent(com.velocitypowered.api.event.connection.PluginMessageEvent) Player(com.velocitypowered.api.proxy.Player) Map(java.util.Map) ProxyInitializeEvent(com.velocitypowered.api.event.proxy.ProxyInitializeEvent) ThreadFactory(java.util.concurrent.ThreadFactory) ServerPostConnectEvent(com.velocitypowered.api.event.player.ServerPostConnectEvent) Method(java.lang.reflect.Method) Path(java.nio.file.Path) ChatResult(com.velocitypowered.api.event.player.PlayerChatEvent.ChatResult) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) TextComponent(net.kyori.adventure.text.TextComponent) ForwardResult(com.velocitypowered.api.event.connection.PluginMessageEvent.ForwardResult) Filter(org.apache.logging.log4j.core.Filter) JSONParser(org.json.simple.parser.JSONParser) SynchronousQueue(java.util.concurrent.SynchronousQueue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VelocityServerConnection(com.velocitypowered.proxy.connection.backend.VelocityServerConnection) ChannelPipeline(io.netty.channel.ChannelPipeline) UUID(java.util.UUID) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) JSONObject(org.json.simple.JSONObject) ClickEventAction(com.loohp.interactivechat.objectholders.CustomPlaceholder.ClickEventAction) ByteStreams(com.google.common.io.ByteStreams) Subscribe(com.velocitypowered.api.event.Subscribe) Optional(java.util.Optional) ChannelMessageSource(com.velocitypowered.api.proxy.messages.ChannelMessageSource) Pattern(java.util.regex.Pattern) CustomPlaceholderClickEvent(com.loohp.interactivechat.objectholders.CustomPlaceholder.CustomPlaceholderClickEvent) HoverEvent(net.kyori.adventure.text.event.HoverEvent) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ProxyPlayerCooldownManager(com.loohp.interactivechat.proxy.objectholders.ProxyPlayerCooldownManager) DisconnectEvent(com.velocitypowered.api.event.connection.DisconnectEvent) ServerConnection(com.velocitypowered.api.proxy.ServerConnection) Metrics(com.loohp.interactivechat.proxy.velocity.metrics.Metrics) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CustomPlaceholder(com.loohp.interactivechat.objectholders.CustomPlaceholder) Connections(com.velocitypowered.proxy.network.Connections) Registry(com.loohp.interactivechat.registry.Registry) Charts(com.loohp.interactivechat.proxy.velocity.metrics.Charts) Config(com.loohp.interactivechat.config.Config) DataTypeIO(com.loohp.interactivechat.utils.DataTypeIO) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) BuiltInPlaceholder(com.loohp.interactivechat.objectholders.BuiltInPlaceholder) CustomPlaceholderReplaceText(com.loohp.interactivechat.objectholders.CustomPlaceholder.CustomPlaceholderReplaceText) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Chat(com.velocitypowered.proxy.protocol.packet.Chat) PlayerChatEvent(com.velocitypowered.api.event.player.PlayerChatEvent) ParseException(org.json.simple.parser.ParseException) PostOrder(com.velocitypowered.api.event.PostOrder) Component(net.kyori.adventure.text.Component) ProxyServer(com.velocitypowered.api.proxy.ProxyServer) ByteArrayDataInput(com.google.common.io.ByteArrayDataInput) ICPlaceholder(com.loohp.interactivechat.objectholders.ICPlaceholder) PostLoginEvent(com.velocitypowered.api.event.connection.PostLoginEvent) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) BackendInteractiveChatData(com.loohp.interactivechat.proxy.objectholders.BackendInteractiveChatData) IOException(java.io.IOException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) File(java.io.File) DataDirectory(com.velocitypowered.api.plugin.annotation.DataDirectory) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) CustomPlaceholderHoverEvent(com.loohp.interactivechat.objectholders.CustomPlaceholder.CustomPlaceholderHoverEvent) LogFilter(com.loohp.interactivechat.objectholders.LogFilter) CommandSource(com.velocitypowered.api.command.CommandSource) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ProxyMessageForwardingHandler(com.loohp.interactivechat.proxy.objectholders.ProxyMessageForwardingHandler) NativeAdventureConverter(com.loohp.interactivechat.utils.NativeAdventureConverter) ThreadFactory(java.util.concurrent.ThreadFactory) ConnectedPlayer(com.velocitypowered.proxy.connection.client.ConnectedPlayer) ParsePlayer(com.loohp.interactivechat.objectholders.CustomPlaceholder.ParsePlayer) Player(com.velocitypowered.api.proxy.Player) InputStreamReader(java.io.InputStreamReader) Optional(java.util.Optional) ProxyPlayerCooldownManager(com.loohp.interactivechat.proxy.objectholders.ProxyPlayerCooldownManager) VelocityServerConnection(com.velocitypowered.proxy.connection.backend.VelocityServerConnection) ServerConnection(com.velocitypowered.api.proxy.ServerConnection) IOException(java.io.IOException) ConnectedPlayer(com.velocitypowered.proxy.connection.client.ConnectedPlayer) Metrics(com.loohp.interactivechat.proxy.velocity.metrics.Metrics) JSONObject(org.json.simple.JSONObject) ProxyMessageForwardingHandler(com.loohp.interactivechat.proxy.objectholders.ProxyMessageForwardingHandler) SynchronousQueue(java.util.concurrent.SynchronousQueue) Chat(com.velocitypowered.proxy.protocol.packet.Chat) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) File(java.io.File) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 3 with ProxyInitializeEvent

use of com.velocitypowered.api.event.proxy.ProxyInitializeEvent in project FastLogin by games647.

the class FastLoginVelocity method onProxyInitialization.

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
    scheduler = new AsyncScheduler(logger, task -> server.getScheduler().buildTask(this, task).schedule());
    core = new FastLoginCore<>(this);
    core.load();
    loadOrGenerateProxyId();
    if (!core.setupDatabase() || proxyId == null) {
        return;
    }
    server.getEventManager().register(this, new ConnectListener(this, core.getRateLimiter()));
    server.getEventManager().register(this, new PluginMessageListener(this));
    server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), ChangePremiumMessage.CHANGE_CHANNEL));
    server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), SuccessMessage.SUCCESS_CHANNEL));
}
Also used : MinecraftChannelIdentifier(com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier) AsyncScheduler(com.github.games647.fastlogin.core.AsyncScheduler) Inject(com.google.inject.Inject) SuccessMessage(com.github.games647.fastlogin.core.message.SuccessMessage) BedrockService(com.github.games647.fastlogin.core.hooks.bedrock.BedrockService) ProxyShutdownEvent(com.velocitypowered.api.event.proxy.ProxyShutdownEvent) ConcurrentMap(java.util.concurrent.ConcurrentMap) FastLoginCore(com.github.games647.fastlogin.core.shared.FastLoginCore) ConnectListener(com.github.games647.fastlogin.velocity.listener.ConnectListener) Player(com.velocitypowered.api.proxy.Player) ProxyServer(com.velocitypowered.api.proxy.ProxyServer) ProxyInitializeEvent(com.velocitypowered.api.event.proxy.ProxyInitializeEvent) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) Path(java.nio.file.Path) ByteArrayDataOutput(com.google.common.io.ByteArrayDataOutput) Plugin(com.velocitypowered.api.plugin.Plugin) ChannelMessage(com.github.games647.fastlogin.core.message.ChannelMessage) Logger(org.slf4j.Logger) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) PlatformPlugin(com.github.games647.fastlogin.core.shared.PlatformPlugin) UUID(java.util.UUID) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) PluginMessageListener(com.github.games647.fastlogin.velocity.listener.PluginMessageListener) DataDirectory(com.velocitypowered.api.plugin.annotation.DataDirectory) List(java.util.List) ByteStreams(com.google.common.io.ByteStreams) Subscribe(com.velocitypowered.api.event.Subscribe) ChangePremiumMessage(com.github.games647.fastlogin.core.message.ChangePremiumMessage) MapMaker(com.google.common.collect.MapMaker) CommandSource(com.velocitypowered.api.command.CommandSource) Collections(java.util.Collections) AsyncScheduler(com.github.games647.fastlogin.core.AsyncScheduler) PluginMessageListener(com.github.games647.fastlogin.velocity.listener.PluginMessageListener) ConnectListener(com.github.games647.fastlogin.velocity.listener.ConnectListener) Subscribe(com.velocitypowered.api.event.Subscribe)

Aggregations

Inject (com.google.inject.Inject)3 CommandSource (com.velocitypowered.api.command.CommandSource)3 Subscribe (com.velocitypowered.api.event.Subscribe)3 ProxyInitializeEvent (com.velocitypowered.api.event.proxy.ProxyInitializeEvent)3 DataDirectory (com.velocitypowered.api.plugin.annotation.DataDirectory)3 ProxyServer (com.velocitypowered.api.proxy.ProxyServer)3 Path (java.nio.file.Path)3 ByteStreams (com.google.common.io.ByteStreams)2 ProxyShutdownEvent (com.velocitypowered.api.event.proxy.ProxyShutdownEvent)2 Arrays (java.util.Arrays)2 Component (net.kyori.adventure.text.Component)2 AsyncScheduler (com.github.games647.fastlogin.core.AsyncScheduler)1 BedrockService (com.github.games647.fastlogin.core.hooks.bedrock.BedrockService)1 ChangePremiumMessage (com.github.games647.fastlogin.core.message.ChangePremiumMessage)1 ChannelMessage (com.github.games647.fastlogin.core.message.ChannelMessage)1 SuccessMessage (com.github.games647.fastlogin.core.message.SuccessMessage)1 FastLoginCore (com.github.games647.fastlogin.core.shared.FastLoginCore)1 PlatformPlugin (com.github.games647.fastlogin.core.shared.PlatformPlugin)1 ConnectListener (com.github.games647.fastlogin.velocity.listener.ConnectListener)1 PluginMessageListener (com.github.games647.fastlogin.velocity.listener.PluginMessageListener)1