NbtList<T extends NbtTag> constructor

NbtList<T extends NbtTag>({
  1. required String name,
  2. required List<T> children,
  3. NbtTagType nbtTagType = NbtTagType.TAG_LIST,
})

Creates a basic NbtList with given parent. nbtTagType defaults to NbtTagType.TAG_LIST and only NbtTagType.TAG_COMPOUND or NbtTagType.TAG_LIST should be used here.

Implementation

NbtList(
    {required String name,
    required List<T> children,
    NbtTagType nbtTagType = NbtTagType.TAG_LIST})
    : super(name, nbtTagType) {
  if (!(nbtTagType == NbtTagType.TAG_LIST ||
      nbtTagType == NbtTagType.TAG_COMPOUND)) {
    throw ArgumentError('nbtTagType must be TAG_LIST or TAG_COMPOUND.');
  }

  if (children.isNotEmpty) {
    childrenTagType = children.first.nbtTagType;
  }

  /// Assign this as the parent of all children.
  /// Also, filter all NbtEnd tags, as they're invalid.
  this.children = (children)
      .map((child) => child..parent = this)
      .toList()
      .where((child) => child.nbtTagType != NbtTagType.TAG_END)
      .toList();

  /// Don't allow different types of [NbtTag] inside a [NbtList].
  if (this.nbtTagType == NbtTagType.TAG_LIST &&
      this.children.where((v) => v.nbtTagType == childrenTagType).length <
          this.children.length) {
    throw ArgumentError.value(children, 'children',
        'Children of [NbtList] must all be of the same type');
  }
}