From f37ab461398ca62f88d92ed4406435327371b208 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 22:12:41 -0400 Subject: [PATCH] Player join & quit event --- .../emu/grasscutter/game/player/Player.java | 4 ++++ .../server/event/game/PlayerJoinEvent.java | 17 +++++++++++++++++ .../server/event/game/PlayerQuitEvent.java | 16 ++++++++++++++++ .../recv/HandlerSetPlayerBornDataReq.java | 9 +++++++-- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java create mode 100644 src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index f440fcb1b..32e583fc5 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -37,6 +37,7 @@ import emu.grasscutter.net.proto.PlayerWorldLocationInfoOuterClass; import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture; import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail; import emu.grasscutter.net.proto.SocialShowAvatarInfoOuterClass; +import emu.grasscutter.server.event.game.PlayerQuitEvent; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.packet.send.*; @@ -1026,6 +1027,9 @@ public class Player { this.save(); this.getTeamManager().saveAvatars(); this.getFriendsList().save(); + + // Call quit event. + PlayerQuitEvent event = new PlayerQuitEvent(this); event.call(); } public enum SceneLoadState { diff --git a/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java b/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java new file mode 100644 index 000000000..1683acce8 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java @@ -0,0 +1,17 @@ +package emu.grasscutter.server.event.game; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.Cancellable; +import emu.grasscutter.server.event.types.GameEvent; + +public final class PlayerJoinEvent extends GameEvent implements Cancellable { + private final Player player; + + public PlayerJoinEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return this.player; + } +} diff --git a/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java b/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java new file mode 100644 index 000000000..df730442d --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java @@ -0,0 +1,16 @@ +package emu.grasscutter.server.event.game; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.types.GameEvent; + +public final class PlayerQuitEvent extends GameEvent { + private final Player player; + + public PlayerQuitEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return this.player; + } +} diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java index 52c52c280..77dc5988b 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java @@ -13,6 +13,7 @@ import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.SetPlayerBornDataReqOuterClass.SetPlayerBornDataReq; import emu.grasscutter.net.packet.PacketHandler; +import emu.grasscutter.server.event.game.PlayerJoinEvent; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession.SessionState; @@ -25,7 +26,7 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { // Sanity checks int avatarId = req.getAvatarId(); - int startingSkillDepot = 0; + int startingSkillDepot; if (avatarId == GameConstants.MAIN_CHARACTER_MALE) { startingSkillDepot = 504; } else if (avatarId == GameConstants.MAIN_CHARACTER_FEMALE) { @@ -91,6 +92,10 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { Grasscutter.getLogger().error("Error creating player object: ", e); session.close(); } - } + // Call join event. + PlayerJoinEvent event = new PlayerJoinEvent(player); event.call(); + if(event.isCanceled()) // If event is not cancelled, continue. + session.close(); + } }