Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logic to traverse the dimensions directory tree #251

Merged
merged 9 commits into from
Aug 16, 2023
32 changes: 31 additions & 1 deletion amulet/level/formats/anvil_forge_world.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
import glob
import os

from amulet_nbt import load as load_nbt
Expand All @@ -14,6 +15,10 @@ class AnvilForgeFormat(AnvilFormat):
Currently there is no extra logic here but this should extend the :class:`AnvilFormat` class to support Forge worlds.
"""

def __init__(self, path: str):
super().__init__(path)
self._register_modded_dimensions()

@staticmethod
def is_valid(path: str) -> bool:
if not check_all_exist(path, "level.dat"):
Expand All @@ -24,7 +29,9 @@ def is_valid(path: str) -> bool:
except:
return False

return "Data" in level_dat_root and "FML" in level_dat_root
return "Data" in level_dat_root and (
"FML" in level_dat_root or "fml" in level_dat_root
)

@property
def game_version_string(self) -> str:
Expand All @@ -33,5 +40,28 @@ def game_version_string(self) -> str:
except Exception:
return f"Java Forge Unknown Version"

def _register_modded_dimensions(self):
for region_path in glob.glob(
os.path.join(glob.escape(self.path), "dimensions", "*", "**", "region"),
recursive=True,
):
dim_path = os.path.dirname(region_path)
child_dir_names = set(
map(
os.path.basename,
filter(
os.path.isdir,
map(lambda d: os.path.join(dim_path, d), os.listdir(dim_path)),
),
)
)
gentlegiantJGC marked this conversation as resolved.
Show resolved Hide resolved
if not {"data", "entities", "poi", "region"}.issubset(child_dir_names):
continue
rel_dim_path = os.path.relpath(dim_path, self.path)
_, dimension, *base_name = rel_dim_path.split(os.sep)

dimension_name = f"{dimension}:{'/'.join(base_name)}"
self._register_dimension(os.path.dirname(rel_dim_path), dimension_name)


export = AnvilForgeFormat