From af9cc4b54602c6b1da4cc995d7a29fd13612280a Mon Sep 17 00:00:00 2001 From: sose Date: Mon, 26 Apr 2021 20:31:00 -0700 Subject: [PATCH] allow connection to offline mode servers without a password --- mosfet/bot.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/mosfet/bot.py b/mosfet/bot.py index cc079e7..d28c2a6 100644 --- a/mosfet/bot.py +++ b/mosfet/bot.py @@ -3,14 +3,15 @@ if __name__ == '__main__': exit(1) import os +import sys import time import importlib from math import floor, ceil from copy import copy -USERNAME = os.environ['USERNAME'] -PASSWORD = os.environ['PASSWORD'] -SERVER = os.environ['SERVER'] +USERNAME = os.getenv('USERNAME') +PASSWORD = os.getenv('PASSWORD') +SERVER = os.getenv('SERVER') PORT = int(os.environ.get('PORT', 25565)) from . import monkey_patch # must be before any possible pyCraft imports @@ -249,15 +250,22 @@ def bot(global_state): if not g.mcdata: g.mcdata = DataManager('./minecraft_data') - if not g.connection: - auth_token = authentication.AuthenticationToken() - try: - auth_token.authenticate(USERNAME, PASSWORD) - except YggdrasilError as e: - print(e) - sys.exit() - print("Logged in as %s..." % auth_token.username) - g.connection = Connection(SERVER, PORT, auth_token=auth_token) + if not SERVER: + print('You must specify a server to connect to.') + sys.exit() + elif not g.connection: + if USERNAME and PASSWORD: + auth_token = authentication.AuthenticationToken() + try: + auth_token.authenticate(USERNAME, PASSWORD) + except YggdrasilError as e: + print(e) + sys.exit() + print("Logged in as %s..." % auth_token.username) + g.connection = Connection(SERVER, PORT, auth_token=auth_token) + elif USERNAME: + print('No password provided, attempting to connect in offline mode...') + g.connection = Connection(SERVER, PORT, username=USERNAME) g.chunks = ChunksManager(g.mcdata)