Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 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. #include <unordered_map>
  5.  
  6. //static UNORDERED_MAP<uint32, std::vector<uint32> > itemList; // holds items from DB after startup
  7. std::unordered_map<uint32, std::vector<uint32>> itemList;
  8.  
  9. class PreviewLoader : public WorldScript // script that loads items from DB so you can customize the vendors without recompile and restart
  10. {
  11. public:
  12. PreviewLoader() : WorldScript("PreviewLoader") { }
  13.  
  14. void OnStartup()
  15. {
  16. itemList.clear(); // reload
  17. QueryResult result = WorldDatabase.Query("SELECT entry, item FROM npc_vendor_preview");
  18. if (!result)
  19. return;
  20. do
  21. {
  22. uint32 entry = (*result)[0].GetUInt32();
  23. uint32 item = (*result)[1].GetUInt32();
  24. if (sObjectMgr->GetItemTemplate(item))
  25. itemList[entry].push_back(item);
  26. } while (result->NextRow());
  27. }
  28.  
  29. // you can reload config to reload the items.
  30. // Too lazy to make a command since all the RBAC stuff changes all the time now and not sure how you have it
  31. void OnConfigLoad(bool reload)
  32. {
  33. if (reload)
  34. OnStartup();
  35. }
  36. };
  37.  
  38. #define private public
  39. #include "WorldSocket.h"
  40. #undef private
  41. #include "Opcodes.h"
  42. #include <cstring>
  43.  
  44.  
  45. class PreviewChecker : public ServerScript
  46. {
  47. public:
  48. PreviewChecker() : ServerScript("Preview_Checker") {}
  49.  
  50. void OnPacketReceive(WorldSession* session, WorldPacket& packet)
  51. {
  52. if (packet.GetOpcode() != CMSG_BUY_ITEM || packet.GetOpcode != CMSG_BUY_ITEM)
  53. return;
  54.  
  55. //WorldSession* session = socket->;
  56. if (!session || !session->GetPlayer())
  57. return;
  58.  
  59. ObjectGuid vendorguid;
  60. uint32 item, slot, count;
  61. uint8 unk1;
  62. packet >> vendorguid >> item >> slot >> count >> unk1;
  63. std::string s = std::to_string(item);
  64. if (Creature* npc = session->GetPlayer()->GetMap()->GetCreature(vendorguid))
  65. {
  66. if (npc->GetScriptName() == "PreviewVendor")
  67. //session->SendNotification(item);
  68. //session->SendNotification(item);
  69. //session->GetPlayer()->BuyItemFromVendorSlot(vendorguid, slot, item, count, NULL_BAG, NULL_SLOT);
  70. //TODO: SQL QUERY TO WARCRY DATABASE TO CHECK HOW MUCH THIS SPECIFIC ITEM COSTS, THEN CHECK IF USER HAS THAT MUCH GOLD!
  71. npc->Say(s, LANG_UNIVERSAL, session->GetPlayer());
  72. AddGossipItemFor(session->GetPlayer(), GOSSIP_ICON_INTERACT_1, "Test", 0, session->GetPlayer()->AddItem(item, count), "Are you sure you want to reforge\n\n", (0), false);
  73. SendGossipMenuFor(session->GetPlayer(), DEFAULT_GOSSIP_MESSAGE, vendorguid);
  74. //session->GetPlayer()->AddItem(item, count);
  75. }
  76. }
  77. };
  78.  
  79.  
  80. class PreviewVendor : public CreatureScript
  81. {
  82. public:
  83. PreviewVendor() : CreatureScript("PreviewVendor") { }
  84.  
  85. bool OnGossipHello(Player* player, Creature* creature)
  86. {
  87. CustomSendListInventory(player, creature->GetGUID());
  88. return true; // stop normal actions
  89. }
  90.  
  91. void CustomSendListInventory(Player* player, ObjectGuid vendorGuid)
  92. {
  93. //TC_LOG_DEBUG(LOG_FILTER_GENERAL, "WORLD: Sent custom SMSG_LIST_INVENTORY");
  94.  
  95. WorldSession* session = player->GetSession();
  96.  
  97. Creature* vendor = player->GetNPCIfCanInteractWith(vendorGuid, UNIT_NPC_FLAG_VENDOR);
  98. if (!vendor)
  99. {
  100. // TC_LOG_DEBUG(LOG_FILTER_GENERAL, "WORLD: SendListInventory - Unit (GUID: %u) not found or you can not interact with him.", uint32(GUID_LOPART(vendorGuid)));
  101. player->SendSellError(SELL_ERR_CANT_FIND_VENDOR, NULL, vendorGuid, 0);
  102. player->GetSession()->SendNotification("Cant find vendor");
  103. return;
  104. }
  105.  
  106. // remove fake death
  107. if (player->HasUnitState(UNIT_STATE_DIED))
  108. player->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
  109.  
  110. // Stop the npc if moving
  111. if (vendor->HasUnitState(UNIT_STATE_MOVING))
  112. vendor->StopMoving();
  113.  
  114. // VendorItemData const* items = vendor->GetVendorItems();
  115. if (itemList.find(vendor->GetEntry()) == itemList.end())
  116. {
  117. WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + 1);
  118. data << uint64(vendorGuid);
  119. data << uint8(0); // count == 0, next will be error code
  120. data << uint8(0); // "Vendor has no inventory"
  121. session->SendPacket(&data);
  122. player->GetSession()->SendNotification("No items?");
  123. return;
  124. }
  125.  
  126. std::vector<uint32> items = itemList[vendor->GetEntry()];
  127. uint8 itemCount = items.size();
  128. uint8 count = 0;
  129.  
  130. WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + itemCount * 8 * 4);
  131. data << uint64(vendorGuid);
  132.  
  133. size_t countPos = data.wpos();
  134. data << uint8(count);
  135.  
  136. // float discountMod = player->GetReputationPriceDiscount(vendor);
  137.  
  138. for (uint8 slot = 0; slot < itemCount; ++slot)
  139. {
  140. if (ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(items[slot]))
  141. {
  142. // Only display items in vendor lists for the team the
  143. // player is on. If GM on, display all items.
  144.  
  145.  
  146. // Items sold out are not displayed in list
  147. // uint32 leftInStock = !item->maxcount ? 0xFFFFFFFF : vendor->GetVendorItemCurrentCount(item);
  148. // if (!player->IsGameMaster() && !leftInStock)
  149. // continue;
  150. uint32 leftInStock = 0xFFFFFFFF; // show all items
  151.  
  152. // ConditionList conditions = sConditionMgr->GetConditionsForNpcVendorEvent(vendor->GetEntry(), items[slot]);
  153. // if (!sConditionMgr->IsObjectMeetToConditions(player, vendor, conditions))
  154. //{
  155. // TC_LOG_DEBUG(LOG_FILTER_GENERAL, "SendListInventory: conditions not met for creature entry %u item %u", vendor->GetEntry(), items[slot]);
  156. // continue;
  157. // }
  158.  
  159. // reputation discount
  160. // int32 price = item->IsGoldRequired(itemTemplate) ? uint32(floor(itemTemplate->BuyPrice * discountMod)) : 0;
  161. int32 price = 5;
  162.  
  163. data << uint32(slot + 1); // client expects counting to start at 1
  164. data << uint32(items[slot]);
  165. data << uint32(itemTemplate->DisplayInfoID);
  166. data << int32(leftInStock); // left in stock
  167. data << uint32(price); // price testing
  168. data << uint32(itemTemplate->MaxDurability);
  169. data << uint32(itemTemplate->BuyCount);
  170. data << uint32(5); // extended cost testing
  171.  
  172. if (++count >= MAX_VENDOR_ITEMS)
  173. break;
  174. }
  175. else
  176. {
  177. player->GetSession()->SendNotification("It couldn't get item");
  178. }
  179. }
  180.  
  181. if (count == 0)
  182. {
  183. player->GetSession()->SendNotification("Count was 0...");
  184. data << uint8(0);
  185. session->SendPacket(&data);
  186. return;
  187. }
  188.  
  189. data.put<uint8>(countPos, count);
  190. session->SendPacket(&data);
  191. }
  192. };
  193.  
  194. void AddSC_PreviewVendor()
  195. {
  196. new PreviewChecker;
  197. new PreviewLoader;
  198. new PreviewVendor;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement