@@ -73,19 +73,20 @@ ClassDefine<ItemClass> ItemClassBuilder = defineClass<ItemClass>("LLSE_Item")
7373
7474// ////////////////// Classes ////////////////////
7575
76- ItemClass::ItemClass (ItemStack* itemStack, bool isManagedByBDS )
76+ ItemClass::ItemClass (std::variant<std::monostate, std::unique_ptr<ItemStack>, ItemStack*> itemStack )
7777: ScriptClass(ScriptClass::ConstructFromCpp<ItemClass>{}) {
78- if (isManagedByBDS) {
79- item = itemStack;
80- } else {
81- item = std::unique_ptr<ItemStack>(itemStack);
82- }
78+ item = std::move (itemStack);
8379 preloadData ();
8480}
8581
8682// 生成函数
87- Local<Object> ItemClass::newItem (ItemStack* itemStack, bool isManagedByBDS) {
88- auto newp = new ItemClass (itemStack, isManagedByBDS);
83+ Local<Object> ItemClass::newItem (ItemStack* itemStack) {
84+ auto newp = new ItemClass (itemStack);
85+ return newp->getScriptObject ();
86+ }
87+
88+ Local<Object> ItemClass::newItem (std::unique_ptr<ItemStack> itemStack) {
89+ auto newp = new ItemClass (std::move (itemStack));
8990 return newp->getScriptObject ();
9091}
9192
@@ -358,9 +359,8 @@ Local<Value> ItemClass::set(const Arguments& args) {
358359Local<Value> ItemClass::clone (const Arguments&) {
359360 try {
360361 auto itemStack = get ();
361- if (!itemStack) return Local<Value>(); // Null
362- auto itemNew = new ItemStack (*itemStack);
363- return ItemClass::newItem (itemNew, false );
362+ if (!itemStack) return Local<Value>();
363+ return ItemClass::newItem (std::make_unique<ItemStack>(*itemStack));
364364 }
365365 CATCH (" Fail in cloneItem!" );
366366}
@@ -469,20 +469,17 @@ Local<Value> McClass::newItem(const Arguments& args) {
469469 std::string type = args[0 ].asString ().toString ();
470470 int cnt = args[1 ].asNumber ().toInt32 ();
471471
472- ItemStack* item = new ItemStack{type, cnt, 0 , nullptr };
473- if (!item) return Local<Value>(); // Null
474- else return ItemClass::newItem (item, false ); // Not managed by BDS, pointer will be saved as unique_ptr
472+ return ItemClass::newItem (std::make_unique<ItemStack>(type, cnt, 0 , nullptr ));
475473 } else {
476474 LOG_TOO_FEW_ARGS (__FUNCTION__);
477475 return Local<Value>();
478476 }
479477 } else {
480478 auto nbt = NbtCompoundClass::extract (args[0 ]);
481479 if (nbt) {
482- auto newItem = new ItemStack{ ItemStack::EMPTY_ITEM ()} ;
480+ auto newItem = std::make_unique< ItemStack>( ItemStack::EMPTY_ITEM ()) ;
483481 ItemHelper::load (*newItem, *nbt);
484- return ItemClass::newItem (newItem,
485- false ); // Not managed by BDS, pointer will be saved as unique_ptr
482+ return ItemClass::newItem (std::move (newItem));
486483 } else {
487484 LOG_WRONG_ARG_TYPE (__FUNCTION__);
488485 return Local<Value>();
0 commit comments