Hooks
They run on the server. Override only the ones you need. A hook that returns bool? answers “may this happen?”:
false refuses, true allows, null lets the game’s rules decide. For those, the first plugin (in file name order)
that returns something other than null decides.
Plugin and players
| Hook | When |
|---|---|
void OnLoaded() |
The plugin starts (server start, or a plugin file changed) |
void OnUnloaded() |
The plugin stops. Its commands and timers are removed for you |
string CanConnect( PluginConnection c ) |
Someone asks to join, before they appear: return a text to refuse them with it (reserved slots, whitelist, bad names), null to let them in. c.SteamId, c.Name, c.IsAdmin |
void OnPlayerConnected( RustPlayer player ) |
A player appears on the server |
void OnPlayerDisconnected( RustPlayer player ) |
A player leaves, before their sleeper and inventory are saved |
string OnPlayerChat( RustPlayer player, string message ) |
A player writes in the public chat (not commands or whispers). null: the line goes out as it is; "": it is blocked; another text goes out instead. Each plugin gets the text the previous one returned |
Combat, death and respawn
| Hook | When |
|---|---|
bool? CanTakeDamage( RustPlayer victim, RustLegacy.DamageInfo info ) |
A player is about to take damage. false: no damage. true: damage even with rl_pvp 0. info.Attacker is the GameObject that hit (null for the environment), info.Amount, info.Weapon, info.Bone |
void OnPlayerDeath( RustPlayer player, RustLegacy.DamageInfo info ) |
A player died. Their inventory is still full. info says who killed them, with what and where (info.Position) |
bool? CanDropDeathBag( RustPlayer player ) |
false: no bag on death, the inventory is lost |
Vector3? OnPlayerRespawn( RustPlayer player ) |
A player respawns. Return a position to replace the sleeping bag or the beach |
void OnPlayerRespawned( RustPlayer player ) |
A player respawned, with the starting Rock, Bandage and Torch |
Building, doors and placed objects
| Hook | When |
|---|---|
bool? CanBuild( RustPlayer player, ItemDefinition item, Vector3 position ) |
A player is about to place an object or a building piece, before it leaves their inventory. item.DisplayTitle is its name (a custom item’s too). false: refused (tell them why with Reply) |
void OnBuilt( RustPlayer player, GameObject placed ) |
A player placed an object or a piece. PlacedItem( placed ) tells what it is, OwnerOf( placed ) whose |
bool? CanDamageBuilding( GameObject target, RustLegacy.DamageInfo info ) |
A building piece or a placed object is about to take damage (bullets, melee, explosives). false: no damage |
bool? CanUseDoor( RustPlayer player, GameObject door ) |
A player opens or closes a placed door. true: they may, even locked (clan or friends’ doors); false: they may not; null: owner, code and lock decide |
void OnDeployableUse( RustPlayer player, GameObject deployable ) |
A player picked the action of a custom placed object (UseLabel, see Custom items) |
Gathering and crafting
| Hook | When |
|---|---|
int? OnGather( RustPlayer player, ItemDefinition item, int amount ) |
A player gathers from ore, a wood pile, a tree or a carcass. Return a number to replace amount, what the player gets (0: nothing); the ore, tree or carcass loses the same as without it. Each plugin gets the number the previous one returned |
bool? CanCraft( RustPlayer player, ItemDefinition item, int amount ) |
A player starts a craft, before the resources are taken. false: refused |
World
| Hook | When |
|---|---|
bool? CanSpawnAnimal( string kind, string species, Vector3 position ) |
A zombie, mutant or animal is about to appear. kind: zombie, mutant or animal; species: “Wolf”, “Fast Zombie”… false: it does not appear, its spawn point tries again later |
void OnAirdrop( SupplyCrate crate ) |
A plane dropped a crate (it is still falling). crate.WorldPosition follows it; crate.IsValid turns false when it is gone |
Vehicles (see Vehicles below)
| Hook | When |
|---|---|
bool? CanEnterVehicle( RustPlayer player, Vehicle vehicle ) |
A player is about to take the wheel of a free car. false: refused, they get “You cannot drive this” |
void OnVehicleEnter( RustPlayer player, Vehicle vehicle ) |
A player took the wheel |
void OnVehicleExit( RustPlayer player, Vehicle vehicle ) |
The driver got out: the use key, death, a teleport, the car removed or blown up |
bool? CanDamageVehicle( Vehicle vehicle, RustLegacy.DamageInfo info ) |
A car is about to lose health: bullets, blows, explosions, and crashes (info.Type Generic, info.Attacker the driver who rammed). false: no damage. It comes before the server’s PvP rule: shots at a driver hit the car, even with rl_pvp 0, so on a PvP-off server this is where a plugin protects other people’s cars |
void OnVehicleDestroyed( Vehicle vehicle, RustLegacy.DamageInfo info ) |
A car blew up: its driver is already out, the wreck takes its place. info.Attacker destroyed it |
A few of them at work:
// VIPs gather twice as muchpublic override int? OnGather( RustPlayer player, ItemDefinition item, int amount ) => HasPermission( player.SteamId, "gather.double" ) ? amount * 2 : null;
// a word filterpublic override string OnPlayerChat( RustPlayer player, string message ) => message.Replace( "noob", "n***" );
// clan members open each other's doors (SameClan: your own method, over your plugin's data)public override bool? CanUseDoor( RustPlayer player, GameObject door ) => SameClan( player.SteamId, OwnerOf( door ) ) ? true : null;
// no building within 100 m of the map's centrepublic override bool? CanBuild( RustPlayer player, ItemDefinition item, Vector3 position ){ if ( Meters( position, Vector3.Zero ) > 100 ) return null; Reply( player, "No building here." ); return false;}