This is a read only copy without any forum functionality of the old Modcraft forum.
If there is anything that you would like to have removed, message me on Discord via Kaev#5208.
Big thanks to Alastor for making this copy!

Menu

Author Topic: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.  (Read 2075 times)

ruben381

  • Registred Member
  • MS Paint Freak
  • *****
  • Posts: 4
    • View Profile
[QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« on: July 31, 2016, 04:16:21 am »
Is there any way to hook into an event for an item purchase?

I'm trying to make a vendor where some items can only be purchased if certain conditions are met. There doesn't seem to be an 'item purchased' hook anywhere, but I'm wondering if anyone has done this in a more hacky way?

Ideally, I'd like to be able to do this:

function itemPurchased(event, player, item)
   local arbitraryCondition = GetPointsFor(player)
   local itemID = item:GetEntry()
   if(itemID == VENDOR_ITEM_1) then
         if(arbitraryCondition >= 3000)then
               return true;
         else
                 player:SendBroadcastMessage("Requires 3000 arbitrary condition points dumbass")
                  return false; (or, like, player:RemoveItem(itemID, 1))
         end
   end
end



Obviously, I'm using LUA. If anyone has any insight on this it would be greatly appreciated. Thanks guys.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Grymskvll

  • Registred Member
  • Polygonshifter
  • *****
  • Posts: 65
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #1 on: July 31, 2016, 11:38:57 am »
Don't think there's an existing hook for it. I guess you could try to add it or request it. Regardless, wouldn't it be more elegant to use the Blizzlike currency system? I guess that might require a client-side patch for new entries in CurrencyType.dbc and maybe CurrencyCategory.dbc, unless you want to reuse an existing currency. You could maybe recycle item ID 37711: [Currency Token Test Token 1], it defaults to the "Miscellaneous" currency category and you could change the name and tooltip of the item server-side. That way, items in the vendor page would also have the right cost displayed. I don't know if that fits your design goal, though.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

ruben381

  • Registred Member
  • MS Paint Freak
  • *****
  • Posts: 4
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #2 on: July 31, 2016, 02:09:04 pm »
Quote from: "Grymskvll"
Don't think there's an existing hook for it. I guess you could try to add it or request it. Regardless, wouldn't it be more elegant to use the Blizzlike currency system? I guess that might require a client-side patch for new entries in CurrencyType.dbc and maybe CurrencyCategory.dbc, unless you want to reuse an existing currency. You could maybe recycle item ID 37711: [Currency Token Test Token 1], it defaults to the "Miscellaneous" currency category and you could change the name and tooltip of the item server-side. That way, items in the vendor page would also have the right cost displayed. I don't know if that fits your design goal, though.

I mean, I could potentially do that. The system is actually for reputations however, so it's not a 'cost', it's just a purchasing condition. The item should still have its normal gold cost, and the reputation shouldn't be 'consumed' on use. For these reasons I think I need a different solution.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Grymskvll

  • Registred Member
  • Polygonshifter
  • *****
  • Posts: 65
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #3 on: July 31, 2016, 02:25:46 pm »
There already exist items that you can only buy if you meet rep requirements (things like item 30623: [Reservoir Key]). I tried adding such an item to an NPC unrelated to the reputation and it still checked my rep when buying, so I assume any item with a reputation requirement (on the item itself) will have that reputation as a purchasing requirement.

In other words, try just putting a rep requirement on items and it wont let you buy them unless you meet the requirement.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

ruben381

  • Registred Member
  • MS Paint Freak
  • *****
  • Posts: 4
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #4 on: August 01, 2016, 02:31:27 am »
Quote from: "Grymskvll"
There already exist items that you can only buy if you meet rep requirements (things like item 30623: [Reservoir Key]). I tried adding such an item to an NPC unrelated to the reputation and it still checked my rep when buying, so I assume any item with a reputation requirement (on the item itself) will have that reputation as a purchasing requirement.

In other words, try just putting a rep requirement on items and it wont let you buy them unless you meet the requirement.

Yeah i understand that, but im using a custom reputation system with reputations that dont exist in WoW, and dont really use the WoW reputation system. Its literally juat comparing two numbers and cancelling the purchase or retracting the item if conditions are not met. But, it looks like its not really possible. Thanks for trying to help anyway dude i appreciate it.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Kaev

  • Contributors
  • Creator of Worlds
  • *****
  • Posts: 308
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #5 on: August 02, 2016, 08:58:02 am »
Quote from: "ruben381"
But, it looks like its not really possible.
It's serverside only so this is 100% possible. Just add the missing hook to your core. It's nearly only copy and paste for new hooks.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Grymskvll

  • Registred Member
  • Polygonshifter
  • *****
  • Posts: 65
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #6 on: August 02, 2016, 12:38:38 pm »
If I'm not mistaken, you'd want to trigger the event from TrinityCore's Player::BuyItemFromVendorSlot. Instead of using SendBuyError, you could just use Eluna's Player:SendNotification method to show the player the red error text (or add a Player:SendBuyError method to Eluna if you really want to, but from what I understand its only purpose is to show the error message).

I'm not sure if it really matters where in Player::BuyItemFromVendorSlot you trigger the event. I'd personally put it above all the other checks, since the custom condition is probably less trivial than something like a gold check.

The event trigger (inside Player::BuyItemFromVendorSlot) should look something like this. The arguments are just the player ("this" refers to the player since it's a player method), followed by all the parameters of Player::BuyItemFromVendorSlot (in case any script wants to use any of it):
Code: [Select]
// Run all event handlers. If any of them return false then...
if (!sEluna->OnBuyFromVendor(this, vendorguid, vendorslot, item, count, bag, slot))
{
// ...abort the purchase!
return false;
}

Just for clarity, OnBuyFromVendor doesn't exist (yet). You'd have to add/request it.

Like Kaev said, you can just look at existing hooks to see how to implement a new one. Be sure to look at hooks that can return false, since you want to be able to abort the purchase.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

schlumpf

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 2967
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #7 on: August 02, 2016, 01:23:20 pm »
Quote from: "Grymskvll"
If I'm not mistaken, you'd want to trigger the event from TrinityCore's Player::BuyItemFromVendorSlot. Instead of using SendBuyError, you could just use Eluna's Player:SendNotification method to show the player the red error text (or add a Player:SendBuyError method to Eluna if you really want to, but from what I understand its only purpose is to show the error message).

Please always prefer to use specialized packets: They are localized. Keeping semantics help for good user experience. There have been a lot of english-only errors in emulators for various things (summoning pets, buying stuff, …) in the past which could just be avoided by sending the correct packet.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Grymskvll

  • Registred Member
  • Polygonshifter
  • *****
  • Posts: 65
    • View Profile
Re: [QUESTION] [WotLk] [ELUNA/LUA] Event for item purchase.
« Reply #8 on: August 02, 2016, 01:43:18 pm »
Quote from: "schlumpf"
Please always prefer to use specialized packets: They are localized. Keeping semantics help for good user experience. There have been a lot of english-only errors in emulators for various things (summoning pets, buying stuff, …) in the past which could just be avoided by sending the correct packet.

Ah yeah, good point. In that case I guess a new Player:SendBuyError Eluna method would be preferable, so that you don't hardcode a specific BuyError in the event trigger.

In the case of the custom rep check, you could have the script send BuyResult BUY_ERR_REPUTATION_REQUIRE. If there's no BuyResult that suits your purpose, maybe you could have the script resort to checking the player's locale with (Eluna's) Player:GetDbLocaleIndex or Player:GetDbcLocale and use Player:SendNotification with a localized string.

Edit:
Here's how the locales are defined in TrinityCore's Common.h (and returned by Eluna's player methods above):
Code: [Select]
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,

    TOTAL_LOCALES
};
« Last Edit: January 01, 1970, 01:00:00 am by Admin »