Adds some helper utilities.
This commit is contained in:
parent
5d9c201e54
commit
5357da3746
4 changed files with 412 additions and 0 deletions
87
lib/nbt/NbtUtils.dart
Normal file
87
lib/nbt/NbtUtils.dart
Normal file
|
@ -0,0 +1,87 @@
|
|||
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/IntTag.dart';
|
||||
import 'package:libac_flutter/nbt/impl/ListTag.dart';
|
||||
import 'package:libac_flutter/utils/Vector3.dart';
|
||||
|
||||
import '../utils/Vector2.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;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
import 'package:libac_flutter/nbt/Stream.dart';
|
||||
import 'package:libac_flutter/nbt/Tag.dart';
|
||||
|
||||
import 'EndTag.dart';
|
||||
|
||||
class ListTag extends Tag {
|
||||
List<Tag> value = [];
|
||||
|
||||
|
@ -42,6 +44,13 @@ class ListTag extends Tag {
|
|||
}
|
||||
}
|
||||
|
||||
Tag get(int index) {
|
||||
if (size() > index)
|
||||
return value[index];
|
||||
else
|
||||
return EndTag();
|
||||
}
|
||||
|
||||
void remove(Tag tag) {
|
||||
value.remove(tag);
|
||||
}
|
||||
|
|
151
lib/utils/Vector2.dart
Normal file
151
lib/utils/Vector2.dart
Normal file
|
@ -0,0 +1,151 @@
|
|||
class Vector2d {
|
||||
static final Vector2d ZERO = Vector2d(X: 0, Z: 0);
|
||||
double X = 0.0;
|
||||
double Z = 0.0;
|
||||
|
||||
Vector2d({this.X = 0.0, this.Z = 0.0});
|
||||
|
||||
@override
|
||||
bool operator ==(Object otherz) {
|
||||
if (otherz is Vector2d) {
|
||||
Vector2d other = otherz as Vector2d;
|
||||
return X == other.X && Z == other.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2d operator +(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X += other.X;
|
||||
n.Z += other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2d operator -(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X -= other.X;
|
||||
n.Z -= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2d operator *(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X *= other.X;
|
||||
n.Z *= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2d operator /(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X /= other.X;
|
||||
n.Z /= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
bool operator >(Vector2d other) {
|
||||
return (X > other.X) || (X > other.Z);
|
||||
}
|
||||
|
||||
bool operator <(Vector2d other) {
|
||||
return (X < other.X) || (X < other.Z);
|
||||
}
|
||||
|
||||
Vector2d Clone() {
|
||||
return Vector2d(X: X, Z: Z);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "<${X}, ${Z}>";
|
||||
}
|
||||
|
||||
bool inside(Vector2d min, Vector2d max) {
|
||||
if (min.X <= X && max.X >= X) {
|
||||
if (min.Z <= Z && max.Z >= Z) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Vector2i {
|
||||
static final Vector2i ZERO = Vector2i(X: 0, Z: 0);
|
||||
int X = 0;
|
||||
int Z = 0;
|
||||
|
||||
Vector2i({this.X = 0, this.Z = 0});
|
||||
|
||||
@override
|
||||
bool operator ==(Object otherz) {
|
||||
if (otherz is Vector2i) {
|
||||
Vector2i other = otherz as Vector2i;
|
||||
return X == other.X && Z == other.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2i operator +(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X += other.X;
|
||||
n.Z += other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2i operator -(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X -= other.X;
|
||||
n.Z -= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2i operator *(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X *= other.X;
|
||||
n.Z *= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2i operator /(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X = (n.X / other.X).round();
|
||||
n.Z = (n.Z / other.Z).round();
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
bool operator >(Vector2i other) {
|
||||
return (X > other.X) || (X > other.Z);
|
||||
}
|
||||
|
||||
bool operator <(Vector2i other) {
|
||||
return (X < other.X) || (X < other.Z);
|
||||
}
|
||||
|
||||
Vector2i Clone() {
|
||||
return Vector2i(X: X, Z: Z);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "<${X}, ${Z}>";
|
||||
}
|
||||
|
||||
bool inside(Vector2i min, Vector2i max) {
|
||||
if (min.X <= X && max.X >= X) {
|
||||
if (min.Z <= Z && max.Z >= Z) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
165
lib/utils/Vector3.dart
Normal file
165
lib/utils/Vector3.dart
Normal file
|
@ -0,0 +1,165 @@
|
|||
class Vector3d {
|
||||
static final Vector3d ZERO = Vector3d(X: 0, Y: 0, Z: 0);
|
||||
double X = 0.0;
|
||||
double Y = 0.0;
|
||||
double Z = 0.0;
|
||||
|
||||
Vector3d({this.X = 0.0, this.Y = 0.0, this.Z = 0.0});
|
||||
|
||||
@override
|
||||
bool operator ==(Object otherz) {
|
||||
if (otherz is Vector3d) {
|
||||
Vector3d other = otherz as Vector3d;
|
||||
return X == other.X && Y == other.Y && Z == other.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3d operator +(Vector3d other) {
|
||||
Vector3d n = Clone();
|
||||
n.X += other.X;
|
||||
n.Y += other.Y;
|
||||
n.Z += other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector3d operator -(Vector3d other) {
|
||||
Vector3d n = Clone();
|
||||
n.X -= other.X;
|
||||
n.Y -= other.Y;
|
||||
n.Z -= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector3d operator *(Vector3d other) {
|
||||
Vector3d n = Clone();
|
||||
n.X *= other.X;
|
||||
n.Y *= other.Y;
|
||||
n.Z *= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector3d operator /(Vector3d other) {
|
||||
Vector3d n = Clone();
|
||||
n.X /= other.X;
|
||||
n.Y /= other.Y;
|
||||
n.Z /= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
bool operator >(Vector3d other) {
|
||||
return (X > other.X) || (Y > other.Y) || (X > other.Z);
|
||||
}
|
||||
|
||||
bool operator <(Vector3d other) {
|
||||
return (X < other.X) || (Y < other.Y) || (X < other.Z);
|
||||
}
|
||||
|
||||
Vector3d Clone() {
|
||||
return Vector3d(X: X, Y: Y, Z: Z);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "<${X}, ${Y}, ${Z}>";
|
||||
}
|
||||
|
||||
bool inside(Vector3d min, Vector3d max) {
|
||||
if (min.X <= X && max.X >= X) {
|
||||
if (min.Y <= Y && max.Y >= Y) {
|
||||
if (min.Z <= Z && max.Z >= Z) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Vector3i {
|
||||
static final Vector3i ZERO = Vector3i(X: 0, Y: 0, Z: 0);
|
||||
int X = 0;
|
||||
int Y = 0;
|
||||
int Z = 0;
|
||||
|
||||
Vector3i({this.X = 0, this.Y = 0, this.Z = 0});
|
||||
|
||||
@override
|
||||
bool operator ==(Object otherz) {
|
||||
if (otherz is Vector3i) {
|
||||
Vector3i other = otherz as Vector3i;
|
||||
return X == other.X && Y == other.Y && Z == other.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3i operator +(Vector3i other) {
|
||||
Vector3i n = Clone();
|
||||
n.X += other.X;
|
||||
n.Y += other.Y;
|
||||
n.Z += other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector3i operator -(Vector3i other) {
|
||||
Vector3i n = Clone();
|
||||
n.X -= other.X;
|
||||
n.Y -= other.Y;
|
||||
n.Z -= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector3i operator *(Vector3i other) {
|
||||
Vector3i n = Clone();
|
||||
n.X *= other.X;
|
||||
n.Y *= other.Y;
|
||||
n.Z *= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector3i operator /(Vector3i other) {
|
||||
Vector3i n = Clone();
|
||||
n.X = (n.X / other.X).round();
|
||||
n.Y = (n.Y / other.Y).round();
|
||||
n.Z = (n.Z / other.Z).round();
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
bool operator >(Vector3i other) {
|
||||
return (X > other.X) || (Y > other.Y) || (X > other.Z);
|
||||
}
|
||||
|
||||
bool operator <(Vector3i other) {
|
||||
return (X < other.X) || (Y < other.Y) || (X < other.Z);
|
||||
}
|
||||
|
||||
Vector3i Clone() {
|
||||
return Vector3i(X: X, Y: Y, Z: Z);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "<${X}, ${Y}, ${Z}>";
|
||||
}
|
||||
|
||||
bool inside(Vector3i min, Vector3i max) {
|
||||
if (min.X <= X && max.X >= X) {
|
||||
if (min.Y <= Y && max.Y >= Y) {
|
||||
if (min.Z <= Z && max.Z >= Z) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue