Example #1
0
def test_tag_replace():
    p1 = DataPack()
    p1["demo:foo"] = Function(["say hello"], tags=["minecraft:load"])

    p2 = DataPack()
    p2["minecraft:load"] = FunctionTag({
        "values": ["demo:bar"],
        "replace": True
    })

    p1.merge(p2)

    assert p1 == {
        "demo": {
            Function: {
                "foo": Function(["say hello"])
            }
        },
        "minecraft": {
            FunctionTag: {
                "load": FunctionTag({
                    "values": ["demo:bar"],
                    "replace": True
                })
            }
        },
    }
Example #2
0
    def _function_tag_handler(self, function_tag: str):
        if self.ctx.meta["render_group"] != "functions":
            raise TypeError("Inline tags can only be used inside functions.")

        self.ctx.data.function_tags.merge(
            {function_tag: FunctionTag({"values": [self.ctx.meta["render_path"]]})}
        )
Example #3
0
def test_on_bind_rename():
    @dataclass
    class RenameTo:
        name: str

        def __call__(self, instance: Function, pack: DataPack, path: str):
            if path != self.name:
                pack[self.name] = instance
                raise Drop()

    pack = DataPack()
    pack["hello:world"] = Function(["say hello"],
                                   tags=["minecraft:load"],
                                   on_bind=RenameTo("hello:other"))

    assert pack == {
        "hello": {
            Function: {
                "other": Function(["say hello"])
            }
        },
        "minecraft": {
            FunctionTag: {
                "load": FunctionTag({"values": ["hello:other"]})
            }
        },
    }
Example #4
0
    def _function_tag_handler(self, context: Any, function_tag: str):
        ctx: Context = context["ctx"]

        if ctx.meta["render_group"] != "functions":
            raise TypeError("Inline tags can only be used inside functions.")

        ctx.data.function_tags.merge(
            {function_tag: FunctionTag({"values": [ctx.meta["render_path"]]})})
Example #5
0
def beet_default(ctx: Context):
    # Add the necessary boilerplate to the data pack.
    # This could also be done by merging a base data pack embedded inside the package.
    ctx.require(base_data_pack)

    # Grab the Lantern Load configuration.
    # The id defaults to the project name and the version to the project version.
    config = ctx.meta.get("lantern_load", cast(JsonDict, {}))

    id = config.get("id", ctx.project_id)
    version = config.get("version", ctx.project_version)
    dependencies = config.get("dependencies", cast(JsonDict, {}))

    # Populate the #load:load tag with the dependencies followed by the pack's
    # own load function.
    load_tag_values = [
        *({"id": f"#{dep}:load", "required": False} for dep in dependencies),
        f"{id}:load",
    ]

    ctx.data.function_tags.setdefault("load:load", FunctionTag()).add(f"#{id}:load")
    ctx.data[f"{id}:load"] = FunctionTag({"values": load_tag_values})

    # Generate and join version checks for all the dependencies.
    # Currently this only matches a major version number against a fixed value or a range.
    version_checks = " ".join(
        f"if score {dep}.major load.status matches {version}"
        for dep, version in dependencies.items()
    )

    prefix = f"execute {version_checks} run " if version_checks else ""

    # Implement the load function by first showing a message if there are any missing dependency
    # and then setting the pack's own version before calling the pack's init tag.
    ctx.data[f"{id}:load"] = Function(
        [
            *(
                f"execute unless score {dep}.major load.status matches {version} run say {id}: missing dependency {dep}=={version}"
                for dep, version in dependencies.items()
            ),
            f"{prefix}scoreboard players set {id}.major load.status {version}",
            f"execute if score {id}.major load.status matches {version} run function #{id}:init",
        ]
    )
Example #6
0
def base_data_pack(ctx: Context):
    ctx.data["minecraft:load"] = FunctionTag({"values": ["#load:_private/load"]})
    ctx.data["load:_private/load"] = FunctionTag(
        {
            "values": [
                "#load:_private/init",
                {"id": "#load:pre_load", "required": False},
                {"id": "#load:load", "required": False},
                {"id": "#load:post_load", "required": False},
            ]
        }
    )

    ctx.data["load:_private/init"] = FunctionTag({"values": ["load:_private/init"]})
    ctx.data["load:_private/init"] = Function(
        [
            "scoreboard objectives add load.status dummy",
            "scoreboard players reset * load.status",
        ]
    )
Example #7
0
def test_with_tags():
    p1 = DataPack()
    p2 = DataPack()

    p1["hello:world"] = Function(["say hello"], tags=["minecraft:load"])

    assert p1 != p2

    p2["hello"]["world"] = Function(["say hello"])
    p2["minecraft"].function_tags["load"] = FunctionTag(
        {"values": ["hello:world"]})
    assert p1 == p2
Example #8
0
def test_on_bind():
    def on_bind_callback(instance: Function, pack: DataPack, path: str):
        pack[path + "_alias"] = Function([f"function {path}"])

    pack = DataPack()
    pack["hello:world"] = Function(["say hello"],
                                   tags=["minecraft:load"],
                                   on_bind=on_bind_callback)

    assert pack.functions == {
        "hello:world": Function(["say hello"]),
        "hello:world_alias": Function(["function hello:world"]),
    }

    assert pack.function_tags == {
        "minecraft:load": FunctionTag({"values": ["hello:world"]})
    }
Example #9
0
File: demo.py Project: mcbeet/beet
def beet_default(ctx: Context):
    ctx.require("beet.contrib.inline_function_tag")

    ctx.generate(render=Function(["say {{ 6 * 7 }}"]))
    ctx.generate("foo",
                 render=Function(source_path="foo.mcfunction"),
                 message="hello")

    ctx.require(plugin1)

    with ctx.override(generate_namespace="other"):
        ctx.generate["thing"](
            render=Function(
                ['#!include "foo.mcfunction"'],
                tags=[ctx.generate(FunctionTag())],
            ),
            message="world",
        )
Example #10
0
def test_prepend_tags():
    tag1 = FunctionTag({"values": ["hello:func1"]})
    tag2 = FunctionTag({"values": ["hello:func2"]})
    tag1.prepend(tag2)

    assert tag1.data == {"values": ["hello:func2", "hello:func1"]}