diff --git a/mcserverwrapper/src/server_properties_helper.py b/mcserverwrapper/src/server_properties_helper.py index fed86d4..0c37c98 100644 --- a/mcserverwrapper/src/server_properties_helper.py +++ b/mcserverwrapper/src/server_properties_helper.py @@ -3,17 +3,10 @@ from __future__ import annotations import os -import re from typing import Any from .mcversion import McVersion -DEFAULT_PORT = 25565 -DEFAULT_MAX_PLAYERS = 20 -DEFAULT_ONLINE_MODE = "true" -DEFAULT_LEVEL_TYPE_PRE_1_19 = "default" -DEFAULT_LEVEL_TYPE_1_19 = "minecraft\\:normal" - ALL_PROPERTIES = [ "port", "maxp", @@ -21,6 +14,23 @@ "levt" ] +LEVEL_TYPES = { + "old": { + "default": "default", + "flat": "flat" + }, + "post_1_19": { + "default": "minecraft\\:normal", + "flat": "minecraft\\:flat" + } +} + +DEFAULT_PORT = 25565 +DEFAULT_MAX_PLAYERS = 20 +DEFAULT_ONLINE_MODE = "true" +DEFAULT_LEVEL_TYPE_OLD = LEVEL_TYPES["old"]["default"] +DEFAULT_LEVEL_TYPE_POST_1_19 = LEVEL_TYPES["post_1_19"]["default"] + # how many different property args are allowed PROPERTY_ARGS_COUNT = len(ALL_PROPERTIES) @@ -71,22 +81,21 @@ def parse_properties_args(server_path: str, server_property_args: dict | None, s server_property_args["onli"] = DEFAULT_ONLINE_MODE if "levt" not in server_property_args: if server_version.id < McVersion.version_name_to_id("1.19"): - server_property_args["levt"] = DEFAULT_LEVEL_TYPE_PRE_1_19 + server_property_args["levt"] = DEFAULT_LEVEL_TYPE_OLD else: - server_property_args["levt"] = DEFAULT_LEVEL_TYPE_1_19 + server_property_args["levt"] = DEFAULT_LEVEL_TYPE_POST_1_19 - # set default level type to the correct version + # convert level types if server_version.id < McVersion.version_name_to_id("1.19"): - if server_property_args["levt"] == DEFAULT_LEVEL_TYPE_1_19: - server_property_args["levt"] = DEFAULT_LEVEL_TYPE_PRE_1_19 - elif re.search(r"^minecraft\\\:*", server_property_args["levt"]) is not None: - server_property_args["levt"] = server_property_args["levt"].replace("minecraft\\:", "") - + if server_property_args["levt"] in LEVEL_TYPES["post_1_19"].values(): + for key, value in LEVEL_TYPES["post_1_19"].items(): + if value == server_property_args["levt"]: + server_property_args["levt"] = LEVEL_TYPES["old"][key] if server_version.id >= McVersion.version_name_to_id("1.19"): - if server_property_args["levt"] == DEFAULT_LEVEL_TYPE_PRE_1_19: - server_property_args["levt"] = DEFAULT_LEVEL_TYPE_1_19 - elif re.search(r"^minecraft\\\:*", server_property_args["levt"]) is None: - server_property_args["levt"] = "minecraft\\:" + server_property_args["levt"] + if server_property_args["levt"] in LEVEL_TYPES["old"].values(): + for key, value in LEVEL_TYPES["old"].items(): + if value == server_property_args["levt"]: + server_property_args["levt"] = LEVEL_TYPES["post_1_19"][key] return server_property_args diff --git a/mcserverwrapper/test/test_server_properties_helper.py b/mcserverwrapper/test/test_server_properties_helper.py index d2cf820..8e508dd 100644 --- a/mcserverwrapper/test/test_server_properties_helper.py +++ b/mcserverwrapper/test/test_server_properties_helper.py @@ -68,7 +68,7 @@ def test_parse_default_params(): assert result["port"] == sph.DEFAULT_PORT assert result["maxp"] == sph.DEFAULT_MAX_PLAYERS assert result["onli"] == sph.DEFAULT_ONLINE_MODE - assert result["levt"] == sph.DEFAULT_LEVEL_TYPE_1_19 + assert result["levt"] == sph.DEFAULT_LEVEL_TYPE_POST_1_19 def test_params_with_file(): """Tests the helper with a server.properties file""" @@ -118,7 +118,7 @@ def test_parse_mixed_params_1(): assert result["port"] == 25541 assert result["maxp"] == sph.DEFAULT_MAX_PLAYERS assert result["onli"] == sph.DEFAULT_ONLINE_MODE - assert result["levt"] == sph.DEFAULT_LEVEL_TYPE_1_19 + assert result["levt"] == sph.DEFAULT_LEVEL_TYPE_POST_1_19 def test_parse_mixed_params_2(): """Tests the helper with custom and default params""" @@ -140,7 +140,7 @@ def test_parse_mixed_params_2(): assert result["port"] == sph.DEFAULT_PORT assert result["maxp"] == 43 assert result["onli"] == "false" - assert result["levt"] == sph.DEFAULT_LEVEL_TYPE_1_19 + assert result["levt"] == sph.DEFAULT_LEVEL_TYPE_POST_1_19 def test_save_existing(): """Tests the helper save function with valid params""" @@ -159,7 +159,7 @@ def test_save_existing(): "port": 25546, "maxp": 21, "onli": "true", - "levt": sph.DEFAULT_LEVEL_TYPE_1_19 + "levt": sph.DEFAULT_LEVEL_TYPE_POST_1_19 } sph.save_properties(props_path, props)