cls#

block_functions#

class BlockFunctions(block_id: str, namespace: str | None = None)[source]#

Bases: ResourceFolder[Function]

The mcfunctions generated for one custom block, under “{ns}:custom_blocks/{id}/”.

>>> funcs = BlockFunctions("machine_block")
>>> funcs.place_main
'your_namespace:custom_blocks/machine_block/place_main'
>>> funcs.place_secondary
'your_namespace:custom_blocks/machine_block/place_secondary'
>>> funcs.destroy
'your_namespace:custom_blocks/machine_block/destroy'
>>> funcs.second
'your_namespace:custom_blocks/machine_block/second'
>>> funcs["any_custom_function"]
'your_namespace:custom_blocks/machine_block/any_custom_function'
>>> funcs.folder
'your_namespace:custom_blocks/machine_block'
property place_main: Resource[Function][source]#

Ran at the block position when the custom block is placed.

property place_secondary: Resource[Function][source]#

Ran as the summoned item_display/item_frame (this is where Block.on_place is appended).

property place_check: Resource[Function][source]#

Checks that the placed block is really this custom block before calling place_main.

property search: Resource[Function][source]#

Searches the placed entity for item frame based custom blocks.

property get_facing: Resource[Function][source]#

Computes the facing of the custom block from the player orientation.

property destroy: Resource[Function][source]#

Ran when the custom block is destroyed.

property replace_item: Resource[Function][source]#

Replaces the dropped item with the custom block item.

property update_seed_model: Resource[Function][source]#

Updates the item model of a growing seed to match its current stage.

property is_fully_grown: Resource[Function][source]#

Ran when a growing seed reaches its last stage.

property tick: Resource[Function][source]#

Ran every tick as the custom block entity.

property tick_2: Resource[Function][source]#

Ran every 2 ticks as the custom block entity.

property second: Resource[Function][source]#

Ran every second as the custom block entity.

property second_5: Resource[Function][source]#

Ran every 5 seconds as the custom block entity.

property minute: Resource[Function][source]#

Ran every minute as the custom block entity.

resource#

resolve_pack(
file_type: type[NamespaceFile],
) → DataPack | ResourcePack[source]#

Get the pack (Mem.ctx.data or Mem.ctx.assets) owning the given file type.

Parameters:

file_type – The beet file type, ex: LootTable, Function, Model

Returns:

The pack owning that file type

Return type:

DataPack | ResourcePack

Raises:

KeyError – If the file type belongs to neither pack.

existing_container(
file_type: type[FileT],
namespace: str,
) → NamespaceContainer | None[source]#

Get the container holding that file type in that namespace, or None if the namespace is absent.

Read-only counterpart of Resource.container: it never creates anything, and resolves the pack only once (going through Resource.pack then Resource.container resolves it twice).

Note: the namespace is looked up by iterating the pack, because both namespace in pack and pack.get(namespace) route through Pack.missing(), which creates the namespace.

Parameters:
  • file_type – The beet file type, ex: LootTable, Function, Model

  • namespace – The namespace to look up, ex: “your_namespace”

Returns:

The container, or None if the namespace isn’t in the pack

Return type:

NamespaceContainer[FileT] | None

class Resource(
file_type: type[FileT],
location: str,
namespace: str | None = None,
)[source]#

Bases: str, Generic

A Minecraft resource location (“namespace:relative/path”) that knows its beet file type.

A Resource is a str holding the full resource location, so it compares equal to the plain string, hashes the same, works as a dict key, formats in f-strings, and can be passed to every writer (write_function, write_tag, write_advancement, ctx.data[…], …) without conversion. On top of that, it gives access to the underlying beet file through .obj, .get(), …

Building resource locations

>>> from beet import Function, LootTable
>>> res = Resource(LootTable, "i/multimeter")
>>> res
'your_namespace:i/multimeter'
>>> res.namespace, res.relative_path
('your_namespace', 'i/multimeter')
>>> res.file_type is LootTable
True

It behaves exactly like a str

>>> res == "your_namespace:i/multimeter"
True
>>> isinstance(res, str)
True
>>> f"loot spawn ~ ~ ~ loot {res}"
'loot spawn ~ ~ ~ loot your_namespace:i/multimeter'
>>> {res: 1}["your_namespace:i/multimeter"]
1

The file extension is stripped

>>> Resource(Function, "custom_blocks/machine_block/place_main.mcfunction")
'your_namespace:custom_blocks/machine_block/place_main'

A foreign namespace can be given either way, and a location already carrying one wins

