forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModEntry.cs
More file actions
170 lines (151 loc) · 7.63 KB
/
ModEntry.cs
File metadata and controls
170 lines (151 loc) · 7.63 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Linq;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Integrations.BetterSprinklers;
using Pathoschild.Stardew.Common.Integrations.Cobalt;
using Pathoschild.Stardew.Common.Integrations.PelicanFiber;
using Pathoschild.Stardew.Common.Integrations.SimpleSprinkler;
using Pathoschild.Stardew.DataMaps.DataMaps;
using Pathoschild.Stardew.DataMaps.DataMaps.Coverage;
using Pathoschild.Stardew.DataMaps.DataMaps.Crops;
using Pathoschild.Stardew.DataMaps.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
namespace Pathoschild.Stardew.DataMaps
{
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Properties
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/// <summary>The current overlay being displayed, if any.</summary>
private DataMapOverlay CurrentOverlay;
/// <summary>The available data maps.</summary>
private IDataMap[] Maps;
/// <summary>Handles access to the Pelican Fiber mod.</summary>
private PelicanFiberIntegration PelicanFiber;
/*********
** Public methods
*********/
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides methods for interacting with the mod directory, such as read/writing a config file or custom JSON files.</param>
public override void Entry(IModHelper helper)
{
// read config
this.Config = helper.ReadConfig<ModConfig>();
// hook up events
SaveEvents.AfterReturnToTitle += this.SaveEvents_AfterReturnToTitle;
GameEvents.FirstUpdateTick += this.GameEvents_FirstUpdateTick;
GameEvents.SecondUpdateTick += this.GameEvents_SecondUpdateTick;
InputEvents.ButtonPressed += this.InputEvents_ButtonPressed;
}
/*********
** Private methods
*********/
/// <summary>The method invoked on the first game update tick.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void GameEvents_FirstUpdateTick(object sender, EventArgs e)
{
IModHelper helper = this.Helper;
this.PelicanFiber = new PelicanFiberIntegration(helper.ModRegistry, helper.Reflection, this.Monitor);
var betterSprinklers = new BetterSprinklersIntegration(helper.ModRegistry, this.Monitor);
var cobalt = new CobaltIntegration(helper.ModRegistry, this.Monitor);
var simpleSprinklers = new SimpleSprinklerIntegration(helper.ModRegistry, this.Monitor);
this.Maps = this.GetDataMaps(this.Config, this.Helper.Translation, this.PelicanFiber, betterSprinklers, cobalt, simpleSprinklers).ToArray();
}
/// <summary>Get the enabled data maps.</summary>
/// <param name="config">The mod configuration.</param>
/// <param name="translation">Provides translations for the mod.</param>
/// <param name="pelicanFiber">Handles access to the Pelican Fiber mod.</param>
/// <param name="betterSprinklers">Handles access to the Better Sprinklers mod.</param>
/// <param name="cobalt">Handles access to the Cobalt mod.</param>
/// <param name="simpleSprinklers">Handles access to the Simple Sprinklers mod.</param>
private IEnumerable<IDataMap> GetDataMaps(ModConfig config, ITranslationHelper translation, PelicanFiberIntegration pelicanFiber, BetterSprinklersIntegration betterSprinklers, CobaltIntegration cobalt, SimpleSprinklerIntegration simpleSprinklers)
{
if (config.EnableMaps.Accessibility)
yield return new AccessibilityMap(translation);
if (config.EnableMaps.CoverageForBeeHouses)
yield return new BeeHouseMap(translation);
if (config.EnableMaps.CoverageForScarecrows)
yield return new ScarecrowMap(translation);
if (config.EnableMaps.CoverageForSprinklers)
yield return new SprinklerMap(translation, betterSprinklers, cobalt, simpleSprinklers);
if (config.EnableMaps.CoverageForJunimoHuts)
yield return new JunimoHutMap(translation, this.PelicanFiber);
if (config.EnableMaps.CropWater)
yield return new CropWaterMap(translation);
if (config.EnableMaps.CropFertilizer)
yield return new CropFertilizerMap(translation);
}
/// <summary>The method invoked when the player returns to the title screen.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void SaveEvents_AfterReturnToTitle(object sender, EventArgs e)
{
this.CurrentOverlay?.Dispose();
this.CurrentOverlay = null;
}
/// <summary>The method invoked when the player presses an input button.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void InputEvents_ButtonPressed(object sender, EventArgsInput e)
{
// perform bound action
this.Monitor.InterceptErrors("handling your input", $"handling input '{e.Button}'", () =>
{
// check context
if (!this.CanOverlayNow())
return;
bool overlayVisible = this.CurrentOverlay != null;
var controls = this.Config.Controls;
// toggle overlay
if (controls.ToggleMap.Contains(e.Button))
{
if (overlayVisible)
{
this.CurrentOverlay.Dispose();
this.CurrentOverlay = null;
}
else
this.CurrentOverlay = new DataMapOverlay(this.Maps, this.CanOverlayNow, this.Config.CombineOverlappingBorders);
e.SuppressButton();
}
// cycle data maps
else if (overlayVisible && controls.NextMap.Contains(e.Button))
{
this.CurrentOverlay.NextMap();
e.SuppressButton();
}
else if (overlayVisible && controls.PrevMap.Contains(e.Button))
{
this.CurrentOverlay.PrevMap();
e.SuppressButton();
}
});
}
/// <summary>Receive an update tick.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void GameEvents_SecondUpdateTick(object sender, EventArgs e)
{
this.CurrentOverlay?.Update();
}
/// <summary>Whether overlays are allowed in the current game context.</summary>
private bool CanOverlayNow()
{
if (!Context.IsWorldReady)
return false;
return
Context.IsPlayerFree // player is free to roam
|| (Game1.activeClickableMenu is CarpenterMenu && this.Helper.Reflection.GetField<bool>(Game1.activeClickableMenu, "onFarm").GetValue()) // on Robin's or Wizard's build screen
|| (this.PelicanFiber.IsLoaded && this.PelicanFiber.IsBuildMenuOpen() && this.Helper.Reflection.GetField<bool>(Game1.activeClickableMenu, "OnFarm").GetValue()); // on Pelican Fiber's build screen
}
}
}