From 2ac7eb83c079ac155b9bc53135a6570806351527 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sun, 23 Apr 2023 11:42:02 +0100 Subject: [PATCH] Add SessionInitialize event (#3691) * Add SessionInitialize event * Move to using GeyserConnection instead of BedrockServerSession * Remove redundent re-expose * Rename geyserSession to just session --- .../event/bedrock/SessionInitializeEvent.java | 39 +++++++++++++++++++ .../network/GeyserServerInitializer.java | 6 ++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionInitializeEvent.java diff --git a/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionInitializeEvent.java b/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionInitializeEvent.java new file mode 100644 index 000000000..91cdea99a --- /dev/null +++ b/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionInitializeEvent.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.api.event.bedrock; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.geysermc.geyser.api.connection.GeyserConnection; +import org.geysermc.geyser.api.event.connection.ConnectionEvent; + +/** + * Called when Geyser initialises a session for a new bedrock client. + */ +public final class SessionInitializeEvent extends ConnectionEvent { + public SessionInitializeEvent(@NonNull GeyserConnection connection) { + super(connection); + } +} diff --git a/core/src/main/java/org/geysermc/geyser/network/GeyserServerInitializer.java b/core/src/main/java/org/geysermc/geyser/network/GeyserServerInitializer.java index be4b9d3b3..9b64a2ee2 100644 --- a/core/src/main/java/org/geysermc/geyser/network/GeyserServerInitializer.java +++ b/core/src/main/java/org/geysermc/geyser/network/GeyserServerInitializer.java @@ -32,6 +32,8 @@ import org.cloudburstmc.protocol.bedrock.BedrockPeer; import org.cloudburstmc.protocol.bedrock.BedrockServerSession; import org.cloudburstmc.protocol.bedrock.netty.initializer.BedrockServerInitializer; import org.geysermc.geyser.GeyserImpl; +import org.geysermc.geyser.api.event.bedrock.SessionInitializeEvent; +import org.geysermc.geyser.api.event.lifecycle.GeyserPreInitializeEvent; import org.geysermc.geyser.session.GeyserSession; import javax.annotation.Nonnull; @@ -49,7 +51,9 @@ public class GeyserServerInitializer extends BedrockServerInitializer { public void initSession(@Nonnull BedrockServerSession bedrockServerSession) { try { bedrockServerSession.setLogging(true); - bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(this.geyser, new GeyserSession(this.geyser, bedrockServerSession, this.eventLoopGroup.next()))); + GeyserSession session = new GeyserSession(this.geyser, bedrockServerSession, this.eventLoopGroup.next()); + bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(this.geyser, session)); + this.geyser.eventBus().fire(new SessionInitializeEvent(session)); } catch (Throwable e) { // Error must be caught or it will be swallowed this.geyser.getLogger().error("Error occurred while initializing player!", e);