Skip to content

Commit 3c7a904

Browse files
committed
overlay + fix tests and generator arbitrary order
1 parent 1aa06e8 commit 3c7a904

File tree

17 files changed

+135
-83
lines changed

17 files changed

+135
-83
lines changed

modules/bs.block/data/bs.block/test/get/block.mcfunction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
setblock ~ ~ ~ minecraft:stone_stairs[facing=west,half=top,shape=straight,waterlogged=false]
1717
function #bs.block:get_block
1818

19-
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,waterlogged=false,facing=west,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,waterlogged=false,facing=west,]", properties: { facing: "west", half: "top", shape: "straight", waterlogged: "false" } }
19+
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,facing=west,waterlogged=false,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,facing=west,waterlogged=false,]", properties: { facing: "west", half: "top", shape: "straight", waterlogged: "false" } }
2020

2121
setblock ~ ~ ~ minecraft:campfire
2222
function #bs.block:get_block

modules/bs.block/data/bs.block/test/transform/map_type.mcfunction

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ setblock ~ ~ ~ minecraft:spruce_stairs[facing=west,half=top,shape=straight,water
1717
function #bs.block:get_block
1818

1919
function #bs.block:map_type {type:"minecraft:oak_planks",mapping_registry:"bs.shapes"}
20-
assert data storage bs:out block{ block: "minecraft:oak_stairs[shape=straight,half=top,waterlogged=false,facing=west,]" }
20+
assert data storage bs:out block{ block: "minecraft:oak_stairs[shape=straight,half=top,facing=west,waterlogged=false,]" }
2121

2222
function #bs.block:map_type {type:"minecraft:spruce_planks",mapping_registry:"bs.shapes"}
23-
assert data storage bs:out block{ block: "minecraft:spruce_stairs[shape=straight,half=top,waterlogged=false,facing=west,]" }
23+
assert data storage bs:out block{ block: "minecraft:spruce_stairs[shape=straight,half=top,facing=west,waterlogged=false,]" }

modules/bs.block/data/bs.block/test/transform/merge_properties.mcfunction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ function #bs.block:get_block
1818
setblock ~ ~ ~ minecraft:furnace[facing=north]
1919
function #bs.block:merge_properties {properties:[{name:"facing"}]}
2020

21-
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,waterlogged=false,facing=north,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,waterlogged=false,facing=north,]", properties: { facing: "north", half: "top", shape: "straight", waterlogged: "false" } }
21+
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,facing=north,waterlogged=false,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,facing=north,waterlogged=false,]", properties: { facing: "north", half: "top", shape: "straight", waterlogged: "false" } }

modules/bs.block/data/bs.block/test/transform/replace_properties.mcfunction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ setblock ~ ~ ~ minecraft:stone_stairs[facing=west,half=top,shape=straight,waterl
1717
function #bs.block:get_block
1818
function #bs.block:replace_properties {properties:[{name:"facing",value:"north"}]}
1919

20-
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,waterlogged=false,facing=north,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,waterlogged=false,facing=north,]", properties: { facing: "north", half: "top", shape: "straight", waterlogged: "false" } }
20+
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,facing=north,waterlogged=false,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,facing=north,waterlogged=false,]", properties: { facing: "north", half: "top", shape: "straight", waterlogged: "false" } }

