aboutsummaryrefslogtreecommitdiff
path: root/BerryClient/events.py
blob: 1b75e24f258948ba9e5741e07f298b71af17c767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import logging

from mcpi.connection import RequestError
from mcpi.event import BlockEvent, ChatEvent

log = logging.getLogger(__name__)

BLOCK_CALLBACKS = []
def on_block_hit():
    def decorator(func):
        def wrapper(core: "BerryCore", event: BlockEvent) -> bool:
            return func(core, event)

        BLOCK_CALLBACKS.append(wrapper)

    return decorator

CHAT_CALLBACKS = []
def on_chat_post():
    def decorator(func):
        def wrapper(core: "BerryCore", event: ChatEvent) -> bool:
            return func(core, event)

        CHAT_CALLBACKS.append(wrapper)

    return decorator

PLAYER_MOVEMENT_CALLBACKS = []
def on_player_movement():
    def decorator(func):
        def wrapper(core: "BerryCore", playerId: int, old_pos, new_pos) -> bool:
            return func(core, playerId, old_pos, new_pos)

        PLAYER_MOVEMENT_CALLBACKS.append(wrapper)

    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:
            if callback(core, hit):
                break

    for post in core.events.pollChatPosts():
        for callback in CHAT_CALLBACKS:
            if callback(core, post):
                break

    OLD_POSITIONS = PLAYER_POSITIONS.copy()
    try:
        playerEntityIds = core.getPlayerEntityIds()
    except RequestError:
        playerEntityIds = []

    for playerId in playerEntityIds:
        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