Parts

Use a part to attach behavior to a specific registry type rather than listen to every event of that kind. You attach parts once, at startup, in Hooks::register. For example, this BlockPart gives minecraft:stone a comparator output of 15:

From Sunrise's modding bookead8a1e

rust
use mod_api::{
    AnalogOutputEvent, BlockPart, HasAnalogOutputEvent, Hooks, LevelHost, Registrar,
    RegistrationContext,
};

#[derive(Default)]
struct MyMod;
struct Gauge;

#[mod_api::sunrise_part]
impl BlockPart for Gauge {
    fn has_analog_output(&self, _: &dyn LevelHost, event: &mut HasAnalogOutputEvent) {
        event.output = true;
    }
    fn analog_output(&self, _: &dyn LevelHost, event: &mut AnalogOutputEvent) {
        event.output = 15;
    }
}

#[mod_api::sunrise_mod]
impl Hooks for MyMod {
    fn register(&self, r: &mut dyn Registrar, _: &RegistrationContext) {
        let stone = r.block_key("minecraft:stone");
        r.block_part(stone, Gauge);
    }
}

Part types

TraitTarget
BlockPartA block type, your own or vanilla's
BlockEntityPartA block entity type, with block behavior through BlockPart
ItemPartAn item type, your own or vanilla's
MenuPartA mod menu type
FluidPartA mod world fluid type
EntityPartAn entity type, your own or vanilla's; registered through Registrar::entity_part; on wasm only its general_entity_tick crosses
  • Annotate each part trait implementation with #[mod_api::sunrise_part]. For scoped hooks, the attribute subscribes the part only to the methods you implement.
  • BlockEntityPart extends BlockPart: provide a separate BlockPart implementation and annotate both implementations.
  • Parts implement Send + Sync + 'static.
  • In has_analog_output, answer from the block state alone. In analog_output, do not change the world. The game clamps the comparator signal to the range 0 to 15.

Campfire smoke sources

BlockPart::smoke_source answers whether its block makes a campfire above it a signal fire. Return Some(true) to enable signal smoke, Some(false) to refuse it, or None to keep vanilla's hay-bale check. It runs on the server and during client placement prediction. Its LevelSide supplies a read-only level on the asking side: answer from the supplied state and level, with no authoritative writes. Parts attached by tag use that side's tag view.

Chapters

Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.