Adds to the Tag a parent tag reference.

This commit is contained in:
zontreck 2024-07-22 04:49:57 -07:00
parent 7ec3ea33d1
commit 2ab89defeb
4 changed files with 44 additions and 15 deletions

View file

@ -155,6 +155,19 @@ abstract class Tag {
TagType getTagType();
TagType _parentTagType = TagType.End;
Tag? _parentTag;
bool get hasParent => _parentTag != null;
void updateParent(Tag? tag) {
if (tag == null) {
_parentTag = null;
setParentTagType(TagType.End);
} else {
_parentTag = tag;
setParentTagType(tag.getTagType());
}
}
Tag? get getParent => _parentTag;
TagType get parentTagType => _parentTagType;
void setParentTagType(TagType type) {

View file

@ -47,7 +47,7 @@ class CompoundTag extends Tag implements Map<String, Tag> {
void put(String name, Tag tag) {
value[name] = tag;
tag.setKey(name);
tag.setParentTagType(TagType.Compound);
tag.updateParent(this);
}
Tag? get(String name) {
@ -114,19 +114,23 @@ class CompoundTag extends Tag implements Map<String, Tag> {
@override
void operator []=(String key, Tag value) {
this.value[key] = value;
value.setParentTagType(TagType.Compound);
value.updateParent(this);
}
@override
void addAll(Map<String, Tag> other) {
value.addAll(other);
updateParent();
for (var entries in other.entries) {
entries.value.updateParent(this);
}
}
@override
void addEntries(Iterable<MapEntry<String, Tag>> newEntries) {
value.addEntries(newEntries);
updateParent();
for (var entries in newEntries) {
entries.value.updateParent(this);
}
}
@override
@ -140,15 +144,10 @@ class CompoundTag extends Tag implements Map<String, Tag> {
value.clear();
}
void updateParent() {
for (MapEntry<String, Tag> Entries in value.entries) {
Entries.value.setParentTagType(TagType.Compound);
}
}
void unsetParent() {
for (MapEntry<String, Tag> Entries in value.entries) {
Entries.value.setParentTagType(TagType.End);
Entries.value.updateParent(null);
}
}
@ -191,28 +190,30 @@ class CompoundTag extends Tag implements Map<String, Tag> {
@override
Tag putIfAbsent(String key, Tag Function() ifAbsent) {
Tag X = this.value.putIfAbsent(key, ifAbsent);
updateParent();
X.updateParent(this);
return X;
}
@override
Tag? remove(Object? key) {
if (key is Tag && value.containsKey(key))
(key).setParentTagType(TagType.End);
if (key is Tag && value.containsKey(key)) (key).updateParent(null);
return value.remove(key);
}
/// DO NOT USE, Parent will not be updated on child tags when this function is used.
@override
void removeWhere(bool Function(String key, Tag value) test) {
value.removeWhere(test);
}
/// DO NOT USE
@override
Tag update(String key, Tag Function(Tag value) update,
{Tag Function()? ifAbsent}) {
return value.update(key, update);
}
/// DO NOT USE
@override
void updateAll(Tag Function(String key, Tag value) update) {
value.updateAll(update);

View file

@ -7,6 +7,9 @@ class ListTag extends Tag {
ListTag();
TagType get getListTagType =>
value.length == 0 ? TagType.End : value[0].getTagType();
@override
void readValue(ByteLayer data) {
TagType type = TagType.get(data.readByte());
@ -40,7 +43,7 @@ class ListTag extends Tag {
}
if (type == TagType.End || type == tag.getTagType()) {
value.add(tag);
tag.setParentTagType(TagType.List);
tag.updateParent(this);
}
}
@ -54,9 +57,11 @@ class ListTag extends Tag {
void remove(Tag tag) {
value.remove(tag);
tag.updateParent(null);
}
void removeAt(int index) {
value[index].updateParent(null);
value.removeAt(index);
}
@ -79,6 +84,16 @@ class ListTag extends Tag {
return value.length;
}
void clear() {
// Clear the list
for (var entry in value) {
entry.updateParent(null);
entry.setParentTagType(TagType.End);
}
value.clear();
}
@override
void prettyPrint(int indent, bool recurse) {
print(

View file

@ -1,6 +1,6 @@
name: libac_dart
description: "Aria's Creations code library"
version: 1.2.072124+0507
version: 1.2.072224+0449
homepage: "https://zontreck.com"