From d2b92f95c51e51021d4a2994de9d436b6543d6db Mon Sep 17 00:00:00 2001 From: zontreck Date: Tue, 11 Mar 2025 00:37:46 -0700 Subject: [PATCH] Change method of time acceleration --- AriasServerUtils/ModSystems/ASUClient.cs | 34 +++++ .../ASUServer.cs} | 116 +++++++++++++++++- AriasServerUtils/modinfo.json | 4 +- .../network/ASUTimeAccelPacket.cs | 7 ++ 4 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 AriasServerUtils/ModSystems/ASUClient.cs rename AriasServerUtils/{ASUModSystem.cs => ModSystems/ASUServer.cs} (84%) create mode 100644 AriasServerUtils/network/ASUTimeAccelPacket.cs diff --git a/AriasServerUtils/ModSystems/ASUClient.cs b/AriasServerUtils/ModSystems/ASUClient.cs new file mode 100644 index 0000000..2f678b3 --- /dev/null +++ b/AriasServerUtils/ModSystems/ASUClient.cs @@ -0,0 +1,34 @@ +using System; +using Vintagestory.API.Client; +using Vintagestory.API.Common; + +public class ASUModClient : ModSystem +{ + public static ICoreClientAPI CAPI; + bool accel = false; + + public override bool ShouldLoad(EnumAppSide forSide) + { + return forSide == EnumAppSide.Client; + } + + public override void StartClientSide(ICoreClientAPI api) + { + CAPI = api; + api.Network.RegisterChannel("asutimeaccel") + .RegisterMessageType() + .SetMessageHandler(onReceiveTimeAccel); + } + + private void onReceiveTimeAccel(ASUTimeAcceleration packet) + { + // Time acceleration handler + accel = packet.Sleeping; + + if (accel) + { + CAPI.World.Calendar.SetTimeSpeedModifier("asu_psp", 500); + } + else CAPI.World.Calendar.RemoveTimeSpeedModifier("asu_psp"); + } +} \ No newline at end of file diff --git a/AriasServerUtils/ASUModSystem.cs b/AriasServerUtils/ModSystems/ASUServer.cs similarity index 84% rename from AriasServerUtils/ASUModSystem.cs rename to AriasServerUtils/ModSystems/ASUServer.cs index 215f848..c5d6d75 100644 --- a/AriasServerUtils/ASUModSystem.cs +++ b/AriasServerUtils/ModSystems/ASUServer.cs @@ -7,6 +7,7 @@ using Vintagestory.API.Common; using Vintagestory.API.Common.CommandAbbr; using Vintagestory.API.Common.Entities; using Vintagestory.API.Config; +using Vintagestory.API.Datastructures; using Vintagestory.API.MathTools; using Vintagestory.API.Server; using Vintagestory.API.Util; @@ -42,6 +43,7 @@ namespace AriasServerUtils float OriginalSpeed { get; set; } = 0.0f; public double Hours { get; private set; } = 0.0; bool Sleeping { get; set; } = false; + public IServerNetworkChannel ServerNetworkChannel { get; private set; } /// /// Method to register all mod blocks @@ -61,6 +63,14 @@ namespace AriasServerUtils { } + + public override bool ShouldLoad(EnumAppSide side) + { + return side == EnumAppSide.Server; + } + + + // Called on server and client public override void Start(ICoreAPI api) { @@ -86,6 +96,10 @@ namespace AriasServerUtils //api.Event.PlayerLeave += OnPlayerDC; + ServerNetworkChannel = api.Network.RegisterChannel("asutimeaccel") + .RegisterMessageType(); + + api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn); api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn); @@ -223,6 +237,20 @@ namespace AriasServerUtils .RequiresPrivilege(Privilege.chat) .HandleWith(Events.HandleASU) .WithDescription("Lists all Aria's Server Utils commands") + .EndSubCommand() + .BeginSubCommand("test") + .RequiresPlayer() + .RequiresPrivilege(Privilege.controlserver) + .BeginSubCommand("sleep") + .RequiresPlayer() + .RequiresPrivilege(Privilege.controlserver) + .HandleWith(TestSleep) + .EndSubCommand() + .BeginSubCommand("calendarspeed") + .RequiresPlayer() + .RequiresPrivilege(Privilege.controlserver) + .HandleWith(TestCalendarSpeed) + .EndSubCommand() .EndSubCommand(); api.ChatCommands.Create("setwarp").RequiresPlayer().RequiresPrivilege(Privilege.chat).WithDescription("Creates a new server warp").WithArgs(parsers.OptionalWord("name")).HandleWith(Events.HandleWarpUpdate); @@ -237,6 +265,78 @@ 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); } + private TextCommandResult TestSleep(TextCommandCallingArgs args) + { + // Initiate the sleep process + // Basically run all the same commands as we would on a player in bed + OriginalSpeed = API.World.Calendar.CalendarSpeedMul; + if (args.Caller.Player is IServerPlayer isp) + { + Hours = API.World.Calendar.TotalHours; + SleepingPlayers.Add(isp.Entity); + EntityAgent Agent = isp.Entity; + EntityBehaviorTiredness ebt = Agent.GetBehavior("tiredness") as EntityBehaviorTiredness; + ebt.IsSleeping = true; + ebt.Tiredness = 100; + Sleeping = true; + + + ServerNetworkChannel.BroadcastPacket(new ASUTimeAcceleration + { + Sleeping = true + }); + + API.World.Calendar.SetTimeSpeedModifier("asu_psp", 500); + } + + return TextCommandResult.Success($"Test initiated, original calendar multiplier: '{OriginalSpeed}'"); + } + + private TextCommandResult TestCalendarSpeed(TextCommandCallingArgs args) + { + if (args.Caller.Player is IServerPlayer isp) + { + EntityAgent agent = isp.Entity; + EntityBehaviorTiredness ebt = agent.GetBehavior("tiredness") as EntityBehaviorTiredness; + + SendMessageTo(isp, $"- Current calendar speed: {API.World.Calendar.CalendarSpeedMul}"); + SendMessageTo(isp, $"- Total Hours: {API.World.Calendar.TotalHours}"); + SendMessageTo(isp, $"- Tiredness: {ebt.Tiredness}"); + if (OriginalSpeed == 0) + { + // Apply multiplier + OriginalSpeed = 0.5f; + ebt.IsSleeping = true; + + ServerNetworkChannel.BroadcastPacket(new ASUTimeAcceleration + { + Sleeping = true + }); + + API.World.Calendar.SetTimeSpeedModifier("asu_psp", 500); + + SendMessageTo(isp, "Applied calendar speed multiplier"); + } + else + { + // Unapply multiplier + OriginalSpeed = 0; + ebt.IsSleeping = false; + + ServerNetworkChannel.BroadcastPacket(new ASUTimeAcceleration + { + Sleeping = false + }); + + API.World.Calendar.RemoveTimeSpeedModifier("asu_psp"); + + SendMessageTo(isp, "Restored default calendar speed"); + } + } + + return TextCommandResult.Success(); + } + private void OnCheckSleepingPlayers() { if (API.Side == EnumAppSide.Client) return; // This must only ever be called on the server! @@ -252,7 +352,14 @@ namespace AriasServerUtils } SleepingPlayers.Clear(); - API.World.Calendar.CalendarSpeedMul = OriginalSpeed; + + + ServerNetworkChannel.BroadcastPacket(new ASUTimeAcceleration + { + Sleeping = false + }); + + API.World.Calendar.RemoveTimeSpeedModifier("asu_psp"); } return; } @@ -311,7 +418,12 @@ namespace AriasServerUtils Hours = API.World.Calendar.TotalHours; Sleeping = true; - API.World.Calendar.CalendarSpeedMul = 4; + ServerNetworkChannel.BroadcastPacket(new ASUTimeAcceleration + { + Sleeping = true + }); + + API.World.Calendar.SetTimeSpeedModifier("asu_psp", 500); } } } diff --git a/AriasServerUtils/modinfo.json b/AriasServerUtils/modinfo.json index 6d14c13..3aec4e6 100644 --- a/AriasServerUtils/modinfo.json +++ b/AriasServerUtils/modinfo.json @@ -3,8 +3,8 @@ "modid": "ariasserverutils", "name": "Aria's Server Utilities", "authors": ["zontreck"], - "description": "A collection of server utilities\n\nBuild Date: 03-10-2025 @ 6:56 PM MST", - "version": "1.0.7-dev.2", + "description": "A collection of server utilities\n\nBuild Date: 03-11-2025 @ 00:37 AM MST", + "version": "1.0.7-dev.3", "dependencies": { "game": "" } diff --git a/AriasServerUtils/network/ASUTimeAccelPacket.cs b/AriasServerUtils/network/ASUTimeAccelPacket.cs new file mode 100644 index 0000000..bb66753 --- /dev/null +++ b/AriasServerUtils/network/ASUTimeAccelPacket.cs @@ -0,0 +1,7 @@ +using System; + +[Serializable] +public class ASUTimeAcceleration +{ + public bool Sleeping = false; +} \ No newline at end of file