OnDieselEngineToggle
OnDieselEngineToggle is called when a player toggles a DieselEngine on or off, before the server reads the requested state from the RPC and changes the engine.
object OnDieselEngineToggle(DieselEngine dieselEngine, BasePlayer player)Parameters
| Parameter | Type | Description |
|---|---|---|
dieselEngine | DieselEngine | The DieselEngine entity whose fuel-burning state is being switched. Use it to read the engine position, its storage inventory or its owner. |
player | BasePlayer | The BasePlayer who sent the toggle RPC. May be null if the message was not sent by a connected player, so always null-check it. |
Return value
Returning a non-null value (for example true) stops EngineSwitch immediately, so the engine is not toggled and the client request is dropped. Returning null lets the original method continue and process the requested state normally.
How it works
The hook fires inside DieselEngine.EngineSwitch, which the client calls when a player interacts with a diesel engine's toggle button. It runs before the bit that carries the requested on/off state is read from the RPC message and before the engine's burning state changes. Because it fires on the incoming request, it is the right place to allow or block the action itself rather than just react to the result.
The dieselEngine parameter gives you the entity, so you can check its position, building privilege, owner, fuel storage or ion level. The player parameter is the sender of the RPC; null-check it because it may not resolve to a connected player. Since the signature is fixed to DieselEngine and BasePlayer, there is no broader entity type to narrow here.
Use it for restrictions such as only letting authorised players start engines inside a base, blocking engines in a no-build zone, or logging who started each generator. For reacting after the state has changed, use OnDieselEngineToggled instead.
Example plugin: Restrict diesel engine control to building owners
using System.Collections.Generic;
using System;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Diesel Engine Guard", "RustPluginGenerator", "1.0.0")]
[Description("Restricts diesel engine toggling to players authorised on the building's cupboard.")]
public class DieselEngineGuard : RustPlugin
{
private const string PermBypass = "dieselengineguard.bypass";
private PluginConfig config;
public class PluginConfig
{
[JsonProperty("Require cupboard authorisation")]
public bool RequireAuth { get; set; } = true;
[JsonProperty("Allow engines outside cupboards")]
public bool AllowUnclaimed { get; set; } = 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>
{
["NotAuthorised"] = "You are not authorised to control this diesel engine.",
["NotClaimed"] = "This diesel engine is not inside a protected base."
}, this);
}
private string Msg(string key, string userId = null) => lang.GetMessage(key, this, userId);
private void Init()
{
permission.RegisterPermission(PermBypass, this);
}
private object OnDieselEngineToggle(DieselEngine dieselEngine, BasePlayer player)
{
if (dieselEngine == null || player == null || player.IsNpc) return null;
if (player.IsAdmin || permission.UserHasPermission(player.UserIDString, PermBypass)) return null;
if (!config.RequireAuth) return null;
var privilege = dieselEngine.GetBuildingPrivilege();
if (privilege == null)
{
if (config.AllowUnclaimed) return null;
player.ChatMessage(Msg("NotClaimed", player.UserIDString));
return true;
}
if (privilege.IsAuthed(player)) return null;
player.ChatMessage(Msg("NotAuthorised", player.UserIDString));
return true;
}
}
}The plugin registers a single permission dieselengineguard.bypass and checks it in OnDieselEngineToggle. If the player is not an admin and lacks the bypass permission, the hook looks up the building privilege the engine sits under with GetBuildingPrivilege(). When a privilege exists and the player is not authorised on it, the toggle is cancelled by returning a non-null value and the player gets a chat message from the language file. Engines outside any cupboard and players with bypass can still use them normally. The response is stored in a configurable message so admins can change the wording without editing code.
Common use cases
Block engine use in unclaimed areas
Return a non-null value when the engine has no building privilege so diesel generators can only be operated inside a protected base.
Raid protection for generators
Deny toggles from players who are not authorised on the engine's cupboard, so intruders cannot switch a base's power source off.
Audit fuel usage
Record the player id, engine position and time on each toggle into a data file so staff can trace who has been running generators.
Pitfalls
The state is not in the signature
This hook fires before the on/off bit is read, so you cannot tell from the parameters whether the player is starting or stopping the engine. Use
OnDieselEngineToggledif you need the resulting state.player can be null
The RPC sender is passed straight through; if no player resolves, dereferencing it throws. Check
player == nullbefore reading permissions or sending chat messages.Cancelling breaks client prediction
Returning non-null drops the request, so the client's toggle animation will not match the server. Send a short chat or console message so the player understands why nothing happened.
Build a plugin with OnDieselEngineToggle
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 OnDieselEngineToggle
Generate