Skip to content

Commit

Permalink
Enhance ".send items" commands by adding possibility ton add enchant …
Browse files Browse the repository at this point in the history
…to sent items (#174)

Co-authored-by: Necrovoice <91923816+Necrovoice@users.noreply.github.com>
  • Loading branch information
Elmsroth and Necrovoice authored Jan 9, 2022
1 parent b92a318 commit d8877d6
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/game/ChatCommands/MailCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ bool ChatHandler::HandleSendMassMailCommand(char* args)

bool ChatHandler::HandleSendItemsHelper(MailDraft& draft, char* args)
{
// format: "subject text" "mail text" item1[:count1] item2[:count2] ... item12[:count12]
// format: "subject text" "mail text" item1[:count1][:enchant1] item2[:count2][:enchant2] ... item12[:count12][:enchant12]
char* msgSubject = ExtractQuotedArg(&args);
if (!msgSubject)
{
Expand All @@ -141,23 +141,32 @@ bool ChatHandler::HandleSendItemsHelper(MailDraft& draft, char* args)
}

// extract items
typedef std::pair<uint32, uint32> ItemPair;
typedef std::list< ItemPair > ItemPairs;
ItemPairs items;
typedef std::tuple<uint32, uint32, uint32> ItemToSend;
typedef std::list< ItemToSend > ItemsToSend;
ItemsToSend items;

// get from tail next item str
while (char* itemStr = ExtractArg(&args))
{
// parse item str
uint32 item_id = 0;
uint32 item_count = 1;
if (sscanf(itemStr, "%u:%u", &item_id, &item_count) != 2)
uint32 item_enchant_id = 0;

// Try with item1[:count1][:enchant1] format
if (sscanf(itemStr, "%u:%u:%u", &item_id, &item_count, &item_enchant_id) == 0)
{
if (sscanf(itemStr, "%u", &item_id) != 1)
// Try perhaps with item1[:count1]
if (sscanf(itemStr, "%u:%u", &item_id, &item_count) == 0)
{
return false;
// Try at least with item1 - if not a integer >0 then => error
if (sscanf(itemStr, "%u", &item_id) == 0)
{
return false;
}
}
}

if (!item_id)
{
PSendSysMessage(LANG_COMMAND_ITEMIDINVALID, item_id);
Expand All @@ -180,13 +189,16 @@ bool ChatHandler::HandleSendItemsHelper(MailDraft& draft, char* args)
return false;
}

while (item_count > item_proto->GetMaxStackSize())
uint32 max_items_count = item_proto->GetMaxStackSize();
uint32 remaining_items_count = item_count;

while (remaining_items_count > max_items_count)
{
items.push_back(ItemPair(item_id, item_proto->GetMaxStackSize()));
item_count -= item_proto->GetMaxStackSize();
items.push_back(ItemToSend(item_id, max_items_count, item_enchant_id));
remaining_items_count -= max_items_count;
}

items.push_back(ItemPair(item_id, item_count));
items.push_back(ItemToSend(item_id, remaining_items_count, item_enchant_id));

if (items.size() > MAX_MAIL_ITEMS)
{
Expand All @@ -199,10 +211,16 @@ bool ChatHandler::HandleSendItemsHelper(MailDraft& draft, char* args)
// fill mail
draft.SetSubjectAndBody(msgSubject, msgText);

for (ItemPairs::const_iterator itr = items.begin(); itr != items.end(); ++itr)
for (ItemsToSend::iterator itr = items.begin(); itr != items.end(); ++itr)
{
if (Item* item = Item::CreateItem(itr->first, itr->second, m_session ? m_session->GetPlayer() : 0))
uint32 item_id = std::get<0>(*itr);
uint32 item_count = std::get<1>(*itr);
if (Item* item = Item::CreateItem(item_id, item_count, m_session ? m_session->GetPlayer() : 0))
{
uint32 item_enchant_id = std::get<2>(*itr);
if (item_enchant_id) {
item->SetEnchantment(PERM_ENCHANTMENT_SLOT, item_enchant_id, 0, 0);
}
item->SaveToDB(); // save for prevent lost at next mail load, if send fail then item will deleted
draft.AddItem(item);
}
Expand All @@ -213,7 +231,7 @@ bool ChatHandler::HandleSendItemsHelper(MailDraft& draft, char* args)

bool ChatHandler::HandleSendItemsCommand(char* args)
{
// format: name "subject text" "mail text" item1[:count1] item2[:count2] ... item12[:count12]
// format: "subject text" "mail text" item1[:count1][:enchant1] item2[:count2][:enchant2] ... item12[:count12][:enchant12]
Player* receiver;
ObjectGuid receiver_guid;
std::string receiver_name;
Expand Down

0 comments on commit d8877d6

Please sign in to comment.