fixes, allow to run as server

This commit is contained in:
əlemi 2021-11-21 23:57:11 +01:00
parent a5b82cb8a6
commit 3cee338ca5
4 changed files with 26 additions and 21 deletions

View file

@ -6,6 +6,7 @@ from .mc.proto.play.clientbound import PacketChat
from .mc.token import Token
from .dispatcher import ConnectionState
from .client import Client
from .server import MinecraftServer
from .util.helpers import parse_chat
async def idle():
@ -13,6 +14,12 @@ async def idle():
await asyncio.sleep(1)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
if sys.argv[1] == "--server":
serv = MinecraftServer("0.0.0.0", 25565)
serv.run() # will block and start asyncio event loop
else:
username = sys.argv[1]
pwd = sys.argv[2]
server = sys.argv[3]
@ -26,8 +33,6 @@ if __name__ == "__main__":
host = server.strip()
port = 25565
logging.basicConfig(level=logging.INFO)
client = Client(host, port, username=username, password=pwd)
@client.on_packet(PacketChat, ConnectionState.PLAY)

View file

@ -142,11 +142,11 @@ class Client:
loop.run_until_complete(self.start())
async def idle():
while self._processing: # TODO don't busywait even if it doesn't matter much
while True: # TODO don't busywait even if it doesn't matter much
await asyncio.sleep(self.options["poll-timeout"])
try:
loop.run_forever(idle())
loop.run_until_complete(idle())
except KeyboardInterrupt:
self._logger.info("Received SIGINT, stopping...")
try:

View file

@ -6,7 +6,7 @@ from typing import Tuple, Dict, Any
from .types import Type, VarInt
class Packet:
__slots__ = 'id', 'definition', '_processed', '_protocol', '_state'
__slots__ = 'definition', '_processed', '_protocol', '_state'
id : int
definition : Tuple[Tuple[str, Type]]

View file

@ -105,7 +105,7 @@ class PacketClassWriter:
name=self.title,
ids='{\n\t\t' + ',\n\t\t'.join(self.ids) + '\n\t}\n',
definitions='{\n\t\t' + '\n\t\t'.join(self.slots) + '\n\t}\n',
slots=', '.join(f"'{x}'" for x in self.attrs) + ' ,',
slots=', '.join(f"'{x}'" for x in (list(self.attrs) + ["id"])), # TODO de-jank!
fields='\n\t'.join(self.fields),
state=self.state,
)