Skip to content

Commit

Permalink
feat: add ctx.minecraft_version and pack_format registries
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 17, 2022
1 parent cfc7507 commit 2328b80
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 7 deletions.
14 changes: 14 additions & 0 deletions beet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"intersperse",
"normalize_string",
"snake_case",
"VersionNumber",
"split_version",
"get_import_string",
"import_from_string",
"resolve_packageable_path",
Expand Down Expand Up @@ -44,6 +46,7 @@
List,
Optional,
Protocol,
Tuple,
TypeVar,
Union,
runtime_checkable,
Expand Down Expand Up @@ -112,6 +115,17 @@ def snake_case(string: str) -> str:
return CAMEL_REGEX.sub(r"_\1", string).lower()


VersionNumber = Union[str, int, float, Tuple[Union[str, int], ...]]


def split_version(version: VersionNumber) -> Tuple[int, ...]:
if isinstance(version, (int, float)):
version = str(version)
if isinstance(version, str):
version = tuple(normalize_string(version).split("_"))
return tuple(map(int, version))


def get_import_string(obj: Any) -> str:
return f"{obj.__module__}.{obj.__qualname__}"

Expand Down
5 changes: 5 additions & 0 deletions beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"MergePolicy",
"PackOverwrite",
"PACK_COMPRESSION",
"LATEST_MINECRAFT_VERSION",
]


Expand Down Expand Up @@ -67,6 +68,9 @@

from .utils import list_extensions, list_files

LATEST_MINECRAFT_VERSION: str = "1.19"


T = TypeVar("T")
PinType = TypeVar("PinType", covariant=True)
PackFileType = TypeVar("PackFileType", bound="PackFile")
Expand Down Expand Up @@ -697,6 +701,7 @@ class Pack(MatchMixin, MergeMixin, Container[str, NamespaceType]):

namespace_type: ClassVar[Type[Namespace]]
default_name: ClassVar[str]
pack_format_registry: ClassVar[Dict[Tuple[int, ...], int]]
latest_pack_format: ClassVar[int]

def __init_subclass__(cls):
Expand Down
24 changes: 20 additions & 4 deletions beet/library/data_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@
TextFileBase,
TextFileContent,
)
from beet.core.utils import JsonDict, extra_field

from .base import Namespace, NamespacePin, NamespaceProxyDescriptor, Pack
from beet.core.utils import JsonDict, extra_field, split_version

from .base import (
LATEST_MINECRAFT_VERSION,
Namespace,
NamespacePin,
NamespaceProxyDescriptor,
Pack,
)

TagFileType = TypeVar("TagFileType", bound="TagFile")

Expand Down Expand Up @@ -264,7 +270,17 @@ class DataPack(Pack[DataPackNamespace]):
"""Class representing a data pack."""

default_name = "untitled_data_pack"
latest_pack_format = 10

pack_format_registry = {
(1, 13): 4,
(1, 14): 4,
(1, 15): 5,
(1, 16): 6,
(1, 17): 7,
(1, 18): 9,
(1, 19): 10,
}
latest_pack_format = pack_format_registry[split_version(LATEST_MINECRAFT_VERSION)]

# fmt: off
advancements: NamespaceProxyDescriptor[Advancement] = NamespaceProxyDescriptor(Advancement)
Expand Down
22 changes: 20 additions & 2 deletions beet/library/resource_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
Image = Any

from beet.core.file import BinaryFile, BinaryFileContent, JsonFile, PngFile, TextFile
from beet.core.utils import JsonDict, extra_field
from beet.core.utils import JsonDict, extra_field, split_version

