This commit is contained in:
Charles Le Maux 2024-06-15 06:25:52 +02:00
commit e5b2de8116
7 changed files with 22 additions and 11 deletions

2
.gitignore vendored
View File

@ -161,7 +161,7 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
/server/ /servers/
/users/ /users/
unit_test.py unit_test.py
!*.py !*.py

View File

@ -2,6 +2,17 @@ import os
import shutil import shutil
import re import re
supported_versions = ["bukkit", "paper", "spigot"]
def get_all_versions(folder_path="servers/paper"):
return [name for name in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, name))]
def version_exists(version, framework = "paper")->bool:
if framework not in supported_versions:
return False
folder_names = get_all_versions("servers/"+framework)
return any(version in name for name in folder_names)
def create_folder(path): def create_folder(path):
os.makedirs(path, exist_ok=True) os.makedirs(path, exist_ok=True)

View File

@ -3,35 +3,36 @@ import file_manager
mc_manager: MinecraftServerManager = MinecraftServerManager() mc_manager: MinecraftServerManager = MinecraftServerManager()
def AccountCreate(port) -> bool: def AccountCreate(port : str) -> bool:
file_manager.create_folder("users/"+port) file_manager.create_folder("users/"+port)
def ServerCreate(port, name, version) -> bool: def ServerCreate(port : str, name : str, version : str) -> bool:
server_path: str = f"users/{port}/{name}" server_path: str = f"users/{port}/{name}"
server_template_path: str = "servers/"+version server_template_path: str = "servers/paper/"+version
file_manager.create_folder(server_path) file_manager.create_folder(server_path)
file_manager.copy_folder_contents(server_template_path, server_path) file_manager.copy_folder_contents(server_template_path, server_path)
file_manager.copy_folder_contents("servers/shared", server_path)
file_manager.update_server_property(server_path+"/server.properties", "server-port", port) file_manager.update_server_property(server_path+"/server.properties", "server-port", port)
def ServerDelete(port, name) -> bool: def ServerDelete(port : str, name : str) -> bool:
server_path: str = f"users/{port}/{name}" server_path: str = f"users/{port}/{name}"
file_manager.delete_non_empty_folder(server_path) file_manager.delete_non_empty_folder(server_path)
def AccountDelete(port) -> bool: def AccountDelete(port : str) -> bool:
file_manager.delete_non_empty_folder("users/"+port) file_manager.delete_non_empty_folder("users/"+port)
def ServerRun(port, name) -> bool: def ServerRun(port : str, name : str) -> bool:
server_id = mc_manager.start_server(f"users/{port}/{name}") server_id = mc_manager.start_server(f"users/{port}/{name}")
mc_manager.servers[server_id]['port'] = int(port) mc_manager.servers[server_id]['port'] = int(port)
def ServerStop(port, name) -> bool: def ServerStop(port : str, name : str) -> bool:
server_id = mc_manager.get_server_id_by_port(int(port)) server_id = mc_manager.get_server_id_by_port(int(port))
mc_manager.stop_server(server_id) mc_manager.stop_server(server_id)
def UpdateProperty(port, name, prop, value) -> bool: def UpdateProperty(port : str, name : str, prop : str, value : str) -> bool:
property_file_path: str = f"users/{port}/{name}/server.properties" property_file_path: str = f"users/{port}/{name}/server.properties"
file_manager.update_server_property(property_file_path, prop, value) file_manager.update_server_property(property_file_path, prop, value)
def RunCommand(port, command) -> bool: def RunCommand(port : str, command : str) -> bool:
server_id = mc_manager.get_server_id_by_port(int(port)) server_id = mc_manager.get_server_id_by_port(int(port))
mc_manager.execute_server_command(server_id, command) mc_manager.execute_server_command(server_id, command)

View File

@ -22,7 +22,6 @@ class MinecraftServerManager:
def execute_server_command(self, server_id, command): def execute_server_command(self, server_id, command):
if server_id in self.servers: if server_id in self.servers:
process = self.servers[server_id]['process'] process = self.servers[server_id]['process']
#process.communicate(input=(command + "\n").encode())
process.stdin.write(command.encode() + b'\n') process.stdin.write(command.encode() + b'\n')
print(f"Server {server_id} executed command : {command}") print(f"Server {server_id} executed command : {command}")
else: else: