Refactor loading player managers

This commit is contained in:
Melledy
2025-10-30 01:01:46 -07:00
parent 89ecd29761
commit 1c05fb2b21
2 changed files with 26 additions and 33 deletions

View File

@@ -13,13 +13,14 @@ import emu.nebula.Nebula;
import emu.nebula.database.GameDatabaseObject;
import emu.nebula.game.player.Player;
import emu.nebula.game.player.PlayerChangeInfo;
import emu.nebula.game.player.PlayerManager;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import lombok.Getter;
@Getter
@Entity(value = "mailbox", useDiscriminator = false)
public class Mailbox implements GameDatabaseObject, Iterable<GameMail> {
public class Mailbox extends PlayerManager implements GameDatabaseObject, Iterable<GameMail> {
@Id
private int uid;
private int lastMailId;
@@ -32,8 +33,11 @@ public class Mailbox implements GameDatabaseObject, Iterable<GameMail> {
}
public Mailbox(Player player) {
super(player);
this.uid = player.getUid();
this.list = new ArrayList<>();
this.save();
}

View File

@@ -303,6 +303,22 @@ public class Player implements GameDatabaseObject {
// Login
private <T extends PlayerManager> T loadManagerFromDatabase(Class<T> cls) {
var manager = Nebula.getGameDatabase().getObjectByField(cls, "_id", this.getUid());
if (manager != null) {
manager.setPlayer(this);
} else {
try {
manager = cls.getDeclaredConstructor(Player.class).newInstance(this);
} catch (Exception e) {
e.printStackTrace();
}
}
return manager;
}
public void onLoad() {
// Debug
this.energy = 240;
@@ -312,38 +328,11 @@ public class Player implements GameDatabaseObject {
this.getInventory().loadFromDatabase();
// Load referenced classes
this.formations = Nebula.getGameDatabase().getObjectByField(FormationManager.class, "_id", this.getUid());
if (this.formations == null) {
this.formations = new FormationManager(this);
} else {
this.formations.setPlayer(this);
}
this.mailbox = Nebula.getGameDatabase().getObjectByField(Mailbox.class, "_id", this.getUid());
if (this.mailbox == null) {
this.mailbox = new Mailbox(this);
}
this.starTowerManager = Nebula.getGameDatabase().getObjectByField(StarTowerManager.class, "_id", this.getUid());
if (this.starTowerManager == null) {
this.starTowerManager = new StarTowerManager(this);
} else {
this.starTowerManager.setPlayer(this);
}
this.instanceManager = Nebula.getGameDatabase().getObjectByField(InstanceManager.class, "_id", this.getUid());
if (this.instanceManager == null) {
this.instanceManager = new InstanceManager(this);
} else {
this.instanceManager.setPlayer(this);
}
this.storyManager = Nebula.getGameDatabase().getObjectByField(StoryManager.class, "_id", this.getUid());
if (this.storyManager == null) {
this.storyManager = new StoryManager(this);
} else {
this.storyManager.setPlayer(this);
}
this.formations = this.loadManagerFromDatabase(FormationManager.class);
this.mailbox = this.loadManagerFromDatabase(Mailbox.class);
this.starTowerManager = this.loadManagerFromDatabase(StarTowerManager.class);
this.instanceManager = this.loadManagerFromDatabase(InstanceManager.class);
this.storyManager = this.loadManagerFromDatabase(StoryManager.class);
}
// Proto