Skip to content

ComposeBlockEntityState

class ComposeBlockEntityState(val pos: BlockPos)

Client-side state holder for a block entity's synchronized properties.

Each property is wrapped in a Compose MutableState, allowing composables to react to changes automatically through recomposition.

Parameters

  • pos: The block position of the block entity.

Constructors

ComposeBlockEntityState

constructor(pos: BlockPos)

Parameters

  • pos: The block position of the block entity.

Types

PropertyState

class PropertyState<T>(
    state: ComposeBlockEntityState, 
    propertyName: String, 
    mutableState: MutableState<T>
) : MutableState<T> 

A MutableState delegate that forwards writes to ComposeBlockEntityState.sendUpdatedProperty, so setting value from a composable both updates local state and pushes the change to the server.

Properties

pos

val pos: BlockPos

Parameters

  • pos: The block position of the block entity.

propertySerializers

val propertySerializers: MutableMap<String, KSerializer<out Any>>

Serializers used to encode/decode each observed property, keyed by property name.

propertyStates

val propertyStates: MutableMap<String, MutableState<Any?>>

Map of property names to their Compose state values

Functions

clear

fun clear()

Clears all tracked properties.

Useful when the block entity is unloaded or the state is no longer needed.

getAllProperties

Gets all currently tracked properties.

Return

A map of property names to their current values.

getProperty

fun getProperty(propertyName: String): Any?

Gets the current value of a property.

Return

The property value, or null if not tracked.

Parameters

  • propertyName: The name of the property.

getPropertyTyped

fun <T : Any> getPropertyTyped(propertyName: String): T?

Gets the current value of a property with type casting.

Return

The property value cast to T, or null if not found/wrong type.

Parameters

  • propertyName: The name of the property.

  • T: The expected type.

observeProperty

fun <T> observeProperty(
    propertyName: String, 
    serializer: KSerializer<T>, 
    initialValue: T? = null
): MutableState<T?>

Gets or creates a Compose state for a property with a specific type.

Return

A MutableState of type T that can be observed in composables.

Parameters

  • propertyName: The name of the property.

  • initialValue: The initial value (optional, defaults to null).

  • T: The expected type of the property.

sendUpdatedProperty

fun <T> sendUpdatedProperty(propertyName: String, value: T)

Updates a property value and sends the change to the server.

This method should be called when a client-side interaction changes a property.

Parameters

  • propertyName: The name of the property.

  • value: The new value.

updateProperty

Updates a property value from a network packet.

If the property doesn't exist yet, it will be created.

Parameters

  • propertyName: The name of the property.

  • value: The new serialized value from the network packet.