Interface AttachmentSyncHandler<T>


public interface AttachmentSyncHandler<T>
Manages how data attachments are written (on the server) and read (on the client) from packets.

Sync is handled automatically in the following cases:

For other cases such as modifications to mutable synced attachments, IAttachmentHolder.syncData(AttachmentType) can be called to trigger syncing.

  • Method Details

    • sendToPlayer

      default boolean sendToPlayer(IAttachmentHolder holder, ServerPlayer to)
      Decides whether data should be sent to some player that can see the holder.

      By default, all players that can see the holder are sent the data. A typical use case for this method is to only send player-specific data to that player.

      The returned value should be consistent for a given holder and player.

      Parameters:
      holder - the holder for the attachment, can be cast if the subtype is known
      to - the player that might receive the data
      Returns:
      true to send data to the player, false otherwise
    • write

      void write(RegistryFriendlyByteBuf buf, T attachment, boolean initialSync)
      Writes attachment data to a buffer.

      If initialSync is true, the data should be written in full because the client does not have any previous data.

      If initialSync is false, the client already received a previous version of the data. In this case, this method is only called once for the attachment, and the resulting data is broadcast to all relevant players.

      If nothing is written to the buffer, nothing is sent to the client at all, and read(net.neoforged.neoforge.attachment.IAttachmentHolder, net.minecraft.network.RegistryFriendlyByteBuf, T) will not be called on the client side.

    • read

      @Nullable T read(IAttachmentHolder holder, RegistryFriendlyByteBuf buf, @Nullable T previousValue)
      Reads attachment data on the client side.
      Parameters:
      holder - the attachment holder, can be cast if the subtype is known
      previousValue - the previous value of the attachment, or null if there was no previous value
      Returns:
      the new value of the attachment, or null if the attachment should be removed