3030#include " main/SafeGuardRecord.h"
3131#include " mc/certificates/WebToken.h"
3232#include " mc/dataloadhelper/DefaultDataLoadHelper.h"
33+ #include " mc/deps/core/mce/UUID.h"
3334#include " mc/enums/BossBarColor.h"
3435#include " mc/enums/MinecraftPacketIds.h"
3536#include " mc/enums/TextPacketType.h"
99100#include < unordered_map>
100101#include < vector>
101102
102-
103103// ////////////////// Class Definition ////////////////////
104104
105105ClassDefine<PlayerClass> PlayerClassBuilder =
@@ -331,29 +331,15 @@ Local<Value> McClass::getPlayerNbt(const Arguments& args) {
331331 CHECK_ARGS_COUNT (args, 1 );
332332 CHECK_ARG_TYPE (args[0 ], ValueKind::kString );
333333 try {
334- auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
335- Player* player = ll::service::getLevel ()->getPlayer (uuid);
336- if (player) {
337- std::unique_ptr<CompoundTag> tag;
338- if (player->save (*tag)) {
339- return NbtCompoundClass::pack (std::move (tag));
340- }
341- } else {
342- DBStorage* db = MoreGlobal::dbStorage;
343- if (db) {
344- if (db->hasKey (" player_" + uuid.asString (), DBHelpers::Category::Player)) {
345- std::unique_ptr<CompoundTag> playerTag =
346- db->getCompoundTag (" player_" + uuid.asString (), DBHelpers::Category::Player);
347- if (playerTag) {
348- std::string serverId = playerTag->at (" ServerId" );
349- if (!serverId.empty ()) {
350- if (db->hasKey (serverId, DBHelpers::Category::Player)) {
351- return NbtCompoundClass::pack (
352- std::move (db->getCompoundTag (serverId, DBHelpers::Category::Player))
353- );
354- }
355- }
356- }
334+ auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
335+ DBStorage* db = MoreGlobal::dbStorage;
336+ if (db && db->hasKey (" player_" + uuid.asString (), DBHelpers::Category::Player)) {
337+ std::unique_ptr<CompoundTag> playerTag =
338+ db->getCompoundTag (" player_" + uuid.asString (), DBHelpers::Category::Player);
339+ if (playerTag) {
340+ std::string serverId = playerTag->at (" ServerId" );
341+ if (!serverId.empty () && db->hasKey (serverId, DBHelpers::Category::Player)) {
342+ return NbtCompoundClass::pack (std::move (db->getCompoundTag (serverId, DBHelpers::Category::Player)));
357343 }
358344 }
359345 }
@@ -366,25 +352,17 @@ Local<Value> McClass::setPlayerNbt(const Arguments& args) {
366352 CHECK_ARGS_COUNT (args, 2 );
367353 CHECK_ARG_TYPE (args[0 ], ValueKind::kString );
368354 try {
369- auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
370- auto tag = NbtCompoundClass::extract (args[1 ]);
371- Player* player = ll::service::getLevel ()->getPlayer (uuid);
372- if (player && !MoreGlobal::defaultDataLoadHelper) {
373- player->load (*tag, *MoreGlobal::defaultDataLoadHelper);
374- return Boolean::newBoolean (true );
375- } else {
376- DBStorage* db = MoreGlobal::dbStorage;
377- if (db) {
378- if (db->hasKey (" player_" + uuid.asString (), DBHelpers::Category::Player)) {
379- std::unique_ptr<CompoundTag> playerTag =
380- db->getCompoundTag (" player_" + uuid.asString (), DBHelpers::Category::Player);
381- if (playerTag) {
382- std::string serverId = playerTag->at (" ServerId" );
383- if (!serverId.empty ()) {
384- db->saveData (serverId, tag->toBinaryNbt (), DBHelpers::Category::Player);
385- return Boolean::newBoolean (true );
386- }
387- }
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 );
388366 }
389367 }
390368 }
@@ -398,25 +376,33 @@ Local<Value> McClass::setPlayerNbtTags(const Arguments& args) {
398376 CHECK_ARG_TYPE (args[0 ], ValueKind::kString );
399377 CHECK_ARG_TYPE (args[2 ], ValueKind::kArray );
400378 try {
401- auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
402- CompoundTag* nbt = NbtCompoundClass::extract (args[1 ]);
403- auto arr = args[2 ].asArray ();
404- Player* player = ll::service::getLevel ()->getPlayer (uuid);
405- CompoundTag playerNbt;
406- player->save (playerNbt);
407- if (player && MoreGlobal::defaultDataLoadHelper) {
408- for (int i = 0 ; i < arr.size (); ++i) {
409- auto value = arr.get (i);
410- if (value.getKind () == ValueKind::kString ) {
411- std::string tagName = value.asString ().toString ();
412- if (!nbt->at (tagName).is_null ()) {
413- playerNbt.at (tagName) = nbt->at (tagName);
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);
397+ }
398+ }
399+ }
400+ db->saveData (serverId, loadedTag->toBinaryNbt (), DBHelpers::Category::Player);
401+ return Boolean::newBoolean (true );
414402 }
403+ return Boolean::newBoolean (true );
415404 }
416405 }
417- player->load (playerNbt, *MoreGlobal::defaultDataLoadHelper);
418- player->refreshInventory ();
419- return Boolean::newBoolean (true );
420406 }
421407 return Boolean::newBoolean (false );
422408 }
@@ -427,7 +413,7 @@ Local<Value> McClass::deletePlayerNbt(const Arguments& args) {
427413 CHECK_ARGS_COUNT (args, 1 );
428414 CHECK_ARG_TYPE (args[0 ], ValueKind::kString );
429415 try {
430- auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
416+ mce::UUID uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
431417 ll::service::getLevel ()->getLevelStorage ().deleteData (" player_" + uuid.asString (), DBHelpers::Category::Player);
432418 return Boolean::newBoolean (true );
433419 }
@@ -443,13 +429,7 @@ Local<Value> McClass::getPlayerScore(const Arguments& args) {
443429 Scoreboard& scoreboard = ll::service::getLevel ()->getScoreboard ();
444430 Objective* objective = scoreboard.getObjective (obj);
445431 DBStorage* db = MoreGlobal::dbStorage;
446- if (!objective) {
447- return Number::newNumber (0 );
448- }
449- if (!db) {
450- return Number::newNumber (0 );
451- }
452- if (!db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
432+ if (!objective || !db || !db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
453433 return Number::newNumber (0 );
454434 }
455435 std::unique_ptr<CompoundTag> playerTag =
@@ -458,10 +438,7 @@ Local<Value> McClass::getPlayerScore(const Arguments& args) {
458438 return Number::newNumber (0 );
459439 }
460440 std::string serverId = playerTag->at (" ServerId" );
461- if (serverId.empty ()) {
462- return Number::newNumber (0 );
463- }
464- if (!db->hasKey (serverId, DBHelpers::Category::Player)) {
441+ if (serverId.empty () || !db->hasKey (serverId, DBHelpers::Category::Player)) {
465442 return Number::newNumber (0 );
466443 }
467444 std::unique_ptr<CompoundTag> serverIdTag = db->getCompoundTag (serverId, DBHelpers::Category::Player);
@@ -487,14 +464,8 @@ Local<Value> McClass::setPlayerScore(const Arguments& args) {
487464 auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
488465 Scoreboard& scoreboard = ll::service::getLevel ()->getScoreboard ();
489466 Objective* objective = scoreboard.getObjective (args[1 ].asString ().toString ());
490- if (!objective) {
491- return Boolean::newBoolean (false );
492- }
493- DBStorage* db = MoreGlobal::dbStorage;
494- if (!db) {
495- return Boolean::newBoolean (false );
496- }
497- if (!db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
467+ DBStorage* db = MoreGlobal::dbStorage;
468+ if (!objective || !db || !db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
498469 return Boolean::newBoolean (false );
499470 }
500471 std::unique_ptr<CompoundTag> playerTag =
@@ -503,10 +474,7 @@ Local<Value> McClass::setPlayerScore(const Arguments& args) {
503474 return Boolean::newBoolean (false );
504475 }
505476 std::string serverId = playerTag->at (" ServerId" );
506- if (serverId.empty ()) {
507- return Boolean::newBoolean (false );
508- }
509- if (!db->hasKey (serverId, DBHelpers::Category::Player)) {
477+ if (serverId.empty () || !db->hasKey (serverId, DBHelpers::Category::Player)) {
510478 return Boolean::newBoolean (false );
511479 }
512480 std::unique_ptr<CompoundTag> serverIdTag = db->getCompoundTag (serverId, DBHelpers::Category::Player);
@@ -535,14 +503,8 @@ Local<Value> McClass::addPlayerScore(const Arguments& args) {
535503 auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
536504 Scoreboard& scoreboard = ll::service::getLevel ()->getScoreboard ();
537505 Objective* objective = scoreboard.getObjective (args[1 ].asString ().toString ());
538- if (!objective) {
539- return Boolean::newBoolean (false );
540- }
541- DBStorage* db = MoreGlobal::dbStorage;
542- if (!db) {
543- return Boolean::newBoolean (false );
544- }
545- if (!db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
506+ DBStorage* db = MoreGlobal::dbStorage;
507+ if (!objective || !db || !db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
546508 return Boolean::newBoolean (false );
547509 }
548510 std::unique_ptr<CompoundTag> playerTag =
@@ -551,10 +513,7 @@ Local<Value> McClass::addPlayerScore(const Arguments& args) {
551513 return Boolean::newBoolean (false );
552514 }
553515 std::string serverId = playerTag->at (" ServerId" );
554- if (serverId.empty ()) {
555- return Boolean::newBoolean (false );
556- }
557- if (!db->hasKey (serverId, DBHelpers::Category::Player)) {
516+ if (serverId.empty () || !db->hasKey (serverId, DBHelpers::Category::Player)) {
558517 return Boolean::newBoolean (false );
559518 }
560519 std::unique_ptr<CompoundTag> serverIdTag = db->getCompoundTag (serverId, DBHelpers::Category::Player);
@@ -583,14 +542,8 @@ Local<Value> McClass::reducePlayerScore(const Arguments& args) {
583542 auto uuid = mce::UUID::fromString (args[0 ].asString ().toString ());
584543 Scoreboard& scoreboard = ll::service::getLevel ()->getScoreboard ();
585544 Objective* objective = scoreboard.getObjective (args[1 ].asString ().toString ());
586- if (!objective) {
587- return Boolean::newBoolean (false );
588- }
589- DBStorage* db = MoreGlobal::dbStorage;
590- if (!db) {
591- return Boolean::newBoolean (false );
592- }
593- if (!db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
545+ DBStorage* db = MoreGlobal::dbStorage;
546+ if (!objective || !db || !db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
594547 return Boolean::newBoolean (false );
595548 }
596549 std::unique_ptr<CompoundTag> playerTag =
@@ -599,10 +552,7 @@ Local<Value> McClass::reducePlayerScore(const Arguments& args) {
599552 return Boolean::newBoolean (false );
600553 }
601554 std::string serverId = playerTag->at (" ServerId" );
602- if (serverId.empty ()) {
603- return Boolean::newBoolean (false );
604- }
605- if (!db->hasKey (serverId, DBHelpers::Category::Player)) {
555+ if (serverId.empty () || !db->hasKey (serverId, DBHelpers::Category::Player)) {
606556 return Boolean::newBoolean (false );
607557 }
608558 std::unique_ptr<CompoundTag> serverIdTag = db->getCompoundTag (serverId, DBHelpers::Category::Player);
@@ -634,14 +584,8 @@ Local<Value> McClass::deletePlayerScore(const Arguments& args) {
634584 try {
635585 Scoreboard& scoreboard = ll::service::getLevel ()->getScoreboard ();
636586 Objective* objective = scoreboard.getObjective (args[1 ].asString ().toString ());
637- if (!objective) {
638- return Boolean::newBoolean (false );
639- }
640- DBStorage* db = MoreGlobal::dbStorage;
641- if (!db) {
642- return Boolean::newBoolean (false );
643- }
644- if (!db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
587+ DBStorage* db = MoreGlobal::dbStorage;
588+ if (!objective || !db || !db->hasKey (" player_" + args[0 ].asString ().toString (), DBHelpers::Category::Player)) {
645589 return Boolean::newBoolean (false );
646590 }
647591 std::unique_ptr<CompoundTag> playerTag =
@@ -650,10 +594,7 @@ Local<Value> McClass::deletePlayerScore(const Arguments& args) {
650594 return Boolean::newBoolean (false );
651595 }
652596 std::string serverId = playerTag->at (" ServerId" );
653- if (serverId.empty ()) {
654- return Boolean::newBoolean (false );
655- }
656- if (!db->hasKey (serverId, DBHelpers::Category::Player)) {
597+ if (serverId.empty () || !db->hasKey (serverId, DBHelpers::Category::Player)) {
657598 return Boolean::newBoolean (false );
658599 }
659600 std::unique_ptr<CompoundTag> serverIdTag = db->getCompoundTag (serverId, DBHelpers::Category::Player);
0 commit comments