Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Scriptname to put into creature_template: PreviewVendor
- // dont forget to add vendor flags to the NPC (128)
- #include <unordered_map>
- //static UNORDERED_MAP<uint32, std::vector<uint32> > itemList; // holds items from DB after startup
- std::unordered_map<uint32, std::vector<uint32>> itemList;
- class PreviewLoader : public WorldScript // script that loads items from DB so you can customize the vendors without recompile and restart
- {
- public:
- PreviewLoader() : WorldScript("PreviewLoader") { }
- void OnStartup()
- {
- itemList.clear(); // reload
- QueryResult result = WorldDatabase.Query("SELECT entry, item FROM npc_vendor_preview");
- if (!result)
- return;
- do
- {
- uint32 entry = (*result)[0].GetUInt32();
- uint32 item = (*result)[1].GetUInt32();
- if (sObjectMgr->GetItemTemplate(item))
- itemList[entry].push_back(item);
- } while (result->NextRow());
- }
- // you can reload config to reload the items.
- // Too lazy to make a command since all the RBAC stuff changes all the time now and not sure how you have it
- void OnConfigLoad(bool reload)
- {
- if (reload)
- OnStartup();
- }
- };
- #define private public
- #include "WorldSocket.h"
- #undef private
- #include "Opcodes.h"
- #include <cstring>
- class PreviewChecker : public ServerScript
- {
- public:
- PreviewChecker() : ServerScript("Preview_Checker") {}
- void OnPacketReceive(WorldSession* session, WorldPacket& packet)
- {
- if (packet.GetOpcode() != CMSG_BUY_ITEM || packet.GetOpcode != CMSG_BUY_ITEM)
- return;
- //WorldSession* session = socket->;
- if (!session || !session->GetPlayer())
- return;
- ObjectGuid vendorguid;
- uint32 item, slot, count;
- uint8 unk1;
- packet >> vendorguid >> item >> slot >> count >> unk1;
- std::string s = std::to_string(item);
- if (Creature* npc = session->GetPlayer()->GetMap()->GetCreature(vendorguid))
- {
- if (npc->GetScriptName() == "PreviewVendor")
- //session->SendNotification(item);
- //session->SendNotification(item);
- //session->GetPlayer()->BuyItemFromVendorSlot(vendorguid, slot, item, count, NULL_BAG, NULL_SLOT);
- //TODO: SQL QUERY TO WARCRY DATABASE TO CHECK HOW MUCH THIS SPECIFIC ITEM COSTS, THEN CHECK IF USER HAS THAT MUCH GOLD!
- npc->Say(s, LANG_UNIVERSAL, session->GetPlayer());
- 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);
- SendGossipMenuFor(session->GetPlayer(), DEFAULT_GOSSIP_MESSAGE, vendorguid);
- //session->GetPlayer()->AddItem(item, count);
- }
- }
- };
- class PreviewVendor : public CreatureScript
- {
- public:
- PreviewVendor() : CreatureScript("PreviewVendor") { }
- bool OnGossipHello(Player* player, Creature* creature)
- {
- CustomSendListInventory(player, creature->GetGUID());
- return true; // stop normal actions
- }
- void CustomSendListInventory(Player* player, ObjectGuid vendorGuid)
- {
- //TC_LOG_DEBUG(LOG_FILTER_GENERAL, "WORLD: Sent custom SMSG_LIST_INVENTORY");
- WorldSession* session = player->GetSession();
- Creature* vendor = player->GetNPCIfCanInteractWith(vendorGuid, UNIT_NPC_FLAG_VENDOR);
- if (!vendor)
- {
- // TC_LOG_DEBUG(LOG_FILTER_GENERAL, "WORLD: SendListInventory - Unit (GUID: %u) not found or you can not interact with him.", uint32(GUID_LOPART(vendorGuid)));
- player->SendSellError(SELL_ERR_CANT_FIND_VENDOR, NULL, vendorGuid, 0);
- player->GetSession()->SendNotification("Cant find vendor");
- return;
- }
- // remove fake death
- if (player->HasUnitState(UNIT_STATE_DIED))
- player->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
- // Stop the npc if moving
- if (vendor->HasUnitState(UNIT_STATE_MOVING))
- vendor->StopMoving();
- // VendorItemData const* items = vendor->GetVendorItems();
- if (itemList.find(vendor->GetEntry()) == itemList.end())
- {
- WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + 1);
- data << uint64(vendorGuid);
- data << uint8(0); // count == 0, next will be error code
- data << uint8(0); // "Vendor has no inventory"
- session->SendPacket(&data);
- player->GetSession()->SendNotification("No items?");
- return;
- }
- std::vector<uint32> items = itemList[vendor->GetEntry()];
- uint8 itemCount = items.size();
- uint8 count = 0;
- WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + itemCount * 8 * 4);
- data << uint64(vendorGuid);
- size_t countPos = data.wpos();
- data << uint8(count);
- // float discountMod = player->GetReputationPriceDiscount(vendor);
- for (uint8 slot = 0; slot < itemCount; ++slot)
- {
- if (ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(items[slot]))
- {
- // Only display items in vendor lists for the team the
- // player is on. If GM on, display all items.
- // Items sold out are not displayed in list
- // uint32 leftInStock = !item->maxcount ? 0xFFFFFFFF : vendor->GetVendorItemCurrentCount(item);
- // if (!player->IsGameMaster() && !leftInStock)
- // continue;
- uint32 leftInStock = 0xFFFFFFFF; // show all items
- // ConditionList conditions = sConditionMgr->GetConditionsForNpcVendorEvent(vendor->GetEntry(), items[slot]);
- // if (!sConditionMgr->IsObjectMeetToConditions(player, vendor, conditions))
- //{
- // TC_LOG_DEBUG(LOG_FILTER_GENERAL, "SendListInventory: conditions not met for creature entry %u item %u", vendor->GetEntry(), items[slot]);
- // continue;
- // }
- // reputation discount
- // int32 price = item->IsGoldRequired(itemTemplate) ? uint32(floor(itemTemplate->BuyPrice * discountMod)) : 0;
- int32 price = 5;
- data << uint32(slot + 1); // client expects counting to start at 1
- data << uint32(items[slot]);
- data << uint32(itemTemplate->DisplayInfoID);
- data << int32(leftInStock); // left in stock
- data << uint32(price); // price testing
- data << uint32(itemTemplate->MaxDurability);
- data << uint32(itemTemplate->BuyCount);
- data << uint32(5); // extended cost testing
- if (++count >= MAX_VENDOR_ITEMS)
- break;
- }
- else
- {
- player->GetSession()->SendNotification("It couldn't get item");
- }
- }
- if (count == 0)
- {
- player->GetSession()->SendNotification("Count was 0...");
- data << uint8(0);
- session->SendPacket(&data);
- return;
- }
- data.put<uint8>(countPos, count);
- session->SendPacket(&data);
- }
- };
- void AddSC_PreviewVendor()
- {
- new PreviewChecker;
- new PreviewLoader;
- new PreviewVendor;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement