Skip to content

Commit a33d674

Browse files
committed
fix: fix DefaultDataLoadHelper
1 parent fbebe28 commit a33d674

File tree

10 files changed

+74
-53
lines changed

10 files changed

+74
-53
lines changed

src/legacy/api/BlockAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ Local<Value> McClass::setBlock(const Arguments& args) {
510510
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), bl, 3, nullptr, nullptr));
511511
} else if (IsInstanceOf<NbtCompoundClass>(block)) {
512512
// Nbt
513-
CompoundTag* nbt = NbtCompoundClass::extract(block);
513+
auto nbt = NbtCompoundClass::extract(block);
514514
optional_ref<const Block> bl = Block::tryGetFromRegistry(*nbt);
515515
if (!bl.has_value()) {
516516
return Boolean::newBoolean(false);

src/legacy/api/BlockEntityAPI.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "mc/world/level/block/actor/BlockActor.h"
1616
#include "mc/world/level/dimension/Dimension.h"
1717

18-
1918
//////////////////// Class Definition ////////////////////
2019

2120
ClassDefine<BlockEntityClass> BlockEntityClassBuilder = defineClass<BlockEntityClass>("LLSE_BlockEntity")
@@ -93,10 +92,10 @@ Local<Value> BlockEntityClass::setNbt(const Arguments& args) {
9392

9493
try {
9594
auto nbt = NbtCompoundClass::extract(args[0]);
96-
if (!nbt || !MoreGlobal::defaultDataLoadHelper) {
95+
if (!nbt) {
9796
return Local<Value>();
9897
}
99-
blockEntity->load(*ll::service::getLevel(), *nbt, *MoreGlobal::defaultDataLoadHelper);
98+
blockEntity->load(*ll::service::getLevel(), *nbt, MoreGlobal::defaultDataLoadHelper());
10099
return Boolean::newBoolean(true);
101100
}
102101
CATCH("Fail in setNbt!")

src/legacy/api/EntityAPI.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include <mc/world/level/material/Material.h>
4747
#include <mc/world/phys/AABB.h>
4848

49-
5049
using magic_enum::enum_integer;
5150

5251
//////////////////// Class Definition ////////////////////
@@ -1304,11 +1303,11 @@ Local<Value> EntityClass::setNbt(const Arguments& args) {
13041303
if (!entity) return Local<Value>();
13051304

13061305
auto nbt = NbtCompoundClass::extract(args[0]);
1307-
if (!nbt || !MoreGlobal::defaultDataLoadHelper) {
1306+
if (!nbt) {
13081307
return Local<Value>();
13091308
}
13101309

1311-
return Boolean::newBoolean(entity->load(*nbt, *MoreGlobal::defaultDataLoadHelper));
1310+
return Boolean::newBoolean(entity->load(*nbt, MoreGlobal::defaultDataLoadHelper()));
13121311
}
13131312
CATCH("Fail in setNbt!")
13141313
}

src/legacy/api/ItemAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ Local<Value> McClass::newItem(const Arguments& args) {
446446
return Local<Value>();
447447
}
448448
} else {
449-
CompoundTag* nbt = NbtCompoundClass::extract(args[0]);
449+
auto nbt = NbtCompoundClass::extract(args[0]);
450450
if (nbt) {
451451
auto newItem = new ItemStack{ItemStack::EMPTY_ITEM};
452452
newItem->load(*nbt);

src/legacy/api/NbtAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,9 +1475,9 @@ NbtCompoundClass* NbtCompoundClass::constructor(const Arguments& args) {
14751475
CATCH_C("Fail in Create CompoundTag!");
14761476
}
14771477

1478-
CompoundTag* NbtCompoundClass::extract(Local<Value> v) {
1478+
std::unique_ptr<CompoundTag> NbtCompoundClass::extract(Local<Value> v) {
14791479
if (EngineScope::currentEngine()->isInstanceOf<NbtCompoundClass>(v))
1480-
return EngineScope::currentEngine()->getNativeInstance<NbtCompoundClass>(v)->nbt.get();
1480+
return std::move(EngineScope::currentEngine()->getNativeInstance<NbtCompoundClass>(v)->nbt);
14811481
else return nullptr;
14821482
}
14831483

src/legacy/api/NbtAPI.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <mc/nbt/ListTag.h>
1212
#include <mc/nbt/ShortTag.h>
1313
#include <mc/nbt/StringTag.h>
14+
#include <memory>
1415

1516
// NBT Static
1617
class NbtStatic : public ScriptClass {
@@ -272,10 +273,10 @@ class NbtCompoundClass : public ScriptClass {
272273
if (!canDelete) nbt.release();
273274
}
274275

275-
static NbtCompoundClass* constructor(const Arguments& args);
276-
static CompoundTag* extract(Local<Value> v);
277-
static Local<Value> pack(CompoundTag* tag, bool noDelete = false);
278-
static Local<Value> pack(std::unique_ptr<CompoundTag> tag);
276+
static NbtCompoundClass* constructor(const Arguments& args);
277+
static std::unique_ptr<CompoundTag> extract(Local<Value> v);
278+
static Local<Value> pack(CompoundTag* tag, bool noDelete = false);
279+
static Local<Value> pack(std::unique_ptr<CompoundTag> tag);
279280

280281
Local<Value> getType(const Arguments& args);
281282
Local<Value> getKeys(const Arguments& args);

src/legacy/api/PlayerAPI.cpp

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,22 @@ Local<Value> McClass::setPlayerNbt(const Arguments& args) {
352352
CHECK_ARGS_COUNT(args, 2);
353353
CHECK_ARG_TYPE(args[0], ValueKind::kString);
354354
try {
355-
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
356-
CompoundTag* tag = NbtCompoundClass::extract(args[1]);
357-
DBStorage* db = MoreGlobal::dbStorage;
358-
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
359-
std::unique_ptr<CompoundTag> playerTag =
360-
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
361-
if (playerTag) {
362-
std::string serverId = playerTag->at("ServerId");
363-
if (!serverId.empty()) {
364-
db->saveData(serverId, tag->toBinaryNbt(), DBHelpers::Category::Player);
365-
return Boolean::newBoolean(true);
355+
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
356+
auto tag = NbtCompoundClass::extract(args[1]);
357+
Player* player = ll::service::getLevel()->getPlayer(uuid);
358+
if (player && tag) {
359+
player->load(*tag, MoreGlobal::defaultDataLoadHelper());
360+
} else if (tag) {
361+
DBStorage* db = MoreGlobal::dbStorage;
362+
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
363+
std::unique_ptr<CompoundTag> playerTag =
364+
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
365+
if (playerTag) {
366+
std::string serverId = playerTag->at("ServerId");
367+
if (!serverId.empty()) {
368+
db->saveData(serverId, tag->toBinaryNbt(), DBHelpers::Category::Player);
369+
return Boolean::newBoolean(true);
370+
}
366371
}
367372
}
368373
}
@@ -376,31 +381,47 @@ Local<Value> McClass::setPlayerNbtTags(const Arguments& args) {
376381
CHECK_ARG_TYPE(args[0], ValueKind::kString);
377382
CHECK_ARG_TYPE(args[2], ValueKind::kArray);
378383
try {
379-
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
380-
CompoundTag* tag = NbtCompoundClass::extract(args[1]);
381-
Local<Array> arr = args[2].asArray();
382-
DBStorage* db = MoreGlobal::dbStorage;
383-
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
384-
std::unique_ptr<CompoundTag> playerTag =
385-
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
386-
if (playerTag) {
387-
std::string serverId = playerTag->at("ServerId");
388-
if (!serverId.empty() && db->hasKey(serverId, DBHelpers::Category::Player)) {
389-
auto loadedTag = db->getCompoundTag(serverId, DBHelpers::Category::Player);
390-
if (loadedTag) {
391-
for (int i = 0; i < arr.size(); ++i) {
392-
auto value = arr.get(i);
393-
if (value.getKind() == ValueKind::kString) {
394-
std::string tagName = value.asString().toString();
395-
if (!tag->at(tagName).is_null()) {
396-
loadedTag->at(tagName) = tag->at(tagName);
384+
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
385+
auto tag = NbtCompoundClass::extract(args[1]);
386+
Local<Array> arr = args[2].asArray();
387+
Player* player = ll::service::getLevel()->getPlayer(uuid);
388+
if (player && tag) {
389+
CompoundTag loadedTag;
390+
player->save(loadedTag);
391+
for (int i = 0; i < arr.size(); ++i) {
392+
auto value = arr.get(i);
393+
if (value.getKind() == ValueKind::kString) {
394+
std::string tagName = value.asString().toString();
395+
if (!tag->at(tagName).is_null()) {
396+
loadedTag.at(tagName) = tag->at(tagName);
397+
}
398+
}
399+
}
400+
player->load(loadedTag, MoreGlobal::defaultDataLoadHelper());
401+
} else if (tag) {
402+
DBStorage* db = MoreGlobal::dbStorage;
403+
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
404+
std::unique_ptr<CompoundTag> playerTag =
405+
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
406+
if (playerTag) {
407+
std::string serverId = playerTag->at("ServerId");
408+
if (!serverId.empty() && db->hasKey(serverId, DBHelpers::Category::Player)) {
409+
auto loadedTag = db->getCompoundTag(serverId, DBHelpers::Category::Player);
410+
if (loadedTag) {
411+
for (int i = 0; i < arr.size(); ++i) {
412+
auto value = arr.get(i);
413+
if (value.getKind() == ValueKind::kString) {
414+
std::string tagName = value.asString().toString();
415+
if (!tag->at(tagName).is_null()) {
416+
loadedTag->at(tagName) = tag->at(tagName);
417+
}
397418
}
398419
}
420+
db->saveData(serverId, loadedTag->toBinaryNbt(), DBHelpers::Category::Player);
421+
return Boolean::newBoolean(true);
399422
}
400-
db->saveData(serverId, loadedTag->toBinaryNbt(), DBHelpers::Category::Player);
401423
return Boolean::newBoolean(true);
402424
}
403-
return Boolean::newBoolean(true);
404425
}
405426
}
406427
}
@@ -2957,10 +2978,10 @@ Local<Value> PlayerClass::setNbt(const Arguments& args) {
29572978
if (!player) return Local<Value>();
29582979

29592980
auto nbt = NbtCompoundClass::extract(args[0]);
2960-
if (!nbt || !MoreGlobal::defaultDataLoadHelper) {
2981+
if (!nbt) {
29612982
return Local<Value>();
29622983
}
2963-
return Boolean::newBoolean(player->load(*nbt, *MoreGlobal::defaultDataLoadHelper));
2984+
return Boolean::newBoolean(player->load(*nbt, MoreGlobal::defaultDataLoadHelper()));
29642985
}
29652986
CATCH("Fail in setNbt!")
29662987
}

src/legacy/api/RemoteCallAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ RemoteCall::ValueType pack(Local<Object> value) {
4444
auto pos = IntPos::extractPos(value);
4545
return std::make_pair(pos->getBlockPos(), pos->getDimensionId());
4646
}
47-
if (IsInstanceOf<NbtCompoundClass>(value)) return NbtCompoundClass::extract(value);
47+
if (IsInstanceOf<NbtCompoundClass>(value)) return NbtCompoundClass::extract(value).get();
4848
std::unordered_map<std::string, RemoteCall::ValueType> result;
4949
for (auto& k : value.getKeyNames()) result.emplace(k, pack(value.get(k)));
5050
return std::move(result);

src/lse/api/MoreGlobal.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "MoreGlobal.h"
22

33
#include "ll/api/memory/Hook.h"
4+
#include "mc/dataloadhelper/DefaultDataLoadHelper.h"
45
#include "mc/world/level/storage/DBStorage.h"
56
#include "mc/world/level/storage/DBStorageConfig.h"
67

78
namespace MoreGlobal {
89
DBStorage* dbStorage;
9-
DefaultDataLoadHelper* defaultDataLoadHelper;
10+
DefaultDataLoadHelper* helper;
11+
DefaultDataLoadHelper& defaultDataLoadHelper() { return (DefaultDataLoadHelper&)helper; }
1012

1113
LL_TYPE_INSTANCE_HOOK(
1214
DBStorageHook,
@@ -25,9 +27,8 @@ LL_TYPE_INSTANCE_HOOK(
2527
void onLoad() { DBStorageHook::hook(); }
2628

2729
bool onEnable() {
28-
defaultDataLoadHelper =
29-
static_cast<DefaultDataLoadHelper*>(ll::memory::resolveSymbol("??_7DefaultDataLoadHelper@@6B@"));
30-
if (defaultDataLoadHelper && dbStorage && DBStorageHook::unhook()) {
30+
helper = static_cast<DefaultDataLoadHelper*>(ll::memory::resolveSymbol("??_7DefaultDataLoadHelper@@6B@"));
31+
if (helper && dbStorage && DBStorageHook::unhook()) {
3132
return true;
3233
}
3334
return false;

src/lse/api/MoreGlobal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class DBStorage;
33
namespace MoreGlobal {
44
extern DBStorage* dbStorage;
5-
extern DefaultDataLoadHelper* defaultDataLoadHelper;
5+
extern DefaultDataLoadHelper& defaultDataLoadHelper();
66
extern void onLoad();
77
extern bool onEnable();
88
}; // namespace MoreGlobal

0 commit comments

Comments
 (0)