diff --git a/build_server.py b/build_server.py new file mode 100644 index 0000000..86036a6 --- /dev/null +++ b/build_server.py @@ -0,0 +1,44 @@ +import os +import config + +def rewriter(file_name): + """Create a decorator that rewrites a file based on given settings.""" + + def exec(func): + """Rewrite a file""" + new_lines = [] + + info_to_remember = {} + line_no = 0 + + with open(file_name, 'r', encoding='utf-8') as open_file: + for line in open_file: + line_no += 1 + new_line = func(line, line_no, data=info_to_remember) + new_lines.append(new_line) + + with open(file_name, 'w', encoding='utf-8') as write_file: + for line in new_lines: + write_file.write(line) + return exec + +@rewriter('eula.txt') +def confirm_eula(line : str, line_no : int, data): + """Confirm the Minecraft EULA""" + if os.getenv('EULA') is None or os.getenv('EULA').lower() != 'true': + return line + else: + return line.replace('eula=false', 'eula=true') + +@rewriter('server.properties') +def fill_in_server_settings(line : str, line_no : int, data): + """Set up the server based on our chosen properties""" + if line.strip().startswith('#'): + return line + + value = line.split('=')[0] + + if value in config.SERVER_SETTINGS: + return value + '=' + str(config.SERVER_SETTINGS[value]) + '\n' + else: + return line \ No newline at end of file diff --git a/config.py b/config.py index 9953438..afb124d 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,11 @@ +import os + # Minecraft bridge credentials -MATRIX_HOMESERVER = "https://homeserv.er" -MATRIX_USERNAME = "@bridge_username:homeserv.er" -MATRIX_PASSWORD = "bridge_password" +MATRIX_HOMESERVER = os.getenv('MATRIX_HOMESERVER') or "https://homeserv.er" +MATRIX_USERNAME = os.getenv('MATRIX_USERNAME') or "@bridge_username:homeserv.er" +MATRIX_PASSWORD = os.getenv('MATRIX_PASSWORD') or "bridge_password" + +SERVER_IP = os.getenv('SERVER_ADDRESS') or 'unknown ip' # Matrix users who are allowed to run OP commands in Minecraft through Matrix MC_ADMINS = [ @@ -9,6 +13,43 @@ MC_ADMINS = [ "@_discord_625632515314548736:t2bot.io" # Bram on Discord (example, feel free to remove) # Your username on Matrix ] +if os.getenv('MATRIX_ADMINS') is not None: + MC_ADMINS = os.getenv('MATRIX_ADMINS').split(',') # Matrix channel that the bot should talk to -MC_CHANNEL = "!channel_id:homeserv.er" +MC_CHANNEL = os.getenv('MC_CHANNEL') or "!channel_id:homeserv.er" + +make_bool = lambda os_value, default_value : default_value if not os_value else ( + False if os_value.lower() == 'false' else True +) + +SERVER_SETTINGS = { + 'level-name': os.getenv('WORLD') or 'world', + + # Server settings + 'port' : 25565 if os.getenv('PORT') == None else int(os.getenv('PORT')), + 'query.port' : 25565 if os.getenv('PORT') == None else int(os.getenv('PORT')), + 'max-players' : 7 if os.getenv('MAX_PLAYERS') == None else int(os.getenv('MAX_PLAYERS')), + + # Server temperature >:3 + 'view-distance' : 10 if os.getenv('RENDER_DISTANCE') == None else int(os.getenv('RENDER_DISTANCE')), + 'enable-command-block' : make_bool(os.getenv('COMMAND_BLOCKS'), True), + + # Environment + 'allow-nether' : make_bool(os.getenv('NETHER'), True), + 'spawn-npcs' : make_bool(os.getenv('NPCS'), True), + 'spawn-animals' : make_bool(os.getenv('ANIMALS'), True), + 'spawn-monsters' : make_bool(os.getenv('MONSTERS'), True), + + # Gamemode + 'pvp' : make_bool(os.getenv('PVP'), True), + 'gamemode' : os.getenv('GAMEMODE') or 'survival', + 'difficulty': os.getenv('DIFFICULTY') or 'medium', + 'hardcore' : make_bool(os.getenv('HARDCORE'), False), + + # Grief protection + 'online-mode' : make_bool(os.getenv('VERIFY_ACCOUNTS'), True), + 'white-list' : make_bool(os.getenv('WHITELIST'), True), + 'enforce-whitelist' : make_bool(os.getenv('WHITELIST'), True), + 'spawn-protection' : 16 if os.getenv('SPAWN_PROTECTION') == None else os.getenv('SPAWN_PROTECTION'), +} diff --git a/main.py b/main.py index 4fedfdb..2fc83d5 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from nio import AsyncClient, MatrixRoom, RoomMessageText import mc_wrapper import config +import build_server STARTUP_TIME = time.time() diff --git a/mc_wrapper.py b/mc_wrapper.py index 8fddbe1..6227527 100644 --- a/mc_wrapper.py +++ b/mc_wrapper.py @@ -5,6 +5,7 @@ import json import sys import re from nbsr import NonBlockingStreamReader as NBSR +import config # run the shell as a subprocess: p = Popen(sys.argv[1:], @@ -60,7 +61,7 @@ def process_message(sentence : str) -> Union[str, None]: r"\[[\d:]+\] \[Server thread\/INFO\]: Done \(\d+.?\d*s\)! For help, type \"help\"", sentence): server_live = True - return "The Minecraft server is live. The server is reacable at mc.noordstar.me.", "The minecraft server is live. The server is reacable at mc.noordstar.me." + return f"The Minecraft server is live. The server is reacable at {config.SERVER_IP}.", f"The minecraft server is live. The server is reacable at {config.SERVER_IP}." if re.fullmatch( r"\[[\d:]+\] \[Server thread\/INFO\]: Stopping server",