Skip to content
RustPluginGenerator

CanUseMailbox

CanUseMailbox is called when the game checks whether a player may open a specific Mailbox, before the default building-privilege check runs.

Signature
object CanUseMailbox(BasePlayer player, Mailbox mailbox)

Parameters

Hook parameters
ParameterTypeDescription
playerBasePlayerThe BasePlayer who is trying to access the mailbox. Always the player who triggered the ownership check.
mailboxMailboxThe Mailbox entity being accessed. Use it to inspect position, owner, and building privilege context.

Return value

Return null (or any non-bool) to let the default player.CanBuild() ownership check decide. Return a bool to override that check entirely: true grants access, false denies it, regardless of building privilege.

How it works

The hook fires inside Mailbox.PlayerIsOwner(BasePlayer player), which the game calls when a player attempts to interact with a mailbox. It is the gatekeeper for mailbox access: whatever the game was about to decide (normally player.CanBuild()) can be replaced by your own decision.

The player parameter is the interacting player. The mailbox parameter is the entity being opened, useful for reading its position, owner, or the building privilege it sits in (mailbox.GetBuildingPrivilege()). Because the patched method only substitutes a result when a boolean is returned, returning null leaves the game's default logic untouched. That default allows access when the player can build at the mailbox's location, i.e. they are authorized on the surrounding cupboard.

You can narrow the hook by declaring a more specific entity type in place of Mailbox only if you are sure the mailbox object is an instance of that type, which it never will be for a dedicated subclass; keep Mailbox. If you need different behaviour by team, permission, or distance, do the check inside the hook and return true or false explicitly.

Example plugin: Restrict mailbox access to the owner's team

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

namespace Oxide.Plugins
{
    [Info("Team Mailbox Access", "RustPluginGenerator", "1.0.0")]
    [Description("Restricts mailbox access to the owner, the owner's team members, and permitted players")]
    public class TeamMailboxAccess : RustPlugin
    {
        private const string PermBypass = "teammailboxaccess.bypass";

        private PluginConfig config;

        private class PluginConfig
        {
            [JsonProperty("Allow admins to always open")]
            public bool AllowAdmins = true;

            [JsonProperty("Allow building privilege holders")]
            public bool AllowBuildingAuth = 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);

        private void Init()
        {
            permission.RegisterPermission(PermBypass, this);
        }

        private object CanUseMailbox(BasePlayer player, Mailbox mailbox)
        {
            if (player == null || mailbox == null) return null;

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

            ulong ownerId = mailbox.OwnerID;
            if (ownerId == 0) return null;
            if (ownerId == player.userID.Get()) return true;

            if (player.currentTeam != 0)
            {
                var owner = BasePlayer.FindByID(ownerId);
                if (owner != null && owner.currentTeam == player.currentTeam) return true;
            }

            if (config.AllowBuildingAuth)
            {
                var priv = mailbox.GetBuildingPrivilege();
                if (priv != null && priv.IsAuthed(player)) return true;
            }

            return false;
        }
    }
}

The plugin overrides mailbox access so only the mailbox owner and players on the same team may open it, plus anyone granted a permission. In CanUseMailbox it reads the mailbox OwnerID; if it matches the player, access is granted (true). Otherwise it checks player.currentTeam against the owner's team by looking up the owner with BasePlayer.FindByID and comparing team ids. If the owner is offline and the ids differ, it falls back to the configurable permission bypass before returning false. Admins on the permission list can always open. Because the hook returns an explicit boolean every call, the vanilla building-privilege check is bypassed, which is what a strict team policy needs.

Common use cases

  • Team-only mailboxes

    Limit mailbox access to the owner's team so groups can share an inbox without letting neighbours loot it.

  • Permission-based mail hubs

    Build a central mailbox that any player with a specific permission can open, useful for economy drop points or event rewards.

  • Distance or zone restriction

    Deny access when the player is not near the mailbox, or allow it only inside a marked safe zone, regardless of cupboard auth.

Pitfalls

  • Returning null keeps the cupboard rule

    If you only want to add restrictions on top of vanilla behaviour, remember that null means the game still runs player.CanBuild(). To deny you must return false explicitly; to grant you must return true.

  • Owner id is not the same as privilege

    mailbox.OwnerID identifies who placed it, not who is authorized on the surrounding cupboard. If your policy depends on zone auth, check mailbox.GetBuildingPrivilege() and its IsAuthed instead.

  • Called on every interaction

    The hook runs each time a player attempts to open a mailbox. Keep the body cheap; avoid scanning activePlayerList or logging to console inside it.

Build a plugin with CanUseMailbox

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

Prompt: Team-only mailboxes using the CanUseMailbox 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 CanUseMailbox

Generate