Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
ItemMethods.cpp
Go to the documentation of this file.
1/*
2* Copyright (C) 2010 - 2013 Eluna Lua Engine <http://emudevs.com/>
3* This program is free software licensed under GPL version 3
4* Please see the included DOCS/LICENSE.TXT for more information
5*/
6
7#include "ItemMethods.h"
8#include "Includes.h"
9
10namespace LuaItem
11{
12 int GetItemLink(lua_State* L, Item* item) // TODO: Implement
13 {
14 // LOCALE_enUS = 0,
15 // LOCALE_koKR = 1,
16 // LOCALE_frFR = 2,
17 // LOCALE_deDE = 3,
18 // LOCALE_zhCN = 4,
19 // LOCALE_zhTW = 5,
20 // LOCALE_esES = 6,
21 // LOCALE_esMX = 7,
22 // LOCALE_ruRU = 8
23
24 int loc_idx = luaL_optint(L, 1, DEFAULT_LOCALE);
25 if (loc_idx < 0 || loc_idx >= MAX_LOCALES)
26 {
27 luaL_error(L, "Invalid locale index (%d)", loc_idx);
28 return 0;
29 }
30
31 const ItemTemplate* temp = item->GetTemplate();
32
33 std::string name = temp->Name1;
34 if (ItemLocale const* il = sObjectMgr->GetItemLocale(temp->ItemId))
35 ObjectMgr::GetLocaleString(il->Name, loc_idx, name);
36
37 if (int32 itemRandPropId = item->GetItemRandomPropertyId())
38 {
39 char* const* suffix = NULL;
40 if (itemRandPropId < 0)
41 {
42 const ItemRandomSuffixEntry* itemRandEntry = sItemRandomSuffixStore.LookupEntry(-item->GetItemRandomPropertyId());
43 if (itemRandEntry)
44 suffix = (char* const*)itemRandEntry->nameSuffix[loc_idx];
45 }
46 else
47 {
48 const ItemRandomPropertiesEntry* itemRandEntry = sItemRandomPropertiesStore.LookupEntry(item->GetItemRandomPropertyId());
49 if (itemRandEntry)
50 suffix = (char* const*)itemRandEntry->nameSuffix[loc_idx];
51 }
52 if (suffix)
53 {
54 std::string test(suffix[(name != temp->Name1) ? loc_idx : DEFAULT_LOCALE]);
55 if (!test.empty())
56 {
57 name += ' ';
58 name += test;
59 }
60 }
61 }
62
63 std::ostringstream oss;
64 oss << "|c" << std::hex << ItemQualityColors[temp->Quality] << std::dec <<
65 "|Hitem:" << temp->ItemId << ":" <<
71 item->GetItemRandomPropertyId() << ":" << item->GetItemSuffixFactor() << ":" <<
72 (uint32)item->GetOwner()->getLevel() << "|h[" << name << "]|h|r";
73
74 sEluna->Push(L, oss.str());
75 return 1;
76 }
77
78 int GetGUID(lua_State* L, Item* item)
79 {
80 sEluna->Push(L, uint64(item->GetGUID()));
81 return 1;
82 }
83
84 int GetOwnerGUID(lua_State* L, Item* item)
85 {
86 sEluna->Push(L, uint64(item->GetOwnerGUID()));
87 return 1;
88 }
89
90 int GetOwner(lua_State* L, Item* item)
91 {
92 sEluna->Push(L, item->GetOwner());
93 return 1;
94 }
95
96 int SetOwner(lua_State* L, Item* item)
97 {
98 Player* player = sEluna->CHECK_PLAYER(L, 1);
99
100 if (player)
101 item->SetOwnerGUID(player->GetGUID());
102 return 0;
103 }
104
105 int SetBinding(lua_State* L, Item* item)
106 {
107 bool soulbound = luaL_checkbool(L, 1);
108
109 item->SetBinding(soulbound);
110 return 0;
111 }
112
113 int IsSoulBound(lua_State* L, Item* item)
114 {
115 sEluna->Push(L, item->IsSoulBound());
116 return 1;
117 }
118
119 int IsBoundAccountWide(lua_State* L, Item* item)
120 {
121 sEluna->Push(L, item->IsBoundAccountWide());
122 return 1;
123 }
124
125 int IsBoundByEnchant(lua_State* L, Item* item)
126 {
127 sEluna->Push(L, item->IsBoundByEnchant());
128 return 1;
129 }
130
131 int IsNotBoundToPlayer(lua_State* L, Item* item)
132 {
133 Player* player = sEluna->CHECK_PLAYER(L, 1);
134 if (!player)
135 return 0;
136
137 sEluna->Push(L, item->IsBindedNotWith(player));
138 return 1;
139 }
140
141 int IsLocked(lua_State* L, Item* item)
142 {
143 sEluna->Push(L, item->IsLocked());
144 return 1;
145 }
146
147 int IsBag(lua_State* L, Item* item)
148 {
149 sEluna->Push(L, item->IsBag());
150 return 1;
151 }
152
153 int IsCurrencyToken(lua_State* L, Item* item)
154 {
155 sEluna->Push(L, item->IsCurrencyToken());
156 return 1;
157 }
158
159 int IsNotEmptyBag(lua_State* L, Item* item)
160 {
161 sEluna->Push(L, item->IsNotEmptyBag());
162 return 1;
163 }
164
165 int IsBroken(lua_State* L, Item* item)
166 {
167 sEluna->Push(L, item->IsBroken());
168 return 1;
169 }
170
171 int CanBeTraded(lua_State* L, Item* item)
172 {
173 bool mail = luaL_optbool(L, 1, false);
174 sEluna->Push(L, item->CanBeTraded(mail));
175 return 1;
176 }
177
178 int IsInTrade(lua_State* L, Item* item)
179 {
180 sEluna->Push(L, item->IsInTrade());
181 return 1;
182 }
183
184 int GetCount(lua_State* L, Item* item)
185 {
186 sEluna->Push(L, item->GetCount());
187 return 1;
188 }
189
190 int SetCount(lua_State* L, Item* item)
191 {
192 uint32 count = luaL_checkunsigned(L, 1);
193 item->SetCount(count);
194 return 0;
195 }
196
197 int GetMaxStackCount(lua_State* L, Item* item)
198 {
199 sEluna->Push(L, item->GetMaxStackCount());
200 return 1;
201 }
202
203 int GetSlot(lua_State* L, Item* item)
204 {
205 sEluna->Push(L, item->GetSlot());
206 return 1;
207 }
208
209 int GetBagSlot(lua_State* L, Item* item)
210 {
211 sEluna->Push(L, item->GetBagSlot());
212 return 1;
213 }
214
215 int IsInBag(lua_State* L, Item* item)
216 {
217 sEluna->Push(L, item->IsInBag());
218 return 1;
219 }
220
221 int IsEquipped(lua_State* L, Item* item)
222 {
223 sEluna->Push(L, item->IsEquipped());
224 return 1;
225 }
226
227 int HasQuest(lua_State* L, Item* item)
228 {
229 uint32 quest = luaL_checkunsigned(L, 1);
230 sEluna->Push(L, item->hasQuest(quest));
231 return 1;
232 }
233
234 int IsPotion(lua_State* L, Item* item)
235 {
236 sEluna->Push(L, item->IsPotion());
237 return 1;
238 }
239
240 int IsWeaponVellum(lua_State* L, Item* item)
241 {
242 // @TODO
243 //sEluna->Push(L, item->IsWeaponVellum());
244 return 1;
245 }
246
247 int IsArmorVellum(lua_State* L, Item* item)
248 {
249 // @TODO
250 //sEluna->Push(L, item->IsArmorVellum());
251 return 1;
252 }
253
254 int IsConjuredConsumable(lua_State* L, Item* item)
255 {
256 sEluna->Push(L, item->IsConjuredConsumable());
257 return 1;
258 }
259
260 int IsRefundExpired(lua_State* L, Item* item)
261 {
262 /* sEluna->Push(L, item->IsRefundExpired());*/
263 return 1;
264 }
265
266 int SetEnchantment(lua_State* L, Item* item)
267 {
268 Player* owner = item->GetOwner();
269 if (!owner)
270 {
271 sEluna->Push(L, false);
272 return 1;
273 }
274
275 uint32 enchant = luaL_checkunsigned(L, 1);
276 if (!sSpellItemEnchantmentStore.LookupEntry(enchant))
277 {
278 sEluna->Push(L, false);
279 return 1;
280 }
281
282 EnchantmentSlot slot = EnchantmentSlot(luaL_checkunsigned(L, 2));
284 {
285 luaL_error(L, "Invalid enchantment slot (%d)", slot);
286 sEluna->Push(L, false);
287 return 1;
288 }
289
290 owner->ApplyEnchantment(item, slot, false);
291 item->SetEnchantment(slot, enchant, 0, 0);
292 owner->ApplyEnchantment(item, slot, true);
293 sEluna->Push(L, true);
294 return 1;
295 }
296
297 int ClearEnchantment(lua_State* L, Item* item)
298 {
299 Player* owner = item->GetOwner();
300 if (!owner)
301 {
302 sEluna->Push(L, false);
303 return 1;
304 }
305
306 EnchantmentSlot slot = EnchantmentSlot(luaL_checkunsigned(L, 1));
308 {
309 luaL_error(L, "Invalid enchantment slot (%d)", slot);
310 sEluna->Push(L, false);
311 return 1;
312 }
313
314 if (!item->GetEnchantmentId(slot))
315 {
316 sEluna->Push(L, false);
317 return 1;
318 }
319
320 owner->ApplyEnchantment(item, slot, false);
321 item->ClearEnchantment(slot);
322 sEluna->Push(L, true);
323 return 1;
324 }
325
326 int GetGUIDLow(lua_State* L, Item* item)
327 {
328 sEluna->Push(L, GUID_LOPART(item->GetGUID()));
329 return 1;
330 }
331
332 int GetEnchantmentId(lua_State* L, Item* item)
333 {
334 uint32 enchant_slot = luaL_checkunsigned(L, 1);
335
336 EnchantmentSlot slot = EnchantmentSlot(luaL_checkunsigned(L, 2));
338 sEluna->Push(L, 0);
339 else
340 sEluna->Push(L, item->GetEnchantmentId(EnchantmentSlot(enchant_slot)));
341 return 1;
342 }
343
344 int GetSpellId(lua_State* L, Item* item)
345 {
346 uint32 index = luaL_checkunsigned(L, 1);
347 if (index >= MAX_ITEM_PROTO_SPELLS)
348 {
349 luaL_error(L, "Invalid index (%d)", index);
350 return 0;
351 }
352
353 sEluna->Push(L, item->GetTemplate()->Spells[index].SpellId);
354 return 1;
355 }
356
357 int GetSpellTrigger(lua_State* L, Item* item)
358 {
359 uint32 index = luaL_checkunsigned(L, 1);
360 if (index >= MAX_ITEM_PROTO_SPELLS)
361 {
362 luaL_error(L, "Invalid index (%d)", index);
363 return 0;
364 }
365
366 sEluna->Push(L, item->GetTemplate()->Spells[index].SpellTrigger);
367 return 1;
368 }
369
370 int GetClass(lua_State* L, Item* item)
371 {
372 sEluna->Push(L, item->GetTemplate()->Class);
373 return 1;
374 }
375
376 int GetSubClass(lua_State* L, Item* item)
377 {
378 sEluna->Push(L, item->GetTemplate()->SubClass);
379 return 1;
380 }
381
382 int GetName(lua_State* L, Item* item)
383 {
384 sEluna->Push(L, item->GetTemplate()->Name1);
385 return 1;
386 }
387
388 int GetDisplayId(lua_State* L, Item* item)
389 {
390 sEluna->Push(L, item->GetTemplate()->DisplayInfoID);
391 return 1;
392 }
393
394 int GetQuality(lua_State* L, Item* item)
395 {
396 sEluna->Push(L, item->GetTemplate()->Quality);
397 return 1;
398 }
399
400 int GetBuyCount(lua_State* L, Item* item)
401 {
402 sEluna->Push(L, item->GetTemplate()->BuyCount);
403 return 1;
404 }
405
406 int GetBuyPrice(lua_State* L, Item* item)
407 {
408 sEluna->Push(L, item->GetTemplate()->BuyPrice);
409 return 1;
410 }
411
412 int GetSellPrice(lua_State* L, Item* item)
413 {
414 sEluna->Push(L, item->GetTemplate()->SellPrice);
415 return 1;
416 }
417
418 int GetInventoryType(lua_State* L, Item* item)
419 {
420 sEluna->Push(L, item->GetTemplate()->InventoryType);
421 return 1;
422 }
423
424 int GetAllowableClass(lua_State* L, Item* item)
425 {
426 sEluna->Push(L, item->GetTemplate()->AllowableClass);
427 return 1;
428 }
429
430 int GetAllowableRace(lua_State* L, Item* item)
431 {
432 sEluna->Push(L, item->GetTemplate()->AllowableRace);
433 return 1;
434 }
435
436 int GetItemLevel(lua_State* L, Item* item)
437 {
438 sEluna->Push(L, item->GetTemplate()->ItemLevel);
439 return 1;
440 }
441
442 int GetRequiredLevel(lua_State* L, Item* item)
443 {
444 sEluna->Push(L, item->GetTemplate()->RequiredLevel);
445 return 1;
446 }
447
448 int GetStatsCount(lua_State* L, Item* item)
449 {
450 // @TODO
451 //sEluna->Push(L, item->GetTemplate()->StatsCount);
452 return 1;
453 }
454
455 int GetRandomProperty(lua_State* L, Item* item)
456 {
457 sEluna->Push(L, item->GetTemplate()->RandomProperty);
458 return 1;
459 }
460
461 int GetRandomSuffix(lua_State* L, Item* item)
462 {
463 sEluna->Push(L, item->GetTemplate()->RandomSuffix);
464 return 1;
465 }
466
467 int GetItemSet(lua_State* L, Item* item)
468 {
469 sEluna->Push(L, item->GetTemplate()->ItemSet);
470 return 1;
471 }
472
473 int GetBagSize(lua_State* L, Item* item)
474 {
475 if (Bag* bag = item->ToBag())
476 sEluna->Push(L, bag->GetBagSize());
477 else
478 sEluna->Push(L, 0);
479 return 1;
480 }
481
482 int SaveToDB(lua_State* L, Item* item)
483 {
484 SQLTransaction trans = SQLTransaction(nullptr);
485 item->SaveToDB(trans);
486 return 0;
487 }
488};
#define DEFAULT_LOCALE
Definition Common.h:154
#define MAX_LOCALES
Definition Common.h:156
DBCStorage< SpellItemEnchantmentEntry > sSpellItemEnchantmentStore(SpellItemEnchantmentfmt)
DBCStorage< ItemRandomSuffixEntry > sItemRandomSuffixStore(ItemRandomSuffixfmt)
DBCStorage< ItemRandomPropertiesEntry > sItemRandomPropertiesStore(ItemRandomPropertiesfmt)
std::int32_t int32
Definition Define.h:73
std::uint32_t uint32
Definition Define.h:77
std::uint64_t uint64
Definition Define.h:76
EnchantmentSlot
Definition Item.h:149
@ PERM_ENCHANTMENT_SLOT
Definition Item.h:150
@ SOCK_ENCHANTMENT_SLOT_3
Definition Item.h:154
@ SOCK_ENCHANTMENT_SLOT_2
Definition Item.h:153
@ SOCK_ENCHANTMENT_SLOT
Definition Item.h:152
@ BONUS_ENCHANTMENT_SLOT
Definition Item.h:155
@ MAX_INSPECTED_ENCHANTMENT_SLOT
Definition Item.h:158
#define MAX_ITEM_PROTO_SPELLS
#define sEluna
Definition LuaEngine.h:710
uint32 GUID_LOPART(uint64 x)
#define sObjectMgr
Definition ObjectMgr.h:1617
const uint32 ItemQualityColors[MAX_ITEM_QUALITY]
Skyfire::AutoPtr< Transaction, Skyfire::Mutex > SQLTransaction
Definition Transaction.h:42
Definition Bag.h:16
Definition Item.h:202
uint8 GetSlot() const
Definition Item.h:265
bool IsCurrencyToken() const
Definition Item.h:245
void SetEnchantment(EnchantmentSlot slot, uint32 id, uint32 duration, uint32 charges, uint64 caster=0)
Definition Item.cpp:856
void SetBinding(bool val)
Definition Item.h:217
uint32 GetEnchantmentId(EnchantmentSlot slot) const
Definition Item.h:287
void SetOwnerGUID(uint64 guid)
Definition Item.h:214
bool IsBoundAccountWide() const
Definition Item.h:219
Bag * ToBag()
Definition Item.h:240
bool CanBeTraded(bool mail=false, bool trade=false) const
Definition Item.cpp:744
virtual void SaveToDB(SQLTransaction &trans)
Definition Item.cpp:305
void SetCount(uint32 value)
Definition Item.h:259
bool IsLocked() const
Definition Item.h:243
int32 GetItemRandomPropertyId() const
Definition Item.h:278
ItemTemplate const * GetTemplate() const
Definition Item.cpp:528
bool IsInBag() const
Definition Item.h:272
bool IsSoulBound() const
Definition Item.h:218
bool IsPotion() const
Definition Item.h:319
bool IsBroken() const
Definition Item.h:247
uint32 GetItemSuffixFactor() const
Definition Item.h:279
bool IsBindedNotWith(Player const *player) const
Definition Item.cpp:1055
bool IsEquipped() const
Definition Item.cpp:739
Player * GetOwner() const
Definition Item.cpp:533
bool hasQuest(uint32 quest_id) const
Definition Item.h:316
bool IsInTrade() const
Definition Item.h:250
bool IsBoundByEnchant() const
Definition Item.cpp:786
bool IsBag() const
Definition Item.h:244
void ClearEnchantment(EnchantmentSlot slot)
Definition Item.cpp:897
uint64 GetOwnerGUID() const
Definition Item.h:213
bool IsNotEmptyBag() const
Definition Item.cpp:280
uint32 GetCount() const
Definition Item.h:258
uint8 GetBagSlot() const
Definition Item.cpp:734
uint32 GetMaxStackCount() const
Definition Item.h:260
bool IsConjuredConsumable() const
Definition Item.h:321
uint64 GetGUID() const
Definition Object.h:119
static void GetLocaleString(const StringVector &data, int loc_idx, std::string &value)
Definition ObjectMgr.h:1353
void ApplyEnchantment(Item *item, EnchantmentSlot slot, bool apply, bool apply_dur=true, bool ignore_condition=false)
Definition Player.cpp:11168
uint8 getLevel() const
Definition Unit.h:1528
int IsArmorVellum(lua_State *L, Item *item)
int IsBoundByEnchant(lua_State *L, Item *item)
int IsConjuredConsumable(lua_State *L, Item *item)
int IsWeaponVellum(lua_State *L, Item *item)
int IsNotBoundToPlayer(lua_State *L, Item *item)
int IsNotEmptyBag(lua_State *L, Item *item)
int SetBinding(lua_State *L, Item *item)
int GetMaxStackCount(lua_State *L, Item *item)
int GetAllowableRace(lua_State *L, Item *item)
int GetSubClass(lua_State *L, Item *item)
int GetCount(lua_State *L, Item *item)
int GetSlot(lua_State *L, Item *item)
int SetEnchantment(lua_State *L, Item *item)
int IsLocked(lua_State *L, Item *item)
int GetItemLink(lua_State *L, Item *item)
int IsPotion(lua_State *L, Item *item)
int GetOwnerGUID(lua_State *L, Item *item)
int GetBuyCount(lua_State *L, Item *item)
int GetGUIDLow(lua_State *L, Item *item)
int IsCurrencyToken(lua_State *L, Item *item)
int GetName(lua_State *L, Item *item)
int IsBoundAccountWide(lua_State *L, Item *item)
int IsRefundExpired(lua_State *L, Item *item)
int IsBag(lua_State *L, Item *item)
int GetClass(lua_State *L, Item *item)
int GetRandomSuffix(lua_State *L, Item *item)
int GetQuality(lua_State *L, Item *item)
int HasQuest(lua_State *L, Item *item)
int IsInTrade(lua_State *L, Item *item)
int GetBagSlot(lua_State *L, Item *item)
int CanBeTraded(lua_State *L, Item *item)
int GetOwner(lua_State *L, Item *item)
int IsSoulBound(lua_State *L, Item *item)
int GetDisplayId(lua_State *L, Item *item)
int SaveToDB(lua_State *L, Item *item)
int IsEquipped(lua_State *L, Item *item)
int ClearEnchantment(lua_State *L, Item *item)
int GetSpellTrigger(lua_State *L, Item *item)
int IsInBag(lua_State *L, Item *item)
int SetOwner(lua_State *L, Item *item)
int GetItemSet(lua_State *L, Item *item)
int GetItemLevel(lua_State *L, Item *item)
int SetCount(lua_State *L, Item *item)
int GetBuyPrice(lua_State *L, Item *item)
int GetStatsCount(lua_State *L, Item *item)
int GetSellPrice(lua_State *L, Item *item)
int GetInventoryType(lua_State *L, Item *item)
int GetEnchantmentId(lua_State *L, Item *item)
int GetGUID(lua_State *L, Item *item)
int IsBroken(lua_State *L, Item *item)
int GetRequiredLevel(lua_State *L, Item *item)
int GetSpellId(lua_State *L, Item *item)
int GetRandomProperty(lua_State *L, Item *item)
int GetBagSize(lua_State *L, Item *item)
int GetAllowableClass(lua_State *L, Item *item)
uint32 SpellTrigger
int32 SpellId
char * nameSuffix
char * nameSuffix
uint32 AllowableClass
_Spell Spells[MAX_ITEM_PROTO_SPELLS]
uint32 RequiredLevel
std::string Name1
uint32 DisplayInfoID
uint32 AllowableRace
uint32 InventoryType