Skip to content
RustPluginGenerator

OnPowergridStageChanged

OnPowergridStageChanged is called by PowergridManager.ServerTick after the grid's On flag is updated and before its fuse boxes are notified of the new stage.

Signature
void OnPowergridStageChanged(PowergridManager powergridManager, int stage)

Parameters

Hook parameters
ParameterTypeDescription
powergridManagerPowergridManagerThe PowergridManager whose electrical stage is being recalculated. Use its net.ID, position or the fuse boxes it manages to identify which grid changed.
stageintThe new integer stage of the powergrid. A value greater than zero means the grid is powered on; zero means it has lost power.

Return value

The return value is ignored. Returning null, a value, or nothing at all has no effect on the manager's stage update, the work queue restart or the fuse box notifications that follow.

How it works

This hook fires inside PowergridManager.ServerTick every time the manager recalculates its power grid and the resulting stage differs from the previous one. Oxide calls it after the Flags.On flag has been set according to whether the new stage is above zero, and before stageChangeWorkQueue restarts and each fuse box receives Server_OnPowergridStageChanged. The first parameter is the manager itself, which is useful for reading its position, net.ID or building privilege; the second is the new stage as an integer, where values above zero mean the grid is powered and 0 means it has gone dark.

The manager instance is the same type for every grid, so you cannot narrow calls by declaring a more specific parameter type for it; there is only one grid class. Instead, filter inside the hook using position, entity ID or a stored set of managers you care about. Because the hook runs during server tick, keep the body short: set flags, schedule timers, and avoid iterating large entity lists or doing expensive scans. Fuse boxes have not yet been notified when your code runs, so state you read from connected entities may still reflect the previous stage.

Example plugin: Announce powergrid outages to nearby players

PowergridMonitor.cs
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("Powergrid Monitor", "RustPluginGenerator", "1.0.0")]
    [Description("Notifies nearby players when a power grid loses or regains power.")]
    public class PowergridMonitor : RustPlugin
    {
        private const string PermAnnounce = "powergridmonitor.announce";

        private class PluginConfig
        {
            [JsonProperty("Notification radius (meters)")]
            public float Radius = 60f;

            [JsonProperty("Announce power restoration")]
            public bool AnnounceOn = true;

            [JsonProperty("Announce power loss")]
            public bool AnnounceOff = true;
        }

        private PluginConfig config;
        private readonly Dictionary<ulong, int> lastStage = new Dictionary<ulong, int>();

        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>
            {
                ["PowerLost"] = "The power grid near you has lost power.",
                ["PowerRestored"] = "The power grid near you is back online."
            }, 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(PermAnnounce, this);
        }

        private void OnPowergridStageChanged(PowergridManager powergridManager, int stage)
        {
            if (powergridManager == null || powergridManager.net == null) return;
            if (powergridManager.IsDestroyed) return;

            ulong id = powergridManager.net.ID.Value;
            int previous;
            bool hadPrevious = lastStage.TryGetValue(id, out previous);
            lastStage[id] = stage;

            if (!hadPrevious) return;

            bool lost = previous > 0 && stage <= 0;
            bool restored = previous <= 0 && stage > 0;
            if ((lost && !config.AnnounceOff) || (restored && !config.AnnounceOn)) return;

            string key = lost ? "PowerLost" : "PowerRestored";
            Vector3 origin = powergridManager.transform.position;

            foreach (BasePlayer player in BasePlayer.activePlayerList)
            {
                if (player == null || !player.IsConnected || player.IsDead()) continue;
                if (Vector3.Distance(player.transform.position, origin) > config.Radius) continue;
                player.ChatMessage(Msg(key, player.UserIDString));
            }
        }

        private void Unload()
        {
            lastStage.Clear();
        }
    }
}

The plugin counts every call to OnPowergridStageChanged where the new stage is zero and the previous stage was above zero, which indicates the grid just lost power. It stores the last known stage per manager entity ID in a dictionary so repeated calls with the same stage are ignored. On a blackout it looks up the manager's position and sends a localized chat warning to every player within the configured radius. A short throttle prevents repeated messages if the grid flips quickly. The configuration holds the notification radius and whether messages should go to all players or only those outside building privilege. State is wiped in Unload.

Common use cases

  • Blackout notifications

    Tell nearby players when a power grid loses or regains power so they can react before turrets or lights fail.

  • Power monitoring dashboards

    Track stage transitions per manager entity ID and feed them into a UI panel or a periodic summary for admins.

  • Triggering events

    Start or stop a custom server event when a designated grid reaches a target stage, for example unlocking a reward crate.

Pitfalls

  • Fires every tick during flapping

    A grid that toggles rapidly calls the hook each tick it changes. Always store the last stage per manager and skip duplicate transitions before doing any work.

  • Fuse boxes not yet updated

    The hook runs before each fuse box is notified, so reading component states from connected entities may still show the old stage. Defer reads with NextTick or a timer.

  • No per-grid type to narrow on

    Every power grid uses the same PowergridManager type, so a more specific parameter type does not filter calls. Filter by entity ID or position inside the hook.

Build a plugin with OnPowergridStageChanged

Describe what should happen and get a compiled plugin that uses this hook.

Prompt: Blackout notifications using the OnPowergridStageChanged hook, with a config file and a permission
Generate this plugin

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 OnPowergridStageChanged

Generate