Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. // Scriptname to put into creature_template: PreviewVendor
  2. // dont forget to add vendor flags to the NPC (128)
  3.  
  4. static UNORDERED_MAP<uint32, std::vector<uint32> > itemList; // holds items from DB after startup
  5.  
  6. class PreviewLoader : public WorldScript // script that loads items from DB so you can customize the vendors without recompile and restart
  7. {
  8. public:
  9. PreviewLoader() : WorldScript("PreviewLoader") { }
  10.  
  11. void OnStartup()
  12. {
  13. itemList.clear(); // reload
  14. QueryResult result = WorldDatabase.Query("SELECT entry, item FROM npc_vendor_preview");
  15. if (!result)
  16. return;
  17. do
  18. {
  19. uint32 entry = (*result)[0].GetUInt32();
  20. uint32 item = (*result)[1].GetUInt32();
  21. if (sObjectMgr->GetItemTemplate(item))
  22. itemList[entry].push_back(item);
  23. } while (result->NextRow());
  24. }
  25.  
  26. // you can reload config to reload the items.
  27. // Too lazy to make a command since all the RBAC stuff changes all the time now and not sure how you have it
  28. void OnConfigLoad(bool reload)
  29. {
  30. if (reload)
  31. OnStartup();
  32. }
  33. };
  34.  
  35.  
  36. class PreviewVendor : public CreatureScript
  37. {
  38. public:
  39. PreviewVendor() : CreatureScript("PreviewVendor") { }
  40.  
  41. bool OnGossipHello(Player* player, Creature* creature)
  42. {
  43. CustomSendListInventory(player, creature->GetGUID());
  44. return true; // stop normal actions
  45. }
  46.  
  47. void CustomSendListInventory(Player* player, uint64 vendorGuid)
  48. {
  49. //("misc", "WORLD: Sent custom SMSG_LIST_INVENTORY");
  50.  
  51. WorldSession* session = player->GetSession();
  52.  
  53. Creature* vendor = player->GetNPCIfCanInteractWith(vendorGuid, UNIT_NPC_FLAG_VENDOR);
  54. if (!vendor)
  55. {
  56. ("network", "WORLD: SendListInventory - Unit (GUID: %u) not found or you can not interact with him.", uint32(GUID_LOPART(vendorGuid)));
  57. player->SendSellError(SELL_ERR_CANT_FIND_VENDOR, NULL, 0);
  58. return;
  59. }
  60.  
  61. // remove fake death
  62. if (player->HasUnitState(UNIT_STATE_DIED))
  63. player->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
  64.  
  65. // Stop the npc if moving
  66. if (vendor->HasUnitState(UNIT_STATE_MOVING))
  67. vendor->StopMoving();
  68.  
  69. // VendorItemData const* items = vendor->GetVendorItems();
  70. if (itemList.find(vendor->GetEntry()) == itemList.end())
  71. {
  72. WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + 1);
  73. data << uint64(vendorGuid);
  74. data << uint8(0); // count == 0, next will be error code
  75. data << uint8(0); // "Vendor has no inventory"
  76. session->SendPacket(&data);
  77. return;
  78. }
  79.  
  80. std::vector<uint32> items = itemList[vendor->GetEntry()];
  81. uint8 itemCount = items.size();
  82. uint8 count = 0;
  83.  
  84. WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + itemCount * 8 * 4);
  85. data << uint64(vendorGuid);
  86.  
  87. size_t countPos = data.wpos();
  88. data << uint8(count);
  89.  
  90. // float discountMod = player->GetReputationPriceDiscount(vendor);
  91.  
  92. for (uint8 slot = 0; slot < itemCount; ++slot)
  93. {
  94. if (ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(items[slot]))
  95. {
  96. if (!(itemTemplate->AllowableClass & player->getClassMask()) && itemTemplate->Bonding == BIND_WHEN_PICKED_UP && !player->isGameMaster())
  97. continue;
  98. // Only display items in vendor lists for the team the
  99. // player is on. If GM on, display all items.
  100. if (!player->isGameMaster() && ((itemTemplate->Flags2 & ITEM_FLAGS_EXTRA_HORDE_ONLY && player->GetTeam() == ALLIANCE) || (itemTemplate->Flags2 == ITEM_FLAGS_EXTRA_ALLIANCE_ONLY && player->GetTeam() == HORDE)))
  101. continue;
  102.  
  103. // Items sold out are not displayed in list
  104. // uint32 leftInStock = !item->maxcount ? 0xFFFFFFFF : vendor->GetVendorItemCurrentCount(item);
  105. // if (!player->IsGameMaster() && !leftInStock)
  106. // continue;
  107. uint32 leftInStock = 0xFFFFFFFF; // show all items
  108.  
  109. ConditionList conditions = sConditionMgr->GetConditionsForNpcVendorEvent(vendor->GetEntry(), items[slot]);
  110. if (!sConditionMgr->IsObjectMeetToConditions(player, vendor, conditions))
  111. {
  112. ("misc", "SendListInventory: conditions not met for creature entry %u item %u", vendor->GetEntry(), items[slot]);
  113. continue;
  114. }
  115.  
  116. // reputation discount
  117. // int32 price = item->IsGoldRequired(itemTemplate) ? uint32(floor(itemTemplate->BuyPrice * discountMod)) : 0;
  118. int32 price = 0;
  119.  
  120. data << uint32(slot + 1); // client expects counting to start at 1
  121. data << uint32(items[slot]);
  122. data << uint32(itemTemplate->DisplayInfoID);
  123. data << int32(leftInStock); // left in stock
  124. data << uint32(price); // price
  125. data << uint32(itemTemplate->MaxDurability);
  126. data << uint32(itemTemplate->BuyCount);
  127. data << uint32(0); // extended cost
  128.  
  129. if (++count >= MAX_VENDOR_ITEMS)
  130. break;
  131. }
  132. }
  133.  
  134. if (count == 0)
  135. {
  136. data << uint8(0);
  137. session->SendPacket(&data);
  138. return;
  139. }
  140.  
  141. data.put<uint8>(countPos, count);
  142. session->SendPacket(&data);
  143. }
  144. };
  145.  
  146. void AddSC_PreviewVendor()
  147. {
  148. new PreviewLoader;
  149. new PreviewVendor;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement