-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
76 lines (66 loc) · 2.27 KB
/
ModEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
namespace TrueTime;
public sealed class ModConfig
{
public Dictionary<ulong, DateTime> LogTimes { get; set; } = new();
}
internal sealed class ModEntry : Mod
{
private ModConfig? _config;
// Public methods
public override void Entry(IModHelper helper)
{
helper.Events.GameLoop.OneSecondUpdateTicked += OnUpdate;
helper.Events.GameLoop.SaveLoaded += OnLoadSave;
helper.Events.GameLoop.Saved += OnSave;
}
// Private methods
private void OnUpdate(object? sender,
OneSecondUpdateTickedEventArgs e)
{
if (!Context.IsWorldReady || Context.IsMultiplayer)
return;
// A fix to be able to play the game after midnight
Game1.timeOfDay = DateTime.Now.Hour < 6 ? 2400 : DateTime.Now.Hour * 100 + DateTime.Now.Minute;
}
private void OnLoadSave(object? sender, SaveLoadedEventArgs e)
{
Game1.gameTimeInterval = 0;
_config = Helper.ReadConfig<ModConfig>();
// checks if save has time data
if (!_config.LogTimes.TryGetValue(Game1.uniqueIDForThisGame, out var time))
return;
var timeInterval =
DateTime.Now.Subtract(time).Days;
Monitor.Log($"{timeInterval}");
// check if at least a day has passed
if (timeInterval < 1)
return;
TimeTravel(timeInterval);
Game1.addHUDMessage(new HUDMessage(
$"It's been {Game1.year} years and {Game1.dayOfMonth} days since you left the {Game1.player.farmName} farm."));
}
private void OnSave(object? sender, SavedEventArgs e)
{
_config ??= new ModConfig();
_config.LogTimes[Game1.uniqueIDForThisGame] = DateTime.Now.Date;
Helper.WriteConfig(_config);
}
private static void TimeTravel(int interval)
{
Game1.year = interval / 112;
var seasonIndex = Game1.dayOfMonth = interval % 112;
Game1.season = seasonIndex switch
{
<= 28 => Season.Spring,
<= 56 => Season.Summer,
<= 84 => Season.Fall,
<= 112 => Season.Winter,
_ => Game1.season
};
Game1.dayOfMonth -= 28 * (int)Game1.season;
Game1.setGraphicsForSeason();
}
}