from .base import (
LATEST_MINECRAFT_VERSION,
ExtraPin,
McmetaPin,
Namespace,
Expand Down Expand Up @@ -276,7 +277,24 @@ class ResourcePack(Pack[ResourcePackNamespace]):
"""Class representing a resource pack."""

default_name = "untitled_resource_pack"
latest_pack_format = 9

pack_format_registry = {
(1, 6): 1,
(1, 7): 1,
(1, 8): 1,
(1, 9): 2,
(1, 10): 2,
(1, 11): 3,
(1, 12): 3,
(1, 13): 4,
(1, 14): 4,
(1, 15): 5,
(1, 16): 6,
(1, 17): 7,
(1, 18): 8,
(1, 19): 9,
}
latest_pack_format = pack_format_registry[split_version(LATEST_MINECRAFT_VERSION)]

language_config = McmetaPin[Dict[str, JsonDict]]("language", default_factory=dict)

Expand Down
2 changes: 2 additions & 0 deletions beet/toolchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class ProjectConfig(BaseModel):
description: TextComponent = ""
author: str = ""
version: str = ""
minecraft: str = ""

directory: FileSystemPath = ""
broadcast: ListOption[FileSystemPath] = ListOption()
Expand Down Expand Up @@ -266,6 +267,7 @@ def with_defaults(self, other: "ProjectConfig") -> "ProjectConfig":
"description": self.description or other.description,
"author": self.author or other.author,
"version": self.version or other.version,
"minecraft": self.minecraft or other.minecraft,
"directory": self.directory,
"extend": self.extend,
"output": self.output,
Expand Down
1 change: 1 addition & 0 deletions beet/toolchain/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class Context:
project_author: str
project_version: str
project_root: bool
minecraft_version: str

directory: Path
output_directory: Optional[Path]
Expand Down
1 change: 1 addition & 0 deletions beet/toolchain/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def plugin(ctx: Context):
project_author=ctx.project_author,
project_version=ctx.project_version,
project_root=False,
minecraft_version=ctx.minecraft_version,
directory=ctx.directory,
output_directory=None,
meta={},
Expand Down
9 changes: 8 additions & 1 deletion beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
intersperse,
log_time,
normalize_string,
split_version,
)
from beet.core.watch import DirectoryWatcher, FileChanges
from beet.library.base import LATEST_MINECRAFT_VERSION

from .config import PackConfig, ProjectConfig, load_config, locate_config
from .context import Context, PluginSpec, ProjectCache
Expand Down Expand Up @@ -235,6 +237,7 @@ def build(self) -> Iterator[Context]:
project_author=self.config.author,
project_version=self.config.version,
project_root=self.root,
minecraft_version=self.config.minecraft or LATEST_MINECRAFT_VERSION,
directory=self.project.directory,
output_directory=self.project.output_directory,
meta=meta,
Expand Down Expand Up @@ -320,7 +323,11 @@ def bootstrap(self, ctx: Context):
PackConfig(
name=default_name,
description=pack.description or description,
pack_format=pack.pack_format,
pack_format=(
pack.pack_format_registry[split_version(self.config.minecraft)]
if self.config.minecraft
else pack.pack_format
),
zipped=pack.zipped,
compression=pack.compression,
compression_level=pack.compression_level,
Expand Down
1 change: 1 addition & 0 deletions examples/minecraft_1_18/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
minecraft: 1.18
1 change: 1 addition & 0 deletions tests/snapshots/config__config_resolution_basic__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/basic",
"broadcast": [],
"extend": [],
Expand Down
4 changes: 4 additions & 0 deletions tests/snapshots/config__config_resolution_crazy_extend__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/crazy_extend",
"broadcast": [],
"extend": [],
Expand Down Expand Up @@ -39,6 +40,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/crazy_extend",
"broadcast": [],
"extend": [],
Expand Down Expand Up @@ -83,6 +85,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/crazy_extend",
"broadcast": [],
"extend": [],
Expand Down Expand Up @@ -130,6 +133,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/crazy_extend/src",
"broadcast": [],
"extend": [],
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/config__config_resolution_empty__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/empty",
"broadcast": [],
"extend": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "foo",
"author": "foo",
"version": "foo",
"minecraft": "",
"directory": "tests/config_examples/extend_everything",
"broadcast": [],
"extend": [],
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/config__config_resolution_load_pack__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/load_pack",
"broadcast": [],
"extend": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/load_with_output",
"broadcast": [],
"extend": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/load_with_templates",
"broadcast": [],
"extend": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "The description of my project",
"author": "N/A",
"version": "1.2.3",
"minecraft": "",
"directory": "tests/config_examples/more_stuff",
"broadcast": [],
"extend": [],
Expand Down
2 changes: 2 additions & 0 deletions tests/snapshots/config__config_resolution_nested__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/nested",
"broadcast": [],
"extend": [],
Expand Down Expand Up @@ -41,6 +42,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/nested",
"broadcast": [],
"extend": [],
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/config__config_resolution_overrides__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/overrides",
"broadcast": [],
"extend": [],
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/config__config_resolution_whitelist__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/whitelist",
"broadcast": [],
"extend": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/whitelist_nested",
"broadcast": [],
"extend": [],
Expand Down Expand Up @@ -46,6 +47,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/whitelist_nested",
"broadcast": [],
"extend": [],
Expand Down Expand Up @@ -88,6 +90,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/whitelist_nested",
"broadcast": [],
"extend": [],
Expand Down Expand Up @@ -127,6 +130,7 @@
"description": "",
"author": "",
"version": "",
"minecraft": "",
"directory": "tests/config_examples/whitelist_nested",
"broadcast": [],
"extend": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 9,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 8,
"description": ""
}
}

0 comments on commit 2328b80

Please sign in to comment.