Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: GameObject:AddLoot() #52

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/LuaEngine/GameObjectMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,69 @@ namespace LuaGameObject
return 0;
}

/**
* Adds an [Item] to the loot of a [GameObject]
* Requires an gameobject with loot_template set to 0.
*
* @param uint32 entry : The entry of the [Item]
* @param uint32 amount = 1 : amount of the [Item] to add to the loot
* @return uint32 itemGUIDlow : low GUID of the [Item]
*/

int AddLoot(lua_State* L, GameObject* go)
{
int i = 1;
int argAmount = lua_gettop(L);

#if defined TRINITY || defined AZEROTHCORE
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
#endif
uint8 addedItems = 0;
while (i + 2 <= argAmount)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, ++i);
uint32 amount = Eluna::CHECKVAL<uint32>(L, ++i);

#if defined TRINITY || AZEROTHCORE
ItemTemplate const* item_proto = eObjectMgr->GetItemTemplate(entry);
#else
ItemTemplate const* item_proto = ObjectMgr::GetItemPrototype(entry);
#endif
if (!item_proto)
{
luaL_error(L, "Item entry %d does not exist", entry);
continue;
}
if (amount < 1 || (item_proto->MaxCount > 0 && amount > uint32(item_proto->MaxCount)))
{
luaL_error(L, "Item entry %d has invalid amount %d", entry, amount);
continue;
}
if (Item* item = Item::CreateItem(entry, amount))
{
#if defined TRINITY || AZEROTHCORE
item->SaveToDB(trans);
#else
item->SaveToDB();
#endif
LootStoreItem storeItem(item->GetEntry(), 0, 100, 0, LOOT_MODE_DEFAULT, 0, item->GetCount(), item->GetCount());
go->loot.AddItem(storeItem);
#if defined TRINITY || AZEROTHCORE
Eluna::Push(L, item->GetGUID().GetCounter());
#else
Eluna::Push(L, item->GetGUIDLow());
#endif
++addedItems;
}
}

#if defined TRINITY || AZEROTHCORE
CharacterDatabase.CommitTransaction(trans);
#endif

return addedItems;
}

/**
* Saves [GameObject] to the database
*
Expand Down
1 change: 1 addition & 0 deletions src/LuaEngine/LuaFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ ElunaRegister<GameObject> GameObjectMethods[] =
{ "Despawn", &LuaGameObject::Despawn },
{ "Respawn", &LuaGameObject::Respawn },
{ "SaveToDB", &LuaGameObject::SaveToDB },
{ "AddLoot", &LuaGameObject::AddLoot },

{ NULL, NULL }
};
Expand Down