Back to mods
Spawn In Bed project artwork

Vintage Story Mod DB · Vintage Story mod

Spawn In Bed

Sleeping in a bed sets your spawn point to it. Just that. Works with modded beds! Made because the usual mods for this aren't working on the latest version, so I took it upon myself to make my own implementation. Source Code (Sorry, too lazy to put this on gi

Choose a version Pick your version below, then grab the matching file.

Quick answer

Which Spawn In Bed release should I use?

Updated 4 months ago
Latest stable file 1.0.0
Game version 1.22.0, 1.22.1, 1.22.2
Loader Built-in Vintage Story mod system

Spawn In Bed 1.0.0 targets 1.22.0, 1.22.1, 1.22.2. It must be installed on the client. Installation on the dedicated server is optional. No extra mods listed for this file.

Where it goes

Is Spawn In Bed required on the client, server, or both?

It must be installed on the client. Installation on the dedicated server is optional.

Client Required
Dedicated server Optional
Loader for this release Built-in Vintage Story mod system
Required install it here Optional supported, not mandatory Not supported do not install here Source doesn’t say do not assume

This release supports both sides, but the source does not require it on the server.

What else does Spawn In Bed 1.0.0 need?

1.0.0. Change the file and its required mods may change too.

No extra mods listed for this file

This file does not list any required mods. Do not add a library just because a different file uses it.

This file does not list any required or optional mods.

Before you install it

Add Spawn In Bed without breaking your instance.

Built for Spawn In Bed 1.0.0. Pick another file and the loader, install side or required mods may change.

  1. 01

    Stick to this file

    Use 1.0.0. It targets 1.22.0, 1.22.1, 1.22.2; another release may have different loader, side or dependency requirements.

  2. 02

    Bring the mods it needs

    This file does not list any required mods. Do not add a library just because a different file uses it.

  3. 03

    Put it on the correct side

    It must be installed on the client. Installation on the dedicated server is optional.

  4. 04

    Pick the file you checked

    Use the “Get this file” button beside 1.0.0. It opens that exact file at the source.

About this project

What does Spawn In Bed add?

Sleeping in a bed sets your spawn point to it. Just that. Works with modded beds!
Made because the usual mods for this aren't working on the latest version, so I took it upon myself to make my own implementation.

 

Source Code (Sorry, too lazy to put this on github lol)
using System;
using System.Collections.Generic;
using Vintagestory.API.Common;
using Vintagestory.API.Common.Entities;
using Vintagestory.API.Datastructures;
using Vintagestory.API.Server;

namespace YangBed
{
	public sealed class YangBedModSystem : ModSystem
	{
		private ICoreServerAPI sapi;
		private readonly Dictionary<string, Action> mountListenersByPlayerUid = new Dictionary<string, Action>();

		public override bool ShouldLoad(EnumAppSide forSide) { return forSide == EnumAppSide.Server; }

		public override void StartServerSide(ICoreServerAPI api)
		{
			sapi = api;

			sapi.Event.PlayerNowPlaying += OnPlayerNowPlaying;
			sapi.Event.PlayerDisconnect += OnPlayerDisconnect;
		}

		private void OnPlayerNowPlaying(IServerPlayer player)
		{
			if (player == null || player.Entity == null) return;

			UnregisterPlayer(player);
			string playerUid = player.PlayerUID;

			// Beds set tiredness.isSleeping via an enqueued task during mount, one-shot callback. No need to check anything each tick or something stupid like that.
			Action listener = () =>  { sapi.Event.RegisterCallback(_ => TrySetSpawnFromSleep(playerUid), 50); };

			mountListenersByPlayerUid[playerUid] = listener;
			player.Entity.WatchedAttributes.RegisterModifiedListener("mountedOn", listener);
		}

		private void OnPlayerDisconnect(IServerPlayer player) { if (player == null) return; UnregisterPlayer(player); }

		private void UnregisterPlayer(IServerPlayer player)
		{
			if (!mountListenersByPlayerUid.TryGetValue(player.PlayerUID, out Action listener)) return;

			player.Entity?.WatchedAttributes.UnregisterListener(listener);
			mountListenersByPlayerUid.Remove(player.PlayerUID);
		}

		private void TrySetSpawnFromSleep(string playerUid)
		{
			IServerPlayer player = sapi.World.PlayerByUid(playerUid) as IServerPlayer;
			if (player == null || player.Entity == null) return;

			EntityPlayer entity = player.Entity;
			if (entity.MountedOn == null || !IsSleeping(entity)) return;

			EntityPos seatPos = entity.MountedOn.SeatPosition ?? entity.Pos;
			player.SetSpawnPosition(new PlayerSpawnPos
			{
				x = (int)Math.Floor(seatPos.X),
				y = (int)Math.Ceiling(seatPos.InternalY),
				z = (int)Math.Floor(seatPos.Z),
				yaw = seatPos.Yaw,
				pitch = 0f,
				roll = 0f,
				RemainingUses = -1
			});

			player.SendIngameError("yangbed-confirm", "Re-spawn Point Set...");
		}

		private static bool IsSleeping(EntityPlayer entity)
		{
			ITreeAttribute tiredness = entity.WatchedAttributes.GetTreeAttribute("tiredness");
			return tiredness != null && tiredness.GetInt("isSleeping") > 0;
		}

		public override void Dispose()
		{
			if (sapi != null)
			{
				sapi.Event.PlayerNowPlaying -= OnPlayerNowPlaying;
				sapi.Event.PlayerDisconnect -= OnPlayerDisconnect;
				foreach (IPlayer player in sapi.World.AllOnlinePlayers) { if (player is IServerPlayer serverPlayer) { UnregisterPlayer(serverPlayer); } }
			}

			mountListenersByPlayerUid.Clear();
			base.Dispose();
		}
	}
}

 

Project description from Vintage Story Mod DB.

Pick your setup

Spawn In Bed releases for each Vintage Story version.

Choose the version and loader you play, then open the matching release.

3 available setups

Check the dependencies, then try the file in a copied instance before changing a world you care about.

Recent files

Spawn In Bed versions and loaders

1 of 1 releases match

Looking for an older file? The official Vintage Story Mod DB project page is in Resources.