From 0ebb7232f97721392ed3115ab752650cf42aa2d5 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 10 Nov 2023 23:44:38 +0100 Subject: [PATCH] Add SessionDisconnectEvent (#4052) * Add SessionDisconnectEvent * Move disconnect event calling to UpstreamSession, debug double calling issues * Prevent duplicate disconnect calling * Tidy up disconnection logic, ensure SessionDisconnectEvent is always fired - but only once. Fix /geyser reload command --- .../event/bedrock/SessionDisconnectEvent.java | 61 +++++++++++++++++++ .../command/defaults/ReloadCommand.java | 5 +- .../geyser/session/GeyserSession.java | 28 +++++++-- .../geyser/session/UpstreamSession.java | 2 +- 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionDisconnectEvent.java diff --git a/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionDisconnectEvent.java b/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionDisconnectEvent.java new file mode 100644 index 000000000..05e3415a0 --- /dev/null +++ b/api/src/main/java/org/geysermc/geyser/api/event/bedrock/SessionDisconnectEvent.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2019-2023 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 a Geyser session disconnects. + */ +public class SessionDisconnectEvent extends ConnectionEvent { + private String disconnectReason; + + public SessionDisconnectEvent(@NonNull GeyserConnection connection, @NonNull String reason) { + super(connection); + this.disconnectReason = reason; + } + + /** + * Gets the disconnect reason. + * + * @return the reason for the disconnect + */ + public @NonNull String disconnectReason() { + return disconnectReason; + } + + /** + * Sets the disconnect reason, thereby overriding th original reason. + * + * @param disconnectReason the reason for the disconnect + */ + public void disconnectReason(@NonNull String disconnectReason) { + this.disconnectReason = disconnectReason; + } +} + diff --git a/core/src/main/java/org/geysermc/geyser/command/defaults/ReloadCommand.java b/core/src/main/java/org/geysermc/geyser/command/defaults/ReloadCommand.java index 8f147cdab..a3cd8fa4c 100644 --- a/core/src/main/java/org/geysermc/geyser/command/defaults/ReloadCommand.java +++ b/core/src/main/java/org/geysermc/geyser/command/defaults/ReloadCommand.java @@ -32,6 +32,8 @@ import org.geysermc.geyser.command.GeyserCommandSource; import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.text.GeyserLocale; +import java.util.concurrent.TimeUnit; + public class ReloadCommand extends GeyserCommand { private final GeyserImpl geyser; @@ -52,7 +54,8 @@ public class ReloadCommand extends GeyserCommand { sender.sendMessage(message); geyser.getSessionManager().disconnectAll("geyser.commands.reload.kick"); - geyser.reload(); + //FIXME Without the tiny wait, players do not get kicked - same happens when Geyser tries to disconnect all sessions on shutdown + geyser.getScheduledThread().schedule(geyser::reload, 10, TimeUnit.MILLISECONDS); } @Override diff --git a/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java b/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java index 4d4b2a5ba..ebb0025da 100644 --- a/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java +++ b/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java @@ -105,6 +105,7 @@ import org.geysermc.geyser.api.bedrock.camera.CameraShake; import org.geysermc.geyser.api.connection.GeyserConnection; import org.geysermc.geyser.api.entity.type.GeyserEntity; import org.geysermc.geyser.api.entity.type.player.GeyserPlayerEntity; +import org.geysermc.geyser.api.event.bedrock.SessionDisconnectEvent; import org.geysermc.geyser.api.event.bedrock.SessionLoginEvent; import org.geysermc.geyser.api.network.AuthType; import org.geysermc.geyser.api.network.RemoteServer; @@ -1038,10 +1039,14 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource { geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.remote.disconnect", authData.name(), remoteServer.address(), disconnectMessage)); } if (cause != null) { - cause.printStackTrace(); + GeyserImpl.getInstance().getLogger().error(cause.getMessage()); + // GeyserSession is disconnected via session.disconnect() called indirectly be the server + // This only needs to be "initiated" here when there is an exception, hence the cause clause + GeyserSession.this.disconnect(disconnectMessage); + if (geyser.getConfig().isDebugMode()) { + cause.printStackTrace(); + } } - - upstream.disconnect(disconnectMessage); } @Override @@ -1068,17 +1073,30 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource { public void disconnect(String reason) { if (!closed) { loggedIn = false; + + // Fire SessionDisconnectEvent + SessionDisconnectEvent disconnectEvent = new SessionDisconnectEvent(this, reason); + geyser.getEventBus().fire(disconnectEvent); + + // Disconnect downstream if necessary if (downstream != null) { - downstream.disconnect(reason); + // No need to disconnect if already closed + if (!downstream.isClosed()) { + downstream.disconnect(reason); + } } else { // Downstream's disconnect will fire an event that prints a log message // Otherwise, we print a message here String address = geyser.getConfig().isLogPlayerIpAddresses() ? upstream.getAddress().getAddress().toString() : ""; geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.disconnect", address, reason)); } + + // Disconnect upstream if necessary if (!upstream.isClosed()) { - upstream.disconnect(reason); + upstream.disconnect(disconnectEvent.disconnectReason()); } + + // Remove from session manager geyser.getSessionManager().removeSession(this); if (authData != null) { PendingMicrosoftAuthentication.AuthenticationTask task = geyser.getPendingMicrosoftAuthentication().getTask(authData.xuid()); diff --git a/core/src/main/java/org/geysermc/geyser/session/UpstreamSession.java b/core/src/main/java/org/geysermc/geyser/session/UpstreamSession.java index ef462a3e3..35ede56a1 100644 --- a/core/src/main/java/org/geysermc/geyser/session/UpstreamSession.java +++ b/core/src/main/java/org/geysermc/geyser/session/UpstreamSession.java @@ -58,7 +58,7 @@ public class UpstreamSession { } public void disconnect(String reason) { - session.disconnect(reason); + this.session.disconnect(reason); } /**