feat: Added PlayerJoin/PlayerLeave events

Co-authored-by: f-tlm <f-tlm@users.noreply.github.com>
This commit is contained in:
əlemi 2022-08-13 14:13:17 +02:00
parent 8f3f8fd69b
commit f5481ace65
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
4 changed files with 19 additions and 2 deletions

View file

@ -2,3 +2,4 @@ from .chat import ChatEvent
from .join_game import JoinGameEvent from .join_game import JoinGameEvent
from .death import DeathEvent from .death import DeathEvent
from .system import ConnectedEvent, DisconnectedEvent from .system import ConnectedEvent, DisconnectedEvent
from .connection import PlayerJoinEvent, PlayerLeaveEvent

View file

@ -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

View file

@ -12,4 +12,4 @@ class JoinGameEvent(BaseEvent):
def __init__(self, dimension:Dimension, difficulty:Difficulty, gamemode:Gamemode): def __init__(self, dimension:Dimension, difficulty:Difficulty, gamemode:Gamemode):
self.gamemode = gamemode self.gamemode = gamemode
self.difficulty = difficulty self.difficulty = difficulty
self.dimension = dimension self.dimension = dimension

View file

@ -8,7 +8,7 @@ from aiocraft.mc.definitions import Player
from aiocraft.mc.proto import PacketPlayerInfo from aiocraft.mc.proto import PacketPlayerInfo
from ..scaffold import Scaffold from ..scaffold import Scaffold
from ..events import ConnectedEvent from ..events import ConnectedEvent, PlayerJoinEvent, PlayerLeaveEvent
class ActionType(Enum): # TODO move this in aiocraft class ActionType(Enum): # TODO move this in aiocraft
ADD_PLAYER = 0 ADD_PLAYER = 0
@ -38,6 +38,7 @@ class GameTablist(Scaffold):
if packet.action == ActionType.ADD_PLAYER.value: if packet.action == ActionType.ADD_PLAYER.value:
record['joinTime'] = datetime.datetime.now() record['joinTime'] = datetime.datetime.now()
self.tablist[uid] = Player.deserialize(record) # TODO have it be a Player type inside packet 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: elif packet.action == ActionType.UPDATE_GAMEMODE.value:
self.tablist[uid].gamemode = record['gamemode'] self.tablist[uid].gamemode = record['gamemode']
elif packet.action == ActionType.UPDATE_LATENCY.value: elif packet.action == ActionType.UPDATE_LATENCY.value:
@ -46,6 +47,7 @@ class GameTablist(Scaffold):
self.tablist[uid].displayName = record['displayName'] self.tablist[uid].displayName = record['displayName']
elif packet.action == ActionType.REMOVE_PLAYER.value: elif packet.action == ActionType.REMOVE_PLAYER.value:
self.tablist.pop(uid, None) self.tablist.pop(uid, None)
self.run_callbacks(PlayerLeaveEvent, PlayerLeaveEvent(Player.deserialize(record)))