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

Keeps players without the 'manage_vendors' permission from using the properties of the context menu on vendors.
Keeps players without the 'manage_vendors' permission from using tools on vendors.
Closes the trade panel of the customers of a vendor that is being removed.
Draws the name and the description of the vendor that the local player is looking at above its head.
Translates the line about money of the trade panel again when the language changes.
Spawns the saved vendors when the framework loads its data.
Updates the line about money of the trade panel when the money of the local player changes.
Stops the trading of a player who drops their character.
Stops the trading of a player whose active character changes.
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.
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.
Keeps players without the 'manage_vendors' permission from moving vendors with the physics gun.
Forgets the trading session of a player who has left.
Spawns the vendors again once the map has been cleaned up.
Saves the vendors before the map is cleaned up, which removes them.
Registers the 'manage_vendors' level design permission.
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.
Sells an item of a vendor to a player who is trading with it.
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.
Checks whether a player may trade with a vendor.
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.
Makes a finite number within a range out of a value received from a client or read from a save.
Creates a vendor.
Opens the vendor editor for a player who has the 'manage_vendors' permission.
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.
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.
Returns the players who have the trade panel of a vendor open.
Returns the settings of a vendor that has not been set up.
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.
Returns the items of a player that a vendor would take from them.
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).
Returns the vendor a player is trading with.
Checks whether something is a vendor entity.
load()server
Removes the vendors that are on the map and spawns the saved ones.
Moves a vendor to another spot.
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.
Removes a vendor from the map and closes the trade panel of its customers.
Rounds an amount of money to the decimals of a currency.
Makes valid vendor settings out of a table that came from a client or from a save.
save()server
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.
Buys an item from a player who is trading with a vendor.
Tells 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.
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.
Sends what the trade panel shows to the customers of a vendor again.

Hooks

Vendors:CanProperty(actor, _property, entity)

shared plugins/vendors/plugin/sh_plugin.lua:101

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

Vendors:CanTool(actor, trace, _tool_name)

shared plugins/vendors/plugin/sh_plugin.lua:89

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

Vendors:EntityRemoved(entity)

server plugins/vendors/plugin/sv_hooks.lua:60

Closes the trade panel of the customers of a vendor that is being removed.

Parameters

  • entity Entity

Vendors:HUDDrawTargetID()

client plugins/vendors/plugin/cl_hooks.lua:37

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.

Vendors:LanguageChanged()

client plugins/vendors/plugin/cl_hooks.lua:128

Implements hook LanguageChanged

Translates the line about money of the trade panel again when the language changes.

Vendors:LoadData()

server plugins/vendors/plugin/sv_hooks.lua:5

Implements hook LoadData

Spawns the saved vendors when the framework loads its data.

Vendors:NetVarChanged(entity, key)

client plugins/vendors/plugin/cl_hooks.lua:121

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

Vendors:OnActiveCharacterCleared(owner, _character)

server plugins/vendors/plugin/sv_hooks.lua:54

Implements hook OnActiveCharacterCleared

Stops the trading of a player who drops their character.

Parameters

  • owner Player
  • _character Character the character that is being dropped

Vendors:OnActiveCharacterSet(owner, _character)

server plugins/vendors/plugin/sv_hooks.lua:47

Implements hook OnActiveCharacterSet

Stops the trading of a player whose active character changes.

Parameters

  • owner Player
  • _character Character

Vendors:OneSecond()

server plugins/vendors/plugin/sv_hooks.lua:26

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.

Vendors:OnInventorySync(inventory)

client plugins/vendors/plugin/cl_hooks.lua:105

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

Vendors:PhysgunPickup(actor, entity)

shared plugins/vendors/plugin/sh_plugin.lua:78

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

Vendors:PlayerDisconnected(actor)

server plugins/vendors/plugin/sv_hooks.lua:40

Implements hook PlayerDisconnected

Forgets the trading session of a player who has left.

Parameters

  • actor Player

Vendors:PostCleanupMap()

server plugins/vendors/plugin/sv_hooks.lua:20

Spawns the vendors again once the map has been cleaned up.

Vendors:PreCleanupMap()

server plugins/vendors/plugin/sv_hooks.lua:15

Saves the vendors before the map is cleaned up, which removes them.

Vendors:RegisterPermissions()

shared plugins/vendors/plugin/sh_plugin.lua:42

Implements hook RegisterPermissions

Registers the 'manage_vendors' level design permission.

Vendors:SaveData()

server plugins/vendors/plugin/sv_hooks.lua:10

Implements hook SaveData

Saves the vendors, with their stock and money, when the framework saves its data.

