getNbt method

Map<String, dynamic> getNbt([
  1. bool useId = true
])

Implementation

Map<String, dynamic> getNbt([bool useId = true]) {
  var nbt = Map<String, dynamic>.from(this.nbt ?? {});

  void addBoolNbt(bool? value, String path) {
    if (value != null) nbt[path] = value ? 1 : 0;
  }

  if (name != null) nbt['CustomName'] = name!.toJson();
  addBoolNbt(invulnerable, 'Invulnerable');
  addBoolNbt(silent, 'Silent');
  addBoolNbt(small, 'Small');
  addBoolNbt(glowing, 'Glowing');
  addBoolNbt(nameVisible, 'CustomNameVisible');
  addBoolNbt(persistent, 'PersistenceRequired');
  addBoolNbt(noAI, 'NoAI');
  if (gravity != null) addBoolNbt(!gravity!, 'NoGravity');
  if (tags != null && tags!.isNotEmpty) {
    nbt['Tags'] = Tag.prefix != null
        ? tags!
            .map((String t) => t.contains(Tag.prefix!) ? t : Tag.prefix! + t)
            .toList()
        : tags;
  }
  if (effects != null && effects!.isNotEmpty) {
    nbt['ActiveEffects'] = effects!.map((effect) => effect.getMap()).toList();
  }
  if (passengers != null) {
    nbt['Passengers'] = passengers!.map((pass) => pass.getNbt()).toList();
  }
  if (fire != null && fire! > 0) nbt['Fire'] = fire;
  if (rotation != null) nbt['Rotation'] = [rotation!.x, rotation!.y];
  if (age != null) nbt['Age'] = age;

  if (useId) nbt['id'] = type.type;
  return nbt;
}