forked from cantorsdust/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
86 lines (76 loc) · 3.12 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
77
78
79
80
81
82
83
84
85
86
using System;
using System.IO;
using SkullCaveSaver.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Locations;
namespace SkullCaveSaver
{
/// <summary>The entry class called by SMAPI.</summary>
internal class ModEntry : Mod
{
/*********
** Properties
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/// <summary>The mod configuration file path.</summary>
private string ConfigPath;
/// <summary>Whether the mod is warping to the saved mine level.</summary>
private bool WarpingToSavedLevel;
/*********
** Public methods
*********/
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
SaveEvents.AfterLoad += this.ReceiveAfterLoad;
GameEvents.HalfSecondTick += this.ReceiveHalfSecondTick;
}
/*********
** Private methods
*********/
/****
** Event handlers
****/
/// <summary>The method called after the player loads a saved game.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void ReceiveAfterLoad(object sender, EventArgs e)
{
// read per-save config
this.ConfigPath = Path.Combine("config", $"{Constants.SaveFolderName}.json");
this.Config = this.Helper.ReadJsonFile<ModConfig>(this.ConfigPath) ?? new ModConfig();
this.Helper.WriteJsonFile(this.ConfigPath, this.Config);
}
/// <summary>The method called roughly twice per second.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void ReceiveHalfSecondTick(object sender, EventArgs e)
{
if (Game1.currentLocation is MineShaft mine)
{
// ignore if currently warping
if (this.WarpingToSavedLevel)
{
if (mine.mineLevel >= this.Config.LastMineLevel)
this.WarpingToSavedLevel = false;
}
// warp to warp level
else if (mine.mineLevel > 120 && mine.mineLevel < this.Config.LastMineLevel && !this.WarpingToSavedLevel)
{
Game1.enterMine(this.Config.LastMineLevel);
this.WarpingToSavedLevel = true;
}
// save mine level
else if (mine.mineLevel > this.Config.LastMineLevel && (mine.mineLevel - this.Config.LastMineLevel) >= this.Config.SaveLevelEveryXFloors)
{
this.Config.LastMineLevel = mine.mineLevel - (mine.mineLevel % this.Config.SaveLevelEveryXFloors);
this.Helper.WriteJsonFile(this.ConfigPath, this.Config);
}
}
}
}
}