[6922] Whitespace and newline fixes
[getmangos.git] / src / game / ItemHandler.cpp
blob7d24fd5a2a23b6c8615e641b3710e9cbaa598b9e
1 /*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "WorldPacket.h"
21 #include "WorldSession.h"
22 #include "World.h"
23 #include "Opcodes.h"
24 #include "Log.h"
25 #include "ObjectMgr.h"
26 #include "Player.h"
27 #include "Item.h"
28 #include "UpdateData.h"
29 #include "ObjectAccessor.h"
31 void WorldSession::HandleSplitItemOpcode( WorldPacket & recv_data )
33 CHECK_PACKET_SIZE(recv_data,1+1+1+1+1);
35 //sLog.outDebug("WORLD: CMSG_SPLIT_ITEM");
36 uint8 srcbag, srcslot, dstbag, dstslot, count;
38 recv_data >> srcbag >> srcslot >> dstbag >> dstslot >> count;
39 //sLog.outDebug("STORAGE: receive srcbag = %u, srcslot = %u, dstbag = %u, dstslot = %u, count = %u", srcbag, srcslot, dstbag, dstslot, count);
41 uint16 src = ( (srcbag << 8) | srcslot );
42 uint16 dst = ( (dstbag << 8) | dstslot );
44 if(src==dst)
45 return;
47 if (count==0)
48 return; //check count - if zero it's fake packet
50 if(!_player->IsValidPos(srcbag,srcslot))
52 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
53 return;
56 if(!_player->IsValidPos(dstbag,dstslot))
58 _player->SendEquipError( EQUIP_ERR_ITEM_DOESNT_GO_TO_SLOT, NULL, NULL );
59 return;
62 _player->SplitItem( src, dst, count );
65 void WorldSession::HandleSwapInvItemOpcode( WorldPacket & recv_data )
67 CHECK_PACKET_SIZE(recv_data,1+1);
69 //sLog.outDebug("WORLD: CMSG_SWAP_INV_ITEM");
70 uint8 srcslot, dstslot;
72 recv_data >> srcslot >> dstslot;
73 //sLog.outDebug("STORAGE: receive srcslot = %u, dstslot = %u", srcslot, dstslot);
75 // prevent attempt swap same item to current position generated by client at special checting sequence
76 if(srcslot==dstslot)
77 return;
79 if(!_player->IsValidPos(INVENTORY_SLOT_BAG_0,srcslot))
81 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
82 return;
85 if(!_player->IsValidPos(INVENTORY_SLOT_BAG_0,dstslot))
87 _player->SendEquipError( EQUIP_ERR_ITEM_DOESNT_GO_TO_SLOT, NULL, NULL );
88 return;
91 uint16 src = ( (INVENTORY_SLOT_BAG_0 << 8) | srcslot );
92 uint16 dst = ( (INVENTORY_SLOT_BAG_0 << 8) | dstslot );
94 _player->SwapItem( src, dst );
97 void WorldSession::HandleAutoEquipItemSlotOpcode( WorldPacket & recv_data )
99 CHECK_PACKET_SIZE(recv_data,8+1);
100 uint64 itemguid;
101 uint8 dstslot;
102 recv_data >> itemguid >> dstslot;
104 // cheating attempt, client should never send opcode in that case
105 if(!Player::IsEquipmentPos(INVENTORY_SLOT_BAG_0, dstslot))
106 return;
108 Item* item = _player->GetItemByGuid(itemguid);
109 uint16 dstpos = dstslot | (INVENTORY_SLOT_BAG_0 << 8);
111 if(!item || item->GetPos() == dstpos)
112 return;
114 _player->SwapItem(item->GetPos(), dstpos);
117 void WorldSession::HandleSwapItem( WorldPacket & recv_data )
119 CHECK_PACKET_SIZE(recv_data,1+1+1+1);
121 //sLog.outDebug("WORLD: CMSG_SWAP_ITEM");
122 uint8 dstbag, dstslot, srcbag, srcslot;
124 recv_data >> dstbag >> dstslot >> srcbag >> srcslot ;
125 //sLog.outDebug("STORAGE: receive srcbag = %u, srcslot = %u, dstbag = %u, dstslot = %u", srcbag, srcslot, dstbag, dstslot);
127 uint16 src = ( (srcbag << 8) | srcslot );
128 uint16 dst = ( (dstbag << 8) | dstslot );
130 // prevent attempt swap same item to current position generated by client at special checting sequence
131 if(src==dst)
132 return;
134 if(!_player->IsValidPos(srcbag,srcslot))
136 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
137 return;
140 if(!_player->IsValidPos(dstbag,dstslot))
142 _player->SendEquipError( EQUIP_ERR_ITEM_DOESNT_GO_TO_SLOT, NULL, NULL );
143 return;
146 _player->SwapItem( src, dst );
149 void WorldSession::HandleAutoEquipItemOpcode( WorldPacket & recv_data )
151 CHECK_PACKET_SIZE(recv_data,1+1);
153 //sLog.outDebug("WORLD: CMSG_AUTOEQUIP_ITEM");
154 uint8 srcbag, srcslot;
156 recv_data >> srcbag >> srcslot;
157 //sLog.outDebug("STORAGE: receive srcbag = %u, srcslot = %u", srcbag, srcslot);
159 Item *pSrcItem = _player->GetItemByPos( srcbag, srcslot );
160 if( !pSrcItem )
161 return; // only at cheat
163 if(pSrcItem->m_lootGenerated) // prevent swap looting item
165 //best error message found for attempting to swap while looting
166 _player->SendEquipError( EQUIP_ERR_CANT_DO_RIGHT_NOW, pSrcItem, NULL );
167 return;
170 uint16 dest;
171 uint8 msg = _player->CanEquipItem( NULL_SLOT, dest, pSrcItem, !pSrcItem->IsBag() );
172 if( msg != EQUIP_ERR_OK )
174 _player->SendEquipError( msg, pSrcItem, NULL );
175 return;
178 uint16 src = pSrcItem->GetPos();
179 if(dest==src) // prevent equip in same slot, only at cheat
180 return;
182 Item *pDstItem = _player->GetItemByPos( dest );
183 if( !pDstItem ) // empty slot, simple case
185 _player->RemoveItem( srcbag, srcslot, true );
186 _player->EquipItem( dest, pSrcItem, true );
187 _player->AutoUnequipOffhandIfNeed();
189 else // have currently equipped item, not simple case
191 uint8 dstbag = pDstItem->GetBagSlot();
192 uint8 dstslot = pDstItem->GetSlot();
194 msg = _player->CanUnequipItem( dest, !pSrcItem->IsBag() );
195 if( msg != EQUIP_ERR_OK )
197 _player->SendEquipError( msg, pDstItem, NULL );
198 return;
201 // check dest->src move possibility
202 ItemPosCountVec sSrc;
203 uint16 eSrc;
204 if( _player->IsInventoryPos( src ) )
206 msg = _player->CanStoreItem( srcbag, srcslot, sSrc, pDstItem, true );
207 if( msg != EQUIP_ERR_OK )
208 msg = _player->CanStoreItem( srcbag, NULL_SLOT, sSrc, pDstItem, true );
209 if( msg != EQUIP_ERR_OK )
210 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, sSrc, pDstItem, true );
212 else if( _player->IsBankPos( src ) )
214 msg = _player->CanBankItem( srcbag, srcslot, sSrc, pDstItem, true );
215 if( msg != EQUIP_ERR_OK )
216 msg = _player->CanBankItem( srcbag, NULL_SLOT, sSrc, pDstItem, true );
217 if( msg != EQUIP_ERR_OK )
218 msg = _player->CanBankItem( NULL_BAG, NULL_SLOT, sSrc, pDstItem, true );
220 else if( _player->IsEquipmentPos( src ) )
222 msg = _player->CanEquipItem( srcslot, eSrc, pDstItem, true);
223 if( msg == EQUIP_ERR_OK )
224 msg = _player->CanUnequipItem( eSrc, true);
227 if( msg != EQUIP_ERR_OK )
229 _player->SendEquipError( msg, pDstItem, pSrcItem );
230 return;
233 // now do moves, remove...
234 _player->RemoveItem(dstbag, dstslot, false);
235 _player->RemoveItem(srcbag, srcslot, false);
237 // add to dest
238 _player->EquipItem(dest, pSrcItem, true);
240 // add to src
241 if( _player->IsInventoryPos( src ) )
242 _player->StoreItem(sSrc, pDstItem, true);
243 else if( _player->IsBankPos( src ) )
244 _player->BankItem(sSrc, pDstItem, true);
245 else if( _player->IsEquipmentPos( src ) )
246 _player->EquipItem(eSrc, pDstItem, true);
248 _player->AutoUnequipOffhandIfNeed();
252 void WorldSession::HandleDestroyItemOpcode( WorldPacket & recv_data )
254 CHECK_PACKET_SIZE(recv_data,1+1+1+1+1+1);
256 //sLog.outDebug("WORLD: CMSG_DESTROYITEM");
257 uint8 bag, slot, count, data1, data2, data3;
259 recv_data >> bag >> slot >> count >> data1 >> data2 >> data3;
260 //sLog.outDebug("STORAGE: receive bag = %u, slot = %u, count = %u", bag, slot, count);
262 uint16 pos = (bag << 8) | slot;
264 // prevent drop unequipable items (in combat, for example) and non-empty bags
265 if(_player->IsEquipmentPos(pos) || _player->IsBagPos(pos))
267 uint8 msg = _player->CanUnequipItem( pos, false );
268 if( msg != EQUIP_ERR_OK )
270 _player->SendEquipError( msg, _player->GetItemByPos(pos), NULL );
271 return;
275 Item *pItem = _player->GetItemByPos( bag, slot );
276 if(!pItem)
278 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
279 return;
282 if(count)
284 uint32 i_count = count;
285 _player->DestroyItemCount( pItem, i_count, true );
287 else
288 _player->DestroyItem( bag, slot, true );
291 // Only _static_ data send in this packet !!!
292 void WorldSession::HandleItemQuerySingleOpcode( WorldPacket & recv_data )
294 CHECK_PACKET_SIZE(recv_data, 4);
296 //sLog.outDebug("WORLD: CMSG_ITEM_QUERY_SINGLE");
297 uint32 item;
298 recv_data >> item;
300 sLog.outDetail("STORAGE: Item Query = %u", item);
302 ItemPrototype const *pProto = objmgr.GetItemPrototype( item );
303 if( pProto )
305 std::string Name = pProto->Name1;
306 std::string Description = pProto->Description;
308 int loc_idx = GetSessionDbLocaleIndex();
309 if ( loc_idx >= 0 )
311 ItemLocale const *il = objmgr.GetItemLocale(pProto->ItemId);
312 if (il)
314 if (il->Name.size() > size_t(loc_idx) && !il->Name[loc_idx].empty())
315 Name = il->Name[loc_idx];
316 if (il->Description.size() > size_t(loc_idx) && !il->Description[loc_idx].empty())
317 Description = il->Description[loc_idx];
320 // guess size
321 WorldPacket data( SMSG_ITEM_QUERY_SINGLE_RESPONSE, 600);
322 data << pProto->ItemId;
323 data << pProto->Class;
324 data << pProto->SubClass;
325 data << uint32(-1); // new 2.0.3, not exist in wdb cache?
326 data << Name;
327 data << uint8(0x00); //pProto->Name2; // blizz not send name there, just uint8(0x00); <-- \0 = empty string = empty name...
328 data << uint8(0x00); //pProto->Name3; // blizz not send name there, just uint8(0x00);
329 data << uint8(0x00); //pProto->Name4; // blizz not send name there, just uint8(0x00);
330 data << pProto->DisplayInfoID;
331 data << pProto->Quality;
332 data << pProto->Flags;
333 data << pProto->BuyPrice;
334 data << pProto->SellPrice;
335 data << pProto->InventoryType;
336 data << pProto->AllowableClass;
337 data << pProto->AllowableRace;
338 data << pProto->ItemLevel;
339 data << pProto->RequiredLevel;
340 data << pProto->RequiredSkill;
341 data << pProto->RequiredSkillRank;
342 data << pProto->RequiredSpell;
343 data << pProto->RequiredHonorRank;
344 data << pProto->RequiredCityRank;
345 data << pProto->RequiredReputationFaction;
346 data << pProto->RequiredReputationRank;
347 data << pProto->MaxCount;
348 data << pProto->Stackable;
349 data << pProto->ContainerSlots;
350 for(int i = 0; i < 10; i++)
352 data << pProto->ItemStat[i].ItemStatType;
353 data << pProto->ItemStat[i].ItemStatValue;
355 for(int i = 0; i < 5; i++)
357 data << pProto->Damage[i].DamageMin;
358 data << pProto->Damage[i].DamageMax;
359 data << pProto->Damage[i].DamageType;
362 // resistances (7)
363 data << pProto->Armor;
364 data << pProto->HolyRes;
365 data << pProto->FireRes;
366 data << pProto->NatureRes;
367 data << pProto->FrostRes;
368 data << pProto->ShadowRes;
369 data << pProto->ArcaneRes;
371 data << pProto->Delay;
372 data << pProto->AmmoType;
373 data << pProto->RangedModRange;
375 for(int s = 0; s < 5; s++)
377 // send DBC data for cooldowns in same way as it used in Spell::SendSpellCooldown
378 // use `item_template` or if not set then only use spell cooldowns
379 SpellEntry const* spell = sSpellStore.LookupEntry(pProto->Spells[s].SpellId);
380 if(spell)
382 bool db_data = pProto->Spells[s].SpellCooldown >= 0 || pProto->Spells[s].SpellCategoryCooldown >= 0;
384 data << pProto->Spells[s].SpellId;
385 data << pProto->Spells[s].SpellTrigger;
386 data << uint32(-abs(pProto->Spells[s].SpellCharges));
388 if(db_data)
390 data << uint32(pProto->Spells[s].SpellCooldown);
391 data << uint32(pProto->Spells[s].SpellCategory);
392 data << uint32(pProto->Spells[s].SpellCategoryCooldown);
394 else
396 data << uint32(spell->RecoveryTime);
397 data << uint32(spell->Category);
398 data << uint32(spell->CategoryRecoveryTime);
401 else
403 data << uint32(0);
404 data << uint32(0);
405 data << uint32(0);
406 data << uint32(-1);
407 data << uint32(0);
408 data << uint32(-1);
411 data << pProto->Bonding;
412 data << Description;
413 data << pProto->PageText;
414 data << pProto->LanguageID;
415 data << pProto->PageMaterial;
416 data << pProto->StartQuest;
417 data << pProto->LockID;
418 data << pProto->Material;
419 data << pProto->Sheath;
420 data << pProto->RandomProperty;
421 data << pProto->RandomSuffix;
422 data << pProto->Block;
423 data << pProto->ItemSet;
424 data << pProto->MaxDurability;
425 data << pProto->Area;
426 data << pProto->Map; // Added in 1.12.x & 2.0.1 client branch
427 data << pProto->BagFamily;
428 data << pProto->TotemCategory;
429 for(int s = 0; s < 3; s++)
431 data << pProto->Socket[s].Color;
432 data << pProto->Socket[s].Content;
434 data << pProto->socketBonus;
435 data << pProto->GemProperties;
436 data << pProto->RequiredDisenchantSkill;
437 data << pProto->ArmorDamageModifier;
438 data << uint32(0); // added in 2.4.2.8209, duration (seconds)
439 SendPacket( &data );
441 else
443 sLog.outDebug( "WORLD: CMSG_ITEM_QUERY_SINGLE - NO item INFO! (ENTRY: %u)", item );
444 WorldPacket data( SMSG_ITEM_QUERY_SINGLE_RESPONSE, 4);
445 data << uint32(item | 0x80000000);
446 SendPacket( &data );
450 void WorldSession::HandleReadItem( WorldPacket & recv_data )
452 CHECK_PACKET_SIZE(recv_data,1+1);
454 //sLog.outDebug( "WORLD: CMSG_READ_ITEM");
456 uint8 bag, slot;
457 recv_data >> bag >> slot;
459 //sLog.outDetail("STORAGE: Read bag = %u, slot = %u", bag, slot);
460 Item *pItem = _player->GetItemByPos( bag, slot );
462 if( pItem && pItem->GetProto()->PageText )
464 WorldPacket data;
466 uint8 msg = _player->CanUseItem( pItem );
467 if( msg == EQUIP_ERR_OK )
469 data.Initialize (SMSG_READ_ITEM_OK, 8);
470 sLog.outDetail("STORAGE: Item page sent");
472 else
474 data.Initialize( SMSG_READ_ITEM_FAILED, 8 );
475 sLog.outDetail("STORAGE: Unable to read item");
476 _player->SendEquipError( msg, pItem, NULL );
478 data << pItem->GetGUID();
479 SendPacket(&data);
481 else
482 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
485 void WorldSession::HandlePageQuerySkippedOpcode( WorldPacket & recv_data )
487 CHECK_PACKET_SIZE(recv_data,4+8);
489 sLog.outDebug( "WORLD: Received CMSG_PAGE_TEXT_QUERY" );
491 uint32 itemid;
492 uint64 guid;
494 recv_data >> itemid >> guid;
496 sLog.outDetail( "Packet Info: itemid: %u guidlow: %u guidentry: %u guidhigh: %u",
497 itemid, GUID_LOPART(guid), GUID_ENPART(guid), GUID_HIPART(guid));
500 void WorldSession::HandleSellItemOpcode( WorldPacket & recv_data )
502 CHECK_PACKET_SIZE(recv_data,8+8+1);
504 sLog.outDebug( "WORLD: Received CMSG_SELL_ITEM" );
505 uint64 vendorguid, itemguid;
506 uint8 _count;
508 recv_data >> vendorguid >> itemguid >> _count;
510 // prevent possible overflow, as mangos uses uint32 for item count
511 uint32 count = _count;
513 if(!itemguid)
514 return;
516 Creature *pCreature = ObjectAccessor::GetNPCIfCanInteractWith(*_player, vendorguid,UNIT_NPC_FLAG_VENDOR);
517 if (!pCreature)
519 sLog.outDebug( "WORLD: HandleSellItemOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(vendorguid)) );
520 _player->SendSellError( SELL_ERR_CANT_FIND_VENDOR, NULL, itemguid, 0);
521 return;
524 // remove fake death
525 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
526 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
528 Item *pItem = _player->GetItemByGuid( itemguid );
529 if( pItem )
531 // prevent sell not owner item
532 if(_player->GetGUID()!=pItem->GetOwnerGUID())
534 _player->SendSellError( SELL_ERR_CANT_SELL_ITEM, pCreature, itemguid, 0);
535 return;
538 // prevent sell non empty bag by drag-and-drop at vendor's item list
539 if(pItem->IsBag() && !((Bag*)pItem)->IsEmpty())
541 _player->SendSellError( SELL_ERR_CANT_SELL_ITEM, pCreature, itemguid, 0);
542 return;
545 // prevent sell currently looted item
546 if(_player->GetLootGUID()==pItem->GetGUID())
548 _player->SendSellError( SELL_ERR_CANT_SELL_ITEM, pCreature, itemguid, 0);
549 return;
552 // special case at auto sell (sell all)
553 if(count==0)
555 count = pItem->GetCount();
557 else
559 // prevent sell more items that exist in stack (possable only not from client)
560 if(count > pItem->GetCount())
562 _player->SendSellError( SELL_ERR_CANT_SELL_ITEM, pCreature, itemguid, 0);
563 return;
567 ItemPrototype const *pProto = pItem->GetProto();
568 if( pProto )
570 if( pProto->SellPrice > 0 )
572 if(count < pItem->GetCount()) // need split items
574 Item *pNewItem = pItem->CloneItem( count, _player );
575 if (!pNewItem)
577 sLog.outError("WORLD: HandleSellItemOpcode - could not create clone of item %u; count = %u", pItem->GetEntry(), count );
578 _player->SendSellError( SELL_ERR_CANT_SELL_ITEM, pCreature, itemguid, 0);
579 return;
582 pItem->SetCount( pItem->GetCount() - count );
583 _player->ItemRemovedQuestCheck( pItem->GetEntry(), count );
584 if( _player->IsInWorld() )
585 pItem->SendUpdateToPlayer( _player );
586 pItem->SetState(ITEM_CHANGED, _player);
588 _player->AddItemToBuyBackSlot( pNewItem );
589 if( _player->IsInWorld() )
590 pNewItem->SendUpdateToPlayer( _player );
592 else
594 _player->ItemRemovedQuestCheck( pItem->GetEntry(), pItem->GetCount());
595 _player->RemoveItem( pItem->GetBagSlot(), pItem->GetSlot(), true);
596 pItem->RemoveFromUpdateQueueOf(_player);
597 _player->AddItemToBuyBackSlot( pItem );
600 _player->ModifyMoney( pProto->SellPrice * count );
602 else
603 _player->SendSellError( SELL_ERR_CANT_SELL_ITEM, pCreature, itemguid, 0);
604 return;
607 _player->SendSellError( SELL_ERR_CANT_FIND_ITEM, pCreature, itemguid, 0);
608 return;
611 void WorldSession::HandleBuybackItem(WorldPacket & recv_data)
613 CHECK_PACKET_SIZE(recv_data,8+4);
615 sLog.outDebug( "WORLD: Received CMSG_BUYBACK_ITEM" );
616 uint64 vendorguid;
617 uint32 slot;
619 recv_data >> vendorguid >> slot;
621 Creature *pCreature = ObjectAccessor::GetNPCIfCanInteractWith(*_player, vendorguid,UNIT_NPC_FLAG_VENDOR);
622 if (!pCreature)
624 sLog.outDebug( "WORLD: HandleBuybackItem - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(vendorguid)) );
625 _player->SendSellError( SELL_ERR_CANT_FIND_VENDOR, NULL, 0, 0);
626 return;
629 // remove fake death
630 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
631 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
633 Item *pItem = _player->GetItemFromBuyBackSlot( slot );
634 if( pItem )
636 uint32 price = _player->GetUInt32Value( PLAYER_FIELD_BUYBACK_PRICE_1 + slot - BUYBACK_SLOT_START );
637 if( _player->GetMoney() < price )
639 _player->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, pCreature, pItem->GetEntry(), 0);
640 return;
643 ItemPosCountVec dest;
644 uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, pItem, false );
645 if( msg == EQUIP_ERR_OK )
647 _player->ModifyMoney( -(int32)price );
648 _player->RemoveItemFromBuyBackSlot( slot, false );
649 _player->ItemAddedQuestCheck( pItem->GetEntry(), pItem->GetCount());
650 _player->StoreItem( dest, pItem, true );
652 else
653 _player->SendEquipError( msg, pItem, NULL );
654 return;
656 else
657 _player->SendBuyError( BUY_ERR_CANT_FIND_ITEM, pCreature, 0, 0);
660 void WorldSession::HandleBuyItemInSlotOpcode( WorldPacket & recv_data )
662 CHECK_PACKET_SIZE(recv_data,8+4+8+1+1);
664 sLog.outDebug( "WORLD: Received CMSG_BUY_ITEM_IN_SLOT" );
665 uint64 vendorguid, bagguid;
666 uint32 item;
667 uint8 slot, count;
669 recv_data >> vendorguid >> item >> bagguid >> slot >> count;
671 GetPlayer()->BuyItemFromVendor(vendorguid,item,count,bagguid,slot);
674 void WorldSession::HandleBuyItemOpcode( WorldPacket & recv_data )
676 CHECK_PACKET_SIZE(recv_data,8+4+1+1);
678 sLog.outDebug( "WORLD: Received CMSG_BUY_ITEM" );
679 uint64 vendorguid;
680 uint32 item;
681 uint8 count, unk1;
683 recv_data >> vendorguid >> item >> count >> unk1;
685 GetPlayer()->BuyItemFromVendor(vendorguid,item,count,NULL_BAG,NULL_SLOT);
688 void WorldSession::HandleListInventoryOpcode( WorldPacket & recv_data )
690 CHECK_PACKET_SIZE(recv_data,8);
692 uint64 guid;
694 recv_data >> guid;
696 if(!GetPlayer()->isAlive())
697 return;
699 sLog.outDebug( "WORLD: Recvd CMSG_LIST_INVENTORY" );
701 SendListInventory( guid );
704 void WorldSession::SendListInventory( uint64 vendorguid )
706 sLog.outDebug( "WORLD: Sent SMSG_LIST_INVENTORY" );
708 Creature *pCreature = ObjectAccessor::GetNPCIfCanInteractWith(*_player, vendorguid,UNIT_NPC_FLAG_VENDOR);
709 if (!pCreature)
711 sLog.outDebug( "WORLD: SendListInventory - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(vendorguid)) );
712 _player->SendSellError( SELL_ERR_CANT_FIND_VENDOR, NULL, 0, 0);
713 return;
716 // remove fake death
717 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
718 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
720 // Stop the npc if moving
721 pCreature->StopMoving();
723 VendorItemData const* vItems = pCreature->GetVendorItems();
724 if(!vItems)
726 _player->SendSellError( SELL_ERR_CANT_FIND_VENDOR, NULL, 0, 0);
727 return;
730 uint8 numitems = vItems->GetItemCount();
731 uint8 count = 0;
733 WorldPacket data( SMSG_LIST_INVENTORY, (8+1+numitems*8*4) );
734 data << uint64(vendorguid);
735 data << uint8(numitems);
737 float discountMod = _player->GetReputationPriceDiscount(pCreature);
739 for(int i = 0; i < numitems; i++ )
741 if(VendorItem const* crItem = vItems->GetItem(i))
743 if(ItemPrototype const *pProto = objmgr.GetItemPrototype(crItem->item))
745 if((pProto->AllowableClass & _player->getClassMask()) == 0 && pProto->Bonding == BIND_WHEN_PICKED_UP && !_player->isGameMaster())
746 continue;
748 ++count;
750 // reputation discount
751 uint32 price = uint32(floor(pProto->BuyPrice * discountMod));
753 data << uint32(count);
754 data << uint32(crItem->item);
755 data << uint32(pProto->DisplayInfoID);
756 data << uint32(crItem->maxcount <= 0 ? 0xFFFFFFFF : pCreature->GetVendorItemCurrentCount(crItem));
757 data << uint32(price);
758 data << uint32(pProto->MaxDurability);
759 data << uint32(pProto->BuyCount);
760 data << uint32(crItem->ExtendedCost);
765 if ( count == 0 || data.size() != 8 + 1 + size_t(count) * 8 * 4 )
766 return;
768 data.put<uint8>(8, count);
769 SendPacket( &data );
772 void WorldSession::HandleAutoStoreBagItemOpcode( WorldPacket & recv_data )
774 CHECK_PACKET_SIZE(recv_data,1+1+1);
776 //sLog.outDebug("WORLD: CMSG_AUTOSTORE_BAG_ITEM");
777 uint8 srcbag, srcslot, dstbag;
779 recv_data >> srcbag >> srcslot >> dstbag;
780 //sLog.outDebug("STORAGE: receive srcbag = %u, srcslot = %u, dstbag = %u", srcbag, srcslot, dstbag);
782 Item *pItem = _player->GetItemByPos( srcbag, srcslot );
783 if( !pItem )
784 return;
786 if(!_player->IsValidPos(dstbag,NULL_SLOT))
788 _player->SendEquipError( EQUIP_ERR_ITEM_DOESNT_GO_TO_SLOT, NULL, NULL );
789 return;
792 uint16 src = pItem->GetPos();
794 // check unequip potability for equipped items and bank bags
795 if(_player->IsEquipmentPos ( src ) || _player->IsBagPos ( src ))
797 uint8 msg = _player->CanUnequipItem( src, !_player->IsBagPos ( src ));
798 if(msg != EQUIP_ERR_OK)
800 _player->SendEquipError( msg, pItem, NULL );
801 return;
805 ItemPosCountVec dest;
806 uint8 msg = _player->CanStoreItem( dstbag, NULL_SLOT, dest, pItem, false );
807 if( msg != EQUIP_ERR_OK )
809 _player->SendEquipError( msg, pItem, NULL );
810 return;
813 // no-op: placed in same slot
814 if(dest.size()==1 && dest[0].pos==src)
816 // just remove grey item state
817 _player->SendEquipError( EQUIP_ERR_NONE, pItem, NULL );
818 return;
821 _player->RemoveItem(srcbag, srcslot, true );
822 _player->StoreItem( dest, pItem, true );
825 void WorldSession::HandleBuyBankSlotOpcode(WorldPacket& /*recvPacket*/)
827 sLog.outDebug("WORLD: CMSG_BUY_BANK_SLOT");
829 uint32 slot = _player->GetByteValue(PLAYER_BYTES_2, 2);
831 // next slot
832 ++slot;
834 sLog.outDetail("PLAYER: Buy bank bag slot, slot number = %u", slot);
836 BankBagSlotPricesEntry const* slotEntry = sBankBagSlotPricesStore.LookupEntry(slot);
838 if(!slotEntry)
839 return;
841 uint32 price = slotEntry->price;
843 if (_player->GetMoney() < price)
844 return;
846 _player->SetByteValue(PLAYER_BYTES_2, 2, slot);
847 _player->ModifyMoney(-int32(price));
850 void WorldSession::HandleAutoBankItemOpcode(WorldPacket& recvPacket)
852 CHECK_PACKET_SIZE(recvPacket,1+1);
854 sLog.outDebug("WORLD: CMSG_AUTOBANK_ITEM");
855 uint8 srcbag, srcslot;
857 recvPacket >> srcbag >> srcslot;
858 sLog.outDebug("STORAGE: receive srcbag = %u, srcslot = %u", srcbag, srcslot);
860 Item *pItem = _player->GetItemByPos( srcbag, srcslot );
861 if( !pItem )
862 return;
864 ItemPosCountVec dest;
865 uint8 msg = _player->CanBankItem( NULL_BAG, NULL_SLOT, dest, pItem, false );
866 if( msg != EQUIP_ERR_OK )
868 _player->SendEquipError( msg, pItem, NULL );
869 return;
872 _player->RemoveItem(srcbag, srcslot, true);
873 _player->BankItem( dest, pItem, true );
876 void WorldSession::HandleAutoStoreBankItemOpcode(WorldPacket& recvPacket)
878 CHECK_PACKET_SIZE(recvPacket,1+1);
880 sLog.outDebug("WORLD: CMSG_AUTOSTORE_BANK_ITEM");
881 uint8 srcbag, srcslot;
883 recvPacket >> srcbag >> srcslot;
884 sLog.outDebug("STORAGE: receive srcbag = %u, srcslot = %u", srcbag, srcslot);
886 Item *pItem = _player->GetItemByPos( srcbag, srcslot );
887 if( !pItem )
888 return;
890 if(_player->IsBankPos(srcbag, srcslot)) // moving from bank to inventory
892 ItemPosCountVec dest;
893 uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, pItem, false );
894 if( msg != EQUIP_ERR_OK )
896 _player->SendEquipError( msg, pItem, NULL );
897 return;
900 _player->RemoveItem(srcbag, srcslot, true);
901 _player->StoreItem( dest, pItem, true );
903 else // moving from inventory to bank
905 ItemPosCountVec dest;
906 uint8 msg = _player->CanBankItem( NULL_BAG, NULL_SLOT, dest, pItem, false );
907 if( msg != EQUIP_ERR_OK )
909 _player->SendEquipError( msg, pItem, NULL );
910 return;
913 _player->RemoveItem(srcbag, srcslot, true);
914 _player->BankItem( dest, pItem, true );
918 void WorldSession::HandleSetAmmoOpcode(WorldPacket & recv_data)
920 CHECK_PACKET_SIZE(recv_data,4);
922 if(!GetPlayer()->isAlive())
924 GetPlayer()->SendEquipError( EQUIP_ERR_YOU_ARE_DEAD, NULL, NULL );
925 return;
928 sLog.outDebug("WORLD: CMSG_SET_AMMO");
929 uint32 item;
931 recv_data >> item;
933 if(!item)
934 GetPlayer()->RemoveAmmo();
935 else
936 GetPlayer()->SetAmmo(item);
939 void WorldSession::SendEnchantmentLog(uint64 Target, uint64 Caster,uint32 ItemID,uint32 SpellID)
941 WorldPacket data(SMSG_ENCHANTMENTLOG, (8+8+4+4+1)); // last check 2.0.10
942 data << Target;
943 data << Caster;
944 data << ItemID;
945 data << SpellID;
946 data << uint8(0);
947 SendPacket(&data);
950 void WorldSession::SendItemEnchantTimeUpdate(uint64 Playerguid, uint64 Itemguid,uint32 slot,uint32 Duration)
952 // last check 2.0.10
953 WorldPacket data(SMSG_ITEM_ENCHANT_TIME_UPDATE, (8+4+4+8));
954 data << uint64(Itemguid);
955 data << uint32(slot);
956 data << uint32(Duration);
957 data << uint64(Playerguid);
958 SendPacket(&data);
961 void WorldSession::HandleItemNameQueryOpcode(WorldPacket & recv_data)
963 CHECK_PACKET_SIZE(recv_data,4);
965 uint32 itemid;
966 recv_data >> itemid;
967 sLog.outDebug("WORLD: CMSG_ITEM_NAME_QUERY %u", itemid);
968 ItemPrototype const *pProto = objmgr.GetItemPrototype( itemid );
969 if( pProto )
971 std::string Name;
972 Name = pProto->Name1;
974 int loc_idx = GetSessionDbLocaleIndex();
975 if (loc_idx >= 0)
977 ItemLocale const *il = objmgr.GetItemLocale(pProto->ItemId);
978 if (il)
980 if (il->Name.size() > size_t(loc_idx) && !il->Name[loc_idx].empty())
981 Name = il->Name[loc_idx];
984 // guess size
985 WorldPacket data(SMSG_ITEM_NAME_QUERY_RESPONSE, (4+10));
986 data << uint32(pProto->ItemId);
987 data << Name;
988 data << uint32(pProto->InventoryType);
989 SendPacket(&data);
990 return;
992 else
993 sLog.outDebug("WORLD: CMSG_ITEM_NAME_QUERY for item %u failed (unknown item)", itemid);
996 void WorldSession::HandleWrapItemOpcode(WorldPacket& recv_data)
998 CHECK_PACKET_SIZE(recv_data,1+1+1+1);
1000 sLog.outDebug("Received opcode CMSG_WRAP_ITEM");
1002 uint8 gift_bag, gift_slot, item_bag, item_slot;
1003 //recv_data.hexlike();
1005 recv_data >> gift_bag >> gift_slot; // paper
1006 recv_data >> item_bag >> item_slot; // item
1008 sLog.outDebug("WRAP: receive gift_bag = %u, gift_slot = %u, item_bag = %u, item_slot = %u", gift_bag, gift_slot, item_bag, item_slot);
1010 Item *gift = _player->GetItemByPos( gift_bag, gift_slot );
1011 if(!gift)
1013 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, gift, NULL );
1014 return;
1017 if(!gift->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPER))// cheating: non-wrapper wrapper
1019 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, gift, NULL );
1020 return;
1023 Item *item = _player->GetItemByPos( item_bag, item_slot );
1025 if( !item )
1027 _player->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, item, NULL );
1028 return;
1031 if(item==gift) // not possable with pacjket from real client
1033 _player->SendEquipError( EQUIP_ERR_WRAPPED_CANT_BE_WRAPPED, item, NULL );
1034 return;
1037 if(item->IsEquipped())
1039 _player->SendEquipError( EQUIP_ERR_EQUIPPED_CANT_BE_WRAPPED, item, NULL );
1040 return;
1043 if(item->GetUInt64Value(ITEM_FIELD_GIFTCREATOR)) // HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED);
1045 _player->SendEquipError( EQUIP_ERR_WRAPPED_CANT_BE_WRAPPED, item, NULL );
1046 return;
1049 if(item->IsBag())
1051 _player->SendEquipError( EQUIP_ERR_BAGS_CANT_BE_WRAPPED, item, NULL );
1052 return;
1055 if(item->IsSoulBound())
1057 _player->SendEquipError( EQUIP_ERR_BOUND_CANT_BE_WRAPPED, item, NULL );
1058 return;
1061 if(item->GetMaxStackCount() != 1)
1063 _player->SendEquipError( EQUIP_ERR_STACKABLE_CANT_BE_WRAPPED, item, NULL );
1064 return;
1067 // maybe not correct check (it is better than nothing)
1068 if(item->GetProto()->MaxCount>0)
1070 _player->SendEquipError( EQUIP_ERR_UNIQUE_CANT_BE_WRAPPED, item, NULL );
1071 return;
1074 CharacterDatabase.BeginTransaction();
1075 CharacterDatabase.PExecute("INSERT INTO character_gifts VALUES ('%u', '%u', '%u', '%u')", GUID_LOPART(item->GetOwnerGUID()), item->GetGUIDLow(), item->GetEntry(), item->GetUInt32Value(ITEM_FIELD_FLAGS));
1076 item->SetEntry(gift->GetEntry());
1078 switch (item->GetEntry())
1080 case 5042: item->SetEntry( 5043); break;
1081 case 5048: item->SetEntry( 5044); break;
1082 case 17303: item->SetEntry(17302); break;
1083 case 17304: item->SetEntry(17305); break;
1084 case 17307: item->SetEntry(17308); break;
1085 case 21830: item->SetEntry(21831); break;
1087 item->SetUInt64Value(ITEM_FIELD_GIFTCREATOR, _player->GetGUID());
1088 item->SetUInt32Value(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED);
1089 item->SetState(ITEM_CHANGED, _player);
1091 if(item->GetState()==ITEM_NEW) // save new item, to have alway for `character_gifts` record in `item_instance`
1093 // after save it will be impossible to remove the item from the queue
1094 item->RemoveFromUpdateQueueOf(_player);
1095 item->SaveToDB(); // item gave inventory record unchanged and can be save standalone
1097 CharacterDatabase.CommitTransaction();
1099 uint32 count = 1;
1100 _player->DestroyItemCount(gift, count, true);
1103 void WorldSession::HandleSocketOpcode(WorldPacket& recv_data)
1105 sLog.outDebug("WORLD: CMSG_SOCKET_GEMS");
1107 CHECK_PACKET_SIZE(recv_data,8*4);
1109 uint64 guids[4];
1110 uint32 GemEnchants[3], OldEnchants[3];
1111 Item *Gems[3];
1112 bool SocketBonusActivated, SocketBonusToBeActivated;
1114 for(int i = 0; i < 4; i++)
1115 recv_data >> guids[i];
1117 if(!guids[0])
1118 return;
1120 //cheat -> tried to socket same gem multiple times
1121 if((guids[1] && (guids[1] == guids[2] || guids[1] == guids[3])) || (guids[2] && (guids[2] == guids[3])))
1122 return;
1124 Item *itemTarget = _player->GetItemByGuid(guids[0]);
1125 if(!itemTarget) //missing item to socket
1126 return;
1128 //this slot is excepted when applying / removing meta gem bonus
1129 uint8 slot = itemTarget->IsEquipped() ? itemTarget->GetSlot() : NULL_SLOT;
1131 for(int i = 0; i < 3; i++)
1132 Gems[i] = guids[i + 1] ? _player->GetItemByGuid(guids[i + 1]) : NULL;
1134 GemPropertiesEntry const *GemProps[3];
1135 for(int i = 0; i < 3; ++i) //get geminfo from dbc storage
1137 GemProps[i] = (Gems[i]) ? sGemPropertiesStore.LookupEntry(Gems[i]->GetProto()->GemProperties) : NULL;
1140 for(int i = 0; i < 3; ++i) //check for hack maybe
1142 // tried to put gem in socket where no socket exists / tried to put normal gem in meta socket
1143 // tried to put meta gem in normal socket
1144 if( GemProps[i] && ( !itemTarget->GetProto()->Socket[i].Color ||
1145 itemTarget->GetProto()->Socket[i].Color == SOCKET_COLOR_META && GemProps[i]->color != SOCKET_COLOR_META ||
1146 itemTarget->GetProto()->Socket[i].Color != SOCKET_COLOR_META && GemProps[i]->color == SOCKET_COLOR_META ) )
1147 return;
1150 for(int i = 0; i < 3; ++i) //get new and old enchantments
1152 GemEnchants[i] = (GemProps[i]) ? GemProps[i]->spellitemenchantement : 0;
1153 OldEnchants[i] = itemTarget->GetEnchantmentId(EnchantmentSlot(SOCK_ENCHANTMENT_SLOT+i));
1156 // check unique-equipped conditions
1157 for(int i = 0; i < 3; ++i)
1159 if (Gems[i] && (Gems[i]->GetProto()->Flags & ITEM_FLAGS_UNIQUE_EQUIPPED))
1161 // for equipped item check all equipment for duplicate equipped gems
1162 if(itemTarget->IsEquipped())
1164 if(GetPlayer()->GetItemOrItemWithGemEquipped(Gems[i]->GetEntry()))
1166 _player->SendEquipError( EQUIP_ERR_ITEM_UNIQUE_EQUIPABLE, itemTarget, NULL );
1167 return;
1171 // continue check for case when attempt add 2 similar unique equipped gems in one item.
1172 for (int j = 0; j < 3; ++j)
1174 if ((i != j) && (Gems[j]) && (Gems[i]->GetProto()->ItemId == Gems[j]->GetProto()->ItemId))
1176 _player->SendEquipError( EQUIP_ERR_ITEM_UNIQUE_EQUIPPABLE_SOCKETED, itemTarget, NULL );
1177 return;
1180 for (int j = 0; j < 3; ++j)
1182 if (OldEnchants[j])
1184 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(OldEnchants[j]);
1185 if(!enchantEntry)
1186 continue;
1188 if ((enchantEntry->GemID == Gems[i]->GetProto()->ItemId) && (i != j))
1190 _player->SendEquipError( EQUIP_ERR_ITEM_UNIQUE_EQUIPPABLE_SOCKETED, itemTarget, NULL );
1191 return;
1198 SocketBonusActivated = itemTarget->GemsFitSockets(); //save state of socketbonus
1199 _player->ToggleMetaGemsActive(slot, false); //turn off all metagems (except for the target item)
1201 //if a meta gem is being equipped, all information has to be written to the item before testing if the conditions for the gem are met
1203 //remove ALL enchants
1204 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+3; ++enchant_slot)
1205 _player->ApplyEnchantment(itemTarget,EnchantmentSlot(enchant_slot),false);
1207 for(int i = 0; i < 3; ++i)
1209 if(GemEnchants[i])
1211 itemTarget->SetEnchantment(EnchantmentSlot(SOCK_ENCHANTMENT_SLOT+i), GemEnchants[i],0,0);
1212 if(Item* guidItem = _player->GetItemByGuid(guids[i + 1]))
1213 _player->DestroyItem(guidItem->GetBagSlot(), guidItem->GetSlot(), true );
1217 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+3; ++enchant_slot)
1218 _player->ApplyEnchantment(itemTarget,EnchantmentSlot(enchant_slot),true);
1220 SocketBonusToBeActivated = itemTarget->GemsFitSockets();//current socketbonus state
1221 if(SocketBonusActivated ^ SocketBonusToBeActivated) //if there was a change...
1223 _player->ApplyEnchantment(itemTarget,BONUS_ENCHANTMENT_SLOT,false);
1224 itemTarget->SetEnchantment(BONUS_ENCHANTMENT_SLOT, (SocketBonusToBeActivated ? itemTarget->GetProto()->socketBonus : 0), 0, 0);
1225 _player->ApplyEnchantment(itemTarget,BONUS_ENCHANTMENT_SLOT,true);
1226 //it is not displayed, client has an inbuilt system to determine if the bonus is activated
1229 _player->ToggleMetaGemsActive(slot, true); //turn on all metagems (except for target item)
1232 void WorldSession::HandleCancelTempItemEnchantmentOpcode(WorldPacket& recv_data)
1234 sLog.outDebug("WORLD: CMSG_CANCEL_TEMP_ENCHANTMENT");
1236 CHECK_PACKET_SIZE(recv_data,4);
1238 uint32 eslot;
1240 recv_data >> eslot;
1242 // apply only to equipped item
1243 if(!Player::IsEquipmentPos(INVENTORY_SLOT_BAG_0,eslot))
1244 return;
1246 Item* item = GetPlayer()->GetItemByPos(INVENTORY_SLOT_BAG_0, eslot);
1248 if(!item)
1249 return;
1251 if(!item->GetEnchantmentId(TEMP_ENCHANTMENT_SLOT))
1252 return;
1254 GetPlayer()->ApplyEnchantment(item,TEMP_ENCHANTMENT_SLOT,false);
1255 item->ClearEnchantment(TEMP_ENCHANTMENT_SLOT);