Factions puts every character into a faction, which is also the team of its player.
A faction is a Faction object, normally defined in a file of the factions folder of a
schema or plugin. It sets the name, color, models and ranks of its members, whether they
choose a name, description and gender of their own, and whether a whitelist is needed to
join. The plugin makes the choice of a faction the first stage of character creation,
generates character names from the name template of the faction, groups the scoreboard by
faction, and adds commands that change factions, ranks and whitelists.
A faction can also limit how many of its members, or holders of one of its ranks, may be
online at once, which is checked when a character is loaded, and how many characters one
player may have in it, which is checked when a character is created. Its members spawn
with the weapons, the maximum health and the maximum armor that the faction and their rank
set, wear the model of their rank if it has one, and are liked, feared or hated by the NPC
classes that the faction names. A rank can let its holders promote and demote other
members with /rank promote and /rank demote, which staff with the 'manage_ranks'
permission may use on anyone.
The Factions functions register and look up factions, and the Player extensions read and
change the faction, rank and whitelists of a player. The OnPlayerFactionChanged and
OnRankChanged hooks report changes, ShouldNameGenerate can stop a name from being
generated, GetFactionLimit and PlayerCanBypassFactionLimit adjust the limits, and
PlayerCanTransferFaction can refuse a transfer by the SetFaction command. The 'faction'
and 'rank' conditions are registered for the Conditions plugin, and the 'faction', 'rank'
and 'model' argument types for the completion of commands.
Summary
Hooks
Factions.can_bypass_creation_limits) or a PlayerCanBypassFactionLimit hook lets them in.Factions.can_bypass_creation_limits lets the player past them), when the gender does not fit the faction (a faction with genders needs a male or female character, any other faction a genderless one), when the model is not one of the faction's models for that gender, or when the on_character_create callback of the faction refuses the character.Functions
Faction:can_transfer callback of that faction, then the PlayerCanTransferFaction hook.Hooks
Implements hook AddCharacterCreationMenuStages
Adds faction selection as the first stage of character creation.
Parameters
- panel Panel the character creation menu
Factions:CharacterGenderChanged(owner, _char, _new_gender, _old_gender)
server plugins/factions/plugin/sv_hooks.lua:233Implements hook CharacterGenderChanged
Gives the player a random model of their faction that matches the new gender. A player whose faction is not registered keeps their model.
Parameters
- owner Player
- _char Character
- _new_gender Number CHAR_GENDER_* value
- _old_gender Number CHAR_GENDER_* value
Factions:CharacterModelChanged(owner, _char, _model, _old_model)
server plugins/factions/plugin/sv_hooks.lua:246Implements hook CharacterModelChanged
Keeps the model of the rank on a player whose character model has been changed.
Parameters
- owner Player
- _char Character the active character, nil if the player has none
- _model String the new model of the character
- _old_model String the model that was networked before the change
Implements hook CharPanelCreated
Hides the gender, description and name controls of the 'ui.char_create.general' stage when the chosen faction does not have them. Factions without gender get the 'universal' one.
Parameters
- id String ID of the stage that has just been opened
- panel Panel the panel of that stage
Implements hook GetCharacterCreationModels
Returns the models of the chosen faction that match the chosen gender.
Parameters
- char_data Map character data collected so far; needs faction and gender
Returns
- List<String> model paths
Implements hook GetCharCreationErrorText
Supplies the error text shown when character creation fails because of the faction: no faction was chosen, the player has too many characters of it, or the faction has refused the character, in which case the reason the server has sent is shown if there is one.
Parameters
- _success Boolean
- status Number CHAR_* status code sent by the server
Returns
- String translated error for the CHAR_ERR_FACTION codes, otherwise nil
Factions:OnActiveCharacterCleared(owner, _character)
server plugins/factions/plugin/sv_hooks.lua:137Implements hook OnActiveCharacterCleared
Takes the faction of a character away from the player who drops it: clears the networked faction and rank, forgets the rank model, undoes the feelings of NPCs and puts the player back into the unassigned team.
Parameters
- owner Player
- _character Character the character that is being dropped
Implements hook OnCharacterDelete
Updates the networked amounts of characters per faction of a player who is deleting one of their characters.
Parameters
- actor Player
- _id Number ID of the character
- character Character the character that is about to be deleted
Makes a newly created NPC feel about the players the way their factions say. This is done on the next tick, once the NPC has spawned and set up its own relationships.
Parameters
- entity Entity
Implements hook OnThemeLoaded
Registers the faction selection panel with the theme.
Parameters
- current_theme ThemeBase
Implements hook PlayerCanUseCharacter
Refuses to load a character of a whitelisted faction when the player is not on its
whitelist, unless they may create characters past the whitelist
(Factions.can_bypass_creation_limits) or a PlayerCanBypassFactionLimit hook lets them in.
Also refuses it when its faction or its rank already has as many members online as the
limit allows, unless a PlayerCanBypassFactionLimit hook lets the player in.
The player themselves is not counted, so switching between two characters of a full
faction is possible. A character whose rank does not exist in its faction is checked
against the default rank, which it is given when it is loaded.
Parameters
- actor Player
- character Character the character the player wants to load
Returns
- Boolean false
- String the reason and Map its arguments when the character is refused, otherwise nil
Implements hook PlayerCreateCharacter
Rejects character creation when the chosen faction is not registered or requires a
whitelist that the player does not have, when the player already has as many characters
of the faction as it allows (neither applies when Factions.can_bypass_creation_limits
lets the player past them), when the gender does not fit the faction (a faction with
genders needs a male or female character, any other faction a genderless one), when the
model is not one of the faction's models for that gender, or when the on_character_create
callback of the faction refuses the character.
Parameters
- actor Player
- data Map character creation data
Returns
- Number CHAR_ERR_FACTION
- CHAR_ERR_FACTION_LIMIT
- CHAR_ERR_GENDER
- CHAR_ERR_MODEL
- CHAR_ERR_FACTION_REFUSED or the code the callback of the faction returned when the data is rejected, otherwise nil
Implements hook PlayerDisconnected
Makes NPCs forget what the faction of a disconnecting player has made them feel.
Parameters
- actor Player
Implements hook PlayerLoadoutGiven
Gives a spawning player the weapons of their faction and rank on top of the default loadout. The record of the weapons given before is dropped first, as the gamemode has just stripped every weapon of the player. The hook runs inside Player:Spawn, so the ammo that the Characters plugin restores once the player has spawned is set on top of these weapons.
Parameters
- actor Player
- _default_loadout List<String> weapon classes of the default loadout
Implements hook PlayerRestored
Networks the IDs of the factions the player is whitelisted for, and how many characters they have in each faction.
Parameters
- actor Player
- record User the player's database record
Implements hook PluginIncludeFolder
Includes a plugin's factions folder when the 'factions' extra is being loaded.
Parameters
- extra String name of the extra being loaded
- folder String path of the plugin folder
Returns
- Boolean true when the extra was handled here, otherwise nil
Implements hook PostCharacterLoaded
Networks the faction and the rank of the character that has just been loaded, giving a character whose rank does not exist in its faction the default rank, and gives the player what the faction grants them in the world: the weapons, the maximum health and armor (full armor if the character had none saved), the model of the rank and the feelings of NPCs towards the faction. The hook runs once the Characters plugin has spawned the player, given them the model of the character and restored their saved health, armor and ammo, so the model of the rank is put over that of the character here no matter which plugin's handler of OnActiveCharacterSet ran first. The team of the player is set as they spawn, see Factions:PostPlayerSpawn. A player whose faction is not registered only loses the rank model and the feelings of NPCs that an earlier character of theirs has left.
Parameters
- owner Player
- character Character
Factions:PostCreateCharacter(owner, char, char_data)
server plugins/factions/plugin/sv_hooks.lua:156Implements hook PostCreateCharacter
Copies the faction and the rank from the creation data to the new character, using the 'player' faction and the default rank of the faction when they are missing. Creation requests of clients never carry a rank (see Factions:PreCreateCharacter), so their characters start at the default rank unless the on_character_create callback of the faction has set one. A faction or rank that sets a maximum health or armor makes the character start with them.
Parameters
- owner Player
- char Character the character being created
- char_data Map character creation data
Implements hook PostPlayerSpawn
Sets the player's team from their faction, puts bots into a random faction, and gives a member of a faction the maximum health and armor of their faction and rank, filled up.
Parameters
- actor Player
Implements hook PreCreateCharacter
Cleans up the creation data a client has sent: discards the rank, which a client has no say in, generates a name from the faction's name template when the data has no name or the faction does not let players pick one, and replaces the description with the default description of a faction that does not let players write one. The Characters plugin counts a replaced name or description as generated, which frees the description from the length limits that apply to the ones players write.
Parameters
- actor Player
- data Map character creation data, modified in place
Implements hook PrePlayerSetModel
Makes a spawning player use the model of their rank, if the rank has one.
Parameters
- actor Player
Returns
- String model path of the rank, nil to leave the choice of the model to others
Implements hook PreRebuildScoreboard
Rebuilds the scoreboard with the players grouped into one collapsible category per faction, replacing the default player list.
Parameters
- panel Panel the scoreboard
- w Number
- h Number
Returns
- Boolean always true, which stops the default rebuild
Implements hook PreStageChange
Stops the player from leaving the 'ui.char_create.general' stage without picking a gender when the chosen faction requires one.
Parameters
- id String ID of the stage being left
- panel Panel the panel of that stage
Returns
- Boolean false to block the change
- String translated error; nothing otherwise
Implements hook RegisterConditions
Registers the 'faction' and 'rank' conditions, which compare a player's faction and their rank within a faction.
Implements hook RegisterPermissions
Registers the 'manage_ranks' permission, which lets staff promote and demote any member of any faction, whatever their own rank.
Implements hook ShouldNameGenerate
Prevents faction name generation for bots.
Parameters
- target Player
Returns
- Boolean false for bots, otherwise nil
Functions
Registers a faction: fills in the default name, description and color, sets up its team and assigns the animation class of each of its models and of the models of its ranks.
local faction = Faction.new('citizen')
faction.name = 'Citizen'
faction.models.male = { 'models/humans/group01/male_02.mdl' }
faction.models.female = { 'models/humans/group01/female_01.mdl' }
-- Same as faction:register()
Factions.add_faction(faction.faction_id, faction)
Parameters
- id String faction ID, normalized with to_id
- data Faction the faction to register; receives faction_id and team_id
See also
Returns every registered faction.
Returns
- Map factions keyed by faction ID
Gives a player everything their faction and rank grant to someone who is already in the world: the maximum health and armor, the model of the rank and, if they are alive, the weapons. The plugin calls it when the faction or the rank of a player changes. Server only.
Parameters
- target Player
Gives a player the model of their rank, if the rank has one, and gives the model of their character back once it has none. The model of the character itself is not changed. Server only.
Parameters
- target Player
See also
Makes the NPCs that exist feel about a player the way the npc_relations field of the faction of the player says, after undoing what an earlier faction has changed. The plugin calls it when a character is loaded and when the faction of a player changes. Server only.
Parameters
- target Player
See also
Sets the maximum health and armor of a player to those of their faction and rank. A player whose faction and rank set none gets the defaults of 100 back if a faction had changed them before, and is left alone otherwise. A maximum health returned by the GetPlayerMaxHealth hook takes the place of the one of the faction and rank. Server only.
Parameters
- target Player
- refill Boolean optional, defaults to
falsealso set the health and armor of the player to these maximums, as is done when they spawn
See also
Factions.can_bypass_creation_limits(target, data)
shared plugins/factions/plugin/lib/sh_faction.lua:299Checks whether a player may create a character in a faction without being whitelisted for it and beyond the amount of characters the faction allows them, which is what a PlayerCanBypassFactionCreationLimits handler decides.
Parameters
- target Player
- data Map creation data on the server; on the client the extra data of the creation screen, nil when it has none
Returns
- Boolean
Checks whether the rank of a player lets them demote another player. Both have to be
members of the same faction, the rank of the actor has to have the can_demote field, and
the target must hold a rank that is not the lowest one and is no higher than that field
allows. Nobody may demote themselves. The permissions of staff are not looked at here:
the /rank demote command accepts either.
Parameters
- actor Player the player who wants to demote
- target Player the player who would be demoted
Returns
- Boolean
See also
Checks whether the rank of a player lets them promote another player. Both have to be
members of the same faction, the rank of the actor has to have the can_promote field, and
the rank the target would be promoted to must exist and be no higher than that field
allows. Nobody may promote themselves. The permissions of staff are not looked at here:
the /rank promote command accepts either.
FACTION:add_rank('recruit')
FACTION:add_rank('officer')
FACTION:add_rank('sergeant', 'Sgt.', { can_promote = 'officer' })
-- A sergeant may promote a recruit to officer, but not an officer to sergeant.
if Factions.can_promote(sergeant, recruit) then
recruit:promote_rank()
end
Parameters
- actor Player the player who wants to promote
- target Player the player who would be promoted
Returns
- Boolean
See also
Checks whether a player may be moved into a faction: asks the Faction:can_transfer
callback of that faction, then the PlayerCanTransferFaction hook. The SetFaction command
uses it; Player:set_faction moves the player without asking. Server only.
local allowed, reason, arguments = Factions.can_transfer(target, faction_table)
if allowed then
target:set_faction(faction_table.faction_id)
else
actor:notify(reason, arguments)
end
Parameters
- target Player the player who is to be moved
- faction_table Faction the faction they are to be moved into
Returns
- Boolean true when the transfer is allowed; otherwise false
- String the reason as a language phrase and Map the arguments of the phrase
Factions.character_limit_reached(target, faction_id)
shared plugins/factions/plugin/lib/sh_faction.lua:285Checks whether a player has as many characters of a faction as its max_characters field allows, so that they cannot create another one. On the client only the local player can be checked.
Parameters
- target Player
- faction_id String
Returns
- Boolean false as well when the faction does not limit the characters
See also
Returns how many characters of a faction a player has. The server counts the characters of the player's record; a client only knows the count of the local player, which the server networks to them.
Parameters
- target Player
- faction_id String
Returns
- Number
Finds a faction by its whole ID or its whole name, ignoring letter case. The name may be given as the language phrase the faction is registered with or as its translation. An ID is matched before any name.
Parameters
- name String faction ID or name
Returns
- Faction/Boolean the faction, or false if no faction has this ID or name
Returns the faction registered under the exact ID.
Parameters
- id String faction ID
Returns
- Faction the faction, or nil if it is not registered
Factions.get_default_description(faction_table, target)
server plugins/factions/plugin/lib/sh_faction.lua:500Returns the physical description that a faction gives to the characters of a player, translated to the language of that player. Server only.
Parameters
- faction_table Faction
- target Player optional, defaults to
nilthe player whose language is used; English without one
Returns
- String
Returns how many members of a faction, or holders of one of its ranks, may be online at once: the limit field of the faction or of the rank, unless a GetFactionLimit hook returns another number.
Parameters
- id String faction ID
- rank Number optional, defaults to
nilrank index; the limit of the faction is returned without it
Returns
- Number 0 when there is no limit, and when the faction or the rank does not exist
Returns every connected player whose faction is the given one.
Parameters
- id String faction ID
Returns
- List<Player>
Returns the model that a player uses because of their rank, instead of the model of their character.
Parameters
- target Player
Returns
- String model path, or nil if the rank of the player does not change the model
See also
Brings the weapons of a player in line with the loadout of their faction and rank: gives the weapons of the loadout that they do not have, and takes back the weapons it gave earlier that are not in the loadout any more. Which weapons it gave is remembered in the faction_weapons field of the player, so a weapon they hold for another reason, such as the default loadout, is left alone. Server only.
Parameters
- target Player a living player
See also
Includes every file of a folder as a faction definition. Each file gets a fresh FACTION global, a Faction named after the file, that is registered once the file has run.
-- factions/sh_police.lua, registered as 'police'
FACTION.name = 'Police'
FACTION.description = 'Keeps order in the city.'
FACTION.color = Color(60, 100, 200)
FACTION.whitelisted = true
FACTION.models.universal = { 'models/police.mdl' }
FACTION:add_rank('recruit', 'Rct.')
FACTION:add_rank('officer', 'Ofc.')
Parameters
- directory String folder to include files from
Checks whether a faction, or one of its ranks, has as many members online as its limit allows. Bots and players with an active character count as members.
Parameters
- id String faction ID
- rank Number optional, defaults to
nilrank index; the whole faction is checked without it - except Player optional, defaults to
nilplayer who is not counted, such as the one who wants to join
Returns
- Boolean false as well when there is no limit
See also
Makes NPCs feel about a player the way they did before the faction of the player changed it. Server only.
Parameters
- target Player
Factions.send_character_counts(target, except)
server plugins/factions/plugin/lib/sh_faction.lua:510Networks to a player how many characters they have in each faction, which lets their client tell whether they may create another character of a faction. The plugin calls it whenever the characters of a player or their factions change. Server only.
Parameters
- target Player
- except Character optional, defaults to
nilcharacter that is not counted, such as one that is about to be deleted
Makes an NPC feel about every player the way the factions of the players say. The plugin calls it for every NPC right after it has been created. Server only.
Parameters
- npc NPC