Skip to content
RustPluginGenerator

CanSetBedPublic

CanSetBedPublic is called when a player toggles a sleeping bag or bed between public and private via the SleepingBag.RPC_MakePublic server RPC, before the visibility change is applied.

Signature
object CanSetBedPublic(BasePlayer player, SleepingBag sleepingBag)

Parameters

Hook parameters
ParameterTypeDescription
playerBasePlayerThe BasePlayer who sent the make-public or make-private request for the sleeping bag. This is the player who triggered the toggle, not necessarily the bag owner.
sleepingBagSleepingBagThe SleepingBag (or bed) entity being toggled. Check its OwnerID to identify the owner and its IsPublic() state for the current visibility.

Return value

Return null (or let the method fall through) to allow the game to apply the new public/private state with SetPublic. Return any non-null value to stop the original method before SetPublic runs, so the bed keeps its previous visibility and the player receives no game-level feedback unless your plugin sends its own.

How it works

The hook fires inside SleepingBag.RPC_MakePublic after the game has already validated that the bed can be public, that the caller can access it, and that the caller is the deployer or has build privilege in the area. At that point the requested state is read from the RPC payload, and Oxide calls CanSetBedPublic with the requesting player and the bag. A non-null return cancels the change, so the bed keeps its current public/private state. The flag == IsPublic() check means the hook is only reached when the requested state actually differs from the current one; redundant requests short-circuit before your hook runs.

Use player to gate by permission, team, or ownership, and sleepingBag to inspect the bed itself, for example its OwnerID to enforce a maximum number of public beds per owner. Declaring the parameter as SleepingBag keeps the hook restricted to bag-type entities; declaring BasePlayer is already the specific type. Blocking here is safe because the original method returns early without side effects when you return a value.

Example plugin: Limit public beds per owner and require permission

PublicBedLimit.cs
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;

namespace Oxide.Plugins
{
    [Info("PublicBedLimit", "RustPluginGenerator", "1.0.0")]
    [Description("Limits how many sleeping bags a player can make public and requires a permission to do so")]
    public class PublicBedLimit : RustPlugin
    {
        private const string PermUse = "publicbedlimit.use";

        private PluginConfig config;

        private class PluginConfig
        {
            [JsonProperty("Max public beds per owner")]
            public int MaxPublicBeds { get; set; } = 2;

            [JsonProperty("Require permission to make beds public")]
            public bool RequirePermission { 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>
            {
                ["NoPermission"] = "You are not allowed to make sleeping bags public.",
                ["LimitReached"] = "You already have {0} public sleeping bags (limit {1})."
            }, 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(PermUse, this);
        }

        private object CanSetBedPublic(BasePlayer player, SleepingBag sleepingBag)
        {
            if (player == null || sleepingBag == null) return null;
            if (sleepingBag.IsPublic()) return null; // only gate transitions to public

            if (config.RequirePermission && !permission.UserHasPermission(player.UserIDString, PermUse))
            {
                player.ChatMessage(Msg("NoPermission", player.UserIDString));
                return true;
            }

            ulong ownerId = sleepingBag.OwnerID;
            int publicCount = 0;
            foreach (var entity in BaseNetworkable.serverEntities)
            {
                var bag = entity as SleepingBag;
                if (bag == null || bag.IsDestroyed) continue;
                if (bag.OwnerID != ownerId) continue;
                if (bag.IsPublic()) publicCount++;
            }

            if (publicCount >= config.MaxPublicBeds)
            {
                player.ChatMessage(Msg("LimitReached", player.UserIDString, publicCount, config.MaxPublicBeds));
                return true;
            }

            return null;
        }
    }
}

The plugin intercepts every public/private toggle and only acts when the request is to make the bed public. It first checks a publicbedlimit.use permission so unpermitted players cannot expose beds at all. It then counts how many sleeping bags the same owner has already made public across the server and rejects the change once the configured cap is reached. A rejection sends a localized chat message and returns true to cancel the RPC. Making a bed private is always allowed, so cleanup is never blocked. The cap and permission name are read from config with defaults.

Common use cases

  • Cap public beds per player

    Count existing public sleeping bags owned by the same player and refuse new ones beyond a configured limit to reduce public spawn-point spam.

  • Restrict public beds to a permission or team

    Block non-VIP players or non-team members from making beds public, or require a group permission for the toggle.

  • Log bed visibility changes

    Record who changed a bed's visibility and when, so admins can audit griefing reports about shared spawn points.

Pitfalls

  • Not the same as bed assignment

    CanSetBedPublic controls public visibility only. Blocking it does not stop a player assigning themselves to or renaming the bed; use CanAssignBed and CanRenameBed for that.

  • Ownership uses deployer ID

    The bed's OwnerID may not equal the requesting player, for example when a builder toggles a bed they did not deploy. Decide whether your rules should follow the owner or the requester.

  • The hook does not fire for no-op toggles

    The game returns before calling the hook when the requested state already matches, so your logic will not run for redundant RPCs and cannot be used to broadcast confirmations.

Build a plugin with CanSetBedPublic

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

Prompt: Cap public beds per player using the CanSetBedPublic 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 CanSetBedPublic

Generate