>>> Resource(LootTable, "base/tin_raw", namespace="mechanization")
'mechanization:base/tin_raw'
>>> Resource(LootTable, "mechanization:base/tin_raw").namespace
'mechanization'
>>> Resource(LootTable, "mechanization:base/tin_raw", namespace="ignored").namespace
'mechanization'

Copies survive (mandatory: components dicts get deep-copied during serialization)

>>> import copy
>>> copied = copy.deepcopy(res)
>>> copied == res and copied.file_type is LootTable
True

Out of a build, pack accessors run against an empty placeholder context

>>> res.exists()
False
>>> res.get() is None
True
namespace: str[source]#

The namespace of the resource, e.g. “stewbeet.core.cls.resource”.

relative_path: str[source]#

The path inside the namespace (the beet NamespaceContainer key), ex: “i/multimeter”.

file_type: type[FileT][source]#

The beet file type this resource points to, ex: LootTable.

property pack: DataPack | ResourcePack[source]#

The pack owning this resource (Mem.ctx.data or Mem.ctx.assets).

property proxy: NamespaceProxy[FileT][source]#

The beet NamespaceProxy for this file type (keys are full “namespace:path” locations).

property container: NamespaceContainer[FileT][source]#

The beet NamespaceContainer for this namespace (keys are relative paths).

property obj: FileT[source]#

The beet file at this location, ex: a LootTable or a Function.

Raises:

KeyError – If the file is not in the pack yet, where .get() returns None instead.

>>> from stewbeet import Function
>>> res = Resource(Function, "custom_blocks/machine_block/place_main")
>>> res.get() is None
True
>>> res.obj = Function("say Hello")
>>> res.obj
Function('say Hello')
>>> res.obj == Function("say Hello")
True
get(default: FileT | None = None) → FileT | None[source]#

Get the beet file at this location, or default when it doesn’t exist yet.

Parameters:

default – The value to return when the file doesn’t exist

Returns:

The beet file, or default

Return type:

FileT | None

exists() → bool[source]#

Whether the file already exists in the pack.

Returns:

True if the file exists

Return type:

bool

setdefault(default: FileT | None = None) → FileT[source]#

Get the existing beet file, or insert and return default (or a new empty file).

Parameters:

default – The file to insert when nothing exists yet

Returns:

The existing or newly inserted beet file

Return type:

FileT

write(value: FileT) → FileT[source]#

Write the beet file at this location, overwriting anything already there.

Parameters:

value – The beet file to write, ex: LootTable({…})

Returns:

The written file

Return type:

FileT

delete() → None[source]#

Remove the file from the pack if it exists (no-op otherwise).

child(name: str) → Resource[FileT][source]#

Get a resource nested under this one.

Parameters:

name – The child name, ex: “place_main”

Returns:

The nested resource

Return type:

Resource[FileT]

>>> from beet import Function
>>> Resource(Function, "custom_blocks/machine_block").child("place_main")
'your_namespace:custom_blocks/machine_block/place_main'
suffixed(suffix: str) → Resource[FileT][source]#

Get a resource with a suffix appended to its path, ex: the “_on” variant of a model.

Parameters:

suffix – The suffix to append, ex: “_on”

Returns:

The suffixed resource

Return type:

Resource[FileT]

>>> from beet import Model
>>> Resource(Model, "item/battery").suffixed("_on")
'your_namespace:item/battery_on'
>>> Resource(Model, "item/battery").suffixed("")
'your_namespace:item/battery'
sibling(name: str) → Resource[FileT][source]#

Get a resource in the same folder as this one.

Parameters:

name – The sibling name, ex: “destroy”

Returns:

The sibling resource

Return type:

Resource[FileT]

>>> from beet import Function
>>> Resource(Function, "custom_blocks/machine_block/place_main").sibling("destroy")
'your_namespace:custom_blocks/machine_block/destroy'

resource_folder#

class ResourceFolder(
file_type: type[FileT],
folder: str,
namespace: str | None = None,
)[source]#

Bases: Generic

A lazy accessor over a folder of resources sharing the same beet file type.

>>> from beet import Function
>>> folder = ResourceFolder(Function, "custom_blocks/machine_block")
>>> folder.folder
'your_namespace:custom_blocks/machine_block'
>>> folder["place_main"]
'your_namespace:custom_blocks/machine_block/place_main'
>>> folder
ResourceFolder('your_namespace:custom_blocks/machine_block/*')
file_type: type[FileT][source]#
namespace: str[source]#
property folder: Resource[FileT][source]#

The folder itself, as a resource location (no trailing slash).