Interface DataSlot<T>
- Type Parameters:
T
- the data slot content type
A holder for an automatically synced data value.
This is similar to vanilla property delegates,
which only work for ints.
You can use data slots to sync a single data value from the server to the client or vice versa. (A data slot can only sync data in one direction, however.) For example, you can use it to create the label of a button on the server and send it to the client.
Example
// Register a server-to-client data slot holding a double.
private static final ScreenMessageKey<Double> MY_DATA_SLOT_KEY = new ScreenMessageKey<>(
Identifier.of("my_mod", "my_data"),
Codec.DOUBLE
);
// This line should be called on both sides of the connection.
DataSlot<Double> myData = registerDataSlot(MY_DATA_SLOT_KEY, 123.456);
// The initial value of 123.456 will never be synced!
// If you want to sync a value regardless, use DataSlot.set:
if (!getWorld().isClient() && someCondition) {
myData.set(Math.PI);
}
// You can listen to data slot updates on both sides:
myData.addChangeListener((dataSlot, from, to) -> {
System.out.println("updated data: " + value);
});
- Since:
- 13.1.0
- See Also:
Property Summary
Properties
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
A listener for data slot value changes. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
addChangeListener
(DataSlot.ChangeListener<T> listener) Adds a change listener to this data slot.default T
get()
Returns the current value of the data slot.getKey()
Returns the key of the message that syncs this data slot.Returns the sync direction of this data slot.default void
Sets the current value of the data slot.Returns the current value of the data slot.
-
Method Details
-
valueProperty
ObservableProperty<T> valueProperty()Returns the current value of the data slot. The result is an observable property that can be modified and listened to.- Returns:
- the
value
property
-
get
Returns the current value of the data slot.- Returns:
- the current value of the data slot
-
set
Sets the current value of the data slot.- Parameters:
value
- the new value
-
addChangeListener
Adds a change listener to this data slot.- Parameters:
listener
- the added listener
-
getKey
ScreenMessageKey<T> getKey()Returns the key of the message that syncs this data slot. The message's content is the newT
value.- Returns:
- the key of the message that syncs this data slot
-
getNetworkDirection
NetworkDirection getNetworkDirection()Returns the sync direction of this data slot.- Returns:
- the sync direction of this data slot
-