Skip to content
RustPluginGenerator

OnQuarryToggle

OnQuarryToggle is called when a player uses the engine switch on a MiningQuarry (pump jack), before the quarry is actually started or stopped.

Signature
object OnQuarryToggle(MiningQuarry miningQuarry, BasePlayer player)

Parameters

Hook parameters
ParameterTypeDescription
miningQuarryMiningQuarryThe MiningQuarry entity whose engine switch was used. Declaring MiningQuarry (instead of BaseEntity) keeps the hook limited to actual quarries; the RPC target is always a quarry here.
playerBasePlayerThe BasePlayer who triggered the switch through the pump jack's engine RPC. It is read from the RPC message and is not re-validated in the injection path, so check that it is non-null and connected before using it.

Return value

Returning a non-null value (for example true) stops the original code path: the engine state is not changed and OnQuarryToggled does not fire. Returning null lets the quarry start or stop as normal.

How it works

The hook fires from EngineSwitch.StartEngine and EngineSwitch.StopEngine. Those methods resolve the parent entity of the switch, confirm it is a MiningQuarry, and then call OnQuarryToggle before calling miningQuarry.EngineSwitch(isOn: true) or isOn: false. Both injection points are the same hook, so a single method handles turning the quarry on and off; there is no separate flag telling you which direction the player requested. If you need to know, read the quarry's current state (miningQuarry.IsOn()) and treat the call as a request to flip it.

miningQuarry gives you the entity for ownership and position checks, and player is the user who sent the RPC. Declaring miningQuarry as MiningQuarry is the natural narrowing; the game method already guarantees that type. Because player comes straight from the RPC message, treat it as untrusted input: verify IsConnected, ownership, team, privilege or permission before deciding what to do.

After the engine state changes, OnQuarryToggled fires with the same quarry and player, so use that hook for post-change reactions and this one for gates.

Example plugin: Restrict pump jack use to owners, teammates and permission holders

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

namespace Oxide.Plugins
{
    [Info("Quarry Toggle Guard", "RustPluginGenerator", "1.0.0")]
    [Description("Restricts who may start or stop pump jack engines on the server")]
    public class QuarryToggleGuard : RustPlugin
    {
        private const string PermAllowOthers = "quarrytoggleguard.allowothers";

        private PluginConfig config;

        private class PluginConfig
        {
            [JsonProperty("Only owner and team members may toggle")]
            public bool OwnerOnly { 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>
            {
                ["Denied"] = "You are not allowed to use this pump jack."
            }, 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(PermAllowOthers, this);
        }

        private object OnQuarryToggle(MiningQuarry miningQuarry, BasePlayer player)
        {
            if (miningQuarry == null || player == null || !player.IsConnected)
                return true;

            if (permission.UserHasPermission(player.UserIDString, PermAllowOthers))
                return null;

            if (!config.OwnerOnly)
                return null;

            ulong ownerId = miningQuarry.OwnerID;
            if (ownerId == 0 || ownerId == player.userID.Get())
                return null;

            if (player.currentTeam != 0 && player.currentTeam == GetTeamOf(ownerId))
                return null;

            player.ChatMessage(Msg("Denied", player.UserIDString));
            return true;
        }

        private ulong GetTeamOf(ulong ownerId)
        {
            var owner = BasePlayer.FindByID(ownerId);
            return owner != null ? owner.currentTeam : 0;
        }

        private void Unload()
        {
            config = null;
        }
    }
}

The plugin answers three questions when a player flips a pump jack's switch: is the player connected, does the player own the quarry or belong to the owner's team, and does the player hold the quarrytoggleguard.allowothers permission. Owned quarries can also be limited to the owner and teammates by a config toggle. When access is denied the hook returns true, which blocks the game from starting or stopping the engine, and the player receives a localized chat message. The config holds the language toggle and the ownership-mode switch, and every text goes through the language file so it can be translated.

Common use cases

  • Lock quarries to owners and teams

    Reject switches from players who are neither the owner nor a team member, so a raided or abandoned pump jack cannot be started by strangers.

  • Gate behind a permission or economy plugin

    Return true for players without a purchased permission, turning pump jacks into a paid feature while leaving the entity untouched.

  • Stop switches during an event or wipe block

    Block engine changes while a server event is running or during a startup window, without altering the quarry's stored state.

Pitfalls

  • A null return is required to allow

    Returning true or any object blocks the switch. Make sure your allow path explicitly returns null, otherwise the quarry never starts.

  • You cannot tell start from stop

    Both engine methods call the same hook. Use miningQuarry.IsOn() if your rule depends on the intended direction, and be aware the value is the pre-toggle state.

  • The player value comes from the RPC

    Do not assume player is valid or connected; null-check it and guard against dead or sleeping players before reading ownership or team data.

Build a plugin with OnQuarryToggle

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

Prompt: Lock quarries to owners and teams using the OnQuarryToggle 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 OnQuarryToggle

Generate