forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathItemMethods.h
768 lines (697 loc) · 19.1 KB
/
ItemMethods.h
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
/*
* Copyright (C) 2010 - 2016 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#ifndef ITEMMETHODS_H
#define ITEMMETHODS_H
/***
* Inherits all methods from: [Object]
*/
namespace LuaItem
{
/**
* Returns 'true' if the [Item] is soulbound, 'false' otherwise
*
* @return bool isSoulBound
*/
int IsSoulBound(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsSoulBound());
return 1;
}
#if (!defined(TBC) && !defined(CLASSIC))
/**
* Returns 'true' if the [Item] is account bound, 'false' otherwise
*
* @return bool isAccountBound
*/
int IsBoundAccountWide(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsBoundAccountWide());
return 1;
}
#endif
/**
* Returns 'true' if the [Item] is bound to a [Player] by an enchant, 'false' otehrwise
*
* @return bool isBoundByEnchant
*/
int IsBoundByEnchant(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsBoundByEnchant());
return 1;
}
/**
* Returns 'true' if the [Item] is not bound to the [Player] specified, 'false' otherwise
*
* @param [Player] player : the [Player] object to check the item against
* @return bool isNotBound
*/
int IsNotBoundToPlayer(lua_State* L, Item* item)
{
Player* player = Eluna::CHECKOBJ<Player>(L, 2);
Eluna::Push(L, item->IsBindedNotWith(player));
return 1;
}
/**
* Returns 'true' if the [Item] is locked, 'false' otherwise
*
* @return bool isLocked
*/
int IsLocked(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsLocked());
return 1;
}
/**
* Returns 'true' if the [Item] is a bag, 'false' otherwise
*
* @return bool isBag
*/
int IsBag(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsBag());
return 1;
}
#ifndef CLASSIC
/**
* Returns 'true' if the [Item] is a currency token, 'false' otherwise
*
* @return bool isCurrencyToken
*/
int IsCurrencyToken(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsCurrencyToken());
return 1;
}
#endif
/**
* Returns 'true' if the [Item] is a not an empty bag, 'false' otherwise
*
* @return bool isNotEmptyBag
*/
int IsNotEmptyBag(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsNotEmptyBag());
return 1;
}
/**
* Returns 'true' if the [Item] is broken, 'false' otherwise
*
* @return bool isBroken
*/
int IsBroken(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsBroken());
return 1;
}
/**
* Returns 'true' if the [Item] can be traded, 'false' otherwise
*
* @return bool isTradeable
*/
int CanBeTraded(lua_State* L, Item* item)
{
#if (defined(TBC) || defined(CLASSIC))
Eluna::Push(L, item->CanBeTraded());
#else
bool mail = Eluna::CHECKVAL<bool>(L, 2, false);
Eluna::Push(L, item->CanBeTraded(mail));
#endif
return 1;
}
/**
* Returns 'true' if the [Item] is currently in a trade window, 'false' otherwise
*
* @return bool isInTrade
*/
int IsInTrade(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsInTrade());
return 1;
}
/**
* Returns 'true' if the [Item] is currently in a bag, 'false' otherwise
*
* @return bool isInBag
*/
int IsInBag(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsInBag());
return 1;
}
/**
* Returns 'true' if the [Item] is currently equipped, 'false' otherwise
*
* @return bool isEquipped
*/
int IsEquipped(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsEquipped());
return 1;
}
/**
* Returns 'true' if the [Item] has the [Quest] specified tied to it, 'false' otherwise
*
* @param uint32 questId : the [Quest] id to be checked
* @return bool hasQuest
*/
int HasQuest(lua_State* L, Item* item)
{
uint32 quest = Eluna::CHECKVAL<uint32>(L, 2);
#if defined TRINITY || AZEROTHCORE
Eluna::Push(L, item->hasQuest(quest));
#else
Eluna::Push(L, item->HasQuest(quest));
#endif
return 1;
}
/**
* Returns 'true' if the [Item] is a potion, 'false' otherwise
*
* @return bool isPotion
*/
int IsPotion(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsPotion());
return 1;
}
#if defined(WOTLK)
/**
* Returns 'true' if the [Item] is a weapon vellum, 'false' otherwise
*
* @return bool isWeaponVellum
*/
int IsWeaponVellum(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsWeaponVellum());
return 1;
}
/**
* Returns 'true' if the [Item] is an armor vellum, 'false' otherwise
*
* @return bool isArmorVellum
*/
int IsArmorVellum(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsArmorVellum());
return 1;
}
#endif
/**
* Returns 'true' if the [Item] is a conjured consumable, 'false' otherwise
*
* @return bool isConjuredConsumable
*/
int IsConjuredConsumable(lua_State* L, Item* item)
{
Eluna::Push(L, item->IsConjuredConsumable());
return 1;
}
/*int IsRefundExpired(lua_State* L, Item* item)// TODO: Implement core support
{
Eluna::Push(L, item->IsRefundExpired());
return 1;
}*/
/**
* Returns the chat link of the [Item]
*
* <pre>
* enum LocaleConstant
* {
* LOCALE_enUS = 0,
* LOCALE_koKR = 1,
* LOCALE_frFR = 2,
* LOCALE_deDE = 3,
* LOCALE_zhCN = 4,
* LOCALE_zhTW = 5,
* LOCALE_esES = 6,
* LOCALE_esMX = 7,
* LOCALE_ruRU = 8
* };
* </pre>
*
* @param [LocaleConstant] locale = DEFAULT_LOCALE : locale to return the [Item]'s name in
* @return string itemLink
*/
int GetItemLink(lua_State* L, Item* item)
{
uint8 locale = Eluna::CHECKVAL<uint8>(L, 2, DEFAULT_LOCALE);
if (locale >= TOTAL_LOCALES)
return luaL_argerror(L, 2, "valid LocaleConstant expected");
const ItemTemplate* temp = item->GetTemplate();
std::string name = temp->Name1;
if (ItemLocale const* il = eObjectMgr->GetItemLocale(temp->ItemId))
ObjectMgr::GetLocaleString(il->Name, static_cast<LocaleConstant>(locale), name);
#ifndef CLASSIC
if (int32 itemRandPropId = item->GetItemRandomPropertyId())
{
#if defined(CATA) || defined (MISTS)
char* suffix = NULL;
#else
#ifdef TRINITY
std::array<char const*, 16> const* suffix = NULL;
#else
char* const* suffix = NULL;
#endif
#endif
if (itemRandPropId < 0)
{
const ItemRandomSuffixEntry* itemRandEntry = sItemRandomSuffixStore.LookupEntry(-item->GetItemRandomPropertyId());
if (itemRandEntry)
#ifdef TRINITY
suffix = &itemRandEntry->Name;
#else
suffix = itemRandEntry->nameSuffix;
#endif
}
else
{
const ItemRandomPropertiesEntry* itemRandEntry = sItemRandomPropertiesStore.LookupEntry(item->GetItemRandomPropertyId());
if (itemRandEntry)
#ifdef TRINITY
suffix = &itemRandEntry->Name;
#else
suffix = itemRandEntry->nameSuffix;
#endif
}
if (suffix)
{
name += ' ';
#if defined TRINITY
name += (*suffix)[(name != temp->Name1) ? locale : uint8(DEFAULT_LOCALE)];
#else
name += suffix[(name != temp->Name1) ? locale : uint8(DEFAULT_LOCALE)];
#endif
}
}
#endif
std::ostringstream oss;
oss << "|c" << std::hex << ItemQualityColors[temp->Quality] << std::dec <<
"|Hitem:" << temp->ItemId << ":" <<
item->GetEnchantmentId(PERM_ENCHANTMENT_SLOT) << ":" <<
#ifndef CLASSIC
item->GetEnchantmentId(SOCK_ENCHANTMENT_SLOT) << ":" <<
item->GetEnchantmentId(SOCK_ENCHANTMENT_SLOT_2) << ":" <<
item->GetEnchantmentId(SOCK_ENCHANTMENT_SLOT_3) << ":" <<
item->GetEnchantmentId(BONUS_ENCHANTMENT_SLOT) << ":" <<
#endif
item->GetItemRandomPropertyId() << ":" << item->GetItemSuffixFactor() << ":" <<
#if defined(TRINITY) || CMANGOS
(uint32)item->GetOwner()->GetLevel() << "|h[" << name << "]|h|r";
#else
(uint32)item->GetOwner()->getLevel() << "|h[" << name << "]|h|r";
#endif
Eluna::Push(L, oss.str());
return 1;
}
int GetOwnerGUID(lua_State* L, Item* item)
{
#if defined TRINITY || AZEROTHCORE
Eluna::Push(L, item->GetOwnerGUID());
#else
Eluna::Push(L, item->GetOwnerGuid());
#endif
return 1;
}
/**
* Returns the [Player] who currently owns the [Item]
*
* @return [Player] player : the [Player] who owns the [Item]
*/
int GetOwner(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetOwner());
return 1;
}
/**
* Returns the [Item]s stack count
*
* @return uint32 count
*/
int GetCount(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetCount());
return 1;
}
/**
* Returns the [Item]s max stack count
*
* @return uint32 maxCount
*/
int GetMaxStackCount(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetMaxStackCount());
return 1;
}
/**
* Returns the [Item]s current slot
*
* @return uint8 slot
*/
int GetSlot(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetSlot());
return 1;
}
/**
* Returns the [Item]s current bag slot
*
* @return uint8 bagSlot
*/
int GetBagSlot(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetBagSlot());
return 1;
}
/**
* Returns the [Item]s enchantment ID by enchant slot specified
*
* @param [EnchantmentSlot] enchantSlot : the enchant slot specified
* @return uint32 enchantId : the id of the enchant slot specified
*/
int GetEnchantmentId(lua_State* L, Item* item)
{
uint32 enchant_slot = Eluna::CHECKVAL<uint32>(L, 2);
if (enchant_slot >= MAX_INSPECTED_ENCHANTMENT_SLOT)
return luaL_argerror(L, 2, "valid EnchantmentSlot expected");
Eluna::Push(L, item->GetEnchantmentId(EnchantmentSlot(enchant_slot)));
return 1;
}
/**
* Returns the spell ID tied to the [Item] by spell index
*
* @param uint32 spellIndex : the spell index specified
* @return uint32 spellId : the id of the spell
*/
int GetSpellId(lua_State* L, Item* item)
{
uint32 index = Eluna::CHECKVAL<uint32>(L, 2);
if (index >= MAX_ITEM_PROTO_SPELLS)
return luaL_argerror(L, 2, "valid SpellIndex expected");
Eluna::Push(L, item->GetTemplate()->Spells[index].SpellId);
return 1;
}
/**
* Returns the spell trigger tied to the [Item] by spell index
*
* @param uint32 spellIndex : the spell index specified
* @return uint32 spellTrigger : the spell trigger of the specified index
*/
int GetSpellTrigger(lua_State* L, Item* item)
{
uint32 index = Eluna::CHECKVAL<uint32>(L, 2);
if (index >= MAX_ITEM_PROTO_SPELLS)
return luaL_argerror(L, 2, "valid SpellIndex expected");
Eluna::Push(L, item->GetTemplate()->Spells[index].SpellTrigger);
return 1;
}
/**
* Returns class of the [Item]
*
* @return uint32 class
*/
int GetClass(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->Class);
return 1;
}
/**
* Returns subclass of the [Item]
*
* @return uint32 subClass
*/
int GetSubClass(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->SubClass);
return 1;
}
/**
* Returns the name of the [Item]
*
* @return string name
*/
int GetName(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->Name1);
return 1;
}
/**
* Returns the display ID of the [Item]
*
* @return uint32 displayId
*/
int GetDisplayId(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->DisplayInfoID);
return 1;
}
/**
* Returns the quality of the [Item]
*
* @return uint32 quality
*/
int GetQuality(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->Quality);
return 1;
}
/**
* Returns the default purchase count of the [Item]
*
* @return uint32 count
*/
int GetBuyCount(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->BuyCount);
return 1;
}
/**
* Returns the purchase price of the [Item]
*
* @return uint32 price
*/
int GetBuyPrice(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->BuyPrice);
return 1;
}
/**
* Returns the sell price of the [Item]
*
* @return uint32 price
*/
int GetSellPrice(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->SellPrice);
return 1;
}
/**
* Returns the inventory type of the [Item]
*
* @return uint32 inventoryType
*/
int GetInventoryType(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->InventoryType);
return 1;
}
/**
* Returns the [Player] classes allowed to use this [Item]
*
* @return uint32 allowableClass
*/
int GetAllowableClass(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->AllowableClass);
return 1;
}
/**
* Returns the [Player] races allowed to use this [Item]
*
* @return uint32 allowableRace
*/
int GetAllowableRace(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->AllowableRace);
return 1;
}
/**
* Returns the [Item]s level
*
* @return uint32 itemLevel
*/
int GetItemLevel(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->ItemLevel);
return 1;
}
/**
* Returns the minimum level required to use this [Item]
*
* @return uint32 requiredLevel
*/
int GetRequiredLevel(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->RequiredLevel);
return 1;
}
#ifdef WOTLK
int GetStatsCount(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->StatsCount);
return 1;
}
#endif
/**
* Returns the random property ID of this [Item]
*
* @return uint32 randomPropertyId
*/
int GetRandomProperty(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->RandomProperty);
return 1;
}
#ifndef CLASSIC
int GetRandomSuffix(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->RandomSuffix);
return 1;
}
#endif
/**
* Returns the item set ID of this [Item]
*
* @return uint32 itemSetId
*/
int GetItemSet(lua_State* L, Item* item)
{
Eluna::Push(L, item->GetTemplate()->ItemSet);
return 1;
}
/**
* Returns the bag size of this [Item], 0 if [Item] is not a bag
*
* @return uint32 bagSize
*/
int GetBagSize(lua_State* L, Item* item)
{
if (Bag* bag = item->ToBag())
Eluna::Push(L, bag->GetBagSize());
else
Eluna::Push(L, 0);
return 1;
}
/**
* Sets the [Player] specified as the owner of the [Item]
*
* @param [Player] player : the [Player] specified
*/
int SetOwner(lua_State* L, Item* item)
{
Player* player = Eluna::CHECKOBJ<Player>(L, 2);
#if defined TRINITY || AZEROTHCORE
item->SetOwnerGUID(player->GET_GUID());
#else
item->SetOwnerGuid(player->GET_GUID());
#endif
return 0;
}
/**
* Sets the binding of the [Item] to 'true' or 'false'
*
* @param bool setBinding
*/
int SetBinding(lua_State* L, Item* item)
{
bool soulbound = Eluna::CHECKVAL<bool>(L, 2);
item->SetBinding(soulbound);
item->SetState(ITEM_CHANGED, item->GetOwner());
return 0;
}
/**
* Sets the stack count of the [Item]
*
* @param uint32 count
*/
int SetCount(lua_State* L, Item* item)
{
uint32 count = Eluna::CHECKVAL<uint32>(L, 2);
item->SetCount(count);
return 0;
}
/**
* Sets the specified enchantment of the [Item] to the specified slot
*
* @param uint32 enchantId : the ID of the enchant to be applied
* @param uint32 enchantSlot : the slot for the enchant to be applied to
* @return bool enchantmentSuccess : if enchantment is successfully set to specified slot, returns 'true', otherwise 'false'
*/
int SetEnchantment(lua_State* L, Item* item)
{
Player* owner = item->GetOwner();
if (!owner)
{
Eluna::Push(L, false);
return 1;
}
uint32 enchant = Eluna::CHECKVAL<uint32>(L, 2);
if (!sSpellItemEnchantmentStore.LookupEntry(enchant))
{
Eluna::Push(L, false);
return 1;
}
EnchantmentSlot slot = (EnchantmentSlot)Eluna::CHECKVAL<uint32>(L, 3);
if (slot >= MAX_INSPECTED_ENCHANTMENT_SLOT)
return luaL_argerror(L, 2, "valid EnchantmentSlot expected");
owner->ApplyEnchantment(item, slot, false);
item->SetEnchantment(slot, enchant, 0, 0);
owner->ApplyEnchantment(item, slot, true);
Eluna::Push(L, true);
return 1;
}
/* OTHER */
/**
* Removes an enchant from the [Item] by the specified slot
*
* @param uint32 enchantSlot : the slot for the enchant to be removed from
* @return bool enchantmentRemoved : if enchantment is successfully removed from specified slot, returns 'true', otherwise 'false'
*/
int ClearEnchantment(lua_State* L, Item* item)
{
Player* owner = item->GetOwner();
if (!owner)
{
Eluna::Push(L, false);
return 1;
}
EnchantmentSlot slot = (EnchantmentSlot)Eluna::CHECKVAL<uint32>(L, 2);
if (slot >= MAX_INSPECTED_ENCHANTMENT_SLOT)
return luaL_argerror(L, 2, "valid EnchantmentSlot expected");
if (!item->GetEnchantmentId(slot))
{
Eluna::Push(L, false);
return 1;
}
owner->ApplyEnchantment(item, slot, false);
item->ClearEnchantment(slot);
Eluna::Push(L, true);
return 1;
}
/**
* Saves the [Item] to the database
*/
int SaveToDB(lua_State* /*L*/, Item* item)
{
#if defined TRINITY || defined AZEROTHCORE
CharacterDatabaseTransaction trans = CharacterDatabaseTransaction(nullptr);
item->SaveToDB(trans);
#else
item->SaveToDB();
#endif
return 0;
}
};
#endif