aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2022-10-20 08:02:32 +0200
committerJan200101 <sentrycraft123@gmail.com>2022-10-20 08:02:32 +0200
commit799044b9ad0bf07c98767c8937afa7e702f4257e (patch)
treee2cadadfc4485eebfda8b3b564178cebe83a0acb
parent6c1d719d115e4a698e64733545951fb168335224 (diff)
downloadBerryClient-799044b9ad0bf07c98767c8937afa7e702f4257e.tar.gz
BerryClient-799044b9ad0bf07c98767c8937afa7e702f4257e.zip
add on join and leave events
-rw-r--r--BerryClient/events.py53
1 files changed, 46 insertions, 7 deletions
diff --git a/BerryClient/events.py b/BerryClient/events.py
index c01b6ee..041a852 100644
--- a/BerryClient/events.py
+++ b/BerryClient/events.py
@@ -35,8 +35,30 @@ def on_player_movement():
return decorator
+PLAYER_JOIN_CALLBACKS = []
+def on_player_join():
+ def decorator(func):
+ def wrapper(core: "BerryCore", playerId: int) -> bool:
+ return func(core, playerId)
+
+ PLAYER_JOIN_CALLBACKS.append(wrapper)
+
+ return decorator
+
+PLAYER_LEAVE_CALLBACKS = []
+def on_player_leave():
+ def decorator(func):
+ def wrapper(core: "BerryCore", playerId: int) -> bool:
+ return func(core, playerId)
+
+ PLAYER_LEAVE_CALLBACKS.append(wrapper)
+
+ return decorator
+
+KNOWN_PLAYERS = []
PLAYER_POSITIONS = {}
def process_events(core: "BerryCore"):
+ global KNOWN_PLAYERS
for hit in core.events.pollBlockHits():
for callback in BLOCK_CALLBACKS:
@@ -55,12 +77,29 @@ def process_events(core: "BerryCore"):
playerEntityIds = []
for playerId in playerEntityIds:
- PLAYER_POSITIONS[playerId] = core.entity.getPos(playerId)
+ try:
+ PLAYER_POSITIONS[playerId] = core.entity.getPos(playerId)
+ except RequestError:
+ # player ID is no longer valid, move on
+ continue
+
+ if playerId in KNOWN_PLAYERS:
+ if OLD_POSITIONS[playerId] != PLAYER_POSITIONS[playerId]:
+ for callback in PLAYER_MOVEMENT_CALLBACKS:
+ if callback(core, playerId,
+ OLD_POSITIONS[playerId], PLAYER_POSITIONS[playerId]):
+ break
+
+ else:
+ for callback in PLAYER_JOIN_CALLBACKS:
+ if callback(core, playerId):
+ break
+
+ for playerLeft in [pId for pId in KNOWN_PLAYERS if pId not in playerEntityIds]:
+ for callback in PLAYER_LEAVE_CALLBACKS:
+ if callback(core, playerLeft):
+ break
+
+ KNOWN_PLAYERS = playerEntityIds
- if (playerId in OLD_POSITIONS
- and OLD_POSITIONS[playerId] != PLAYER_POSITIONS[playerId]):
- for callback in PLAYER_MOVEMENT_CALLBACKS:
- if callback(core, playerId,
- OLD_POSITIONS[playerId], PLAYER_POSITIONS[playerId]):
- break