diff --git a/aiocraft/__main__.py b/aiocraft/__main__.py index d65282f..cc0b512 100644 --- a/aiocraft/__main__.py +++ b/aiocraft/__main__.py @@ -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,29 +14,33 @@ async def idle(): await asyncio.sleep(1) if __name__ == "__main__": - username = sys.argv[1] - pwd = sys.argv[2] - server = sys.argv[3] - color = not (len(sys.argv) > 4 and sys.argv[4] == "--no-color" ) + logging.basicConfig(level=logging.DEBUG) - if ":" in server: - _host, _port = server.split(":") - host = _host.strip() - port = int(_port) + if sys.argv[1] == "--server": + serv = MinecraftServer("0.0.0.0", 25565) + serv.run() # will block and start asyncio event loop else: - host = server.strip() - port = 25565 + username = sys.argv[1] + pwd = sys.argv[2] + server = sys.argv[3] + color = not (len(sys.argv) > 4 and sys.argv[4] == "--no-color" ) - logging.basicConfig(level=logging.INFO) + if ":" in server: + _host, _port = server.split(":") + host = _host.strip() + port = int(_port) + else: + host = server.strip() + port = 25565 - client = Client(host, port, username=username, password=pwd) + client = Client(host, port, username=username, password=pwd) - @client.on_packet(PacketChat, ConnectionState.PLAY) - async def print_chat(packet: PacketChat): - msg = parse_chat(json.loads(packet.message), color=color) - print(f"[{packet.position}] {msg}") + @client.on_packet(PacketChat, ConnectionState.PLAY) + async def print_chat(packet: PacketChat): + msg = parse_chat(json.loads(packet.message), color=color) + print(f"[{packet.position}] {msg}") - client.run() # will block and start asyncio event loop + client.run() # will block and start asyncio event loop logging.info("Terminated") diff --git a/aiocraft/client.py b/aiocraft/client.py index 714f097..1034b30 100644 --- a/aiocraft/client.py +++ b/aiocraft/client.py @@ -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: diff --git a/aiocraft/mc/packet.py b/aiocraft/mc/packet.py index 0badf0c..76b4908 100644 --- a/aiocraft/mc/packet.py +++ b/aiocraft/mc/packet.py @@ -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]] diff --git a/compiler/proto.py b/compiler/proto.py index 7cdda83..73341e7 100644 --- a/compiler/proto.py +++ b/compiler/proto.py @@ -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, )