Vendors are NPC merchants that staff place on the map.
A vendor is an fl_vendor entity with a model, a name, a description and an idle
animation. It has a list of items it sells and a list of items it buys, each with a price
that defaults to the cost field of the item, an optional finite stock for every item it
sells, an optional finite pool of money, a buy-back rate and the currency it trades in.
Vendors are saved with the plugin data of the map.
Staff with the 'manage_vendors' permission place, edit, move and remove vendors with the
Vendor Tool: left click creates a vendor or opens the editor of the one that is aimed at,
right click removes it and reload moves it. A player who uses a vendor gets the trade
panel, which lists what the vendor sells and which of the player's items it buys. Every
purchase and sale is carried out by the server (Vendors:buy, Vendors:sell), which
checks the distance, the stock, the money of both sides, the space in the inventory and
who may trade.
A vendor can be limited to factions: a customer then needs one of the listed factions. A
schema restricts vendors further through the PlayerCanUseVendor hook. The vendor says a
few phrases to its customer in the chat, each of which can be replaced in the editor.
Their IDs are listed in Vendors.phrases: 'greeting' when the trade panel opens, 'refuse'
when the customer may not trade, 'no_money' when the customer cannot afford an item,
'no_stock' when an item is sold out, 'broke' when the vendor cannot afford an item and
'thanks' when a customer who has traded leaves. The default text of a phrase is the
language phrase 'vendor.phrase.<id>'. Vendors.entity_class is the class of the vendor
entity and Vendors.default_model the model of a vendor whose own model is missing.
Hooks: PlayerCanUseVendor decides who may trade, AdjustVendorPrice changes prices,
PlayerCanBuyFromVendor and PlayerCanSellToVendor veto single trades,
PlayerBoughtFromVendor and PlayerSoldToVendor report them, and VendorSay changes or
mutes what a vendor says.
Summary
Hooks
Methods
cost of the item, scaled down by the share of uses the instance has left and adjusted by the AdjustVendorPrice hook.cost of the item, adjusted by the AdjustVendorPrice hook.name, description, currency and money of the vendor, sells (a list of { id, price, stock }, one entry for every item the vendor sells) and buys (a list of { instance_id, price }, one entry for every item of the customer that the vendor takes).Hooks
Keeps players without the 'manage_vendors' permission from using the properties of the context menu on vendors.
Parameters
- actor Player
- _property String property ID
- entity Entity
Returns
- Boolean false to block the property, nil otherwise
Keeps players without the 'manage_vendors' permission from using tools on vendors.
Parameters
- actor Player
- trace Map trace result of the tool use
- _tool_name String tool ID
Returns
- Boolean false to block the tool, nil otherwise
Closes the trade panel of the customers of a vendor that is being removed.
Parameters
- entity Entity
Draws the name and the description of the vendor that the local player is looking at above its head. It is drawn from here rather than as an ordinary target ID, because those are hidden when the feet of the entity are out of sight, as they are behind a counter.
Implements hook LanguageChanged
Translates the line about money of the trade panel again when the language changes.
Implements hook LoadData
Spawns the saved vendors when the framework loads its data.
Implements hook NetVarChanged
Updates the line about money of the trade panel when the money of the local player changes.
Parameters
- entity Entity the entity whose variable has changed
- key String variable name
Implements hook OnActiveCharacterCleared
Stops the trading of a player who drops their character.
Parameters
- owner Player
- _character Character the character that is being dropped
Implements hook OnActiveCharacterSet
Stops the trading of a player whose active character changes.
Parameters
- owner Player
- _character Character
Implements hook OneSecond
Stops the trading of the players who have died, walked out of reach of their vendor or whose vendor is gone, so that it does not depend on the client closing the trade panel.
Implements hook OnInventorySync
Asks the server for fresh prices shortly after an inventory of the local player has changed while the trade panel is open, unless the server sends them first.
Parameters
- inventory Inventory the inventory that has been received
Keeps players without the 'manage_vendors' permission from moving vendors with the physics gun.
Parameters
- actor Player
- entity Entity the entity that is about to be picked up
Returns
- Boolean false to prevent the pickup, nil otherwise
Implements hook PlayerDisconnected
Forgets the trading session of a player who has left.
Parameters
- actor Player
Spawns the vendors again once the map has been cleaned up.
Saves the vendors before the map is cleaned up, which removes them.
Implements hook RegisterPermissions
Registers the 'manage_vendors' level design permission.
Implements hook SaveData
Saves the vendors, with their stock and money, when the framework saves its data.
Methods
Changes the settings of a vendor and sends the new prices and stock to its customers.
Parameters
- vendor Entity
- data Map the settings to change; whatever is left out stays as it is
Returns
- Map the settings of the vendor after the change, or nil if it is not a vendor
See also
Sells an item of a vendor to a player who is trading with it. Refused if the vendor does not sell the item or has none left, if the PlayerCanBuyFromVendor hook vetoes it, if the player cannot afford the item, if the CanItemTransfer hook keeps it out of their default inventory (as it does while that inventory is disabled) or if they have no room for it, where the hook is given the template of the item. Otherwise a new item instance goes to the default inventory of the player, the price is taken from them and added to the money pool of the vendor, and the stock goes down by one.
Parameters
- actor Player the customer
- vendor Entity
- item_id String
Returns
- Boolean whether the item has been sold
Checks whether a vendor would take an item instance: the vendor has to buy items of that kind, the item must not be equipped and, if it holds other items, has to be empty.
Parameters
- vendor Entity
- item_obj Item the item instance
Returns
- Boolean
- String error phrase if the item is one the vendor buys but cannot take
Checks whether a player may trade with a vendor. The PlayerCanUseVendor hook decides first; if it has no opinion, the player needs one of the factions of the vendor, unless the vendor has none. A schema that restricts vendors further does so through the hook.
Parameters
- actor Player
- vendor Entity
Returns
- Boolean
- String error phrase given by the hook
- Map arguments of the phrase
Checks that a player is still entitled to trade with the vendor they have open: it is that vendor, the player is alive and within reach of it, and may still trade with it. Trading is stopped if they are not.
Parameters
- actor Player
- vendor Entity
Returns
- Map the session of the player, or nil if they may not trade
Makes a finite number within a range out of a value received from a client or read from a save.
Parameters
- value Any
- min Number
- max Number
Returns
- Number the clamped number, or nil if the value is not a finite number
Stops trading. The vendor thanks a player who has bought or sold something.
Parameters
- actor Player
- by_client Boolean optional, defaults to
falsetrue if the player has closed the trade panel themselves, so that their client does not have to be told to close it - silent Boolean optional, defaults to
falsetrue to keep the vendor from thanking the player
Returns
- Boolean false if the player was not trading
Creates a vendor.
local vendor = Vendors:create(trace.HitPos, Angle(0, 90, 0), {
name = 'Grocer',
sells = { canned_food = { price = 15, stock = 20 } },
buys = { canned_food = { price = false } }
})
Vendors:save()
Parameters
- position Vector where the vendor stands
- angles Angle optional, defaults to
Angle(0, 0, 0) - data Map optional, defaults to
nilvendor settings; whatever is left out gets its default
Returns
- Entity the vendor, or nil if the entity could not be created
See also
Opens the vendor editor for a player who has the 'manage_vendors' permission. The vendor is sent as an entity index, because a vendor that has just been created does not exist on the client yet.
Parameters
- actor Player
- vendor Entity
Returns
- Boolean whether the editor has been opened
Turns an amount of money into text: the amount followed by the symbol of the currency, or by its translated name if it has no symbol.
Parameters
- amount Number
- currency String currency ID
Returns
- String
Returns what a vendor pays for an item instance: the price set for the item, or the
buy-back rate of the vendor applied to the cost of the item, scaled down by the share of
uses the instance has left and adjusted by the AdjustVendorPrice hook.
Parameters
- vendor Entity
- item_obj Item the item instance, or a template to get the price of a new item
- actor Player optional, defaults to
nilthe customer
Returns
- Number the price, or nil if the vendor does not buy the item
Returns the players who have the trade panel of a vendor open.
Parameters
- vendor Entity
Returns
- List<Player>
Returns the settings of a vendor that has not been set up.
Returns
- Map vendor settings, see the description of this file
Returns what a customer pays for an item that a vendor sells: the price set for it, or
the cost of the item, adjusted by the AdjustVendorPrice hook.
Parameters
- vendor Entity
- item_id String
- actor Player optional, defaults to
nilthe customer
Returns
- Number the price, or nil if the vendor does not sell the item
Returns the items of a player that a vendor would take from them.
Parameters
- vendor Entity
- actor Player
Returns
- List<Item> item instances
See also
Builds what the trade panel of a customer shows: name, description, currency and
money of the vendor, sells (a list of { id, price, stock }, one entry for every item
the vendor sells) and buys (a list of { instance_id, price }, one entry for every item
of the customer that the vendor takes).
Parameters
- vendor Entity
- actor Player the customer
Returns
- Map
Returns the vendor a player is trading with.
Parameters
- actor Player
Returns
- Entity the vendor, or nil if the player is not trading
Checks whether something is a vendor entity.
Parameters
- entity Any
Returns
- Boolean
Removes the vendors that are on the map and spawns the saved ones.
Moves a vendor to another spot. Its customers stop trading once they are out of reach.
Call Vendors:save afterwards to make the move last.
Parameters
- vendor Entity
- position Vector where the vendor is going to stand
- angles Angle optional, defaults to
nilnew angles; the vendor keeps its own if nil
Returns
- Boolean false if it is not a vendor
Starts trading: checks that the player is alive, has a character that can hold money, is within reach of the vendor and may trade with it, opens the trade panel and makes the vendor greet the player. A player who may not trade hears the 'refuse' phrase of the vendor instead.
Parameters
- actor Player
- vendor Entity
Returns
- Boolean whether the trade panel has been opened
See also
Removes a vendor from the map and closes the trade panel of its customers. Call
Vendors:save afterwards to make the removal last.
Parameters
- vendor Entity
Returns
- Boolean false if it is not a vendor
Rounds an amount of money to the decimals of a currency.
Parameters
- currency String currency ID
- amount Number
- down Boolean optional, defaults to
falseround down instead of to the nearest value
Returns
- Number
Makes valid vendor settings out of a table that came from a client or from a save. Every field that is missing or not usable is taken from the current settings instead: texts are trimmed and cut, the model has to be an existing .mdl file, the currency has to be registered, numbers have to be finite and are clamped, and unknown items are dropped.
Parameters
- data Any the settings to check
- current Map optional, defaults to
nilsettings to fall back to; the defaults if nil
Returns
- Map vendor settings, a new table
Writes every vendor of the map to the plugin data storage.
Makes a vendor say one of its phrases to a player: the text set for the vendor, or the default phrase in the language of the player. The line goes to the chat of that player only, or into a notification if the Chatbox plugin is not loaded.
Parameters
- vendor Entity
- actor Player who the vendor talks to
- id String phrase ID, one of
Vendors.phrases
Buys an item from a player who is trading with a vendor. Refused if the player does not have the item, if the vendor does not take it, if the CanPlayerDropItem or the PlayerCanSellToVendor hook vetoes it, if the money pool of the vendor does not cover the price or if the AdjustReceivedMoney hook refuses the money. The player is paid first, and only then is the item taken out of their inventory and removed; the money pool of the vendor goes down by what the player has received, which the AdjustReceivedMoney hook may have lowered.
Parameters
- actor Player the customer
- vendor Entity
- instance_id Number instance ID of the item
Returns
- Boolean whether the item has been bought
See also
Vendors:send_trade_change(vendor, actor, item_id)
server plugins/vendors/plugin/lib/sv_sessions.lua:99Tells the customers of a vendor what a trade has changed: the customer who traded gets the whole trade panel again, because their items have changed, and every other customer gets just the money pool of the vendor and the stock of the item that was traded.
Parameters
- vendor Entity
- actor Player the customer who has traded
- item_id String optional, defaults to
nilID of the item whose stock has changed, nil if none has
Builds the table that a vendor is saved as: its settings with the item lists and the ID sets turned into lists, plus its position and angles.
Parameters
- vendor Entity
Returns
- Map
Sends what the trade panel shows to the customers of a vendor again.
Parameters
- vendor Entity
- actor Player optional, defaults to
nilthe only customer to send it to; all of them if nil
Defined in
- plugins/vendors/plugin/cl_hooks.lua
- plugins/vendors/plugin/lib/sv_data.lua
- plugins/vendors/plugin/lib/sv_editor.lua
- plugins/vendors/plugin/lib/sv_pricing.lua
- plugins/vendors/plugin/lib/sv_sessions.lua
- plugins/vendors/plugin/lib/sv_talk.lua
- plugins/vendors/plugin/lib/sv_trade.lua
- plugins/vendors/plugin/lib/sv_vendors.lua
- plugins/vendors/plugin/sh_plugin.lua
- plugins/vendors/plugin/sv_hooks.lua