diff --git a/src/treepuncher/events/__init__.py b/src/treepuncher/events/__init__.py index a8b6c6f..eca99b5 100644 --- a/src/treepuncher/events/__init__.py +++ b/src/treepuncher/events/__init__.py @@ -2,3 +2,4 @@ from .chat import ChatEvent from .join_game import JoinGameEvent from .death import DeathEvent from .system import ConnectedEvent, DisconnectedEvent +from .connection import PlayerJoinEvent, PlayerLeaveEvent diff --git a/src/treepuncher/events/connection.py b/src/treepuncher/events/connection.py new file mode 100644 index 0000000..c4bdd57 --- /dev/null +++ b/src/treepuncher/events/connection.py @@ -0,0 +1,14 @@ +from aiocraft.mc.definitions import Player +from .base import BaseEvent + +class PlayerJoinEvent(BaseEvent): + player: Player + + def __init__(self, p:Player): + self.player = p + +class PlayerLeaveEvent(BaseEvent): + player: Player + + def __init__(self, p:Player): + self.player = p diff --git a/src/treepuncher/events/join_game.py b/src/treepuncher/events/join_game.py index 50bbc65..9b37ee5 100644 --- a/src/treepuncher/events/join_game.py +++ b/src/treepuncher/events/join_game.py @@ -12,4 +12,4 @@ class JoinGameEvent(BaseEvent): def __init__(self, dimension:Dimension, difficulty:Difficulty, gamemode:Gamemode): self.gamemode = gamemode self.difficulty = difficulty - self.dimension = dimension \ No newline at end of file + self.dimension = dimension diff --git a/src/treepuncher/game/tablist.py b/src/treepuncher/game/tablist.py index 4685835..143bb9d 100644 --- a/src/treepuncher/game/tablist.py +++ b/src/treepuncher/game/tablist.py @@ -8,7 +8,7 @@ from aiocraft.mc.definitions import Player from aiocraft.mc.proto import PacketPlayerInfo from ..scaffold import Scaffold -from ..events import ConnectedEvent +from ..events import ConnectedEvent, PlayerJoinEvent, PlayerLeaveEvent class ActionType(Enum): # TODO move this in aiocraft ADD_PLAYER = 0 @@ -38,6 +38,7 @@ class GameTablist(Scaffold): if packet.action == ActionType.ADD_PLAYER.value: record['joinTime'] = datetime.datetime.now() self.tablist[uid] = Player.deserialize(record) # TODO have it be a Player type inside packet + self.run_callbacks(PlayerJoinEvent, PlayerJoinEvent(Player.deserialize(record))) elif packet.action == ActionType.UPDATE_GAMEMODE.value: self.tablist[uid].gamemode = record['gamemode'] elif packet.action == ActionType.UPDATE_LATENCY.value: @@ -46,6 +47,7 @@ class GameTablist(Scaffold): self.tablist[uid].displayName = record['displayName'] elif packet.action == ActionType.REMOVE_PLAYER.value: self.tablist.pop(uid, None) + self.run_callbacks(PlayerLeaveEvent, PlayerLeaveEvent(Player.deserialize(record)))