Edit: I am running this on windows as the servers are administered on my main gaming rig
I am trying to create a minecraft server controller so that I can streamline the process of administering servers. I have been able to use subprocess to start and stop the server with no issues however when I try to get it to issue commands It will not. Unless I use subprocess.communicate()
the problem with this solution however is that it causes the script to wait forever and eventually crash, leaving the server running. Here is the code for what I have.
import subprocess
class Server:
def __init__(self, server_start_bat, dir):
self.server_start_bat = server_start_bat
self.dir = dir
def start_server(self):
self.server = subprocess.Popen(self.server_start_bat, cwd=self.dir, shell=True, stdin=subprocess.PIPE,
universal_newlines=True, text=True)
def stop_server(self):
self.run_command('stop')
self.server.communicate()
def op_me(self):
self.run_command(f'op Username')
def clear_weather(self):
self.run_command(f'weather clear 1000000')
def mob_griefing_on(self):
self.run_command(f'gamerule mobGriefing True')
def mob_griefing_off(self):
self.run_command(f'gamerule mobGriefing True')
def set_time_day(self):
self.run_command(f'time set day')
def set_time_night(self):
self.run_command(f'time set night')
def run_command(self, command_text):
self.server.stdin.write(f'{command_text}\n')