diff --git a/src/main/java/emu/grasscutter/database/DatabaseHelper.java b/src/main/java/emu/grasscutter/database/DatabaseHelper.java index cb0731afb..2696860c9 100644 --- a/src/main/java/emu/grasscutter/database/DatabaseHelper.java +++ b/src/main/java/emu/grasscutter/database/DatabaseHelper.java @@ -39,11 +39,11 @@ public final class DatabaseHelper { if (reservedUid == GameConstants.SERVER_CONSOLE_UID) { return null; } - + if (DatabaseHelper.checkIfAccountExists(reservedUid)) { return null; } - + // Make sure no existing player already has this id. if (DatabaseHelper.checkIfPlayerExists(reservedUid)) { return null; @@ -105,11 +105,11 @@ public final class DatabaseHelper { public static Account getAccountByPlayerId(int playerId) { return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", playerId)).first(); } - + public static boolean checkIfAccountExists(String name) { return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("username", name)).count() > 0; } - + public static boolean checkIfAccountExists(int reservedUid) { return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", reservedUid)).count() > 0; } @@ -120,11 +120,11 @@ public final class DatabaseHelper { // database in an inconsistent state, but unfortunately Mongo only supports that when we have a replica set ... Player player = Grasscutter.getGameServer().getPlayerByAccountId(target.getId()); - + if (player != null) { // Close session first player.getSession().close(); - + // Delete data from collections DatabaseManager.getGameDatabase().getCollection("mail").deleteMany(eq("ownerUid", player.getUid())); DatabaseManager.getGameDatabase().getCollection("avatars").deleteMany(eq("ownerId", player.getUid())); @@ -153,11 +153,16 @@ public final class DatabaseHelper { public static Player getPlayerByUid(int id) { return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", id)).first(); } - + + @Deprecated public static Player getPlayerByAccount(Account account) { return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first(); } - + + public static Player getPlayerByAccount(Account account, Class playerClass) { + return DatabaseManager.getGameDatastore().find(playerClass).filter(Filters.eq("accountId", account.getId())).first(); + } + public static boolean checkIfPlayerExists(int uid) { return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0; } @@ -218,7 +223,7 @@ public final class DatabaseHelper { public static List getInventoryItems(Player player) { return DatabaseManager.getGameDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList(); } - + public static List getFriends(Player player) { return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList(); } @@ -272,40 +277,40 @@ public final class DatabaseHelper { public static void saveGachaRecord(GachaRecord gachaRecord){ DatabaseManager.getGameDatastore().save(gachaRecord); } - + public static List getAllMail(Player player) { return DatabaseManager.getGameDatastore().find(Mail.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList(); } - + public static void saveMail(Mail mail) { DatabaseManager.getGameDatastore().save(mail); } - + public static boolean deleteMail(Mail mail) { DeleteResult result = DatabaseManager.getGameDatastore().delete(mail); return result.wasAcknowledged(); } - + public static List getAllQuests(Player player) { return DatabaseManager.getGameDatastore().find(GameMainQuest.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList(); } - + public static void saveQuest(GameMainQuest quest) { DatabaseManager.getGameDatastore().save(quest); } - + public static boolean deleteQuest(GameMainQuest quest) { return DatabaseManager.getGameDatastore().delete(quest).wasAcknowledged(); } - + public static GameHome getHomeByUid(int id) { return DatabaseManager.getGameDatastore().find(GameHome.class).filter(Filters.eq("ownerUid", id)).first(); } - + public static void saveHome(GameHome gameHome) { DatabaseManager.getGameDatastore().save(gameHome); } - + public static BattlePassManager loadBattlePass(Player player) { BattlePassManager manager = DatabaseManager.getGameDatastore().find(BattlePassManager.class).filter(Filters.eq("ownerUid", player.getUid())).first(); if (manager == null) { @@ -316,7 +321,7 @@ public final class DatabaseHelper { } return manager; } - + public static void saveBattlePass(BattlePassManager manager) { DatabaseManager.getGameDatastore().save(manager); } diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java index 464d7a277..68ac9af9c 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java @@ -11,24 +11,23 @@ import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.GetPlayerTokenReqOuterClass.GetPlayerTokenReq; import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.server.event.game.PlayerCreationEvent; -import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession.SessionState; import emu.grasscutter.server.packet.send.PacketGetPlayerTokenRsp; @Opcodes(PacketOpcodes.GetPlayerTokenReq) public class HandlerGetPlayerTokenReq extends PacketHandler { - + @Override public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { GetPlayerTokenReq req = GetPlayerTokenReq.parseFrom(payload); - + // Authenticate Account account = DatabaseHelper.getAccountById(req.getAccountUid()); if (account == null || !account.getToken().equals(req.getAccountToken())) { return; } - + // Set account session.setAccount(account); @@ -58,25 +57,25 @@ public class HandlerGetPlayerTokenReq extends PacketHandler { } } - // Get player - Player player = DatabaseHelper.getPlayerByAccount(account); + // Call creation event. + PlayerCreationEvent event = new PlayerCreationEvent(session, Player.class); event.call(); + + // Get player. + Player player = DatabaseHelper.getPlayerByAccount(account, event.getPlayerClass()); if (player == null) { int nextPlayerUid = DatabaseHelper.getNextPlayerId(session.getAccount().getReservedPlayerUid()); - - // Call creation event. - PlayerCreationEvent event = new PlayerCreationEvent(session, Player.class); event.call(); - + // Create player instance from event. player = event.getPlayerClass().getDeclaredConstructor(GameSession.class).newInstance(session); - + // Save to db DatabaseHelper.generatePlayerUid(player, nextPlayerUid); } // Set player object for session session.setPlayer(player); - + // Checks if the player is banned if (session.getAccount().isBanned()) { session.send(new PacketGetPlayerTokenRsp(session, 21, "FORBID_CHEATING_PLUGINS", session.getAccount().getBanEndTime())); @@ -86,7 +85,7 @@ public class HandlerGetPlayerTokenReq extends PacketHandler { // Load player from database player.loadFromDatabase(); - + // Set session state session.setUseSecretKey(true); session.setState(SessionState.WAITING_FOR_LOGIN);