feat: Added PlayerJoin/PlayerLeave events
Co-authored-by: f-tlm <f-tlm@users.noreply.github.com>
This commit is contained in:
parent
8f3f8fd69b
commit
f5481ace65
4 changed files with 19 additions and 2 deletions
|
@ -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
|
||||
|
|
14
src/treepuncher/events/connection.py
Normal file
14
src/treepuncher/events/connection.py
Normal 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
|
|
@ -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
|
||||
self.dimension = dimension
|
||||
|
|
|
@ -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)))
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue