diff --git a/src/server/game/Globals/ObjectAccessor.cpp b/src/server/game/Globals/ObjectAccessor.cpp index 0e3a67ac3..a529e4e00 100644 --- a/src/server/game/Globals/ObjectAccessor.cpp +++ b/src/server/game/Globals/ObjectAccessor.cpp @@ -282,6 +282,58 @@ Player* ObjectAccessor::FindPlayerByName(std::string const& name, bool checkInWo return nullptr; } +/** + * @brief Get a spawned creature by DB `guid` column. MODULE USAGE ONLY - USE IT FOR CUSTOM CONTENT. + * + * @param uint32 mapId The map id where the creature is spawned. + * @param uint64 guid Database guid of the creature we are accessing. + */ +Creature* ObjectAccessor::GetSpawnedCreatureByDBGUID(uint32 mapId, uint64 guid) +{ + if (Map* map = sMapMgr->FindBaseMap(mapId)) + { + auto bounds = map->GetCreatureBySpawnIdStore().equal_range(guid); + + if (bounds.first == bounds.second) + { + return nullptr; + } + + if (Creature* creature = bounds.first->second) + { + return creature; + } + } + + return nullptr; +} + +/** + * @brief Get a spawned gameobject by DB `guid` column. MODULE USAGE ONLY - USE IT FOR CUSTOM CONTENT. + * + * @param uint32 mapId The map id where the gameobject is spawned. + * @param uint64 guid Database guid of the gameobject we are accessing. + */ +GameObject* ObjectAccessor::GetSpawnedGameObjectByDBGUID(uint32 mapId, uint64 guid) +{ + if (Map* map = sMapMgr->FindBaseMap(mapId)) + { + auto bounds = map->GetGameObjectBySpawnIdStore().equal_range(guid); + + if (bounds.first == bounds.second) + { + return nullptr; + } + + if (GameObject* go = bounds.first->second) + { + return go; + } + } + + return nullptr; +} + template<> void ObjectAccessor::AddObject(Player* player) { diff --git a/src/server/game/Globals/ObjectAccessor.h b/src/server/game/Globals/ObjectAccessor.h index dd39251f0..7c6c003f5 100644 --- a/src/server/game/Globals/ObjectAccessor.h +++ b/src/server/game/Globals/ObjectAccessor.h @@ -83,6 +83,8 @@ namespace ObjectAccessor Player* FindPlayerByLowGUID(ObjectGuid::LowType lowguid); Player* FindConnectedPlayer(ObjectGuid const guid); Player* FindPlayerByName(std::string const& name, bool checkInWorld = true); + Creature* GetSpawnedCreatureByDBGUID(uint32 mapId, uint64 guid); + GameObject* GetSpawnedGameObjectByDBGUID(uint32 mapId, uint64 guid); // when using this, you must use the hashmapholder's lock HashMapHolder::MapType const& GetPlayers();