added join_game event, ctx, logic module registration

This commit is contained in:
əlemi 2021-11-29 03:52:43 +01:00
parent 4dc5222523
commit 8537ca3e45

View file

@ -1,7 +1,7 @@
import re import re
import logging import logging
from typing import List, Dict, Union, Optional from typing import List, Dict, Union, Optional, Any, Type
from enum import Enum from enum import Enum
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
@ -18,11 +18,13 @@ from aiocraft.mc.proto.play.serverbound import PacketTeleportConfirm, PacketClie
from .events import ChatEvent from .events import ChatEvent
from .events.chat import MessageType from .events.chat import MessageType
from .modules.module import LogicModule
REMOVE_COLOR_FORMATS = re.compile(r"§[0-9a-z]") REMOVE_COLOR_FORMATS = re.compile(r"§[0-9a-z]")
class BotEvents(Enum): class TreepuncherEvents(Enum):
DIED = 0 DIED = 0
IN_GAME = 1
class Treepuncher(MinecraftClient): class Treepuncher(MinecraftClient):
in_game : bool in_game : bool
@ -48,9 +50,12 @@ class Treepuncher(MinecraftClient):
# flags : int # flags : int
scheduler : AsyncIOScheduler scheduler : AsyncIOScheduler
modules : List[LogicModule]
ctx : Dict[Any, Any]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.ctx = dict()
self.in_game = False self.in_game = False
self.gamemode = Gamemode.SURVIVAL self.gamemode = Gamemode.SURVIVAL
@ -67,6 +72,7 @@ class Treepuncher(MinecraftClient):
self.position = Position(0, 0, 0) self.position = Position(0, 0, 0)
self._register_handlers() self._register_handlers()
self.modules = []
self.scheduler = AsyncIOScheduler() self.scheduler = AsyncIOScheduler()
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING) # So it's way less spammy logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING) # So it's way less spammy
@ -75,11 +81,19 @@ class Treepuncher(MinecraftClient):
async def start(self): async def start(self):
await super().start() await super().start()
self.scheduler.resume() self.scheduler.resume()
for m in self.modules:
await m.initialize(self)
async def stop(self, force:bool=False): async def stop(self, force:bool=False):
self.scheduler.pause() self.scheduler.pause()
for m in self.modules:
await m.cleanup(self)
await super().stop(force=force) await super().stop(force=force)
def add(self, module:LogicModule):
module.register(self)
self.modules.append(module)
def on_chat(self, msg_type:Union[str, MessageType] = None): def on_chat(self, msg_type:Union[str, MessageType] = None):
if isinstance(msg_type, str): if isinstance(msg_type, str):
msg_type = MessageType(msg_type) msg_type = MessageType(msg_type)
@ -93,7 +107,12 @@ class Treepuncher(MinecraftClient):
def on_death(self): def on_death(self):
def wrapper(fun): def wrapper(fun):
return self.register(BotEvents.DIED, fun) return self.register(TreepuncherEvents.DIED, fun)
return wrapper
def on_joined_world(self):
def wrapper(fun):
return self.register(TreepuncherEvents.IN_GAME, fun)
return wrapper return wrapper
async def write(self, packet:Packet, wait:bool=False): async def write(self, packet:Packet, wait:bool=False):
@ -146,6 +165,7 @@ class Treepuncher(MinecraftClient):
self.difficulty.name, self.difficulty.name,
self.gamemode.name self.gamemode.name
) )
self.run_callbacks(TreepuncherEvents.IN_GAME)
@self.on_packet(PacketPosition) @self.on_packet(PacketPosition)
async def player_rubberband_cb(packet:PacketPosition): async def player_rubberband_cb(packet:PacketPosition):
@ -165,12 +185,14 @@ class Treepuncher(MinecraftClient):
await self.dispatcher.write( await self.dispatcher.write(
PacketClientCommand(self.dispatcher.proto, actionId=0) # respawn PacketClientCommand(self.dispatcher.proto, actionId=0) # respawn
) )
self.run_callbacks(BotEvents.DIED) self.run_callbacks(TreepuncherEvents.DIED)
self.hp = packet.health self.hp = packet.health
self.food = packet.food self.food = packet.food
@self.on_packet(PacketExperience) @self.on_packet(PacketExperience)
async def player_hp_cb(packet:PacketExperience): async def player_hp_cb(packet:PacketExperience):
if packet.level != self.lvl:
self._logger.info("Level up : %d", packet.level)
self.xp = packet.experienceBar self.xp = packet.experienceBar
self.lvl = packet.level self.lvl = packet.level
self.total_xp = packet.totalExperience self.total_xp = packet.totalExperience