mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
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
This commit is contained in:
parent
9dad1acfe5
commit
0ebb7232f9
4 changed files with 89 additions and 7 deletions
|
@ -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
|
||||
|
|
|
@ -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() : "<IP address withheld>";
|
||||
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());
|
||||
|
|
|
@ -58,7 +58,7 @@ public class UpstreamSession {
|
|||
}
|
||||
|
||||
public void disconnect(String reason) {
|
||||
session.disconnect(reason);
|
||||
this.session.disconnect(reason);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue