Skip to content

Commit

Permalink
add superflat property parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ableytner committed Mar 12, 2024
1 parent 2cd58b2 commit 356be60
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
47 changes: 28 additions & 19 deletions mcserverwrapper/src/server_properties_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@
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",
"onli",
"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)

Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions mcserverwrapper/test/test_server_properties_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand All @@ -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"""
Expand All @@ -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)
Expand Down

0 comments on commit 356be60

Please sign in to comment.