-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
8
examples/bolt_patchers/src/data/demo/functions/foo.mcfunction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
examples/bolt_patchers/src/data/demo/patchers/arrow_recipes.bolt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
examples/bolt_patchers/src/data/minecraft/recipes/arrow.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/bolt_patchers/src/data/minecraft/recipes/spectral_arrow.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/bolt_patchers/src/data/minecraft/recipes/tipped_arrow.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "minecraft:crafting_special_tippedarrow", | ||
"category": "misc" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
``` |