Skip to content

Commit 9c61385

Browse files
committed
fix: fix NbtAPI constructor #160
1 parent 3e3628d commit 9c61385

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/legacy/api/EntityAPI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <mc/world/level/biome/Biome.h>
4646
#include <mc/world/level/material/Material.h>
4747
#include <mc/world/phys/AABB.h>
48+
#include <memory>
4849

4950
using magic_enum::enum_integer;
5051

@@ -1288,7 +1289,7 @@ Local<Value> EntityClass::getNbt(const Arguments& args) {
12881289
Actor* entity = get();
12891290
if (!entity) return Local<Value>();
12901291

1291-
CompoundTag* tag = new CompoundTag();
1292+
std::unique_ptr<CompoundTag> tag = std::make_unique<CompoundTag>();
12921293
entity->save(*tag);
12931294
return NbtCompoundClass::pack(std::move(tag));
12941295
}

src/legacy/api/NbtAPI.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ Local<Value> NbtByteClass::pack(ByteTag* tag, bool noDelete) {
357357
NbtByteClass* nbtObj = new NbtByteClass(std::unique_ptr<ByteTag>(tag));
358358
nbtObj->canDelete = false;
359359
return nbtObj->getScriptObject();
360-
} else return (new NbtByteClass(std::unique_ptr<ByteTag>(tag)))->getScriptObject();
360+
} else
361+
return (new NbtByteClass(std::unique_ptr<ByteTag>(tag->copy()->as_ptr<ByteTag>())))
362+
->getScriptObject(); // Raw pointer usually from BDS, so we have to copy it before pack it.
361363
}
362364
CATCH("Fail in construct NbtByte!");
363365
}
@@ -427,7 +429,7 @@ Local<Value> NbtIntClass::pack(IntTag* tag, bool noDelete) {
427429
NbtIntClass* nbtObj = new NbtIntClass(std::unique_ptr<IntTag>(tag));
428430
nbtObj->canDelete = false;
429431
return nbtObj->getScriptObject();
430-
} else return (new NbtIntClass(std::unique_ptr<IntTag>(tag)))->getScriptObject();
432+
} else return (new NbtIntClass(std::unique_ptr<IntTag>(tag->copy()->as_ptr<IntTag>())))->getScriptObject();
431433
}
432434
CATCH("Fail in construct NbtInt!");
433435
}
@@ -498,7 +500,8 @@ Local<Value> NbtShortClass::pack(ShortTag* tag, bool noDelete) {
498500
NbtShortClass* nbtObj = new NbtShortClass(std::unique_ptr<ShortTag>(tag));
499501
nbtObj->canDelete = false;
500502
return nbtObj->getScriptObject();
501-
} else return (new NbtShortClass(std::unique_ptr<ShortTag>(tag)))->getScriptObject();
503+
} else
504+
return (new NbtShortClass(std::unique_ptr<ShortTag>(tag->copy()->as_ptr<ShortTag>())))->getScriptObject();
502505
}
503506
CATCH("Fail in construct NbtShort!");
504507
}
@@ -568,7 +571,7 @@ Local<Value> NbtLongClass::pack(Int64Tag* tag, bool noDelete) {
568571
NbtLongClass* nbtObj = new NbtLongClass(std::unique_ptr<Int64Tag>(tag));
569572
nbtObj->canDelete = false;
570573
return nbtObj->getScriptObject();
571-
} else return (new NbtLongClass(std::unique_ptr<Int64Tag>(tag)))->getScriptObject();
574+
} else return (new NbtLongClass(std::unique_ptr<Int64Tag>(tag->copy()->as_ptr<Int64Tag>())))->getScriptObject();
572575
}
573576
CATCH("Fail in construct NbtLong!");
574577
}
@@ -639,7 +642,8 @@ Local<Value> NbtFloatClass::pack(FloatTag* tag, bool noDelete) {
639642
NbtFloatClass* nbtObj = new NbtFloatClass(std::unique_ptr<FloatTag>(tag));
640643
nbtObj->canDelete = false;
641644
return nbtObj->getScriptObject();
642-
} else return (new NbtFloatClass(std::unique_ptr<FloatTag>(tag)))->getScriptObject();
645+
} else
646+
return (new NbtFloatClass(std::unique_ptr<FloatTag>(tag->copy()->as_ptr<FloatTag>())))->getScriptObject();
643647
}
644648
CATCH("Fail in construct NbtFloat!");
645649
}
@@ -710,7 +714,9 @@ Local<Value> NbtDoubleClass::pack(DoubleTag* tag, bool noDelete) {
710714
NbtDoubleClass* nbtObj = new NbtDoubleClass(std::unique_ptr<DoubleTag>(tag));
711715
nbtObj->canDelete = false;
712716
return nbtObj->getScriptObject();
713-
} else return (new NbtDoubleClass(std::unique_ptr<DoubleTag>(tag)))->getScriptObject();
717+
} else
718+
return (new NbtDoubleClass(std::unique_ptr<DoubleTag>(tag->copy()->as_ptr<DoubleTag>())))
719+
->getScriptObject();
714720
}
715721
CATCH("Fail in construct NbtDouble!");
716722
}
@@ -781,7 +787,9 @@ Local<Value> NbtStringClass::pack(StringTag* tag, bool noDelete) {
781787
NbtStringClass* nbtObj = new NbtStringClass(std::unique_ptr<StringTag>(tag));
782788
nbtObj->canDelete = false;
783789
return nbtObj->getScriptObject();
784-
} else return (new NbtStringClass(std::unique_ptr<StringTag>(tag)))->getScriptObject();
790+
} else
791+
return (new NbtStringClass(std::unique_ptr<StringTag>(tag->copy()->as_ptr<StringTag>())))
792+
->getScriptObject();
785793
}
786794
CATCH("Fail in construct NbtString!");
787795
}
@@ -859,7 +867,9 @@ Local<Value> NbtByteArrayClass::pack(ByteArrayTag* tag, bool noDelete) {
859867
NbtByteArrayClass* nbtObj = new NbtByteArrayClass(std::unique_ptr<ByteArrayTag>(tag));
860868
nbtObj->canDelete = false;
861869
return nbtObj->getScriptObject();
862-
} else return (new NbtByteArrayClass(std::unique_ptr<ByteArrayTag>(tag)))->getScriptObject();
870+
} else
871+
return (new NbtByteArrayClass(std::unique_ptr<ByteArrayTag>(tag->copy()->as_ptr<ByteArrayTag>())))
872+
->getScriptObject();
863873
}
864874
CATCH("Fail in construct NbtByteArray!");
865875
}
@@ -989,7 +999,7 @@ Local<Value> NbtListClass::pack(ListTag* tag, bool noDelete) {
989999
NbtListClass* nbtObj = new NbtListClass(std::unique_ptr<ListTag>(tag));
9901000
nbtObj->canDelete = false;
9911001
return nbtObj->getScriptObject();
992-
} else return (new NbtListClass(std::unique_ptr<ListTag>(tag)))->getScriptObject();
1002+
} else return (new NbtListClass(tag->copyList()))->getScriptObject();
9931003
}
9941004
CATCH("Fail in construct NbtList!");
9951005
}
@@ -1499,7 +1509,7 @@ Local<Value> NbtCompoundClass::pack(CompoundTag* tag, bool noDelete) {
14991509
NbtCompoundClass* nbtObj = new NbtCompoundClass(std::unique_ptr<CompoundTag>(tag));
15001510
nbtObj->canDelete = false;
15011511
return nbtObj->getScriptObject();
1502-
} else return (new NbtCompoundClass(std::unique_ptr<CompoundTag>(tag)))->getScriptObject();
1512+
} else return (new NbtCompoundClass(tag->clone()))->getScriptObject();
15031513
}
15041514
CATCH("Fail in construct NbtCompound!");
15051515
}

0 commit comments

Comments
 (0)