from mcrcon import MCRcon from flask import Flask, render_template, redirect, request import json import os app = Flask(__name__) # load from env vars host = os.environ['RCON_HOST'] password = os.environ['RCON_PASSWORD'] port = int(os.environ.setdefault('RCON_PORT', '25575')) print(f'Connecting to Minecraft Server: {host}:{port} with {password}') @app.route('/') def index(): return render_template('index.html', players=current_players_list()) @app.route('/tp', methods=['POST']) def tp(): # TODO: sanitize inputs to avoid possible command injection src = request.form['src'] dest = request.form['dest'] # this echoes a string that contains the src player's coordinates src_coords = rcon_command(f"/tp {src} ~ ~ ~") # teleport the player rcon_command(f"/tp {src} {dest}") # have the server report the old coordinates in case they need teleporting # back rcon_command(f"/say TP: {src_coords.replace(f'Teleported {src} to', f'{src} just teleported from', 1)} to {dest}".replace("[Rcon] ", "")) return redirect('/') def rcon_command(cmd): print(f"RCON: Sending \"{cmd}\"...") response = "" with MCRcon(host, password, port) as mcr: response = mcr.command(cmd) print(f"RCON: \"{cmd}\" Response:\n\t{response}") return response def current_players_list(): # TODO: this breaks if a player has a comma in their name # is that even possible? plist = rcon_command(f"/list") print(f'PLIST: {plist}') print(f'PLIST: {plist.split(":")[1].split(", ")}') return plist.split(":")[1].split(", ")