1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WorldSession.h"
#include "BattlePetMgr.h"
#include "Common.h"
#include "Creature.h"
#include "DatabaseEnv.h"
#include "DB2Stores.h"
#include "Item.h"
#include "ItemPackets.h"
#include "Log.h"
#include "NPCPackets.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "World.h"
void WorldSession::HandleSplitItemOpcode(WorldPackets::Item::SplitItem& splitItem)
{
if (!splitItem.Inv.Items.empty())
{
TC_LOG_ERROR("network", "HandleSplitItemOpcode - Invalid ItemCount (" SZFMTD ")", splitItem.Inv.Items.size());
return;
}
TC_LOG_DEBUG("network", "HandleSplitItemOpcode: receive FromPackSlot: %u, FromSlot: %u, ToPackSlot: %u, ToSlot: %u, Quantity: %u",
splitItem.FromPackSlot, splitItem.FromSlot, splitItem.ToPackSlot, splitItem.ToSlot, splitItem.Quantity);
uint16 src = ((splitItem.FromPackSlot << 8) | splitItem.FromSlot);
uint16 dst = ((splitItem.ToPackSlot << 8) | splitItem.ToSlot);
if (src == dst)
return;
// check count - if zero it's fake packet
if (!splitItem.Quantity)
return;
if (!_player->IsValidPos(splitItem.FromPackSlot, splitItem.FromSlot, true))
{
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND);
return;
}
if (!_player->IsValidPos(splitItem.ToPackSlot, splitItem.ToSlot, false)) // can be autostore pos
{
_player->SendEquipError(EQUIP_ERR_WRONG_SLOT);
return;
}
_player->SplitItem(src, dst, splitItem.Quantity);
}
void WorldSession::HandleSwapInvItemOpcode(WorldPackets::Item::SwapInvItem& swapInvItem)
{
if (swapInvItem.Inv.Items.size() != 2)
{
TC_LOG_ERROR("network", "HandleSwapInvItemOpcode - Invalid itemCount (" SZFMTD ")", swapInvItem.Inv.Items.size());
return;
}
TC_LOG_DEBUG("network", "HandleSwapInvItemOpcode: receive Slot1: %u, Slot2: %u",
swapInvItem.Slot1, swapInvItem.Slot2);
// prevent attempt swap same item to current position generated by client at special checting sequence
if (swapInvItem.Slot1 == swapInvItem.Slot2)
return;
if (!_player->IsValidPos(INVENTORY_SLOT_BAG_0, swapInvItem.Slot1, true))
{
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND);
return;
}
if (!_player->IsValidPos(INVENTORY_SLOT_BAG_0, swapInvItem.Slot2, true))
{
_player->SendEquipError(EQUIP_ERR_WRONG_SLOT);
return;
}
if (_player->IsBankPos(INVENTORY_SLOT_BAG_0, swapInvItem.Slot1) && !CanUseBank())
{
TC_LOG_DEBUG("network", "HandleSwapInvItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
if (_player->IsBankPos(INVENTORY_SLOT_BAG_0, swapInvItem.Slot2) && !CanUseBank())
{
TC_LOG_DEBUG("network", "HandleSwapInvItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
uint16 src = ((INVENTORY_SLOT_BAG_0 << 8) | swapInvItem.Slot1);
uint16 dst = ((INVENTORY_SLOT_BAG_0 << 8) | swapInvItem.Slot2);
_player->SwapItem(src, dst);
}
void WorldSession::HandleAutoEquipItemSlotOpcode(WorldPackets::Item::AutoEquipItemSlot& autoEquipItemSlot)
{
// cheating attempt, client should never send opcode in that case
if (autoEquipItemSlot.Inv.Items.size() != 1 || !Player::IsEquipmentPos(INVENTORY_SLOT_BAG_0, autoEquipItemSlot.ItemDstSlot))
return;
Item* item = _player->GetItemByGuid(autoEquipItemSlot.Item);
uint16 dstPos = autoEquipItemSlot.ItemDstSlot | (INVENTORY_SLOT_BAG_0 << 8);
uint16 srcPos = autoEquipItemSlot.Inv.Items[0].Slot | (uint32(autoEquipItemSlot.Inv.Items[0].ContainerSlot) << 8);
if (!item || item->GetPos() != srcPos || srcPos == dstPos)
return;
_player->SwapItem(srcPos, dstPos);
}
void WorldSession::HandleSwapItem(WorldPackets::Item::SwapItem& swapItem)
{
if (swapItem.Inv.Items.size() != 2)
{
TC_LOG_ERROR("network", "HandleSwapItem - Invalid itemCount (" SZFMTD ")", swapItem.Inv.Items.size());
return;
}
TC_LOG_DEBUG("network", "HandleSwapItem: receive ContainerSlotA: %u, SlotA: %u, ContainerSlotB: %u, SlotB: %u",
swapItem.ContainerSlotA, swapItem.SlotA, swapItem.ContainerSlotB, swapItem.SlotB);
uint16 src = ((swapItem.ContainerSlotA << 8) | swapItem.SlotA);
uint16 dst = ((swapItem.ContainerSlotB << 8) | swapItem.SlotB);
// prevent attempt swap same item to current position generated by client at special checting sequence
if (src == dst)
return;
if (!_player->IsValidPos(swapItem.ContainerSlotA, swapItem.SlotA, true))
{
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND);
return;
}
if (!_player->IsValidPos(swapItem.ContainerSlotB, swapItem.SlotB, true))
{
_player->SendEquipError(EQUIP_ERR_WRONG_SLOT);
return;
}
if (_player->IsBankPos(swapItem.ContainerSlotA, swapItem.SlotA) && !CanUseBank())
{
TC_LOG_DEBUG("network", "HandleSwapItem - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
if (_player->IsBankPos(swapItem.ContainerSlotB, swapItem.SlotB) && !CanUseBank())
{
TC_LOG_DEBUG("network", "HandleSwapItem - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str());
return;
}
_player->SwapItem(src, dst);
}
void WorldSession::HandleAutoEquipItemOpcode(WorldPackets::Item::AutoEquipItem& autoEquipItem)
{
if (autoEquipItem.Inv.Items.size() != 1)
{
TC_LOG_ERROR("network", "HandleAutoEquipItemOpcode - Invalid itemCount (" SZFMTD ")", autoEquipItem.Inv.Items.size());
return;
}
TC_LOG_DEBUG("network", "HandleAutoEquipItemOpcode: receive PackSlot: %u, Slot: %u",
autoEquipItem.PackSlot, autoEquipItem.Slot);
Item* srcItem = _player->GetItemByPos(autoEquipItem.PackSlot, autoEquipItem.Slot);
if (!srcItem)
return; // only at cheat
uint16 dest;
InventoryResult msg = _player->CanEquipItem(NULL_SLOT, dest, srcItem, !srcItem->IsBag());
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, srcItem);
return;
}
uint16 src = srcItem->GetPos();
if (dest == src) // prevent equip in same slot, only at cheat
return;
Item* dstItem = _player->GetItemByPos(dest);
if (!dstItem) // empty slot, simple case
{
if (!srcItem->GetChildItem().IsEmpty())
{
InventoryResult childEquipResult = _player->CanEquipChildItem(srcItem);
if (childEquipResult != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, srcItem);
return;
}
}
_player->RemoveItem(autoEquipItem.PackSlot, autoEquipItem.Slot, true);
_player->EquipItem(dest, srcItem, true);
if (!srcItem->GetChildItem().IsEmpty())
_player->EquipChildItem(autoEquipItem.PackSlot, autoEquipItem.Slot, srcItem);
_player->AutoUnequipOffhandIfNeed();
}
else // have currently equipped item, not simple case
{
uint8 dstbag = dstItem->GetBagSlot();
uint8 dstslot = dstItem->GetSlot();
msg = _player->CanUnequipItem(dest, !srcItem->IsBag());
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, dstItem);
return;
}
if (!dstItem->HasItemFlag(ITEM_FIELD_FLAG_CHILD))
{
// check dest->src move possibility
ItemPosCountVec sSrc;
uint16 eSrc = 0;
if (_player->IsInventoryPos(src))
{
msg = _player->CanStoreItem(autoEquipItem.PackSlot, autoEquipItem.Slot, sSrc, dstItem, true);
if (msg != EQUIP_ERR_OK)
msg = _player->CanStoreItem(autoEquipItem.PackSlot, NULL_SLOT, sSrc, dstItem, true);
if (msg != EQUIP_ERR_OK)
msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, sSrc, dstItem, true);
}
else if (_player->IsBankPos(src))
{
msg = _player->CanBankItem(autoEquipItem.PackSlot, autoEquipItem.Slot, sSrc, dstItem, true);
if (msg != EQUIP_ERR_OK)
msg = _player->CanBankItem(autoEquipItem.PackSlot, NULL_SLOT, sSrc, dstItem, true);
if (msg != EQUIP_ERR_OK)
msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, sSrc, dstItem, true);
}
else if (_player->IsEquipmentPos(src))
{
msg = _player->CanEquipItem(autoEquipItem.Slot, eSrc, dstItem, true);
if (msg == EQUIP_ERR_OK)
msg = _player->CanUnequipItem(eSrc, true);
}
if (msg == EQUIP_ERR_OK && Player::IsEquipmentPos(dest) && !srcItem->GetChildItem().IsEmpty())
msg = _player->CanEquipChildItem(srcItem);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, dstItem, srcItem);
return;
}
// now do moves, remove...
_player->RemoveItem(dstbag, dstslot, false);
_player->RemoveItem(autoEquipItem.PackSlot, autoEquipItem.Slot, false);
// add to dest
_player->EquipItem(dest, srcItem, true);
// add to src
if (_player->IsInventoryPos(src))
_player->StoreItem(sSrc, dstItem, true);
else if (_player->IsBankPos(src))
_player->BankItem(sSrc, dstItem, true);
else if (_player->IsEquipmentPos(src))
_player->EquipItem(eSrc, dstItem, true);
if (Player::IsEquipmentPos(dest) && !srcItem->GetChildItem().IsEmpty())
_player->EquipChildItem(autoEquipItem.PackSlot, autoEquipItem.Slot, srcItem);
}
else
{
if (Item* parentItem = _player->GetItemByGuid(dstItem->GetCreator()))
{
if (Player::IsEquipmentPos(dest))
{
_player->AutoUnequipChildItem(parentItem);
// dest is now empty
_player->SwapItem(src, dest);
// src is now empty
_player->SwapItem(parentItem->GetPos(), src);
}
}
}
_player->AutoUnequipOffhandIfNeed();
// if inventory item was moved, check if we can remove dependent auras, because they were not removed in Player::RemoveItem (update was set to false)
// do this after swaps are done, we pass nullptr because both weapons could be swapped and none of them should be ignored
if ((autoEquipItem.PackSlot == INVENTORY_SLOT_BAG_0 && autoEquipItem.Slot < INVENTORY_SLOT_BAG_END) || (dstbag == INVENTORY_SLOT_BAG_0 && dstslot < INVENTORY_SLOT_BAG_END))
_player->ApplyItemDependentAuras((Item*)nullptr, false);
}
}
void WorldSession::HandleDestroyItemOpcode(WorldPackets::Item::DestroyItem& destroyItem)
{
TC_LOG_DEBUG("network", "HandleDestroyItemOpcode: receive ContainerId: %u, SlotNum: %u, Count: %u",
destroyItem.ContainerId, destroyItem.SlotNum, destroyItem.Count);
uint16 pos = (destroyItem.ContainerId << 8) | destroyItem.SlotNum;
// prevent drop unequipable items (in combat, for example) and non-empty bags
if (_player->IsEquipmentPos(pos) || _player->IsBagPos(pos))
{
InventoryResult msg = _player->CanUnequipItem(pos, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, _player->GetItemByPos(pos));
return;
}
}
Item* item = _player->GetItemByPos(destroyItem.ContainerId, destroyItem.SlotNum);
if (!item)
{
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND);
return;
}
if (item->GetTemplate()->HasFlag(ITEM_FLAG_NO_USER_DESTROY))
{
_player->SendEquipError(EQUIP_ERR_DROP_BOUND_ITEM, nullptr, nullptr);
return;
}
if (destroyItem.Count)
{
uint32 i_count = destroyItem.Count;
_player->DestroyItemCount(item, i_count, true);
}
else
_player->DestroyItem(destroyItem.ContainerId, destroyItem.SlotNum, true);
}
void WorldSession::HandleReadItem(WorldPackets::Item::ReadItem& readItem)
{
Item* item = _player->GetItemByPos(readItem.PackSlot, readItem.Slot);
if (item && item->GetTemplate()->GetPageText())
{
InventoryResult msg = _player->CanUseItem(item);
if (msg == EQUIP_ERR_OK)
{
WorldPackets::Item::ReadItemResultOK packet;
packet.Item = item->GetGUID();
SendPacket(packet.Write());
TC_LOG_INFO("network", "STORAGE: Item page sent");
}
else
{
/// @todo: 6.x research new values
/*WorldPackets::Item::ReadItemResultFailed packet;
packet.Item = item->GetGUID();
packet.Subcode = ??;
packet.Delay = ??;
SendPacket(packet.Write());*/
TC_LOG_INFO("network", "STORAGE: Unable to read item");
_player->SendEquipError(msg, item, nullptr);
}
}
else
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, nullptr, nullptr);
}
void WorldSession::HandleSellItemOpcode(WorldPackets::Item::SellItem& packet)
{
TC_LOG_DEBUG("network", "WORLD: Received CMSG_SELL_ITEM: Vendor %s, Item %s, Amount: %u",
packet.VendorGUID.ToString().c_str(), packet.ItemGUID.ToString().c_str(), packet.Amount);
if (packet.ItemGUID.IsEmpty())
return;
Creature* creature = GetPlayer()->GetNPCIfCanInteractWith(packet.VendorGUID, UNIT_NPC_FLAG_VENDOR, UNIT_NPC_FLAG_2_NONE);
if (!creature)
{
TC_LOG_DEBUG("network", "WORLD: HandleSellItemOpcode - %s not found or you can not interact with him.", packet.VendorGUID.ToString().c_str());
_player->SendSellError(SELL_ERR_CANT_FIND_VENDOR, nullptr, packet.ItemGUID);
return;
}
if ((creature->GetCreatureTemplate()->flags_extra & CREATURE_FLAG_EXTRA_NO_SELL_VENDOR) != 0)
{
_player->SendSellError(SELL_ERR_CANT_SELL_TO_THIS_MERCHANT, creature, packet.ItemGUID);
return;
}
// remove fake death
if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
Item* pItem = _player->GetItemByGuid(packet.ItemGUID);
if (pItem)
{
// prevent sell not owner item
if (_player->GetGUID() != pItem->GetOwnerGUID())
{
_player->SendSellError(SELL_ERR_CANT_SELL_ITEM, creature, packet.ItemGUID);
return;
}
// prevent sell non empty bag by drag-and-drop at vendor's item list
if (pItem->IsNotEmptyBag())
{
_player->SendSellError(SELL_ERR_CANT_SELL_ITEM, creature, packet.ItemGUID);
return;
}
// prevent sell currently looted item
if (_player->GetLootGUID() == pItem->GetGUID())
{
_player->SendSellError(SELL_ERR_CANT_SELL_ITEM, creature, packet.ItemGUID);
return;
}
// prevent selling item for sellprice when the item is still refundable
// this probably happens when right clicking a refundable item, the client sends both
// CMSG_SELL_ITEM and CMSG_REFUND_ITEM (unverified)
if (pItem->IsRefundable())
return; // Therefore, no feedback to client
// special case at auto sell (sell all)
if (packet.Amount == 0)
packet.Amount = pItem->GetCount();
else
{
// prevent sell more items that exist in stack (possible only not from client)
if (packet.Amount > pItem->GetCount())
{
_player->SendSellError(SELL_ERR_CANT_SELL_ITEM, creature, packet.ItemGUID);
return;
}
}
ItemTemplate const* pProto = pItem->GetTemplate();
if (pProto)
{
if (pProto->GetSellPrice() > 0)
{
uint64 money = uint64(pProto->GetSellPrice()) * packet.Amount;
if (!_player->ModifyMoney(money)) // ensure player doesn't exceed gold limit
{
_player->SendSellError(SELL_ERR_CANT_SELL_ITEM, creature, packet.ItemGUID);
return;
}
_player->UpdateCriteria(CriteriaType::MoneyEarnedFromSales, money);
_player->UpdateCriteria(CriteriaType::SellItemsToVendors, 1);
if (packet.Amount < pItem->GetCount()) // need split items
{
Item* pNewItem = pItem->CloneItem(packet.Amount, _player);
if (!pNewItem)
{
TC_LOG_ERROR("network", "WORLD: HandleSellItemOpcode - could not create clone of item %u; count = %u", pItem->GetEntry(), packet.Amount);
_player->SendSellError(SELL_ERR_CANT_SELL_ITEM, creature, packet.ItemGUID);
return;
}
pItem->SetCount(pItem->GetCount() - packet.Amount);
_player->ItemRemovedQuestCheck(pItem->GetEntry(), packet.Amount);
if (_player->IsInWorld())
pItem->SendUpdateToPlayer(_player);
pItem->SetState(ITEM_CHANGED, _player);
_player->AddItemToBuyBackSlot(pNewItem);
if (_player->IsInWorld())
pNewItem->SendUpdateToPlayer(_player);
}
else
{
_player->RemoveItem(pItem->GetBagSlot(), pItem->GetSlot(), true);
_player->ItemRemovedQuestCheck(pItem->GetEntry(), pItem->GetCount());
RemoveItemFromUpdateQueueOf(pItem, _player);
_player->AddItemToBuyBackSlot(pItem);
}
}
else
_player->SendSellError(SELL_ERR_CANT_SELL_ITEM, creature, packet.ItemGUID);
return;
}
}
_player->SendSellError(SELL_ERR_CANT_FIND_ITEM, creature, packet.ItemGUID);
return;
}
void WorldSession::HandleBuybackItem(WorldPackets::Item::BuyBackItem& packet)
{
TC_LOG_DEBUG("network", "WORLD: Received CMSG_BUYBACK_ITEM: Vendor %s, Slot: %u", packet.VendorGUID.ToString().c_str(), packet.Slot);
Creature* creature = GetPlayer()->GetNPCIfCanInteractWith(packet.VendorGUID, UNIT_NPC_FLAG_VENDOR, UNIT_NPC_FLAG_2_NONE);
if (!creature)
{
TC_LOG_DEBUG("network", "WORLD: HandleBuybackItem - Unit (%s) not found or you can not interact with him.", packet.VendorGUID.ToString().c_str());
_player->SendSellError(SELL_ERR_CANT_FIND_VENDOR, nullptr, ObjectGuid::Empty);
return;
}
// remove fake death
if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
Item* pItem = _player->GetItemFromBuyBackSlot(packet.Slot);
if (pItem)
{
uint32 price = _player->m_activePlayerData->BuybackPrice[packet.Slot - BUYBACK_SLOT_START];
if (!_player->HasEnoughMoney(uint64(price)))
{
_player->SendBuyError(BUY_ERR_NOT_ENOUGHT_MONEY, creature, pItem->GetEntry(), 0);
return;
}
ItemPosCountVec dest;
InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, pItem, false);
if (msg == EQUIP_ERR_OK)
{
_player->ModifyMoney(-(int32)price);
_player->RemoveItemFromBuyBackSlot(packet.Slot, false);
_player->ItemAddedQuestCheck(pItem->GetEntry(), pItem->GetCount());
_player->StoreItem(dest, pItem, true);
}
else
_player->SendEquipError(msg, pItem, nullptr);
return;
}
else
_player->SendBuyError(BUY_ERR_CANT_FIND_ITEM, creature, 0, 0);
}
void WorldSession::HandleBuyItemOpcode(WorldPackets::Item::BuyItem& packet)
{
// client expects count starting at 1, and we send vendorslot+1 to client already
if (packet.Muid > 0)
--packet.Muid;
else
return; // cheating
switch (packet.ItemType)
{
case ITEM_VENDOR_TYPE_ITEM:
{
Item* bagItem = _player->GetItemByGuid(packet.ContainerGUID);
uint8 bag = NULL_BAG;
if (bagItem && bagItem->IsBag())
bag = bagItem->GetSlot();
else if (packet.ContainerGUID == GetPlayer()->GetGUID()) // The client sends the player guid when trying to store an item in the default backpack
bag = INVENTORY_SLOT_BAG_0;
GetPlayer()->BuyItemFromVendorSlot(packet.VendorGUID, packet.Muid, packet.Item.ItemID,
packet.Quantity, bag, packet.Slot);
break;
}
case ITEM_VENDOR_TYPE_CURRENCY:
{
GetPlayer()->BuyCurrencyFromVendorSlot(packet.VendorGUID, packet.Muid, packet.Item.ItemID, packet.Quantity);
break;
}
default:
{
TC_LOG_DEBUG("network", "WORLD: received wrong itemType (%u) in HandleBuyItemOpcode", packet.ItemType);
break;
}
}
}
void WorldSession::HandleListInventoryOpcode(WorldPackets::NPC::Hello& packet)
{
if (!GetPlayer()->IsAlive())
return;
SendListInventory(packet.Unit);
}
void WorldSession::SendListInventory(ObjectGuid vendorGuid)
{
Creature* vendor = GetPlayer()->GetNPCIfCanInteractWith(vendorGuid, UNIT_NPC_FLAG_VENDOR, UNIT_NPC_FLAG_2_NONE);
if (!vendor)
{
TC_LOG_DEBUG("network", "WORLD: SendListInventory - %s not found or you can not interact with him.", vendorGuid.ToString().c_str());
_player->SendSellError(SELL_ERR_CANT_FIND_VENDOR, nullptr, ObjectGuid::Empty);
return;
}
// remove fake death
if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
// Stop the npc if moving
if (uint32 pause = vendor->GetMovementTemplate().GetInteractionPauseTimer())
vendor->PauseMovement(pause);
vendor->SetHomePosition(vendor->GetPosition());
VendorItemData const* vendorItems = vendor->GetVendorItems();
uint32 rawItemCount = vendorItems ? vendorItems->GetItemCount() : 0;
WorldPackets::NPC::VendorInventory packet;
packet.Vendor = vendor->GetGUID();
packet.Items.resize(rawItemCount);
const float discountMod = _player->GetReputationPriceDiscount(vendor);
uint8 count = 0;
for (uint32 slot = 0; slot < rawItemCount; ++slot)
{
VendorItem const* vendorItem = vendorItems->GetItem(slot);
if (!vendorItem)
continue;
WorldPackets::NPC::VendorItem& item = packet.Items[count];
if (PlayerConditionEntry const* playerCondition = sPlayerConditionStore.LookupEntry(vendorItem->PlayerConditionId))
if (!ConditionMgr::IsPlayerMeetingCondition(_player, playerCondition))
item.PlayerConditionFailed = playerCondition->ID;
if (vendorItem->Type == ITEM_VENDOR_TYPE_ITEM)
{
ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(vendorItem->item);
if (!itemTemplate)
continue;
int32 leftInStock = !vendorItem->maxcount ? -1 : vendor->GetVendorItemCurrentCount(vendorItem);
if (!_player->IsGameMaster()) // ignore conditions if GM on
{
// Respect allowed class
if (!(itemTemplate->GetAllowableClass() & _player->GetClassMask()) && itemTemplate->GetBonding() == BIND_ON_ACQUIRE)
continue;
// Only display items in vendor lists for the team the player is on
if ((itemTemplate->HasFlag(ITEM_FLAG2_FACTION_HORDE) && _player->GetTeam() == ALLIANCE) ||
(itemTemplate->HasFlag(ITEM_FLAG2_FACTION_ALLIANCE) && _player->GetTeam() == HORDE))
continue;
// Items sold out are not displayed in list
if (leftInStock == 0)
continue;
}
if (!sConditionMgr->IsObjectMeetingVendorItemConditions(vendor->GetEntry(), vendorItem->item, _player, vendor))
{
TC_LOG_DEBUG("condition", "SendListInventory: conditions not met for creature entry %u item %u", vendor->GetEntry(), vendorItem->item);
continue;
}
int32 price = vendorItem->IsGoldRequired(itemTemplate) ? uint32(floor(itemTemplate->GetBuyPrice() * discountMod)) : 0;
if (int32 priceMod = _player->GetTotalAuraModifier(SPELL_AURA_MOD_VENDOR_ITEMS_PRICES))
price -= CalculatePct(price, priceMod);
item.MuID = slot + 1; // client expects counting to start at 1
item.Durability = itemTemplate->MaxDurability;
item.ExtendedCostID = vendorItem->ExtendedCost;
item.Type = vendorItem->Type;
item.Quantity = leftInStock;
item.StackCount = itemTemplate->GetBuyCount();
item.Price = price;
item.DoNotFilterOnVendor = vendorItem->IgnoreFiltering;
item.Refundable = itemTemplate->HasFlag(ITEM_FLAG_ITEM_PURCHASE_RECORD) && vendorItem->ExtendedCost && itemTemplate->GetMaxStackSize() == 1;
item.Item.ItemID = vendorItem->item;
if (!vendorItem->BonusListIDs.empty())
{
item.Item.ItemBonus.emplace();
item.Item.ItemBonus->BonusListIDs = vendorItem->BonusListIDs;
}
}
else if (vendorItem->Type == ITEM_VENDOR_TYPE_CURRENCY)
{
CurrencyTypesEntry const* currencyTemplate = sCurrencyTypesStore.LookupEntry(vendorItem->item);
if (!currencyTemplate)
continue;
if (!vendorItem->ExtendedCost)
continue; // there's no price defined for currencies, only extendedcost is used
item.MuID = slot + 1; // client expects counting to start at 1
item.ExtendedCostID = vendorItem->ExtendedCost;
item.Item.ItemID = vendorItem->item;
item.Type = vendorItem->Type;
item.StackCount = vendorItem->maxcount;
item.DoNotFilterOnVendor = vendorItem->IgnoreFiltering;
}
else
continue;
if (++count >= MAX_VENDOR_ITEMS)
break;
}
// Resize vector to real size (some items can be skipped due to checks)
packet.Items.resize(count);
packet.Reason = AsUnderlyingType(count ? VendorInventoryReason::None : VendorInventoryReason::Empty);
SendPacket(packet.Write());
}
void WorldSession::HandleAutoStoreBagItemOpcode(WorldPackets::Item::AutoStoreBagItem& packet)
{
if (!packet.Inv.Items.empty())
{
TC_LOG_ERROR("network", "HandleAutoStoreBagItemOpcode - Invalid itemCount (" SZFMTD ")", packet.Inv.Items.size());
return;
}
TC_LOG_DEBUG("network", "HandleAutoStoreBagItemOpcode: receive ContainerSlotA: %u, SlotA: %u, ContainerSlotB: %u",
packet.ContainerSlotA, packet.SlotA, packet.ContainerSlotB);
Item* item = _player->GetItemByPos(packet.ContainerSlotA, packet.SlotA);
if (!item)
return;
if (!_player->IsValidPos(packet.ContainerSlotB, NULL_SLOT, false)) // can be autostore pos
{
_player->SendEquipError(EQUIP_ERR_WRONG_SLOT);
return;
}
uint16 src = item->GetPos();
// check unequip potability for equipped items and bank bags
if (_player->IsEquipmentPos(src) || _player->IsBagPos(src))
{
InventoryResult msg = _player->CanUnequipItem(src, !_player->IsBagPos(src));
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, item);
return;
}
}
ItemPosCountVec dest;
InventoryResult msg = _player->CanStoreItem(packet.ContainerSlotB, NULL_SLOT, dest, item, false);
if (msg != EQUIP_ERR_OK)
{
_player->SendEquipError(msg, item);
return;
}
// no-op: placed in same slot
if (dest.size() == 1 && dest[0].pos == src)
{
// just remove grey item state
_player->SendEquipError(EQUIP_ERR_INTERNAL_BAG_ERROR, item);
return;
}
_player->RemoveItem(packet.ContainerSlotA, packet.SlotA, true);
_player->StoreItem(dest, item, true);
}
void WorldSession::SendEnchantmentLog(ObjectGuid owner, ObjectGuid caster, ObjectGuid itemGuid, uint32 itemId, uint32 enchantId, uint32 enchantSlot)
{
WorldPackets::Item::EnchantmentLog enchantmentLog;
enchantmentLog.Owner = owner;
enchantmentLog.Caster = caster;
enchantmentLog.ItemGUID = itemGuid;
enchantmentLog.ItemID = itemId;
enchantmentLog.Enchantment = enchantId;
enchantmentLog.EnchantSlot = enchantSlot;
GetPlayer()->SendMessageToSet(enchantmentLog.Write(), true);
}
void WorldSession::SendItemEnchantTimeUpdate(ObjectGuid Playerguid, ObjectGuid Itemguid, uint32 slot, uint32 Duration)
{
WorldPackets::Item::ItemEnchantTimeUpdate data;
data.ItemGuid = Itemguid;
data.DurationLeft = Duration;
data.Slot = slot;
data.OwnerGuid = Playerguid;
SendPacket(data.Write());
}
void WorldSession::HandleWrapItem(WorldPackets::Item::WrapItem& packet)
{
if (packet.Inv.Items.size() != 2)
{
TC_LOG_ERROR("network", "HandleWrapItem - Invalid itemCount (" SZFMTD ")", packet.Inv.Items.size());
return;
}
/// @todo: 6.x find better way for read
// Gift
uint8 giftContainerSlot = packet.Inv.Items[0].ContainerSlot;
uint8 giftSlot = packet.Inv.Items[0].Slot;
// Item
uint8 itemContainerSlot = packet.Inv.Items[1].ContainerSlot;
uint8 itemSlot = packet.Inv.Items[1].Slot;
TC_LOG_DEBUG("network", "HandleWrapItem - Receive giftContainerSlot = %u, giftSlot = %u, itemContainerSlot = %u, itemSlot = %u", giftContainerSlot, giftSlot, itemContainerSlot, itemSlot);
Item* gift = _player->GetItemByPos(giftContainerSlot, giftSlot);
if (!gift)
{
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, gift, nullptr);
return;
}
if (!gift->GetTemplate()->HasFlag(ITEM_FLAG_IS_WRAPPER)) // cheating: non-wrapper wrapper
{
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, gift, nullptr);
return;
}
Item* item = _player->GetItemByPos(itemContainerSlot, itemSlot);
if (!item)
{
_player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, item, nullptr);
return;
}
if (item == gift) // not possable with pacjket from real client
{
_player->SendEquipError(EQUIP_ERR_CANT_WRAP_WRAPPED, item, nullptr);
return;
}
if (item->IsEquipped())
{
_player->SendEquipError(EQUIP_ERR_CANT_WRAP_EQUIPPED, item, nullptr);
return;
}
if (!item->GetGiftCreator().IsEmpty()) // HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED);
{
_player->SendEquipError(EQUIP_ERR_CANT_WRAP_WRAPPED, item, nullptr);
return;
}
if (item->IsBag())
{
_player->SendEquipError(EQUIP_ERR_CANT_WRAP_BAGS, item, nullptr);
return;
}
if (item->IsSoulBound())
{
_player->SendEquipError(EQUIP_ERR_CANT_WRAP_BOUND, item, nullptr);
return;
}
if (item->GetMaxStackCount() != 1)
{
_player->SendEquipError(EQUIP_ERR_CANT_WRAP_STACKABLE, item, nullptr);
return;
}
// maybe not correct check (it is better than nothing)
if (item->GetTemplate()->GetMaxCount() > 0)
{
_player->SendEquipError(EQUIP_ERR_CANT_WRAP_UNIQUE, item, nullptr);
return;
}
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHAR_GIFT);
stmt->setUInt64(0, item->GetOwnerGUID().GetCounter());
stmt->setUInt64(1, item->GetGUID().GetCounter());
stmt->setUInt32(2, item->GetEntry());
stmt->setUInt32(3, item->m_itemData->DynamicFlags);
trans->Append(stmt);
item->SetEntry(gift->GetEntry());
switch (item->GetEntry())
{
case 5042:
item->SetEntry(5043);
break;
case 5048:
item->SetEntry(5044);
break;
case 17303:
item->SetEntry(17302);
break;
case 17304:
item->SetEntry(17305);
break;
case 17307:
item->SetEntry(17308);
break;
case 21830:
item->SetEntry(21831);
break;
}
item->SetGiftCreator(_player->GetGUID());
item->ReplaceAllItemFlags(ITEM_FIELD_FLAG_WRAPPED);
item->SetState(ITEM_CHANGED, _player);
if (item->GetState() == ITEM_NEW) // save new item, to have alway for `character_gifts` record in `item_instance`
{
// after save it will be impossible to remove the item from the queue
RemoveItemFromUpdateQueueOf(item, _player);
item->SaveToDB(trans); // item gave inventory record unchanged and can be save standalone
}
CharacterDatabase.CommitTransaction(trans);
uint32 count = 1;
_player->DestroyItemCount(gift, count, true);
}
void WorldSession::HandleSocketGems(WorldPackets::Item::SocketGems& socketGems)
{
if (!socketGems.ItemGuid)
return;
//cheat -> tried to socket same gem multiple times
if ((!socketGems.GemItem[0].IsEmpty() && (socketGems.GemItem[0] == socketGems.GemItem[1] || socketGems.GemItem[0] == socketGems.GemItem[2])) ||
(!socketGems.GemItem[1].IsEmpty() && (socketGems.GemItem[1] == socketGems.GemItem[2])))
return;
Item* itemTarget = _player->GetItemByGuid(socketGems.ItemGuid);
if (!itemTarget) //missing item to socket
return;
ItemTemplate const* itemProto = itemTarget->GetTemplate();
if (!itemProto)
return;
//this slot is excepted when applying / removing meta gem bonus
uint8 slot = itemTarget->IsEquipped() ? itemTarget->GetSlot() : uint8(NULL_SLOT);
Item* gems[MAX_GEM_SOCKETS];
memset(gems, 0, sizeof(gems));
ItemDynamicFieldGems gemData[MAX_GEM_SOCKETS];
memset(gemData, 0, sizeof(gemData));
GemPropertiesEntry const* gemProperties[MAX_GEM_SOCKETS];
memset(gemProperties, 0, sizeof(gemProperties));
UF::SocketedGem const* oldGemData[MAX_GEM_SOCKETS];
memset(oldGemData, 0, sizeof(oldGemData));
for (uint32 i = 0; i < MAX_GEM_SOCKETS; ++i)
{
if (Item* gem = _player->GetItemByGuid(socketGems.GemItem[i]))
{
gems[i] = gem;
gemData[i].ItemId = gem->GetEntry();
gemData[i].Context = gem->m_itemData->Context;
for (std::size_t b = 0; b < gem->GetBonusListIDs().size() && b < 16; ++b)
gemData[i].BonusListIDs[b] = gem->GetBonusListIDs()[b];
gemProperties[i] = sGemPropertiesStore.LookupEntry(gem->GetTemplate()->GetGemProperties());
}
oldGemData[i] = itemTarget->GetGem(i);
}
// Find first prismatic socket
uint32 firstPrismatic = 0;
while (firstPrismatic < MAX_GEM_SOCKETS && itemTarget->GetSocketColor(firstPrismatic))
++firstPrismatic;
for (uint32 i = 0; i < MAX_GEM_SOCKETS; ++i) //check for hack maybe
{
if (!gemProperties[i])
continue;
// tried to put gem in socket where no socket exists (take care about prismatic sockets)
if (!itemTarget->GetSocketColor(i))
{
// no prismatic socket
if (!itemTarget->GetEnchantmentId(PRISMATIC_ENCHANTMENT_SLOT))
return;
if (i != firstPrismatic)
return;
}
// Gem must match socket color
if (SocketColorToGemTypeMask[itemTarget->GetSocketColor(i)] != gemProperties[i]->Type)
{
// unless its red, blue, yellow or prismatic
if (!(SocketColorToGemTypeMask[itemTarget->GetSocketColor(i)] & SOCKET_COLOR_PRISMATIC) || !(gemProperties[i]->Type & SOCKET_COLOR_PRISMATIC))
return;
}
}
// check unique-equipped conditions
for (uint32 i = 0; i < MAX_GEM_SOCKETS; ++i)
{
if (!gems[i])
continue;
// continue check for case when attempt add 2 similar unique equipped gems in one item.
ItemTemplate const* iGemProto = gems[i]->GetTemplate();
// unique item (for new and already placed bit removed enchantments
if (iGemProto->HasFlag(ITEM_FLAG_UNIQUE_EQUIPPABLE))
{
for (uint32 j = 0; j < MAX_GEM_SOCKETS; ++j)
{
if (i == j) // skip self
continue;
if (gems[j])
{
if (iGemProto->GetId() == gems[j]->GetEntry())
{
_player->SendEquipError(EQUIP_ERR_ITEM_UNIQUE_EQUIPPABLE_SOCKETED, itemTarget, nullptr);
return;
}
}
else if (oldGemData[j])
{
if (int32(iGemProto->GetId()) == oldGemData[j]->ItemID)
{
_player->SendEquipError(EQUIP_ERR_ITEM_UNIQUE_EQUIPPABLE_SOCKETED, itemTarget, nullptr);
return;
}
}
}
}
// unique limit type item
int32 limit_newcount = 0;
if (iGemProto->GetItemLimitCategory())
{
if (ItemLimitCategoryEntry const* limitEntry = sItemLimitCategoryStore.LookupEntry(iGemProto->GetItemLimitCategory()))
{
// NOTE: limitEntry->Flags is not checked because if item has limit then it is applied in equip case
for (int j = 0; j < MAX_GEM_SOCKETS; ++j)
{
if (gems[j])
{
// new gem
if (iGemProto->GetItemLimitCategory() == gems[j]->GetTemplate()->GetItemLimitCategory())
++limit_newcount;
}
else if (oldGemData[j])
{
// existing gem
if (ItemTemplate const* jProto = sObjectMgr->GetItemTemplate(oldGemData[j]->ItemID))
if (iGemProto->GetItemLimitCategory() == jProto->GetItemLimitCategory())
++limit_newcount;
}
}
if (limit_newcount > 0 && uint32(limit_newcount) > _player->GetItemLimitCategoryQuantity(limitEntry))
{
_player->SendEquipError(EQUIP_ERR_ITEM_UNIQUE_EQUIPPABLE_SOCKETED, itemTarget, nullptr);
return;
}
}
}
// for equipped item check all equipment for duplicate equipped gems
if (itemTarget->IsEquipped())
{
if (InventoryResult res = _player->CanEquipUniqueItem(gems[i], slot, std::max(limit_newcount, 0)))
{
_player->SendEquipError(res, itemTarget, nullptr);
return;
}
}
}
bool SocketBonusActivated = itemTarget->GemsFitSockets(); //save state of socketbonus
_player->ToggleMetaGemsActive(slot, false); //turn off all metagems (except for the target item)
//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
//remove ALL mods - gem can change item level
if (itemTarget->IsEquipped())
_player->_ApplyItemMods(itemTarget, itemTarget->GetSlot(), false);
for (uint16 i = 0; i < MAX_GEM_SOCKETS; ++i)
{
if (gems[i])
{
uint32 gemScalingLevel = _player->GetLevel();
if (uint32 fixedLevel = gems[i]->GetModifier(ITEM_MODIFIER_TIMEWALKER_LEVEL))
gemScalingLevel = fixedLevel;
itemTarget->SetGem(i, &gemData[i], gemScalingLevel);
if (gemProperties[i] && gemProperties[i]->EnchantId)
itemTarget->SetEnchantment(EnchantmentSlot(SOCK_ENCHANTMENT_SLOT + i), gemProperties[i]->EnchantId, 0, 0, _player->GetGUID());
uint32 gemCount = 1;
_player->DestroyItemCount(gems[i], gemCount, true);
}
}
if (itemTarget->IsEquipped())
_player->_ApplyItemMods(itemTarget, itemTarget->GetSlot(), true);
if (Item* childItem = _player->GetChildItemByGuid(itemTarget->GetChildItem()))
{
if (childItem->IsEquipped())
_player->_ApplyItemMods(childItem, childItem->GetSlot(), false);
childItem->CopyArtifactDataFromParent(itemTarget);
if (childItem->IsEquipped())
_player->_ApplyItemMods(childItem, childItem->GetSlot(), true);
}
bool SocketBonusToBeActivated = itemTarget->GemsFitSockets();//current socketbonus state
if (SocketBonusActivated ^ SocketBonusToBeActivated) //if there was a change...
{
_player->ApplyEnchantment(itemTarget, BONUS_ENCHANTMENT_SLOT, false);
itemTarget->SetEnchantment(BONUS_ENCHANTMENT_SLOT, (SocketBonusToBeActivated ? itemTarget->GetTemplate()->GetSocketBonus() : 0), 0, 0, _player->GetGUID());
_player->ApplyEnchantment(itemTarget, BONUS_ENCHANTMENT_SLOT, true);
//it is not displayed, client has an inbuilt system to determine if the bonus is activated
}
_player->ToggleMetaGemsActive(slot, true); //turn on all metagems (except for target item)
_player->RemoveTradeableItem(itemTarget);
itemTarget->ClearSoulboundTradeable(_player); // clear tradeable flag
itemTarget->SendUpdateSockets();
}
void WorldSession::HandleCancelTempEnchantmentOpcode(WorldPackets::Item::CancelTempEnchantment& cancelTempEnchantment)
{
// apply only to equipped item
if (!Player::IsEquipmentPos(INVENTORY_SLOT_BAG_0, cancelTempEnchantment.Slot))
return;
Item* item = GetPlayer()->GetItemByPos(INVENTORY_SLOT_BAG_0, cancelTempEnchantment.Slot);
if (!item)
return;
if (!item->GetEnchantmentId(TEMP_ENCHANTMENT_SLOT))
return;
GetPlayer()->ApplyEnchantment(item, TEMP_ENCHANTMENT_SLOT, false);
item->ClearEnchantment(TEMP_ENCHANTMENT_SLOT);
}
void WorldSession::HandleGetItemPurchaseData(WorldPackets::Item::GetItemPurchaseData& packet)
{
Item* item = _player->GetItemByGuid(packet.ItemGUID);
if (!item)
{
TC_LOG_DEBUG("network", "HandleGetItemPurchaseData: Item %s not found!", packet.ItemGUID.ToString().c_str());
return;
}
TC_LOG_DEBUG("network", "HandleGetItemPurchaseData: Item %s", packet.ItemGUID.ToString().c_str());
GetPlayer()->SendRefundInfo(item);
}
void WorldSession::HandleItemRefund(WorldPackets::Item::ItemPurchaseRefund& packet)
{
Item* item = _player->GetItemByGuid(packet.ItemGUID);
if (!item)
{
TC_LOG_DEBUG("network", "WorldSession::HandleItemRefund: Item (%s) not found!", packet.ItemGUID.ToString().c_str());
return;
}
// Don't try to refund item currently being disenchanted
if (_player->GetLootGUID() == packet.ItemGUID)
return;
GetPlayer()->RefundItem(item);
}
bool WorldSession::CanUseBank(ObjectGuid bankerGUID) const
{
// bankerGUID parameter is optional, set to 0 by default.
if (!bankerGUID)
bankerGUID = m_currentBankerGUID;
bool isUsingBankCommand = (bankerGUID == GetPlayer()->GetGUID() && bankerGUID == m_currentBankerGUID);
if (!isUsingBankCommand)
{
Creature* creature = GetPlayer()->GetNPCIfCanInteractWith(bankerGUID, UNIT_NPC_FLAG_BANKER, UNIT_NPC_FLAG_2_NONE);
if (!creature)
return false;
}
return true;
}
void WorldSession::HandleUseCritterItem(WorldPackets::Item::UseCritterItem& useCritterItem)
{
Item* item = _player->GetItemByGuid(useCritterItem.ItemGuid);
if (!item)
return;
for (ItemEffectEntry const* itemEffect : item->GetEffects())
{
if (itemEffect->TriggerType != ITEM_SPELLTRIGGER_ON_LEARN)
continue;
if (BattlePetSpeciesEntry const* speciesEntry = BattlePets::BattlePetMgr::GetBattlePetSpeciesBySpell(uint32(itemEffect->SpellID)))
GetBattlePetMgr()->AddPet(speciesEntry->ID, BattlePets::BattlePetMgr::SelectPetDisplay(speciesEntry),
BattlePets::BattlePetMgr::RollPetBreed(speciesEntry->ID), BattlePets::BattlePetMgr::GetDefaultPetQuality(speciesEntry->ID));
}
_player->DestroyItem(item->GetBagSlot(), item->GetSlot(), true);
}
void WorldSession::HandleSortBags(WorldPackets::Item::SortBags& /*sortBags*/)
{
// TODO: Implement sorting
// Placeholder to prevent completely locking out bags clientside
SendPacket(WorldPackets::Item::BagCleanupFinished().Write());
}
void WorldSession::HandleSortBankBags(WorldPackets::Item::SortBankBags& /*sortBankBags*/)
{
// TODO: Implement sorting
// Placeholder to prevent completely locking out bags clientside
SendPacket(WorldPackets::Item::BagCleanupFinished().Write());
}
void WorldSession::HandleSortReagentBankBags(WorldPackets::Item::SortReagentBankBags& /*sortReagentBankBags*/)
{
// TODO: Implement sorting
// Placeholder to prevent completely locking out bags clientside
SendPacket(WorldPackets::Item::BagCleanupFinished().Write());
}
void WorldSession::HandleRemoveNewItem(WorldPackets::Item::RemoveNewItem& removeNewItem)
{
Item* item = _player->GetItemByGuid(removeNewItem.ItemGuid);
if (!item)
{
TC_LOG_DEBUG("network", "WorldSession::HandleRemoveNewItem: Item (%s) not found for %s!", removeNewItem.ItemGuid.ToString().c_str(), GetPlayerInfo().c_str());
return;
}
if (item->HasItemFlag(ITEM_FIELD_FLAG_NEW_ITEM))
{
item->RemoveItemFlag(ITEM_FIELD_FLAG_NEW_ITEM);
item->SetState(ITEM_CHANGED, _player);
}
}
|