modules/bs.block/data/bs.block/test/transform/replace_type.mcfunction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ setblock ~ ~ ~ minecraft:stone_stairs[facing=west,half=top,shape=straight,waterl
1717
function #bs.block:get_block
1818

1919
function #bs.block:replace_type {type:"minecraft:spruce_stairs"}
20-
assert data storage bs:out block{ block: "minecraft:spruce_stairs[shape=straight,half=top,waterlogged=false,facing=west,]", type: "minecraft:spruce_stairs", state: "[shape=straight,half=top,waterlogged=false,facing=west,]", properties: { facing: "west", half: "top", shape: "straight", waterlogged: "false" } }
20+
assert data storage bs:out block{ block: "minecraft:spruce_stairs[shape=straight,half=top,facing=west,waterlogged=false,]", type: "minecraft:spruce_stairs", state: "[shape=straight,half=top,facing=west,waterlogged=false,]", properties: { facing: "west", half: "top", shape: "straight", waterlogged: "false" } }
2121

2222
function #bs.block:replace_type {type:"minecraft:stone"}
2323
assert data storage bs:out block{ block: "minecraft:stone", type: "minecraft:stone" }

modules/bs.block/data/bs.block/test/transform/shift_properties.mcfunction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ setblock ~ ~ ~ minecraft:stone_stairs[facing=west,half=top,shape=straight,waterl
1717
function #bs.block:get_block
1818
function #bs.block:shift_properties {properties:[{name:"facing",by:2}]}
1919

20-
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,waterlogged=false,facing=north,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,waterlogged=false,facing=north,]", properties: { facing: "north", half: "top", shape: "straight", waterlogged: "false" } }
20+
assert data storage bs:out block{ block: "minecraft:stone_stairs[shape=straight,half=top,facing=north,waterlogged=false,]", type: "minecraft:stone_stairs", state: "[shape=straight,half=top,facing=north,waterlogged=false,]", properties: { facing: "north", half: "top", shape: "straight", waterlogged: "false" } }

modules/bs.block/gen_block.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
from collections import defaultdict
44
from typing import TYPE_CHECKING
55

6-
from beet import Context, Function, LootTable, Predicate
6+
from beet import Context, Function, LootTable, PackFile, Predicate
77

8-
from bookshelf.definitions import MC_VERSIONS
98
from bookshelf.models import Block, StateNode, StatePredicate, StateProperty, StateValue
109
from bookshelf.services import minecraft
1110

1211
if TYPE_CHECKING:
13-
from collections.abc import Sequence
14-
12+
from collections.abc import Iterable, Sequence
1513

1614
ATTR_TAGS = [
1715
("has_state", lambda b: b.group > 0),
@@ -50,41 +48,47 @@
5048
"sounds",
5149
}
5250

51+
LOOT_TABLE_INCLUDE = {
52+
"type",
53+
"item",
54+
"group",
55+
"sounds", # @deprecated sounds (removed in in 4.0.0)
56+
}
57+
5358

54-
def beet_default(ctx: Context) -> None:
55-
"""Generate files used by the bs.block module."""
56-
namespace = ctx.directory.name
57-
blocks = minecraft.get_blocks(ctx.cache, MC_VERSIONS[-1])
59+
@minecraft.generator
60+
def beet_default(ctx: Context, version: str) -> Iterable[tuple[str, PackFile]]:
61+
"""Generate files used by the module for a given version."""
62+
ns = ctx.directory.name
63+
blocks = minecraft.get_blocks(ctx.cache, version)
5864

59-
loot_table = make_block_loot_table(blocks)
60-
ctx.generate(f"{namespace}:internal/get_type", render=loot_table)
61-
loot_table = make_block_loot_table(blocks, f"{namespace}:internal")
62-
ctx.generate(f"{namespace}:internal/get_block", render=loot_table)
65+
yield f"{ns}:internal/get_type", make_block_loot_table(blocks)
66+
yield f"{ns}:internal/get_block", make_block_loot_table(blocks, f"{ns}:internal")
6367

6468
for state in {s.group: s for b in blocks for s in b.properties}.values():
65-
loot_table = make_block_state_loot_table(state, f"{namespace}:internal")
66-
ctx.generate(f"{namespace}:internal/group_{state.group}", render=loot_table)
69+
location = f"{ns}:internal/group_{state.group}"
70+
yield location, make_block_state_loot_table(state, f"{ns}:internal")
6771

6872
for name, formatter in [
6973
("types", format_types_table),
7074
("items", format_items_table),
7175
("groups", format_groups_table),
7276
]:
73-
ctx.generate(
74-
f"{namespace}:import/{name}_table",
75-
render=Function(source_path=f"{name}_table.jinja"),
76-
data=formatter(blocks),
77-
)
77+
location = f"{ns}:import/{name}_table"
78+
content = ctx.template.render(f"{name}_table.jinja", data=formatter(blocks))
79+
yield location, Function(content)
7880

7981
for name, predicate in ATTR_TAGS:
80-
tag = ctx.data.block_tags[f"{namespace}:{name}"]
81-
minecraft.update_block_tag(tag, blocks, predicate)
82+
base = ctx.data.block_tags[location := f"{ns}:{name}"]
83+
yield location, minecraft.make_block_tag(base, blocks, predicate)
8284

8385
for name, attribute in ATTR_PREDICATES:
8486
groups = defaultdict(list)
8587
for block in blocks:
8688
groups[attribute(block)].append(block)
87-
merge_attr_predicate(ctx.data.predicates[f"{namespace}:{name}"], groups)
89+
90+
base = ctx.data.predicates[location := f"{ns}:{name}"]
91+
yield location, make_attr_predicate(base, groups)
8892

8993
for name, attribute in ATTR_LOOT_TABLES:
9094
seen = set()
@@ -93,11 +97,11 @@ def beet_default(ctx: Context) -> None:
9397
groups[value := attribute(block)].append(block)
9498
if isinstance(value, StatePredicate) and value not in seen:
9599
seen.add(value)
96-
file = make_attr_state_loot_table(name, value)
97-
ctx.generate(f"{namespace}:internal/{name}_{value.group}", render=file)
100+
location = f"{ns}:internal/{name}_{value.group}"
101+
yield location, make_attr_state_loot_table(name, value)
98102

99-
file = make_attr_loot_table(name, groups, f"{namespace}:internal")
100-
ctx.generate(f"{namespace}:internal/get_{name}", render=file)
103+
location = f"{ns}:internal/get_{name}"
104+
yield location, make_attr_loot_table(name, groups, f"{ns}:internal")
101105

102106

103107
def format_types_table(blocks: Sequence[Block]) -> dict:
@@ -126,11 +130,10 @@ def format_groups_table(blocks: Sequence[Block]) -> dict:
126130

127131
def format_block_loot_entry(entry: dict, block: Block) -> dict:
128132
"""Attach block data to a loot entry."""
129-
# @deprecated sounds (removed in in 4.0.0)
130133
return {**entry, "functions": [{
131134
"function": "set_custom_data",
132135
"tag": minecraft.render_snbt(block.model_dump(
133-
include={"type", "item", "group", "sounds"},
136+
include=LOOT_TABLE_INCLUDE,
134137
)),
135138
}]}
136139

@@ -225,10 +228,10 @@ def make_attr_state_loot_table(attr: str, entry: StatePredicate) -> LootTable:
225228
})
226229

