Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Native mods: native_state_block names the block a state belongs to

Federico Tagliani989eb8a0be1a
diff --git a/crates/horizon-client/src/game/native_block_entity_draw.rs b/crates/horizon-client/src/game/native_block_entity_draw.rs
index 0748da0d1..2e5ba6e30 100644
--- a/crates/horizon-client/src/game/native_block_entity_draw.rs
+++ b/crates/horizon-client/src/game/native_block_entity_draw.rs
@@ -559,6 +559,11 @@ impl Game<'_> {
.get(key)
.map(String::as_str)
}
+ /// The block `state` is a state of: its name, as `GameLevel`'s helper of
+ /// the same name gives it for the hooks that get a state in the world.
+ pub(super) fn native_state_block(&self, state: BlockStateId) -> Option<&str> {
+ self.pack.assets.registry.state(state).map(|s| &*s.name)
+ }
}
#[cfg(test)]
diff --git a/crates/horizon-client/src/game/native_hooks.rs b/crates/horizon-client/src/game/native_hooks.rs
index 14b884c4a..e169f195d 100644
--- a/crates/horizon-client/src/game/native_hooks.rs
+++ b/crates/horizon-client/src/game/native_hooks.rs
@@ -773,7 +773,8 @@ impl GameLevel<'_> {
/// the clicked position, whether the clicked block is being replaced, the
/// item in hand) and `fluid` the fluid in the position the block would go
/// in, which a waterlogged block reads. Build the state from `state` with
- /// `native_state_with_property`. It is asked for every placer that goes
+ /// `native_state_with_property`, and know whether `state` is yours with
+ /// `native_state_block`. It is asked for every placer that goes
/// through vanilla's placement (a player's `BlockItem.place`, a
/// dispenser's `DirectionalPlaceContext`), and the answer still takes its
/// survival and entity obstruction checks. Authoritative servers only.
@@ -4140,6 +4141,14 @@ impl GameLevel<'_> {
) -> Option<BlockStateId> {
with_property(self.registry, state, key, value)
}
+ /// The block `state` is a state of (`BlockState.getBlock()`): its name,
+ /// `mymod:cable`. `native_state_for_placement` is asked for every block
+ /// placed and given the block's default state: compare this with your
+ /// block to answer only for it. None for an unknown state. API
+ /// @API:state_block@.
+ pub(super) fn native_state_block(&self, state: BlockStateId) -> Option<&str> {
+ self.registry.state(state).map(|s| &*s.name)
+ }
/// This level's own random (vanilla's `Level.random`): the stream
/// `native_level_random(dimension)` hands a server hook, for a world hook
/// that draws as a vanilla block does (`isBonemealSuccess`), so the mod's
diff --git a/tools/native-hook-fragments/state_block.json b/tools/native-hook-fragments/state_block.json
new file mode 100644
index 000000000..2ac31b1b1
--- /dev/null
+++ b/tools/native-hook-fragments/state_block.json
@@ -0,0 +1,6 @@
+{
+ "hooks": [],
+ "rust_summary": "a helper that names the block a state belongs to (`native_state_block`).",
+ "doc_summary": "`native_state_block(state)` names the block a state belongs to (`BlockState.getBlock()`), on a server hook's level and on the game: `native_state_for_placement` is asked for every block placed, so a mod compares it with its own block to answer only for it. Bundles that use it need \"hooks\": @API:state_block@.",
+ "comments": ["crates/horizon-client/src/game/native_hooks.rs"]
+}