Skip to content

Commit b88533b

Browse files
Sort versions by newest (#19)
* Sort versions by newest When searching for a compatible version we should consider newer versions first. If two versions have the same block version we should always pick the newer version. It should also make it a little quicker for newer versions. * Add import * Reformat
1 parent 6c28992 commit b88533b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/amulet/game/game.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import logging
1111

1212
from amulet.core.version import VersionNumber
13+
from amulet.utils.cast import dynamic_cast
1314

1415

1516
if TYPE_CHECKING:
@@ -28,9 +29,25 @@ def _get_versions() -> dict[str, list[GameVersion]]:
2829
with _lock:
2930
if _versions is None:
3031
_log.debug("Loading Minecraft translations")
32+
from .abc import GameVersion
33+
3134
pkl_path = os.path.join(os.path.dirname(__file__), "versions.pkl.gz")
3235
with open(pkl_path, "rb") as pkl:
33-
_versions = pickle.loads(gzip.decompress(pkl.read()))
36+
versions: object = pickle.loads(gzip.decompress(pkl.read()))
37+
38+
def version_sort(v: GameVersion) -> VersionNumber:
39+
return v.min_version
40+
41+
sorted_versions: dict[str, list[GameVersion]] = {}
42+
for platform, version_list in dynamic_cast(versions, dict).items():
43+
if isinstance(platform, str) and isinstance(version_list, list):
44+
sorted_versions[platform] = sorted(
45+
[v for v in version_list if isinstance(v, GameVersion)],
46+
key=version_sort,
47+
reverse=True,
48+
)
49+
_versions = sorted_versions
50+
3451
_log.debug("Finished loading Minecraft translations")
3552

3653
assert _versions is not None

0 commit comments

Comments
 (0)