Skip to content

Commit

Permalink
test: add patchers example
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 27, 2023
1 parent 27d5cd0 commit 24a9e6e
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/bolt_patchers/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require:
- bolt.contrib.sandbox
- patchers.define_custom_resources
data_pack:
load: "src"
pipeline:
- patchers.process_files
44 changes: 44 additions & 0 deletions examples/bolt_patchers/patchers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import ClassVar

from beet import Context, TextFile
from mecha import Mecha

from bolt import Runtime


class Patcher(TextFile):
scope: ClassVar[tuple[str, ...]] = ("patchers",)
extension: ClassVar[str] = ".bolt"


def define_custom_resources(ctx: Context):
ctx.data.extend_namespace.append(Patcher)


def process_files(ctx: Context):
mc = ctx.inject(Mecha)
for patcher_name, patcher in ctx[Patcher]:
mc.compile(
patcher,
resource_location=patcher_name,
readonly=True,
report=mc.diagnostics,
)

if mc.diagnostics.error:
return

runtime = ctx.inject(Runtime)
for patcher_name, patcher in ctx[Patcher]:
with runtime.modules.error_handler(
message="Patcher raised an exception.",
resource_location=patcher_name,
):
impl = runtime.modules[patcher].namespace
for file_instance, (_, name) in ctx.select(match=impl["targets"]).items():
result = impl["patch"](name, file_instance.ensure_deserialized())
if result is not None:
file_instance.set_content(result)

for pack in [ctx.data, *ctx.data.overlays.values()]:
pack[Patcher].clear()
8 changes: 8 additions & 0 deletions examples/bolt_patchers/src/data/demo/functions/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
say this is not compiled

value = 123
print(value)

# As long as you don't mc.compile(ctx.data) or include "mecha" in the pipeline
# you can leverage mecha's compiler and the bolt runtime for other purposes
# without affecting function files in the output pack.
17 changes: 17 additions & 0 deletions examples/bolt_patchers/src/data/demo/patchers/arrow_recipes.bolt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
targets = {
recipes: "minecraft:*arrow"
}

def patch(name, recipe):
if recipe.type == minecraft:crafting_shaped:
result = recipe.result

recipe.key."#" = {
tag: minecraft:coals
}

if result.item == minecraft:arrow:
del recipe.key["X"]
recipe.pattern[0] = "#"

result["count"] += 2 + (10 - 9) * 3
21 changes: 21 additions & 0 deletions examples/bolt_patchers/src/data/minecraft/recipes/arrow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "minecraft:crafting_shaped",
"category": "equipment",
"key": {
"#": {
"item": "minecraft:stick"
},
"X": {
"item": "minecraft:flint"
},
"Y": {
"item": "minecraft:feather"
}
},
"pattern": ["X", "#", "Y"],
"result": {
"count": 4,
"item": "minecraft:arrow"
},
"show_notification": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "minecraft:crafting_shaped",
"category": "equipment",
"key": {
"#": {
"item": "minecraft:glowstone_dust"
},
"X": {
"item": "minecraft:arrow"
}
},
"pattern": [" # ", "#X#", " # "],
"result": {
"count": 2,
"item": "minecraft:spectral_arrow"
},
"show_notification": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "minecraft:crafting_special_tippedarrow",
"category": "misc"
}
94 changes: 94 additions & 0 deletions tests/snapshots/examples__build_bolt_patchers__0.pack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Lectern snapshot

## Data pack

`@data_pack pack.mcmeta`

```json
{
"pack": {
"pack_format": 18,
"description": ""
}
}
```

### demo

`@function demo:foo`

```mcfunction
say this is not compiled
value = 123
print(value)
# As long as you don't mc.compile(ctx.data) or include "mecha" in the pipeline
# you can leverage mecha's compiler and the bolt runtime for other purposes
# without affecting function files in the output pack.
```

### minecraft

`@recipe minecraft:arrow`

```json
{
"type": "minecraft:crafting_shaped",
"category": "equipment",
"key": {
"#": {
"tag": "minecraft:coals"
},
"Y": {
"item": "minecraft:feather"
}
},
"pattern": [
"#",
"#",
"Y"
],
"result": {
"count": 9,
"item": "minecraft:arrow"
},
"show_notification": true
}
```

`@recipe minecraft:spectral_arrow`

```json
{
"type": "minecraft:crafting_shaped",
"category": "equipment",
"key": {
"#": {
"tag": "minecraft:coals"
},
"X": {
"item": "minecraft:arrow"
}
},
"pattern": [
" # ",
"#X#",
" # "
],
"result": {
"count": 7,
"item": "minecraft:spectral_arrow"
},
"show_notification": true
}
```

`@recipe minecraft:tipped_arrow`

```json
{
"type": "minecraft:crafting_special_tippedarrow",
"category": "misc"
}
```

0 comments on commit 24a9e6e

Please sign in to comment.