File tree Expand file tree Collapse file tree
src/main/java/org/glavo/nbt Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020import org .glavo .nbt .internal .Access ;
2121import org .glavo .nbt .internal .ChunkUtils ;
2222import org .glavo .nbt .tag .CompoundTag ;
23+ import org .glavo .nbt .tag .Tag ;
2324import org .jetbrains .annotations .Contract ;
2425import org .jetbrains .annotations .Nullable ;
2526
@@ -72,7 +73,27 @@ public int getLocalZ() {
7273
7374 @ Contract (mutates = "this,param1" )
7475 public void setRootTag (@ Nullable CompoundTag rootTag ) {
75- // TODO: remove old root tag from its parent
76+ if (rootTag == this .rootTag ) {
77+ return ;
78+ }
79+
80+ if (this .rootTag != null ) {
81+ remove (this .rootTag );
82+ }
83+
84+ if (rootTag != null ) {
85+ if (rootTag .getParent () != null ) {
86+ assert rootTag .getParent () != this ;
87+
88+ // The root tag is already a child of another tag, so we need to remove it from its parent first.
89+ @ SuppressWarnings ("unchecked" )
90+ var oldParent = (NBTParent <Tag >) rootTag .getParent ();
91+ oldParent .remove (rootTag );
92+ }
93+
94+ Access .TAG .setParent (rootTag , this , 0 );
95+ }
96+
7697 this .rootTag = rootTag ;
7798 }
7899
Original file line number Diff line number Diff line change @@ -121,7 +121,7 @@ public int getIndex() {
121121 }
122122
123123 /// If the tag is a child of a [parent][NBTParent], returns the parent; otherwise, returns `null`.
124- public @ Nullable NBTParent <?> getParent () {
124+ public @ Nullable NBTParent <? extends Tag > getParent () {
125125 return parent ;
126126 }
127127
You can’t perform that action at this time.
0 commit comments