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,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")

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,
)