OnDieselEngineToggled
OnDieselEngineToggled is called after a DieselEngine switches its running state, either starting or stopping. It fires at the end of both DieselEngine.EngineOn and DieselEngine.EngineOff, so you cannot tell the two apart from the hook alone.
void OnDieselEngineToggled(DieselEngine dieselEngine)Parameters
| Parameter | Type | Description |
|---|---|---|
dieselEngine | DieselEngine | The DieselEngine entity whose run state just changed. It is a BaseEntity, so OwnerID, net.ID and transform are available, and HasFlag(BaseEntity.Flags.Open) reflects whether the engine is now on. |
Return value
Return value is ignored at both injection sites. The original EngineOn and EngineOff methods return void and the hook call is the last statement, so returning a value or null changes nothing about the toggle. Use the hook only for side effects.
How it works
The hook fires inside DieselEngine.EngineOn and DieselEngine.EngineOff after the engine has updated its Flags.On flag and after the game broadcasts the DieselEngineOn or DieselEngineOff entity message to nearby clients. Both paths call the same hook, so the parameter is always the engine that changed state; check dieselEngine.HasFlag(BaseEntity.Flags.Open) to know whether it just started or stopped. There is no separate start/stop hook, so all toggles arrive here.
Because the call happens after the flag change and the network broadcast, reacting here is safe for UI, sound effects, logging or saving state. The parameter is typed DieselEngine, so Oxide only invokes your method for that exact entity type; declaring BaseEntity would not widen it because the game only calls it for engines. dieselEngine.IsDestroyed guards against a plugin acting on a removed entity, and dieselEngine.OwnerID identifies who placed it when you need to attribute the action.
Diesel engines typically only toggle on player interaction, so the hook is low-frequency and cheap enough for chat feedback, timers or persistence writes.
Example plugin: Warn owners and log when a diesel engine starts or stops
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Diesel Engine Monitor", "RustPluginGenerator", "1.0.0")]
[Description("Notifies diesel engine owners and logs start/stop events")]
public class DieselEngineMonitor : RustPlugin
{
private PluginConfig config;
private int toggleCount;
private readonly Dictionary<string, int> perOwner = new Dictionary<string, int>();
private class PluginConfig
{
[JsonProperty("Notify owners in chat")]
public bool NotifyOwners = true;
[JsonProperty("Print admin summary on each toggle")]
public bool PrintAdminSummary = false;
}
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>
{
["Started"] = "Your diesel engine at {0} started.",
["Stopped"] = "Your diesel engine at {0} stopped."
}, this);
}
private string Msg(string key, string userId = null, params object[] args) =>
string.Format(lang.GetMessage(key, this, userId), args);
private void OnDieselEngineToggled(DieselEngine dieselEngine)
{
if (dieselEngine == null || dieselEngine.IsDestroyed) return;
bool running = dieselEngine.HasFlag(BaseEntity.Flags.Open);
toggleCount++;
ulong ownerId = dieselEngine.OwnerID;
if (ownerId == 0) return;
string ownerKey = ownerId.ToString();
perOwner.TryGetValue(ownerKey, out int count);
perOwner[ownerKey] = count + 1;
if (config.NotifyOwners)
{
BasePlayer owner = BasePlayer.FindByID(ownerId);
if (owner != null && owner.IsConnected)
{
Vector3 pos = dieselEngine.transform.position;
string where = $"{Mathf.RoundToInt(pos.x)}, {Mathf.RoundToInt(pos.z)}";
owner.ChatMessage(Msg(running ? "Started" : "Stopped", owner.UserIDString, where));
}
}
if (config.PrintAdminSummary)
{
Puts($"Engine {dieselEngine.net.ID.Value} at {dieselEngine.transform.position} " +
$"{(running ? "started" : "stopped")} by owner {ownerKey} (total {toggleCount})");
}
}
private void Unload()
{
Interface.Oxide.DataFileSystem.WriteObject(Name, new Dictionary<string, object>
{
["TotalToggles"] = toggleCount,
["PerOwner"] = perOwner
});
}
}
}The plugin listens on OnDieselEngineToggled, reads HasFlag(BaseEntity.Flags.Open) to decide whether the engine just started or stopped, and reacts. If the engine has an owner (OwnerID is set to a real player who is online), it prints a short localized chat message telling them their engine changed state, including the position rounded to whole coordinates so they can find it. Every toggle is also appended to a per-day in-memory counter and broadcast to admins as a console line, which keeps high-traffic servers from spamming chat. Config fields toggle the chat notification and the admin console line independently. On Unload the counter is flushed to oxide/data so a restart does not lose the day's activity summary.
Common use cases
Notify owners about engine state
Send a localized chat or CUI notice to the engine's OwnerID when it starts or stops, useful on large bases where hearing the engine is not guaranteed.
Track fuel-farm activity
Count toggles per owner over a wipe to see which players run diesel engines heavily, then feed a leaderboard or an Economy plugin reward.
Audit unusual toggles
Log engine toggles with owner, position and timestamp to a data file so admins can review suspicious start/stop patterns after a raid.
Pitfalls
No direction in the parameter
The same hook fires for start and stop. Without reading
HasFlag(BaseEntity.Flags.Open)you cannot know which happened, and the flag is already updated by the time the hook runs.Broadcast is already sent
The engine's
DieselEngineOn/DieselEngineOffentity message has already gone out to clients. The hook cannot suppress the sound for nearby players; it only lets you react.Owner may be gone
OwnerID can point at a disconnected or deleted player, so resolve it with
BasePlayer.FindByIDand null-check before sending a chat message.
Build a plugin with OnDieselEngineToggled
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 OnDieselEngineToggled
Generate