Search in sources :

Example 6 with MinecraftConnection

use of com.velocitypowered.proxy.connection.MinecraftConnection in project LimboAPI by Elytrium.

the class LoginListener method hookLoginSession.

public void hookLoginSession(GameProfileRequestEvent event) throws IllegalAccessException {
    // Changing mcConnection to the closed one. For what? To break the "initializePlayer"
    // method (which checks mcConnection.isActive()) and to override it. :)
    InitialInboundConnection inbound = (InitialInboundConnection) delegate.get(event.getConnection());
    MinecraftConnection connection = inbound.getConnection();
    LoginSessionHandler handler = (LoginSessionHandler) connection.getSessionHandler();
    loginConnectionField.set(handler, closed);
    if (connection.isClosed()) {
        return;
    }
    connection.eventLoop().execute(() -> {
        try {
            // Initiate a regular connection and move over to it.
            ConnectedPlayer player = ctor.newInstance(this.server, event.getGameProfile(), connection, event.getConnection().getVirtualHost().orElse(null), this.onlineMode.contains(event.getUsername()));
            if (!this.server.canRegisterConnection(player)) {
                player.disconnect0(Component.translatable("velocity.error.already-connected-proxy", NamedTextColor.RED), true);
                return;
            }
            if (connection.isClosed()) {
                return;
            }
            // Completing the Login process
            int threshold = this.server.getConfiguration().getCompressionThreshold();
            if (threshold >= 0 && connection.getProtocolVersion().compareTo(MINECRAFT_1_8) >= 0) {
                connection.write(new SetCompression(threshold));
                if (connection.isClosed()) {
                    return;
                }
                connection.setCompressionThreshold(threshold);
            }
            VelocityConfiguration configuration = this.server.getConfiguration();
            UUID playerUniqueId = player.getUniqueId();
            if (configuration.getPlayerInfoForwardingMode() == PlayerInfoForwarding.NONE) {
                playerUniqueId = UuidUtils.generateOfflinePlayerUuid(player.getUsername());
            }
            ServerLoginSuccess success = new ServerLoginSuccess();
            success.setUsername(player.getUsername());
            success.setUuid(playerUniqueId);
            connection.write(success);
            this.plugin.setInitialID(player, playerUniqueId);
            connection.setState(StateRegistry.PLAY);
            this.server.getEventManager().fire(new LoginLimboRegisterEvent(player)).thenAcceptAsync(limboEvent -> {
                LoginTasksQueue queue = new LoginTasksQueue(this.plugin, handler, this.server, player, inbound, limboEvent.getCallbacks());
                this.plugin.addLoginQueue(player, queue);
                queue.next();
            }, connection.eventLoop());
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    });
}
Also used : LoginSessionHandler(com.velocitypowered.proxy.connection.client.LoginSessionHandler) ServerLoginSuccess(com.velocitypowered.proxy.protocol.packet.ServerLoginSuccess) ClosedMinecraftConnection(net.elytrium.limboapi.injection.dummy.ClosedMinecraftConnection) MinecraftConnection(com.velocitypowered.proxy.connection.MinecraftConnection) ConnectedPlayer(com.velocitypowered.proxy.connection.client.ConnectedPlayer) InvocationTargetException(java.lang.reflect.InvocationTargetException) VelocityConfiguration(com.velocitypowered.proxy.config.VelocityConfiguration) LoginLimboRegisterEvent(net.elytrium.limboapi.api.event.LoginLimboRegisterEvent) SetCompression(com.velocitypowered.proxy.protocol.packet.SetCompression) InitialInboundConnection(com.velocitypowered.proxy.connection.client.InitialInboundConnection) UUID(java.util.UUID)

Example 7 with MinecraftConnection

use of com.velocitypowered.proxy.connection.MinecraftConnection in project LimboAPI by Elytrium.

the class LimboImpl method spawnPlayer.

@Override
public void spawnPlayer(Player apiPlayer, LimboSessionHandler handler) {
    ConnectedPlayer player = (ConnectedPlayer) apiPlayer;
    MinecraftConnection connection = player.getConnection();
    Class<? extends LimboSessionHandler> handlerClass = handler.getClass();
    if (this.limboName == null) {
        this.limboName = handlerClass.getSimpleName();
    }
    connection.eventLoop().execute(() -> {
        ChannelPipeline pipeline = connection.getChannel().pipeline();
        if (Settings.IMP.MAIN.LOGGING_ENABLED) {
            this.plugin.getLogger().info(player.getUsername() + " (" + player.getRemoteAddress() + ") has connected to the " + this.limboName + " Limbo");
        }
        if (!pipeline.names().contains("prepared-encoder")) {
            // and an error occurs that "minecraft-encoder" doesn't exist.
            if (!pipeline.names().contains(Connections.MINECRAFT_ENCODER)) {
                connection.close();
                return;
            }
            pipeline.addAfter(Connections.MINECRAFT_ENCODER, "prepared-encoder", new PreparedPacketEncoder(connection.getProtocolVersion()));
        }
        RegisteredServer previousServer = null;
        if (connection.getState() != LimboProtocol.getLimboRegistry()) {
            connection.setState(LimboProtocol.getLimboRegistry());
            VelocityServerConnection server = player.getConnectedServer();
            if (server != null) {
                server.disconnect();
                player.setConnectedServer(null);
                previousServer = server.getServer();
                this.plugin.setLimboJoined(player);
            }
        }
        if (this.plugin.isLimboJoined(player)) {
            if (connection.getType() == ConnectionTypes.LEGACY_FORGE) {
                connection.delayedWrite(this.safeRejoinPackets);
            } else {
                connection.delayedWrite(this.fastRejoinPackets);
            }
        } else {
            connection.delayedWrite(this.joinPackets);
        }
        connection.delayedWrite(this.postJoinPackets);
        connection.delayedWrite(this.getBrandMessage(handlerClass));
        this.plugin.setLimboJoined(player);
        LimboSessionHandlerImpl sessionHandler = new LimboSessionHandlerImpl(this.plugin, player, handler, connection.getSessionHandler(), previousServer, () -> this.limboName);
        connection.setSessionHandler(sessionHandler);
        connection.flush();
        if (connection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_18_2) >= 0) {
            this.plugin.getServer().getScheduler().buildTask(this.plugin, () -> connection.eventLoop().execute(() -> this.proceed(player, sessionHandler))).delay(Settings.IMP.MAIN.RECEIVER_LEVEL_1_18_2_FIXER_DELAY, TimeUnit.MILLISECONDS).schedule();
        } else {
            this.proceed(player, sessionHandler);
        }
    });
}
Also used : PreparedPacketEncoder(net.elytrium.limboapi.injection.packet.PreparedPacketEncoder) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) VelocityServerConnection(com.velocitypowered.proxy.connection.backend.VelocityServerConnection) MinecraftConnection(com.velocitypowered.proxy.connection.MinecraftConnection) ConnectedPlayer(com.velocitypowered.proxy.connection.client.ConnectedPlayer) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

