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

View File

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