Add some helper functions, adjust visibility on variables

This commit is contained in:
zontreck 2024-05-04 23:21:40 -07:00
parent 6c83fbc371
commit 43aa3bc6d0
13 changed files with 141 additions and 65 deletions

View file

@ -147,4 +147,84 @@ abstract class Tag {
}
}
}
int asByte() {
if (this is ByteTag) {
return (this as ByteTag).value;
} else {
return 0;
}
}
List<int> asByteArray() {
if (this is ByteArrayTag) {
return (this as ByteArrayTag).value;
} else {
return [];
}
}
double asDouble() {
if (this is DoubleTag) {
return (this as DoubleTag).value;
} else {
return 0.0;
}
}
double asFloat() {
if (this is FloatTag) {
return (this as FloatTag).value;
} else {
return 0.0;
}
}
List<int> asIntArray() {
if (this is IntArrayTag) {
return (this as IntArrayTag).value;
} else {
return [];
}
}
int asInt() {
if (this is IntTag) {
return (this as IntTag).value;
} else {
return 0;
}
}
List<int> asLongArray() {
if (this is LongArrayTag) {
return (this as LongArrayTag).value;
} else {
return [];
}
}
int asLong() {
if (this is LongTag) {
return (this as LongTag).value;
} else {
return 0;
}
}
int asShort() {
if (this is ShortTag) {
return (this as ShortTag).value;
} else {
return 0;
}
}
String asString() {
if (this is StringTag) {
return (this as StringTag).value;
} else {
return "";
}
}
}

View file

