Add part that dynamically builds and sets up server

main
Bram 2021-07-31 15:26:09 +02:00
parent 3a3042192d
commit 30f51d8d2b
4 changed files with 92 additions and 5 deletions

44
build_server.py Normal file
View File

@ -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

View File

@ -1,7 +1,11 @@
import os
# Minecraft bridge credentials # Minecraft bridge credentials
MATRIX_HOMESERVER = "https://homeserv.er" MATRIX_HOMESERVER = os.getenv('MATRIX_HOMESERVER') or "https://homeserv.er"
MATRIX_USERNAME = "@bridge_username:homeserv.er" MATRIX_USERNAME = os.getenv('MATRIX_USERNAME') or "@bridge_username:homeserv.er"
MATRIX_PASSWORD = "bridge_password" 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 # Matrix users who are allowed to run OP commands in Minecraft through Matrix
MC_ADMINS = [ MC_ADMINS = [
@ -9,6 +13,43 @@ MC_ADMINS = [
"@_discord_625632515314548736:t2bot.io" # Bram on Discord (example, feel free to remove) "@_discord_625632515314548736:t2bot.io" # Bram on Discord (example, feel free to remove)
# Your username on Matrix # 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 # 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'),
}

View File

@ -6,6 +6,7 @@ from nio import AsyncClient, MatrixRoom, RoomMessageText
import mc_wrapper import mc_wrapper
import config import config
import build_server
STARTUP_TIME = time.time() STARTUP_TIME = time.time()

View File

@ -5,6 +5,7 @@ import json
import sys import sys
import re import re
from nbsr import NonBlockingStreamReader as NBSR from nbsr import NonBlockingStreamReader as NBSR
import config
# run the shell as a subprocess: # run the shell as a subprocess:
p = Popen(sys.argv[1:], 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\"", r"\[[\d:]+\] \[Server thread\/INFO\]: Done \(\d+.?\d*s\)! For help, type \"help\"",
sentence): sentence):
server_live = True server_live = True
return "The Minecraft server is live. The server is reacable at <code>mc.noordstar.me</code>.", "The minecraft server is live. The server is reacable at <code>mc.noordstar.me</code>." return f"The Minecraft server is live. The server is reacable at <code>{config.SERVER_IP}</code>.", f"The minecraft server is live. The server is reacable at <code>{config.SERVER_IP}</code>."
if re.fullmatch( if re.fullmatch(
r"\[[\d:]+\] \[Server thread\/INFO\]: Stopping server", r"\[[\d:]+\] \[Server thread\/INFO\]: Stopping server",