119 lines
3.4 KiB
Dart
119 lines
3.4 KiB
Dart
import '../utils/Vector2.dart';
|
|
import '../utils/Vector3.dart';
|
|
import '../utils/uuid/NbtUUID.dart';
|
|
import 'impl/ByteTag.dart';
|
|
import 'impl/CompoundTag.dart';
|
|
import 'impl/DoubleTag.dart';
|
|
import 'impl/IntArrayTag.dart';
|
|
import 'impl/IntTag.dart';
|
|
import 'impl/ListTag.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.containsKey(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.containsKey(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.containsKey(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.containsKey(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.containsKey(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<int> _msbLsbToIntArray(int msb, int lsb) {
|
|
return [msb >> 32, msb, lsb >> 32, lsb];
|
|
}
|
|
|
|
static List<int> _uuidToIntArray(NbtUUID ID) {
|
|
return _msbLsbToIntArray(
|
|
ID.getMostSignificantBits(), ID.getLeastSignificantBits());
|
|
}
|
|
|
|
static NbtUUID _uuidFromIntArray(List<int> values) {
|
|
return NbtUUID((values[0] << 32 | values[1] & 4294967295),
|
|
(values[2] << 32 | values[3] & 4294967295));
|
|
}
|
|
|
|
static void writeUUID(CompoundTag tag, String name, NbtUUID ID) {
|
|
tag.put(name, IntArrayTag.valueOf(_uuidToIntArray(ID)));
|
|
}
|
|
|
|
static NbtUUID readUUID(CompoundTag tag, String name) {
|
|
if (!tag.containsKey(name)) {
|
|
return NbtUUID.ZERO;
|
|
} else {
|
|
return _uuidFromIntArray(tag.get(name)!.asIntArray());
|
|
}
|
|
}
|
|
}
|