OnTurretToggle
OnTurretToggle is called from AutoTurret.SetIsOnline when the turret's online flag is about to change, and returning a non-null value blocks that change.
object OnTurretToggle(AutoTurret autoTurret)Parameters
| Parameter | Type | Description |
|---|---|---|
autoTurret | AutoTurret | The AutoTurret whose online state is about to change. It is still in its previous state when the hook runs, so calling IsOn() gives you the old value and the parameter passed to SetIsOnline is the desired new one. |
Return value
Returning null (or nothing) lets the toggle proceed and the On flag is written as requested. Returning any non-null value stops the original code path, so the turret stays in its current state and the flag update is skipped.
How it works
The hook fires inside AutoTurret.SetIsOnline only when the requested state differs from the current one (online != IsOn()), so it does not fire for redundant calls that would leave the flag as it is. That method is the shared entry point for turning a turret on or off: a player pressing the power button on the turret's UI, a server command, or plugin code calling SetIsOnline all funnel through it. When the hook runs, the On flag has not been written yet, so autoTurret.IsOn() still reports the old state and flag checks reflect the turret before the toggle. Use autoTurret.OwnerID, net.ID.Value or GetBuildingPrivilege() to identify who controls it. There is only one parameter type; declaring it narrower or broader than AutoTurret is not useful here, but you can still confirm the entity is not destroyed before reading it. Because the hook only fires on real state changes, it is the right place to enforce per-turret power policies without spamming your logic on every network update.
Example plugin: Require authorization to power turrets
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Turret Power Guard", "RustPluginGenerator", "1.0.0")]
[Description("Blocks AutoTurret power changes when the owner is not authorized on the building privilege")]
public class TurretPowerGuard : RustPlugin
{
private const string PermBypass = "turretpowerguard.bypass";
private PluginConfig config;
private class PluginConfig
{
[JsonProperty("Guard enabled")]
public bool Enabled = true;
[JsonProperty("Admins bypass the check")]
public bool AdminsBypass = true;
[JsonProperty("Require privilege authorization")]
public bool RequireAuth = true;
[JsonProperty("Skip turrets without a building privilege")]
public bool SkipWithoutPrivilege = true;
}
protected override void LoadDefaultConfig() => config = new PluginConfig();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<PluginConfig>();
if (config == null) throw new Exception();
}
catch
{
PrintWarning("Configuration file is invalid, using defaults");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void SaveConfig() => Config.WriteObject(config);
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["NotAuthorized"] = "This turret is not authorized on its building privilege and cannot be powered."
}, this);
}
private string Msg(string key, string userId = null, params object[] args) =>
string.Format(lang.GetMessage(key, this, userId), args);
private void Init()
{
permission.RegisterPermission(PermBypass, this);
}
private object OnTurretToggle(AutoTurret autoTurret)
{
if (!config.Enabled || autoTurret == null || autoTurret.IsDestroyed)
return null;
if (config.AdminsBypass && autoTurret.OwnerID.IsSteamId())
{
BasePlayer owner = BasePlayer.FindByID(autoTurret.OwnerID);
if (owner != null && owner.IsAdmin)
return null;
}
if (autoTurret.OwnerID != 0 && permission.UserHasPermission(autoTurret.OwnerID.ToString(), PermBypass))
return null;
BuildingPrivlidge privilege = autoTurret.GetBuildingPrivilege();
if (privilege == null)
return null;
if (!config.RequireAuth && config.SkipWithoutPrivilege)
return null;
if (privilege.IsAuthed(autoTurret.OwnerID))
return null;
BasePlayer ownerPlayer = BasePlayer.FindByID(autoTurret.OwnerID);
if (ownerPlayer != null && ownerPlayer.IsConnected)
ownerPlayer.ChatMessage(Msg("NotAuthorized", ownerPlayer.UserIDString));
return true;
}
}
}The plugin intercepts every attempt to change an AutoTurret's online state. In OnTurretToggle it reads the building privilege the turret is attached to and checks whether the requesting logic is allowed: turrets with a privilege only switch on if the owner is authorized on that cupboard, and turrets owned by an admin bypass the check per config. When the check fails the hook returns a non-null value, which blocks the flag write and leaves the turret off. Failures are sent as a chat notice to the owner instead of printing inside the hook. Config controls whether the guard is active, whether admins are exempt, and whether turrets without a privilege are ignored. Messages are localized through the language file.
Common use cases
Restrict turret activation to cupboard members
Block turning a turret on when the owner is no longer authorized on the building privilege it sits in, so raided turrets cannot be revived by anyone who happens to reach the power button.
Per-map turret limits
Track how many turrets each player has online and refuse toggles past a configured limit, keeping base defense tuned to server size.
Scheduled night shutdown
Combine the hook with a timer that calls SetIsOnline(false) during a maintenance window and use the hook to reject any attempts to re-enable turrets until the window ends.
Pitfalls
Only fires on actual state changes
Calling SetIsOnline with the same value the turret already has never reaches the hook, so do not rely on it to detect repeated power input.
State is still the old one inside the hook
IsOn() reflects the previous state when the hook runs. If you need the requested value, compare it with your own bookkeeping or read the desired parameter from the code path that called SetIsOnline.
Blocking can strand turrets off
Returning non-null cancels the toggle silently from the game's perspective. Provide a chat or console explanation, otherwise players see the button do nothing.
Build a plugin with OnTurretToggle
Describe what should happen and get a compiled plugin that uses this hook.
Edit the prompt on the next screen. 2 free plugins a month, no card.
Last updated Oct 26, 2026. Found a mistake? Email support@rustplugingenerator.com.
Build a plugin with OnTurretToggle
Generate