2021-11-10 18:57:49 +01:00
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from .mc.proto.play.clientbound import PacketChat
|
|
|
|
from .mc.identity import Token
|
|
|
|
from .dispatcher import ConnectionState
|
|
|
|
from .client import Client
|
|
|
|
from .helpers import parse_chat
|
|
|
|
|
|
|
|
async def idle():
|
|
|
|
while True:
|
|
|
|
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" )
|
|
|
|
|
|
|
|
if ":" in server:
|
|
|
|
_host, _port = server.split(":")
|
|
|
|
host = _host.strip()
|
|
|
|
port = int(_port)
|
|
|
|
else:
|
|
|
|
host = server.strip()
|
|
|
|
port = 25565
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
# TODO rework how this is started! Maybe implement client context manager?
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
token = loop.run_until_complete(Token.authenticate(username, pwd))
|
|
|
|
|
|
|
|
client = Client(token, host, port)
|
|
|
|
|
|
|
|
@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}")
|
|
|
|
|
|
|
|
loop.run_until_complete(client.start())
|
|
|
|
|
|
|
|
try:
|
|
|
|
loop.run_until_complete(idle())
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.info("Received SIGINT, stopping...")
|
|
|
|
loop.run_until_complete(client.stop())
|
|
|
|
|
|
|
|
logging.info("Terminated")
|
|
|
|
|