Commit f4ba460d authored by KingRainbow44's avatar KingRainbow44
Browse files

Fix `PlayerCreationEvent` to match expected logic

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