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 .mc.token import Token
from .dispatcher import ConnectionState from .dispatcher import ConnectionState
from .client import Client from .client import Client
from .server import MinecraftServer
from .util.helpers import parse_chat from .util.helpers import parse_chat
async def idle(): async def idle():
@ -13,29 +14,33 @@ async def idle():
await asyncio.sleep(1) await asyncio.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
username = sys.argv[1] logging.basicConfig(level=logging.DEBUG)
pwd = sys.argv[2]
server = sys.argv[3]
color = not (len(sys.argv) > 4 and sys.argv[4] == "--no-color" )
if ":" in server: if sys.argv[1] == "--server":
_host, _port = server.split(":") serv = MinecraftServer("0.0.0.0", 25565)
host = _host.strip() serv.run() # will block and start asyncio event loop
port = int(_port)
else: else:
host = server.strip() username = sys.argv[1]
port = 25565 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) @client.on_packet(PacketChat, ConnectionState.PLAY)
async def print_chat(packet: PacketChat): async def print_chat(packet: PacketChat):
msg = parse_chat(json.loads(packet.message), color=color) msg = parse_chat(json.loads(packet.message), color=color)
print(f"[{packet.position}] {msg}") 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") logging.info("Terminated")

View file

@ -142,11 +142,11 @@ class Client:
loop.run_until_complete(self.start()) loop.run_until_complete(self.start())
async def idle(): 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"]) await asyncio.sleep(self.options["poll-timeout"])
try: try:
loop.run_forever(idle()) loop.run_until_complete(idle())
except KeyboardInterrupt: except KeyboardInterrupt:
self._logger.info("Received SIGINT, stopping...") self._logger.info("Received SIGINT, stopping...")
try: try:

View file

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

View file

@ -105,7 +105,7 @@ class PacketClassWriter:
name=self.title, name=self.title,
ids='{\n\t\t' + ',\n\t\t'.join(self.ids) + '\n\t}\n', 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', 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), fields='\n\t'.join(self.fields),
state=self.state, state=self.state,
) )