This commit is contained in:
əlemi 2021-11-22 03:21:26 +01:00
parent b4d93b3b2f
commit f6835709b1
3 changed files with 14 additions and 11 deletions

View file

@ -186,7 +186,7 @@ class MinecraftClient(CallbacksHolder, Runnable):
if not self.options.reconnect: if not self.options.reconnect:
break break
await asyncio.sleep(self.options.reconnect_delay) 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 async def _handshake(self) -> bool: # TODO make this fancier! poll for version and status first
await self.dispatcher.write( await self.dispatcher.write(

View file

@ -220,7 +220,7 @@ class Dispatcher:
if self.state != ConnectionState.PLAY: if self.state != ConnectionState.PLAY:
await self._incoming.join() # During play we can pre-process packets await self._incoming.join() # During play we can pre-process packets
except AttributeError: 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: except ConnectionResetError:
self._logger.error("Connection reset while reading packet") self._logger.error("Connection reset while reading packet")
await self.disconnect(block=False) await self.disconnect(block=False)

View file

@ -1,12 +1,12 @@
import asyncio import asyncio
from uuid import uuid4 import uuid
from typing import Dict, List, Any, Callable from typing import Dict, List, Any, Callable
class CallbacksHolder: class CallbacksHolder:
_callbacks : Dict[Any, List[Callable]] _callbacks : Dict[Any, List[Callable]]
_tasks : Dict[str, asyncio.Event] _tasks : Dict[uuid.UUID, asyncio.Event]
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -23,17 +23,20 @@ class CallbacksHolder:
return [] return []
return self._callbacks[key] 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: def run_callbacks(self, key:Any, *args) -> None:
for cb in self.trigger(key): for cb in self.trigger(key):
task_id = str(uuid4()) task_id = uuid.uuid4()
self._tasks[task_id] = asyncio.Event() self._tasks[task_id] = asyncio.Event()
async def wrapper(*args): asyncio.get_event_loop().create_task(self._wrap(cb, task_id)(*args))
await cb(*args)
self._tasks[task_id].set()
self._tasks.pop(task_id)
asyncio.get_event_loop().create_task(wrapper(*args))
async def join_callbacks(self): async def join_callbacks(self):
await asyncio.gather(*list(t.wait() for t in self._tasks.values())) await asyncio.gather(*list(t.wait() for t in self._tasks.values()))