@@ -781,6 +781,56 @@ void TESObjectREFR::SetInventory(const Inventory& aInventory) noexcept
781781 }
782782}
783783
784+ Vector<uint32_t > TESObjectREFR::RemoveNonQuestItems (Inventory& aCurrentInventory) noexcept
785+ {
786+ ScopedEquipOverride equipOverride_;
787+
788+ Vector<uint32_t > questEntries{};
789+
790+ auto & modSystem = World::Get ().GetModSystem ();
791+
792+ for (auto & entry : aCurrentInventory.Entries )
793+ {
794+ if (entry.IsQuestItem )
795+ {
796+ uint32_t gameId = modSystem.GetGameId (entry.BaseId );
797+ questEntries.emplace_back (gameId);
798+ continue ;
799+ }
800+
801+ if (entry.Count <= 0 )
802+ continue ;
803+
804+ entry.Count = -entry.Count ;
805+ AddOrRemoveItem (entry, true );
806+ }
807+
808+ return questEntries;
809+ }
810+
811+ void TESObjectREFR::SetInventoryRetainingQuestItems (Inventory& aCurrentInventory, const Inventory& acSourceInventory) noexcept
812+ {
813+ spdlog::debug (" Setting inventory for {:X}" , formID);
814+
815+ ScopedInventoryOverride _;
816+
817+ // Remove all non-quest items first
818+ Vector<uint32_t > questItemIds = RemoveNonQuestItems (aCurrentInventory);
819+ auto & modSystem = World::Get ().GetModSystem ();
820+
821+ for (const auto & entry : acSourceInventory.Entries )
822+ {
823+ uint32_t gameId = modSystem.GetGameId (entry.BaseId );
824+
825+ // If the item is not in the list of quest items, add it
826+ if (std::find (questItemIds.begin (), questItemIds.end (), gameId) == questItemIds.end ())
827+ {
828+ if (entry.Count != 0 )
829+ AddOrRemoveItem (entry, true );
830+ }
831+ }
832+ }
833+
784834void TESObjectREFR::AddOrRemoveItem (const Inventory::Entry& arEntry, bool aIsSettingInventory) noexcept
785835{
786836 ModSystem& modSystem = World::Get ().GetModSystem ();
0 commit comments