From f6835709b1ed7c64e9a1bdc578f1ec40c2ceea77 Mon Sep 17 00:00:00 2001 From: alemidev Date: Mon, 22 Nov 2021 03:21:26 +0100 Subject: [PATCH] fixes --- aiocraft/client.py | 2 +- aiocraft/dispatcher.py | 2 +- aiocraft/traits/callbacks.py | 21 ++++++++++++--------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/aiocraft/client.py b/aiocraft/client.py index 9c2422c..3ab88d6 100644 --- a/aiocraft/client.py +++ b/aiocraft/client.py @@ -186,7 +186,7 @@ class MinecraftClient(CallbacksHolder, Runnable): if not self.options.reconnect: break await asyncio.sleep(self.options.reconnect_delay) - await self.stop(block=False) + await self.stop(force=True) async def _handshake(self) -> bool: # TODO make this fancier! poll for version and status first await self.dispatcher.write( diff --git a/aiocraft/dispatcher.py b/aiocraft/dispatcher.py index 5f37667..fd06ac7 100644 --- a/aiocraft/dispatcher.py +++ b/aiocraft/dispatcher.py @@ -220,7 +220,7 @@ class Dispatcher: if self.state != ConnectionState.PLAY: await self._incoming.join() # During play we can pre-process packets except AttributeError: - self._logger.warning("Received unimplemented packet [%d] %s", packet_id, cls.__name__) + self._logger.debug("Received unimplemented packet [%d] %s", packet_id, cls.__name__) # TODO this is cheating! implement them! except ConnectionResetError: self._logger.error("Connection reset while reading packet") await self.disconnect(block=False) diff --git a/aiocraft/traits/callbacks.py b/aiocraft/traits/callbacks.py index f7b4fd4..06ccba7 100644 --- a/aiocraft/traits/callbacks.py +++ b/aiocraft/traits/callbacks.py @@ -1,12 +1,12 @@ import asyncio -from uuid import uuid4 +import uuid from typing import Dict, List, Any, Callable class CallbacksHolder: _callbacks : Dict[Any, List[Callable]] - _tasks : Dict[str, asyncio.Event] + _tasks : Dict[uuid.UUID, asyncio.Event] def __init__(self): super().__init__() @@ -23,17 +23,20 @@ class CallbacksHolder: return [] return self._callbacks[key] + def _wrap(self, cb:Callable, uid:uuid.UUID) -> Callable: + async def wrapper(*args): + ret = await cb(*args) + self._tasks[uid].set() + self._tasks.pop(uid) + return ret + return wrapper + def run_callbacks(self, key:Any, *args) -> None: for cb in self.trigger(key): - task_id = str(uuid4()) + task_id = uuid.uuid4() self._tasks[task_id] = asyncio.Event() - async def wrapper(*args): - await cb(*args) - self._tasks[task_id].set() - self._tasks.pop(task_id) - - asyncio.get_event_loop().create_task(wrapper(*args)) + asyncio.get_event_loop().create_task(self._wrap(cb, task_id)(*args)) async def join_callbacks(self): await asyncio.gather(*list(t.wait() for t in self._tasks.values()))