Update player session removal

This commit is contained in:
Melledy
2025-12-07 23:52:23 -08:00
parent f542ea7cb4
commit 1b0b6873b9
2 changed files with 24 additions and 29 deletions

View File

@@ -1,7 +1,6 @@
package emu.nebula.game.player;
import java.util.Stack;
import java.util.concurrent.TimeUnit;
import dev.morphia.annotations.AlsoLoad;
import dev.morphia.annotations.Entity;
@@ -189,31 +188,27 @@ public class Player implements GameDatabaseObject {
}
public void setSession(GameSession session) {
int time = Nebula.getConfig().getServerOptions().sessionTimeout;
long timeout = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(time);
// Don't set session if it's the same session
if (this.session == session) {
return;
}
// Cache previous session
var prevSession = this.session;
// Set session
this.session = session;
// Clear player reference from the previous session
if (prevSession != null) {
prevSession.clearPlayer();
}
// We cleared session, now remove player from cache
if (this.session == null) {
// Set session
this.session = session;
return;
}
// 1. Sanity check
// 2. Prevent incorrect deletion of players when re-logging into the game
if (this.session == session || this.lastLogin > timeout) {
return;
}
// Clear player from session
this.session.clearPlayer();
// Set session
this.session = session;
}
public void removeSession() {
this.session = null;
Nebula.getGameContext().getPlayerModule().removeFromCache(this);
}
}
public boolean hasSession() {
return this.session != null;
@@ -237,15 +232,13 @@ public class Player implements GameDatabaseObject {
public void setRemoteToken(String token) {
// Skip if tokens are the same
if (this.remoteToken == null) {
if (this.getRemoteToken() == null) {
if (token == null) {
return;
}
} else if (this.remoteToken != null) {
if (this.remoteToken.equals(token)) {
} else if (this.getRemoteToken().equals(token)) {
return;
}
}
// Set remote token
this.remoteToken = token;

View File

@@ -60,10 +60,12 @@ public class GameSession {
var player = this.player;
this.player = null;
// Remove session from player
player.removeSession();
// Remove session reference from player ONLY if their session wasn't replaced yet
if (player.getSession() == this) {
player.setSession(null);
}
// Set remove flag
// Set session removal flag
this.remove = true;
}