Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/mecha/examples/basic_26_3/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data_pack:
load: "src"
pipeline:
- mecha

meta:
mecha:
multiline: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
compute default integer example:integer
compute default integer {type:constant, value:256}
compute default float example:integer
compute default float {type:constant, value:143.221}


execute positioned ~ ~3 ~ run place feature {
"type": "minecraft:tree",
"below_trunk_provider": "minecraft:soil_beneath_tree",
"decorators": [],
"foliage_placer": {
"type": "minecraft:acacia_foliage_placer",
"offset": 0,
"radius": 2
},
"foliage_provider": {
"id": "minecraft:acacia_leaves",
"properties": {
"distance": "7",
"persistent": "false",
"waterlogged": "false"
}
},
"ignore_vines": 1,
"minimum_size": {
"type": "minecraft:two_layers_feature_size",
"upper_size": 2
},
"trunk_placer": {
"type": "minecraft:forking_trunk_placer",
"base_height": 5,
"height_rand_a": 2,
"height_rand_b": 2
},
"trunk_provider": {
"id": "minecraft:acacia_log",
"properties": {
"axis": "y"
}
}
}

swing @s mainhand stab 500t
14 changes: 14 additions & 0 deletions packages/mecha/src/mecha/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,20 @@ class AstItemSlots(AstOption):
}


@dataclass(frozen=True, slots=True)
class AstSwingAnimation(AstOption):
"""Ast swing animation node."""

parser = "item_slot"
options = (
{
"none",
"stab",
"whack",
}
)


@dataclass(frozen=True, slots=True)
class AstRange(AstNode):
"""Ast range node."""
Expand Down
39 changes: 25 additions & 14 deletions packages/mecha/src/mecha/contrib/nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

from dataclasses import dataclass, field, replace
from importlib.resources import files
from typing import Generator, List, cast
from typing import Generator, List, Literal, cast

from beet import Context, Function
from beet import Generator as BeetGenerator
from beet import configurable
from beet.core.utils import required_field
from beet.core.utils import required_field, split_version
from pydantic import BaseModel
from tokenstream import InvalidSyntax, TokenStream, set_location

Expand Down Expand Up @@ -52,7 +52,13 @@ def nesting(ctx: Context, opts: NestingOptions):

mc.spec.multiline = True

commands_json = files("mecha.resources").joinpath("nesting.json").read_text()
if split_version(ctx.minecraft_version) >= (26, 3):
commands_json = files("mecha.resources").joinpath("nesting_after_26_3.json").read_text()
block_source_type = "source"
else:
commands_json = files("mecha.resources").joinpath("nesting.json").read_text()
block_source_type = "sourcePos"

mc.spec.add_commands(CommandTree.model_validate_json(commands_json))

mc.spec.parsers["nested_root"] = parse_nested_root
Expand All @@ -66,6 +72,7 @@ def nesting(ctx: Context, opts: NestingOptions):
generate_macro_template=opts.generate_macro,
generate_return_template=opts.generate_return,
nested_location_resolver=ctx.inject(NestedLocationResolver),
block_source_type=block_source_type,
)
)

Expand Down Expand Up @@ -117,20 +124,24 @@ class NestedCommandsTransformer(MutatingReducer):
generate_return_template: str = required_field()
nested_location_resolver: NestedLocationResolver = required_field()

identifier_map: dict[str, str] = field(
default_factory=lambda: {
identifier_map: dict[str, str] = field(init=False)
block_source_type: Literal["source", "sourcePos"] = field(default="sourcePos")

def __post_init__(self):
self.identifier_map = {
"function:name:commands": "function:name",
"function:name:arguments:commands": "function:name:arguments",
"function:name:with:block:sourcePos:commands": "function:name:with:block:sourcePos",
"function:name:with:block:sourcePos:path:commands": "function:name:with:block:sourcePos:path",
f"function:name:with:block:{self.block_source_type}:commands": f"function:name:with:block:{self.block_source_type}",
f"function:name:with:block:{self.block_source_type}:path:commands": f"function:name:with:block:{self.block_source_type}:path",
"function:name:with:entity:source:commands": "function:name:with:entity:source",
"function:name:with:entity:source:path:commands": "function:name:with:entity:source:path",
"function:name:with:storage:source:commands": "function:name:with:storage:source",
"function:name:with:storage:source:path:commands": "function:name:with:storage:source:path",
"append:function:name:commands": "function:name",
"prepend:function:name:commands": "function:name",
}
)
return super().__post_init__()


def emit_function(self, path: str, root: AstRoot):
"""Helper method for emitting nested commands into a separate function."""
Expand Down Expand Up @@ -212,10 +223,10 @@ def nesting_execute_commands(self, node: AstCommand):
arguments=AstChildren([subcommand]),
)

@rule(AstCommand, identifier="with:block:sourcePos:commands")
@rule(AstCommand, identifier="with:block:sourcePos")
@rule(AstCommand, identifier="with:block:sourcePos:path:commands")
@rule(AstCommand, identifier="with:block:sourcePos:path")
@rule(AstCommand, identifier=f"with:block:{block_source_type}:commands")
@rule(AstCommand, identifier=f"with:block:{block_source_type}")
@rule(AstCommand, identifier=f"with:block:{block_source_type}:path:commands")
@rule(AstCommand, identifier=f"with:block:{block_source_type}:path")
@rule(AstCommand, identifier="with:entity:source:commands")
@rule(AstCommand, identifier="with:entity:source")
@rule(AstCommand, identifier="with:entity:source:path:commands")
Expand Down Expand Up @@ -330,8 +341,8 @@ def handle_function(
if top_level:
if node.identifier in (
"function:name:arguments:commands",
"function:name:with:block:sourcePos:commands",
"function:name:with:block:sourcePos:path:commands",
f"function:name:with:block:{self.block_source_type}:commands",
f"function:name:with:block:{self.block_source_type}:path:commands",
"function:name:with:entity:source:commands",
"function:name:with:entity:source:path:commands",
"function:name:with:storage:source:commands",
Expand Down
9 changes: 9 additions & 0 deletions packages/mecha/src/mecha/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
AstShriekParticleParameters,
AstSortOrder,
AstString,
AstSwingAnimation,
AstSwizzle,
AstTeam,
AstTemplateMirror,
Expand Down Expand Up @@ -575,6 +576,14 @@ def get_default_parsers() -> Dict[str, Parser]:
"command:argument:minecraft:uuid": delegate("uuid"),
"command:argument:minecraft:vec2": delegate("vec2"),
"command:argument:minecraft:vec3": delegate("vec3"),
"command:argument:minecraft:context_float_provider": MultilineParser(delegate("resource_location_or_nbt")),
"command:argument:minecraft:context_int_provider": MultilineParser(delegate("resource_location_or_nbt")),
"command:argument:minecraft:feature": MultilineParser(delegate("resource_location_or_nbt")),
"command:argument:minecraft:swing_animation": BasicLiteralParser(AstSwingAnimation),
"command:argument:minecraft:slot_source": AlternativeParser([
delegate("item_slot"),
MultilineParser(delegate("resource_location_or_nbt"))
]),
################################################################################
# Macro line
################################################################################
Expand Down
Loading
Loading