@ -2,12 +2,12 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class ByteArrayTag extends Tag {
final List<int> _value = [];
final List<int> value = [];
ByteArrayTag();
ByteArrayTag._(List<int> value) {
_value.addAll(value);
this.value.addAll(value);
}
static ByteArrayTag valueOf(List<int> value) {
@ -18,14 +18,14 @@ class ByteArrayTag extends Tag {
void readValue(ByteLayer data) {
int len = data.readInt();
for (int i = 0; i < len; i++) {
_value.add(data.readByte());
value.add(data.readByte());
}
}
@override
void writeValue(ByteLayer data) {
data.writeInt(_value.length);
for (int i in _value) {
data.writeInt(value.length);
for (int i in value) {
data.writeByte(i);
}
}

View file

@ -2,10 +2,10 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class ByteTag extends Tag {
int _value = 0;
int value = 0;
ByteTag._(int value) {
_value = value;
this.value = value;
}
ByteTag();
@ -16,12 +16,12 @@ class ByteTag extends Tag {
@override
void readValue(ByteLayer data) {
_value = data.readByte();
value = data.readByte();
}
@override
void writeValue(ByteLayer data) {
data.writeByte(_value);
data.writeByte(value);
}
@override

View file

@ -2,15 +2,13 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class CompoundTag extends Tag {
late final Map<String, Tag> _value;
late final Map<String, Tag> value = {};
CompoundTag() {
_value = <String, Tag>{};
}
CompoundTag();
@override
void readValue(ByteLayer data) {
_value.clear();
value.clear();
while (true) {
Tag tag = Tag.readNamedTag(data);
@ -24,7 +22,7 @@ class CompoundTag extends Tag {
@override
void writeValue(ByteLayer data) {
Iterator<Tag> it = _value.values.iterator;
Iterator<Tag> it = value.values.iterator;
while (it.moveNext()) {
Tag tag = it.current;
@ -35,16 +33,16 @@ class CompoundTag extends Tag {
}
void put(String name, Tag tag) {
_value[name] = tag;
value[name] = tag;
}
bool contains(String name) {
return _value.containsKey(name);
return value.containsKey(name);
}
Tag? get(String name) {
if (contains(name)) {
return _value[name] as Tag;
return value[name] as Tag;
} else {
// Does not exist!
return null;
@ -52,7 +50,7 @@ class CompoundTag extends Tag {
}
void remove(String name) {
_value.remove(name);
value.remove(name);
}
@override

View file

@ -2,12 +2,12 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class DoubleTag extends Tag {
double _value = 0.0;
double value = 0.0;
DoubleTag();
DoubleTag._(double value) {
_value = value;
this.value = value;
}
static DoubleTag valueOf(double value) {
@ -16,12 +16,12 @@ class DoubleTag extends Tag {
@override
void readValue(ByteLayer data) {
_value = data.readDouble();
value = data.readDouble();
}
@override
void writeValue(ByteLayer data) {
data.writeDouble(_value);
data.writeDouble(value);
}
@override

View file

@ -2,11 +2,11 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class FloatTag extends Tag {
double _value = 0.0;
double value = 0.0;
FloatTag();
FloatTag._(double value) {
_value = value;
this.value = value;
}
static FloatTag valueOf(double value) {
@ -15,12 +15,12 @@ class FloatTag extends Tag {
@override
void readValue(ByteLayer data) {
_value = data.readFloat();
value = data.readFloat();
}
@override
void writeValue(ByteLayer data) {
data.writeFloat(_value);
data.writeFloat(value);
}
@override

View file

@ -2,25 +2,25 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class IntArrayTag extends Tag {
final List<int> _value = [];
final List<int> value = [];
IntArrayTag();
IntArrayTag._(List<int> value) {
_value.addAll(value);
this.value.addAll(value);
}
@override
void readValue(ByteLayer data) {
int count = data.readInt();
for (int i = 0; i < count; i++) {
_value.add(data.readInt());
value.add(data.readInt());
}
}
@override
void writeValue(ByteLayer data) {
data.writeInt(_value.length);
for (int i in _value) {
data.writeInt(value.length);
for (int i in value) {
data.writeInt(i);
}
}

View file

@ -2,11 +2,11 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class IntTag extends Tag {
int _value = 0;
int value = 0;
IntTag();
IntTag._(int value) {
_value = value;
this.value = value;
}
static IntTag valueOf(int value) {
@ -15,12 +15,12 @@ class IntTag extends Tag {
@override
void readValue(ByteLayer data) {
_value = data.readInt();
value = data.readInt();
}
@override
void writeValue(ByteLayer data) {
data.writeInt(_value);
data.writeInt(value);
}
@override

View file

@ -2,11 +2,9 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class ListTag extends Tag {
List<Tag> _value = [];
List<Tag> value = [];
ListTag() {
_value = List.empty(growable: true);
}
ListTag();
@override
void readValue(ByteLayer data) {
@ -24,36 +22,36 @@ class ListTag extends Tag {
void writeValue(ByteLayer data) {
TagType type = TagType.End;
if (size() > 0) {
type = _value[0].getTagType();
type = value[0].getTagType();
}
data.writeByte(type.byte);
data.writeInt(size());
for (int i = 0; i < size(); i++) {
_value[i].writeValue(data);
value[i].writeValue(data);
}
}
void add(Tag tag) {
TagType type = TagType.End;
if (size() > 0) {
type = _value[0].getTagType();
type = value[0].getTagType();
}
if (type == TagType.End || type == tag.getTagType()) {
_value.add(tag);
value.add(tag);
}
}
void remove(Tag tag) {
_value.remove(tag);
value.remove(tag);
}
void removeAt(int index) {
_value.removeAt(index);
value.removeAt(index);
}
int indexOf(Tag tag) {
return _value.indexOf(tag);
return value.indexOf(tag);
}
@override
@ -62,6 +60,6 @@ class ListTag extends Tag {
}
int size() {
return _value.length;
return value.length;
}
}

View file

@ -2,11 +2,11 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class LongArrayTag extends Tag {
final List<int> _value = [];
final List<int> value = [];
LongArrayTag();
LongArrayTag._(List<int> lst) {
_value.addAll(lst);
value.addAll(lst);
}
static LongArrayTag valueOf(List<int> value) {
@ -17,20 +17,20 @@ class LongArrayTag extends Tag {
void readValue(ByteLayer data) {
int count = data.readInt();
for (int i = 0; i < count; i++) {
_value.add(data.readLong());
value.add(data.readLong());
}
}
@override
void writeValue(ByteLayer data) {
data.writeInt(size());
for (int i in _value) {
for (int i in value) {
data.writeLong(i);
}
}
int size() {
return _value.length;
return value.length;
}
@override

View file

@ -2,11 +2,11 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class LongTag extends Tag {
int _value = 0;
int value = 0;
LongTag();
LongTag._(int value) {
_value = value;
this.value = value;
}
static LongTag valueOf(int value) {
@ -15,12 +15,12 @@ class LongTag extends Tag {
@override
void readValue(ByteLayer data) {
_value = data.readLong();
value = data.readLong();
}
@override
void writeValue(ByteLayer data) {
data.writeLong(_value);
data.writeLong(value);
}
@override

View file

@ -2,11 +2,11 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class ShortTag extends Tag {
int _value = 0;
int value = 0;
ShortTag();
ShortTag._(int value) {
_value = value;
this.value = value;
}
static ShortTag valueOf(int value) {
@ -15,12 +15,12 @@ class ShortTag extends Tag {
@override
void readValue(ByteLayer data) {
_value = data.readShort();
value = data.readShort();
}
@override
void writeValue(ByteLayer data) {
data.writeShort(_value);
data.writeShort(value);
}
@override

View file

@ -2,11 +2,11 @@ import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
class StringTag extends Tag {
String _value = "";
String value = "";
StringTag();
StringTag._(String str) {
_value = str;
value = str;
}
static StringTag valueOf(String str) {
@ -15,12 +15,12 @@ class StringTag extends Tag {
@override
void readValue(ByteLayer data) {
_value = data.readString();
value = data.readString();
}
@override
void writeValue(ByteLayer data) {
data.writeString(_value);
data.writeString(value);
}
@override