Interface DataSlot<T>

Type Parameters:
T - the data slot content type

@NonExtendable public interface DataSlot<T>
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
    Modifier and Type
    Property
    Description
    public T
    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

      default T get()
      Returns the current value of the data slot.
      Returns:
      the current value of the data slot
    • set

      default void set(T value)
      Sets the current value of the data slot.
      Parameters:
      value - the new value
    • addChangeListener

      default void addChangeListener(DataSlot.ChangeListener<T> listener)
      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 new T 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