Begin to add the player sleeping percentage system

This commit is contained in:
zontreck 2025-03-10 17:17:25 -07:00
parent c609dde0ee
commit 9a8b9d1545
2 changed files with 52 additions and 2 deletions

View file

@ -38,6 +38,8 @@ namespace AriasServerUtils
GlobalConstants.characterInvClassName GlobalConstants.characterInvClassName
}; };
List<EntityAgent> SleepingPlayers { get; set; }
/// <summary> /// <summary>
/// Method to register all mod blocks /// Method to register all mod blocks
/// </summary> /// </summary>
@ -76,6 +78,7 @@ namespace AriasServerUtils
api.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, OnShutdown); api.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, OnShutdown);
api.Event.Timer(OnCheckModDirty, 20); api.Event.Timer(OnCheckModDirty, 20);
api.Event.Timer(OnCheckPlayerCooldowns, 1); api.Event.Timer(OnCheckPlayerCooldowns, 1);
api.Event.Timer(OnCheckSleepingPlayers, 5);
api.Event.Timer(RTPFactory.HandleRTPChecking, 1); api.Event.Timer(RTPFactory.HandleRTPChecking, 1);
api.Event.PlayerDeath += OnPlayerDeath; api.Event.PlayerDeath += OnPlayerDeath;
api.Event.PlayerJoin += OnPlayerJoin; api.Event.PlayerJoin += OnPlayerJoin;
@ -235,6 +238,53 @@ namespace AriasServerUtils
api.ChatCommands.Create("listcooldowns").RequiresPrivilege(Privilege.chat).WithDescription("Lists the cooldown settings on the server, as well as your own active cooldowns if applicable.").HandleWith(Events.HandleListCooldowns); api.ChatCommands.Create("listcooldowns").RequiresPrivilege(Privilege.chat).WithDescription("Lists the cooldown settings on the server, as well as your own active cooldowns if applicable.").HandleWith(Events.HandleListCooldowns);
} }
private void OnCheckSleepingPlayers()
{
if (API.Side == EnumAppSide.Client) return; // This must only ever be called on the server!
if (config.PlayerSleepingPercentage == 100) return; // Normal behavior
// Iterate over all players, get their entity, check if mounted on a bed.
// If mounted on a bed, check tiredness
int TotalOnline = API.World.AllOnlinePlayers.Length;
int TotalInBed = 0;
bool isAlreadySleeping = false;
List<BlockEntityBed> BEBs = new();
foreach (var player in API.World.AllOnlinePlayers)
{
EntityAgent ePlay = player.Entity;
if (ePlay.MountedOn is BlockEntityBed beb)
{
BEBs.Add(beb);
TotalInBed++;
}
if (ePlay.MountedOn == null)
{
if (SleepingPlayers.Contains(ePlay))
{
EntityBehaviorTiredness ebt = ePlay.GetBehavior("tiredness") as EntityBehaviorTiredness;
if (ebt != null)
ebt.IsSleeping = false;
}
}
EntityBehaviorTiredness EBT = ePlay.GetBehavior("tiredness") as EntityBehaviorTiredness;
if (EBT == null) continue;
if (EBT.IsSleeping) isAlreadySleeping = true;
}
if (isAlreadySleeping) return; // Abort
SleepingPlayers.Clear();
int Percentage = TotalInBed * 100 / TotalOnline;
if (Percentage >= config.PlayerSleepingPercentage)
{
// Call the API to make sleep happen
}
}
private void OnCheckPlayerCooldowns() private void OnCheckPlayerCooldowns()
{ {
foreach (var cdEntry in ServerUtilities.mPlayerData) foreach (var cdEntry in ServerUtilities.mPlayerData)

View file

@ -3,8 +3,8 @@
"modid": "ariasserverutils", "modid": "ariasserverutils",
"name": "Aria's Server Utilities", "name": "Aria's Server Utilities",
"authors": ["zontreck"], "authors": ["zontreck"],
"description": "A collection of server utilities\n\nBuild Date: 03-10-2025 @ 12:04 PM MST", "description": "A collection of server utilities\n\nBuild Date: 03-10-2025 @ 4:37 PM MST",
"version": "1.0.6", "version": "1.0.7-dev.1",
"dependencies": { "dependencies": {
"game": "" "game": ""
} }