227230

228-
def merge_attr_predicate(
229-
predicate: Predicate,
231+
def make_attr_predicate(
232+
base: Predicate,
230233
groups: dict[StateValue[bool], list[Block]],
231-
) -> None:
234+
) -> Predicate:
232235
"""Create a predicate for a collection of blocks grouped by attribute."""
233236
def optimize_entry(entry: dict) -> dict | None:
234237
terms = list(filter(None, entry.get("terms", [])))
@@ -257,8 +260,8 @@ def build_node[T: bool](node: StateNode[T] | T) -> dict | None:
257260

258261
return optimize_entry({"condition":"any_of","terms":terms})
259262

260-
predicate.set_content(optimize_entry({
261-
**predicate.data,
263+
return Predicate({
264+
**base.data,
262265
"condition": "any_of",
263266
"terms": [optimize_entry({
264267
"condition": "all_of", "terms":[{
@@ -269,4 +272,4 @@ def build_node[T: bool](node: StateNode[T] | T) -> dict | None:
269272
"condition": "location_check",
270273
"predicate": {"block": {"blocks": [b.type[10:] for b in blocks]}},
271274
} for group, blocks in groups.items() if group],
272-
}))
275+
})

modules/bs.environment/data/bs.environment/function/biome/get_biome.mcfunction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
# ------------------------------------------------------------------------------------------------------------
1515

1616
data remove storage bs:out environment.get_biome
17-
loot replace entity B5-0-0-0-3 contents loot bs.environment:biome/get
17+
loot replace entity B5-0-0-0-3 contents loot bs.environment:internal/get_biome
1818
data modify storage bs:out environment.get_biome set from entity B5-0-0-0-3 item.components."minecraft:custom_data"

modules/bs.environment/data/bs.environment/function/temperature/get_temperature.mcfunction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# For more details, refer to the MPL v2.0.
1414
# ------------------------------------------------------------------------------------------------------------
1515

16-
loot replace entity B5-0-0-0-3 contents loot bs.environment:biome/get
16+
loot replace entity B5-0-0-0-3 contents loot bs.environment:internal/get_biome
1717
execute store result score $environment.celestial_angle.daytime bs.in run data get entity B5-0-0-0-3 item.components."minecraft:custom_data".temperature
1818
execute store result storage bs:ctx y double .00000001 summon minecraft:marker run function bs.environment:temperature/variation
1919
$execute store result score $environment.get_temperature bs.out run return run data get storage bs:ctx y $(scale)

modules/bs.environment/gen_environment.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,47 @@
33
from collections import defaultdict
44
from typing import TYPE_CHECKING
55

6-
from beet import Context, LootTable, Predicate
6+
from beet import Context, LootTable, PackFile, Predicate
77

8-
from bookshelf.definitions import MC_VERSIONS
98
from bookshelf.services import minecraft
109

1110
if TYPE_CHECKING:
12-
from collections.abc import Sequence
11+
from collections.abc import Iterable, Sequence
1312

1413
from bookshelf.models import Biome
1514

16-
1715
SNOW_THRESHOLD = 0.4
1816

1917

20-
def beet_default(ctx: Context) -> None:
18+
@minecraft.generator
19+
def beet_default(ctx: Context, version: str) -> Iterable[tuple[str, PackFile]]:
2120
"""Generate files used by the environment module."""
22-
namespace = ctx.directory.name
23-
biomes = minecraft.get_biomes(ctx.cache, MC_VERSIONS[-1])
24-
25-
loot_table = make_biome_loot_table(biomes)
26-
ctx.generate(f"{namespace}:biome/get", render=loot_table)
21+
ns = ctx.directory.name
22+
biomes = minecraft.get_biomes(ctx.cache, version)
2723

28-
if predicate := ctx.data.predicates.get(f"{namespace}:can_snow"):
29-
predicate.data.update(make_can_snow_predicate(biomes).data)
30-
if predicate := ctx.data.predicates.get(f"{namespace}:has_precipitation"):
31-
predicate.data.update(make_has_precipitation_predicate(biomes).data)
24+
yield f"{ns}:internal/get_biome", make_biome_loot_table(biomes)
25+
base = ctx.data.predicates[location := f"{ns}:can_snow"]
26+
yield location, make_can_snow_predicate(base, biomes)
27+
base = ctx.data.predicates[location := f"{ns}:has_precipitation"]
28+
yield location, make_has_precipitation_predicate(base, biomes)
3229

3330

34-
def make_has_precipitation_predicate(biomes: Sequence[Biome]) -> Predicate:
31+
def make_has_precipitation_predicate(
32+
base: Predicate,
33+
biomes: Sequence[Biome],
34+
) -> Predicate:
3535
"""Create a predicate to determine biomes with precipitation."""
3636
return Predicate({
37+
**base.data,
3738
"condition": "minecraft:location_check",
3839
"predicate": {"biomes": [b.type for b in biomes if b.has_precipitation]},
3940
})
4041

4142

42-
def make_can_snow_predicate(biomes: Sequence[Biome]) -> Predicate:
43+
def make_can_snow_predicate(
44+
base: Predicate,
45+
biomes: Sequence[Biome],
46+
) -> Predicate:
4347
"""Create a predicate to determine where snow can occur."""
4448
groups = defaultdict(list[str])
4549
for biome in filter(
@@ -51,6 +55,7 @@ def make_can_snow_predicate(biomes: Sequence[Biome]) -> Predicate:
5155
groups[str(y if y > 0 else -2147483648)].append(biome.type)
5256

5357
return Predicate({
58+
**base.data,
5459
"condition":"minecraft:any_of",
5560
"terms": [{
5661
"condition": "minecraft:location_check",

0 commit comments

Comments
 (0)