Skip to content

ComposeItemState

class ComposeItemState(val containerId: Int)

Client- and server-side state holder for a ComposeItemContainerMenu's synchronized properties. The ComposeBlockEntityState equivalent for item-backed menus, keyed by containerId instead of a net.minecraft.core.BlockPos.

Unlike block entities - which can be watched by multiple players simultaneously, needing a position-keyed global registry on both sides - an item-backed menu is inherently 1:1 with a single player's currently-open session. Client and server each already have exactly one live instance of "my currently open menu" (this one, owned directly by the ComposeItemContainerMenu itself), so no equivalent client-side registry is needed here.

Parameters

Constructors

ComposeItemState

constructor(containerId: Int)

Parameters

Types

PropertyState

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

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

Properties

containerId

Parameters

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

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.