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

View File

@@ -303,6 +303,22 @@ public class Player implements GameDatabaseObject {
// Login // 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() { public void onLoad() {
// Debug // Debug
this.energy = 240; this.energy = 240;
@@ -312,38 +328,11 @@ public class Player implements GameDatabaseObject {
this.getInventory().loadFromDatabase(); this.getInventory().loadFromDatabase();
// Load referenced classes // Load referenced classes
this.formations = Nebula.getGameDatabase().getObjectByField(FormationManager.class, "_id", this.getUid()); this.formations = this.loadManagerFromDatabase(FormationManager.class);
if (this.formations == null) { this.mailbox = this.loadManagerFromDatabase(Mailbox.class);
this.formations = new FormationManager(this); this.starTowerManager = this.loadManagerFromDatabase(StarTowerManager.class);
} else { this.instanceManager = this.loadManagerFromDatabase(InstanceManager.class);
this.formations.setPlayer(this); this.storyManager = this.loadManagerFromDatabase(StoryManager.class);
}
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);
}
} }
// Proto // Proto