import 'package:libac_flutter/nbt/impl/ByteTag.dart'; import 'package:libac_flutter/nbt/impl/CompoundTag.dart'; import 'package:libac_flutter/nbt/impl/DoubleTag.dart'; import 'package:libac_flutter/nbt/impl/IntArrayTag.dart'; import 'package:libac_flutter/nbt/impl/IntTag.dart'; import 'package:libac_flutter/nbt/impl/ListTag.dart'; import 'package:libac_flutter/utils/Vector3.dart'; import '../utils/Vector2.dart'; import '../utils/uuid/UUID.dart'; class NbtUtils { static void writeBoolean(CompoundTag tag, String name, bool b) { tag.put(name, ByteTag.valueOf(b ? 1 : 0)); } static bool readBoolean(CompoundTag tag, String name) { if (tag.contains(name)) { return tag.get(name)!.asByte() == 1 ? true : false; } else { return false; } } static void writeVector2d(CompoundTag tag, String name, Vector2d pos) { ListTag posTag = ListTag(); posTag.add(DoubleTag.valueOf(pos.X)); posTag.add(DoubleTag.valueOf(pos.Z)); tag.put(name, posTag); } static Vector2d readVector2d(CompoundTag tag, String name) { if (tag.contains(name)) { ListTag lst = tag.get(name)! as ListTag; return Vector2d(X: lst.get(0).asDouble(), Z: lst.get(1).asDouble()); } else { return Vector2d.ZERO; } } static void writeVector2i(CompoundTag tag, String name, Vector2i pos) { ListTag posTag = ListTag(); posTag.add(IntTag.valueOf(pos.X)); posTag.add(IntTag.valueOf(pos.Z)); tag.put(name, posTag); } static Vector2i readVector2i(CompoundTag tag, String name) { if (tag.contains(name)) { ListTag lst = tag.get(name)! as ListTag; return Vector2i(X: lst.get(0).asInt(), Z: lst.get(1).asInt()); } else { return Vector2i.ZERO; } } static void writeVector3d(CompoundTag tag, String name, Vector3d pos) { ListTag posTag = ListTag(); posTag.add(DoubleTag.valueOf(pos.X)); posTag.add(DoubleTag.valueOf(pos.Y)); posTag.add(DoubleTag.valueOf(pos.Z)); tag.put(name, posTag); } static Vector3d readVector3d(CompoundTag tag, String name) { if (tag.contains(name)) { ListTag lst = tag.get(name)! as ListTag; return Vector3d( X: lst.get(0).asDouble(), Y: lst.get(1).asDouble(), Z: lst.get(2).asDouble()); } else { return Vector3d.ZERO; } } static void writeVector3i(CompoundTag tag, String name, Vector3i pos) { ListTag posTag = ListTag(); posTag.add(IntTag.valueOf(pos.X)); posTag.add(IntTag.valueOf(pos.Y)); posTag.add(IntTag.valueOf(pos.Z)); tag.put(name, posTag); } static Vector3i readVector3i(CompoundTag tag, String name) { if (tag.contains(name)) { ListTag lst = tag.get(name)! as ListTag; return Vector3i( X: lst.get(0).asInt(), Y: lst.get(1).asInt(), Z: lst.get(2).asInt()); } else { return Vector3i.ZERO; } } static List _msbLsbToIntArray(int msb, int lsb) { return [msb >> 32, msb, lsb >> 32, lsb]; } static List _uuidToIntArray(UUID ID) { return _msbLsbToIntArray( ID.getMostSignificantBits(), ID.getLeastSignificantBits()); } static UUID _uuidFromIntArray(List values) { return UUID((values[0] << 32 | values[1] & 4294967295), (values[2] << 32 | values[3] & 4294967295)); } static void writeUUID(CompoundTag tag, String name, UUID ID) { tag.put(name, IntArrayTag.valueOf(_uuidToIntArray(ID))); } static UUID readUUID(CompoundTag tag, String name) { if (!tag.contains(name)) return UUID.ZERO; else return _uuidFromIntArray(tag.get(name)!.asIntArray()); } }