Change method of time acceleration

This commit is contained in:
zontreck 2025-03-11 00:37:46 -07:00
parent 720dafea87
commit d2b92f95c5
4 changed files with 157 additions and 4 deletions

View file

@ -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<ASUTimeAcceleration>()
.SetMessageHandler<ASUTimeAcceleration>(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");
}
}

View file

@ -7,6 +7,7 @@ using Vintagestory.API.Common;
using Vintagestory.API.Common.CommandAbbr; using Vintagestory.API.Common.CommandAbbr;
using Vintagestory.API.Common.Entities; using Vintagestory.API.Common.Entities;
using Vintagestory.API.Config; using Vintagestory.API.Config;
using Vintagestory.API.Datastructures;
using Vintagestory.API.MathTools; using Vintagestory.API.MathTools;
using Vintagestory.API.Server; using Vintagestory.API.Server;
using Vintagestory.API.Util; using Vintagestory.API.Util;
@ -42,6 +43,7 @@ namespace AriasServerUtils
float OriginalSpeed { get; set; } = 0.0f; float OriginalSpeed { get; set; } = 0.0f;
public double Hours { get; private set; } = 0.0; public double Hours { get; private set; } = 0.0;
bool Sleeping { get; set; } = false; bool Sleeping { get; set; } = false;
public IServerNetworkChannel ServerNetworkChannel { get; private set; }
/// <summary> /// <summary>
/// Method to register all mod blocks /// 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 // Called on server and client
public override void Start(ICoreAPI api) public override void Start(ICoreAPI api)
{ {
@ -86,6 +96,10 @@ namespace AriasServerUtils
//api.Event.PlayerLeave += OnPlayerDC; //api.Event.PlayerLeave += OnPlayerDC;
ServerNetworkChannel = api.Network.RegisterChannel("asutimeaccel")
.RegisterMessageType<ASUTimeAcceleration>();
api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn); api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn);
api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn); api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn);
@ -223,6 +237,20 @@ namespace AriasServerUtils
.RequiresPrivilege(Privilege.chat) .RequiresPrivilege(Privilege.chat)
.HandleWith(Events.HandleASU) .HandleWith(Events.HandleASU)
.WithDescription("Lists all Aria's Server Utils commands") .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(); .EndSubCommand();
api.ChatCommands.Create("setwarp").RequiresPlayer().RequiresPrivilege(Privilege.chat).WithDescription("Creates a new server warp").WithArgs(parsers.OptionalWord("name")).HandleWith(Events.HandleWarpUpdate); 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); 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() private void OnCheckSleepingPlayers()
{ {
if (API.Side == EnumAppSide.Client) return; // This must only ever be called on the server! if (API.Side == EnumAppSide.Client) return; // This must only ever be called on the server!
@ -252,7 +352,14 @@ namespace AriasServerUtils
} }
SleepingPlayers.Clear(); SleepingPlayers.Clear();
API.World.Calendar.CalendarSpeedMul = OriginalSpeed;
ServerNetworkChannel.BroadcastPacket(new ASUTimeAcceleration
{
Sleeping = false
});
API.World.Calendar.RemoveTimeSpeedModifier("asu_psp");
} }
return; return;
} }
@ -311,7 +418,12 @@ namespace AriasServerUtils
Hours = API.World.Calendar.TotalHours; Hours = API.World.Calendar.TotalHours;
Sleeping = true; Sleeping = true;
API.World.Calendar.CalendarSpeedMul = 4; ServerNetworkChannel.BroadcastPacket(new ASUTimeAcceleration
{
Sleeping = true
});
API.World.Calendar.SetTimeSpeedModifier("asu_psp", 500);
} }
} }
} }

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 @ 6:56 PM MST", "description": "A collection of server utilities\n\nBuild Date: 03-11-2025 @ 00:37 AM MST",
"version": "1.0.7-dev.2", "version": "1.0.7-dev.3",
"dependencies": { "dependencies": {
"game": "" "game": ""
} }

View file

@ -0,0 +1,7 @@
using System;
[Serializable]
public class ASUTimeAcceleration
{
public bool Sleeping = false;
}