Skip to content
RustPluginGenerator

CanDemolish

CanDemolish is called when a player tries to demolish a decayable entity with the demolish action, before the game decides whether the entity can be removed. Returning a bool overrides the default decision.

Signature
object CanDemolish(BasePlayer player, DecayEntity decayEntity)

Parameters

Hook parameters
ParameterTypeDescription
playerBasePlayerThe BasePlayer attempting to demolish the entity. Check this for permissions, building authorization or team membership before allowing the action.
decayEntityDecayEntityThe DecayEntity (a deployable or building piece that supports decay) being demolished. Use it to identify the target by prefab, owner, position or building ID.

Return value

Return true to allow the demolish, false to block it, or null to fall through to the vanilla IsDemolishSupported/IsDemolishable checks. Any non-bool return is ignored and the original method continues.

How it works

The hook fires inside DecayEntity.CanDemolish at the moment the game is about to evaluate whether a given player may demolish that entity. Rust calls this when the demolish input is used on a deployable or building piece that is flagged as demolishable, so it runs before the built-in checks (such as demolish support and demolish state) produce their result. The player argument is the actor performing the action, and decayEntity is the exact entity being targeted, so you can inspect its prefab, owner, building ID or container contents before granting or denying. Because the game passes the post-hook value straight to the return statement, whatever bool you return becomes the answer; returning nothing keeps the vanilla logic. You can narrow the dispatch by declaring the second parameter as a more specific type like StorageContainer or BuildingBlock; Oxide only calls your method when the argument matches, so other decays fall through to default behavior. Keep the hook lightweight: it fires per demolish attempt, not per frame, but admins may spam the action.

Example plugin: Allow demolition only for the entity owner and teammates

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

namespace Oxide.Plugins
{
    [Info("OwnerOnlyDemolish", "RustPluginGenerator", "1.0.0")]
    [Description("Restricts demolishing of structures to players authed on the building privilege.")]
    public class OwnerOnlyDemolish : RustPlugin
    {
        private const string PermAdmin = "owneronlydemolish.admin";

        private PluginConfig config;

        private class PluginConfig
        {
            [JsonProperty("Allow admins to demolish anything")]
            public bool AllowAdmins { 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>
            {
                ["NotAuthorized"] = "You are not authorized to demolish this structure."
            }, 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(PermAdmin, this);
        }

        private object CanDemolish(BasePlayer player, DecayEntity decayEntity)
        {
            if (player == null || decayEntity == null) return null;
            if (player.IsNpc) return null;

            var privilege = decayEntity.GetBuildingPrivilege();
            if (privilege == null) return null;

            if (privilege.IsAuthed(player)) return true;

            if (config.AllowAdmins && permission.UserHasPermission(player.UserIDString, PermAdmin))
                return true;

            player.ChatMessage(Msg("NotAuthorized", player.UserIDString));
            return false;
        }
    }
}

The plugin registers a permission and blocks demolish attempts that come from players outside the entity's building privilege. In CanDemolish it first null-checks the player and entity, then looks up the building privilege via decayEntity.GetBuildingPrivilege(). If a privilege exists and the player is authed on it, the hook returns true so the vanilla demolish path still runs normal checks. If the attacker is not authed but has the owneronlydemolish.admin permission, the demolish is also allowed. Everyone else gets false. A language message is sent only when the action is explicitly denied, keeping the chat quiet for legitimate demolishes.

Common use cases

  • Team-protected base cleanup

    Only let players who are authed on the building privilege demolish its pieces, so raiders who gain temporary access cannot wipe a base's interior.

  • Protect deployed storage

    Deny demolish on StorageContainer entities while they still hold items, forcing players to empty the box first to avoid losing looted contents.

  • Admin-only demolition of others' builds

    Restrict demolish on buildings not owned by the caller to a staff permission, letting admins clean up abandoned structures without granting that ability to regular players.

Pitfalls

  • Return type must be a bool

    Returning anything other than true, false or null is discarded by the dispatch logic, so a string or integer will silently fall back to vanilla behavior.

  • Do not break vanilla eligibility checks

    Returning true does not bypass IsDemolishSupported and IsDemolishable. If you return false you block the action entirely, so only deny when you are sure the player should not demolish, and let the game handle the rest.

  • Building privilege may be null

    GetBuildingPrivilege() returns null for entities outside any building block. Guard against this before calling IsAuthed, and remember that players building alone have no teammate to fall back on.

Build a plugin with CanDemolish

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

Prompt: Team-protected base cleanup using the CanDemolish 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 CanDemolish

Generate