Skip to content

Commit

Permalink
🐛 NBTTree toCompound bug fixed.
Browse files Browse the repository at this point in the history
Signed-off-by: 秋雨落 <[email protected]>
  • Loading branch information
qyl27 committed Nov 26, 2022
1 parent 0ca8528 commit 8e0e42a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/1286750-
## Bug report(Bug反馈/催更):
Discord: [Here](https://discord.gg/7k6NcRRpGg)

## Update log:
A fatal bug was fixed.
### Update log:
```
1.19.2-4.0.3:
Some fatal bug was fixed.
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'dev.architectury.loom'
group = 'cx.rain.mc.nbtedit'
archivesBaseName = 'nbtedit'

version = '1.19.2-4.0.2'
version = '1.19.2-4.0.3'
if (System.getenv("JITPACK") == "true") {
version += "-${System.getenv("VERSION")}"
}
Expand Down
85 changes: 60 additions & 25 deletions src/main/java/cx/rain/mc/nbtedit/nbt/NBTTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,87 @@ public static NBTTree root(CompoundTag tag) {
}

public CompoundTag toCompound() {
var tag = new CompoundTag();
toCompoundInternal(rootNode, tag);
return tag;
// var tag = new CompoundTag();
// toCompoundInternal(rootNode, tag);
// return tag;
return compoundNodeToTag(rootNode);
}

public Node<CompoundTag> getRoot() {
return rootNode;
}

private void toCompoundInternal(Node<?> node, CompoundTag tag) {
// Fixme: work, plz.
private CompoundTag compoundNodeToTag(Node<?> node) {
var tag = new CompoundTag();
for (var child : node.getChildren()) {
String name = child.getName();
Tag base = child.getTag();
if (base instanceof CompoundTag compound) {
toCompoundInternal(child, compound);
tag.put(name, compound);
} else if (base instanceof ListTag list) {
toCompoundInternal(child, list);
tag.put(name, list);
var name = child.getName();
var childTag = child.getTag();

if (childTag instanceof CompoundTag) {
tag.put(name, compoundNodeToTag(child));
} else if (childTag instanceof ListTag) {
tag.put(name, listNodeToTag(child));
} else {
tag.put(name, base.copy());
tag.put(name, childTag);
}
}
return tag;
}

public void toCompoundInternal(Node<?> node, ListTag list) {
private ListTag listNodeToTag(Node<?> node) {
var tag = new ListTag();
for (var child : node.getChildren()) {
Tag childTag = child.getTag();
if (childTag instanceof CompoundTag compound) {
toCompoundInternal(child, compound);
list.add(compound);
} else if (childTag instanceof ListTag childListTag) {
toCompoundInternal(child, childListTag);
list.add(childListTag);
var childTag = child.getTag();

if (childTag instanceof CompoundTag) {
tag.add(compoundNodeToTag(child));
} else if (childTag instanceof ListTag) {
tag.add(listNodeToTag(child));
} else {
list.add(childTag.copy());
tag.add(childTag);
}
}
return tag;
}

// private void toCompoundInternal(Node<?> node, CompoundTag tag) {
// for (var child : node.getChildren()) {
// String name = child.getName();
// Tag base = child.getTag();
// if (base instanceof CompoundTag compound) {
// toCompoundInternal(child, compound);
// tag.put(name, compound);
// } else if (base instanceof ListTag list) {
// toCompoundInternal(child, list);
// tag.put(name, list);
// } else {
// tag.put(name, base.copy());
// }
// }
// }
//
// public void toCompoundInternal(Node<?> node, ListTag list) {
// for (var child : node.getChildren()) {
// Tag childTag = child.getTag();
// if (childTag instanceof CompoundTag compound) {
// toCompoundInternal(child, compound);
//// list.add(compound);
// } else if (childTag instanceof ListTag childListTag) {
// toCompoundInternal(child, childListTag);
//// list.add(childListTag);
// } else {
// list.add(childTag.copy());
// }
// }
// }

public static class Node<T extends Tag> {
private String nbtName;
private T nbtTag;

private Node<?> parent = null;
private List<Node<?>> children = new ArrayList<>();
private Node<? extends Tag> parent = null;
private final List<Node<? extends Tag>> children = new ArrayList<>();
private boolean shouldShowChildren = false;

private Node(T tag) {
Expand Down Expand Up @@ -140,7 +175,7 @@ public void removeChild(Node<?> node) {
}

public boolean hasChild() {
return children.size() > 0;
return !children.isEmpty();
}

public List<Node<?>> getChildren() {
Expand Down

0 comments on commit 8e0e42a

Please sign in to comment.