MinecraftConnection (com.velocitypowered.proxy.connection.MinecraftConnection)7 ConnectedPlayer (com.velocitypowered.proxy.connection.client.ConnectedPlayer)6 LoginSessionHandler (com.velocitypowered.proxy.connection.client.LoginSessionHandler)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 DisconnectEvent (com.velocitypowered.api.event.connection.DisconnectEvent)2 LoginEvent (com.velocitypowered.api.event.connection.LoginEvent)2 PostLoginEvent (com.velocitypowered.api.event.connection.PostLoginEvent)2 PermissionsSetupEvent (com.velocitypowered.api.event.permission.PermissionsSetupEvent)2 GameProfileRequestEvent (com.velocitypowered.api.event.player.GameProfileRequestEvent)2 PermissionFunction (com.velocitypowered.api.permission.PermissionFunction)2 PermissionProvider (com.velocitypowered.api.permission.PermissionProvider)2 InboundConnection (com.velocitypowered.api.proxy.InboundConnection)2 VelocityServer (com.velocitypowered.proxy.VelocityServer)2 InitialConnectSessionHandler (com.velocitypowered.proxy.connection.client.InitialConnectSessionHandler)2 StateRegistry (com.velocitypowered.proxy.protocol.StateRegistry)2 MinecraftDecoder (com.velocitypowered.proxy.protocol.netty.MinecraftDecoder)2 MinecraftEncoder (com.velocitypowered.proxy.protocol.netty.MinecraftEncoder)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 Constructor (java.lang.reflect.Constructor)2 Field (java.lang.reflect.Field)2