[+] Masterclass conditional minecraft version checker

- Added version parameter to 'start_server' function in MinecraftManager class

- Implemented a version checker that uses a pointer of Tuples, each of them containing a lower and upper bound, as well as the associated sdk

- Used currying method to concatenate 'version_range_checker' into the fastest and most efficient code

This system will allow easy versioning, as well as easy future sdk implementations.
It will also now be easy to fetch a server's version directly in the backend.

Co-authored-by: charleslemaux <charles@le-maux.dev>
This commit is contained in:
Charles Le Maux 2024-09-13 04:14:37 +02:00
parent 11fcb0f89c
commit f62c5fc856

View File

@ -1,7 +1,7 @@
import shlex
import subprocess
import time
from typing import Union
from typing import Union, Callable
import mcipc.query
import mcipc.query.client
@ -21,7 +21,7 @@ class MinecraftServerManager:
self.offline_ports: list[int] = []
def start_server(self, server_directory: str, port: int, user_id: str, server_name: str,
java_executable='java', jar_file='server.jar', memory_size='2048M') -> Union[int, None]:
version: str, jar_file='server.jar', memory_size='4G') -> Union[int, None]:
if port in self.servers:
return None
@ -35,7 +35,15 @@ class MinecraftServerManager:
" -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"
get_sdk_version: Callable[[str], str] = version_range_checker(("1.0", "1.15.2", "11"),
("1.16.1", "1.18.1", "17"),
default_sdk="21")
java: str = f"/usr/lib/jvm/java-{get_sdk_version(version)}-openjdk/bin/java"
command = f"{java} -Xmx{memory_size} {reg_flags} -jar {jar_file} --nogui"
process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE)
#TODO: Track process behavior and stderr, while excepting Advanced Terminal features not to be avail.
@ -46,6 +54,7 @@ class MinecraftServerManager:
'port': port,
'user_id': user_id,
'name': server_name,
'version': version,
'time': time.time(),
}
return port
@ -136,5 +145,34 @@ class MinecraftServerManager:
return
def version_range_checker(*ranges: tuple[str, str, str], default_sdk: str = "java") -> Callable[[str], str]:
def compare_mc_versions(version1: str):
def inner(version2: str) -> int:
v1_parts: list[int] = list(map(int, version1.split('.')))
v2_parts: list[int] = list(map(int, version2.split('.')))
for v1, v2 in zip(v1_parts, v2_parts):
if v1 > v2:
return 1
elif v1 < v2:
return -1
if len(v1_parts) > len(v2_parts):
return 1
elif len(v1_parts) < len(v2_parts):
return -1
return 0
return inner
def check_range(lower_bound: str, upper_bound: str, version: str) -> bool:
return compare_mc_versions(lower_bound)(version) <= 0 <= compare_mc_versions(upper_bound)(version)
def check_version(version: str) -> str:
for lower_bound, upper_bound, sdk in ranges:
if check_range(lower_bound, upper_bound, version):
return sdk
return default_sdk
return check_version
if __name__ == "__main__":
pass