import subprocess import shlex import time import mcipc.query import mcipc.query.client from typing import Union class MinecraftServerManager: allowed_properties: list[str] = ["difficulty", "gamemode", "force-gamemode", "hardcore", "generate-structures", "motd", "pvp", "online-mode", "max-players", "enable-command-block"] def __init__(self): self.servers: dict = {} self.servers_count: int = 0 self.cooldowns = {} def start_server(self, server_directory: str, port: int, java_executable='java', jar_file='server.jar', memory_size='2048M') -> Union[int, None]: if port in self.servers: return None reg_flags: str = ("-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200" " -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch" " -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M" " -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4" " -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90" " -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem" " -XX:MaxTenuringThreshold=1 -Daikars.new.flags=true" " -Dusing.aikars.flags=https://mcutils.com") command = f"{java_executable} -Xmx{memory_size} {reg_flags} -jar {jar_file} --nogui" process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE) self.servers_count = len(self.servers) + 1 self.servers[port] = { 'process': process, 'directory': server_directory, 'port': port } return port def execute_server_command(self, port, command) -> bool: if port not in self.servers: return False process = self.servers[port]['process'] process.stdin.write(command.encode('utf-8') + b'\n') process.stdin.flush() return True def stop_server(self, port: int) -> bool: if port not in self.servers: return False process = self.servers[port]['process'] process.communicate(input=b"stop\n") del self.servers[port] return True def stop_server_forcefully(self, port) -> bool: if port not in self.servers: return False process = self.servers[port]['process'] process.terminate() del self.servers[port] return True def get_servers(self): return self.servers.values() def get_server_id_by_port(self, port): for server_id, server_info in self.servers.items(): if server_info['port'] == port: return server_id return None def get_online_players(self, port) -> int: with mcipc.query.Client('127.0.0.1', port) as client: stats: mcipc.query.proto.FullStats = client.stats(full=True) stats: int = stats.num_players return stats def set_cooldown(self, user_id): expiry_timestamp = time.time() + 30 self.cooldowns[user_id] = expiry_timestamp def has_cooldown(self, user_id): expiry_timestamp = self.cooldowns.get(user_id) if expiry_timestamp is None: return False current_time = time.time() if current_time < expiry_timestamp: return True else: del self.cooldowns[user_id] return False if __name__ == "__main__": pass