From 24a9e6eff5be8005a5696c1d3860effe272c4ded Mon Sep 17 00:00:00 2001 From: Valentin Berlier Date: Sat, 28 Oct 2023 00:12:29 +0200 Subject: [PATCH] test: add patchers example --- examples/bolt_patchers/beet.yml | 7 ++ examples/bolt_patchers/patchers.py | 44 +++++++++ .../src/data/demo/functions/foo.mcfunction | 8 ++ .../src/data/demo/patchers/arrow_recipes.bolt | 17 ++++ .../src/data/minecraft/recipes/arrow.json | 21 +++++ .../minecraft/recipes/spectral_arrow.json | 18 ++++ .../data/minecraft/recipes/tipped_arrow.json | 4 + .../examples__build_bolt_patchers__0.pack.md | 94 +++++++++++++++++++ 8 files changed, 213 insertions(+) create mode 100644 examples/bolt_patchers/beet.yml create mode 100644 examples/bolt_patchers/patchers.py create mode 100644 examples/bolt_patchers/src/data/demo/functions/foo.mcfunction create mode 100644 examples/bolt_patchers/src/data/demo/patchers/arrow_recipes.bolt create mode 100644 examples/bolt_patchers/src/data/minecraft/recipes/arrow.json create mode 100644 examples/bolt_patchers/src/data/minecraft/recipes/spectral_arrow.json create mode 100644 examples/bolt_patchers/src/data/minecraft/recipes/tipped_arrow.json create mode 100644 tests/snapshots/examples__build_bolt_patchers__0.pack.md diff --git a/examples/bolt_patchers/beet.yml b/examples/bolt_patchers/beet.yml new file mode 100644 index 0000000..671ddbc --- /dev/null +++ b/examples/bolt_patchers/beet.yml @@ -0,0 +1,7 @@ +require: + - bolt.contrib.sandbox + - patchers.define_custom_resources +data_pack: + load: "src" +pipeline: + - patchers.process_files diff --git a/examples/bolt_patchers/patchers.py b/examples/bolt_patchers/patchers.py new file mode 100644 index 0000000..e88e87e --- /dev/null +++ b/examples/bolt_patchers/patchers.py @@ -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() diff --git a/examples/bolt_patchers/src/data/demo/functions/foo.mcfunction b/examples/bolt_patchers/src/data/demo/functions/foo.mcfunction new file mode 100644 index 0000000..1554236 --- /dev/null +++ b/examples/bolt_patchers/src/data/demo/functions/foo.mcfunction @@ -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. diff --git a/examples/bolt_patchers/src/data/demo/patchers/arrow_recipes.bolt b/examples/bolt_patchers/src/data/demo/patchers/arrow_recipes.bolt new file mode 100644 index 0000000..9e8e1a2 --- /dev/null +++ b/examples/bolt_patchers/src/data/demo/patchers/arrow_recipes.bolt @@ -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 diff --git a/examples/bolt_patchers/src/data/minecraft/recipes/arrow.json b/examples/bolt_patchers/src/data/minecraft/recipes/arrow.json new file mode 100644 index 0000000..7d8c54c --- /dev/null +++ b/examples/bolt_patchers/src/data/minecraft/recipes/arrow.json @@ -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 +} diff --git a/examples/bolt_patchers/src/data/minecraft/recipes/spectral_arrow.json b/examples/bolt_patchers/src/data/minecraft/recipes/spectral_arrow.json new file mode 100644 index 0000000..0d0e452 --- /dev/null +++ b/examples/bolt_patchers/src/data/minecraft/recipes/spectral_arrow.json @@ -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 +} diff --git a/examples/bolt_patchers/src/data/minecraft/recipes/tipped_arrow.json b/examples/bolt_patchers/src/data/minecraft/recipes/tipped_arrow.json new file mode 100644 index 0000000..26222e2 --- /dev/null +++ b/examples/bolt_patchers/src/data/minecraft/recipes/tipped_arrow.json @@ -0,0 +1,4 @@ +{ + "type": "minecraft:crafting_special_tippedarrow", + "category": "misc" +} diff --git a/tests/snapshots/examples__build_bolt_patchers__0.pack.md b/tests/snapshots/examples__build_bolt_patchers__0.pack.md new file mode 100644 index 0000000..1aaf50c --- /dev/null +++ b/tests/snapshots/examples__build_bolt_patchers__0.pack.md @@ -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" +} +```