From bfa54b480447748ff4e3a67b551143579d71472d Mon Sep 17 00:00:00 2001 From: zontreck Date: Thu, 6 Jun 2024 23:42:47 -0700 Subject: [PATCH] Expose a parent tag type value --- lib/nbt/Tag.dart | 7 +++++++ lib/nbt/impl/CompoundTag.dart | 24 +++++++++++++++++++++++- lib/nbt/impl/ListTag.dart | 1 + pubspec.yaml | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/nbt/Tag.dart b/lib/nbt/Tag.dart index 718a4e4..26fd57c 100644 --- a/lib/nbt/Tag.dart +++ b/lib/nbt/Tag.dart @@ -147,6 +147,13 @@ abstract class Tag { } TagType getTagType(); + TagType _parentTagType = TagType.End; + + TagType get parentTagType => _parentTagType; + void setParentTagType(TagType type) { + _parentTagType = type; + } + void writeValue(ByteLayer data); void readValue(ByteLayer data); String? _key; diff --git a/lib/nbt/impl/CompoundTag.dart b/lib/nbt/impl/CompoundTag.dart index 1a5c8ea..04c3ca7 100644 --- a/lib/nbt/impl/CompoundTag.dart +++ b/lib/nbt/impl/CompoundTag.dart @@ -20,6 +20,7 @@ class CompoundTag extends Tag implements Map { return; } + tag.setParentTagType(TagType.Compound); put(tag.getKey(), tag); /* if (tag.getTagType() != TagType.Compound && @@ -46,6 +47,7 @@ class CompoundTag extends Tag implements Map { void put(String name, Tag tag) { value[name] = tag; tag.setKey(name); + tag.setParentTagType(TagType.Compound); } Tag? get(String name) { @@ -104,16 +106,19 @@ class CompoundTag extends Tag implements Map { @override void operator []=(String key, Tag value) { this.value[key] = value; + value.setParentTagType(TagType.Compound); } @override void addAll(Map other) { value.addAll(other); + updateParent(); } @override void addEntries(Iterable> newEntries) { value.addEntries(newEntries); + updateParent(); } @override @@ -123,9 +128,22 @@ class CompoundTag extends Tag implements Map { @override void clear() { + unsetParent(); value.clear(); } + void updateParent() { + for (MapEntry Entries in value.entries) { + Entries.value.setParentTagType(TagType.Compound); + } + } + + void unsetParent() { + for (MapEntry Entries in value.entries) { + Entries.value.setParentTagType(TagType.End); + } + } + @override bool containsKey(Object? key) { return value.containsKey(key); @@ -164,11 +182,15 @@ class CompoundTag extends Tag implements Map { @override Tag putIfAbsent(String key, Tag Function() ifAbsent) { - return this.value.putIfAbsent(key, ifAbsent); + Tag X = this.value.putIfAbsent(key, ifAbsent); + updateParent(); + return X; } @override Tag? remove(Object? key) { + if (key is Tag && value.containsKey(key)) + (key as Tag).setParentTagType(TagType.End); return value.remove(key); } diff --git a/lib/nbt/impl/ListTag.dart b/lib/nbt/impl/ListTag.dart index 60ca16c..0cfefc1 100644 --- a/lib/nbt/impl/ListTag.dart +++ b/lib/nbt/impl/ListTag.dart @@ -40,6 +40,7 @@ class ListTag extends Tag { } if (type == TagType.End || type == tag.getTagType()) { value.add(tag); + tag.setParentTagType(TagType.List); } } diff --git a/pubspec.yaml b/pubspec.yaml index 6bc5c9e..f352ac0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: libac_dart description: "Aria's Creations code library" -version: 1.0.32 +version: 1.0.33 homepage: "https://zontreck.com"