Methods

Vendors:apply(vendor, data)

server plugins/vendors/plugin/lib/sv_vendors.lua:61

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

Vendors:buy(actor, vendor, item_id)

server plugins/vendors/plugin/lib/sv_trade.lua:49

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

Vendors:can_buy_item(vendor, item_obj)

server plugins/vendors/plugin/lib/sv_pricing.lua:89

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

Vendors:can_trade(actor, vendor)

server plugins/vendors/plugin/lib/sv_sessions.lua:30

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

Vendors:check_session(actor, vendor)

server plugins/vendors/plugin/lib/sv_sessions.lua:192

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

Vendors:clean_number(value, min, max)

server plugins/vendors/plugin/lib/sv_data.lua:65

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

Vendors:close(actor, by_client, silent)

server plugins/vendors/plugin/lib/sv_sessions.lua:166

Stops trading. The vendor thanks a player who has bought or sold something.

Parameters

  • actor Player
  • by_client Boolean optional, defaults to false true 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 false true to keep the vendor from thanking the player

Returns

  • Boolean false if the player was not trading

Vendors:create(position, angles, data)

server plugins/vendors/plugin/lib/sv_vendors.lua:39

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 nil vendor settings; whatever is left out gets its default

Returns

  • Entity the vendor, or nil if the entity could not be created

See also

Vendors:edit(actor, vendor)

server plugins/vendors/plugin/lib/sv_editor.lua:16

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

Vendors:format_money(amount, currency)

shared plugins/vendors/plugin/sh_plugin.lua:63

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

Vendors:get_buy_price(vendor, item_obj, actor)

server plugins/vendors/plugin/lib/sv_pricing.lua:67

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 nil the customer

Returns

  • Number the price, or nil if the vendor does not buy the item

Vendors:get_customers(vendor)

server plugins/vendors/plugin/lib/sv_sessions.lua:61

Returns the players who have the trade panel of a vendor open.

Parameters

  • vendor Entity

Returns

  • List<Player>

Vendors:get_default_data()

server plugins/vendors/plugin/lib/sv_data.lua:150

Returns the settings of a vendor that has not been set up.

Returns

  • Map vendor settings, see the description of this file

Vendors:get_sell_price(vendor, item_id, actor)

server plugins/vendors/plugin/lib/sv_pricing.lua:51

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 nil the customer

Returns

  • Number the price, or nil if the vendor does not sell the item

Vendors:get_sellable_items(vendor, actor)

server plugins/vendors/plugin/lib/sv_pricing.lua:110

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

Vendors:get_trade_data(vendor, actor)

server plugins/vendors/plugin/lib/sv_pricing.lua:135

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

Vendors:get_vendor(actor)

server plugins/vendors/plugin/lib/sv_sessions.lua:76

Returns the vendor a player is trading with.

Parameters

  • actor Player

Returns

  • Entity the vendor, or nil if the player is not trading

Vendors:is_vendor(entity)

shared plugins/vendors/plugin/sh_plugin.lua:54

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.

Vendors:move(vendor, position, angles)

server plugins/vendors/plugin/lib/sv_vendors.lua:79

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 nil new angles; the vendor keeps its own if nil

Returns

  • Boolean false if it is not a vendor

Vendors:open(actor, vendor)

server plugins/vendors/plugin/lib/sv_sessions.lua:126

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

Vendors:remove(vendor)

server plugins/vendors/plugin/lib/sv_vendors.lua:97

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

Vendors:round_money(currency, amount, down)

server plugins/vendors/plugin/lib/sv_data.lua:78

Rounds an amount of money to the decimals of a currency.

Parameters

  • currency String currency ID
  • amount Number
  • down Boolean optional, defaults to false round down instead of to the nearest value

Returns

  • Number

Vendors:sanitize(data, current)

server plugins/vendors/plugin/lib/sv_data.lua:179

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 nil settings 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.

Vendors:say(vendor, actor, id)

server plugins/vendors/plugin/lib/sv_talk.lua:15

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

Vendors:sell(actor, vendor, instance_id)

server plugins/vendors/plugin/lib/sv_trade.lua:170

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:99

Tells 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 nil ID of the item whose stock has changed, nil if none has

Vendors:to_saveable(vendor)

server plugins/vendors/plugin/lib/sv_vendors.lua:113

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

Vendors:update_customers(vendor, actor)

server plugins/vendors/plugin/lib/sv_sessions.lua:85

Sends what the trade panel shows to the customers of a vendor again.

Parameters

  • vendor Entity
  • actor Player optional, defaults to nil the only customer to send it to; all of them if nil

Defined in