forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChestsAnywhereMod.cs
232 lines (206 loc) · 9.3 KB
/
ChestsAnywhereMod.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Pathoschild.Stardew.ChestsAnywhere.Framework;
using Pathoschild.Stardew.ChestsAnywhere.Menus.Overlays;
using Pathoschild.Stardew.Common;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
using StardewValley.Objects;
namespace Pathoschild.Stardew.ChestsAnywhere
{
/// <summary>The mod entry point.</summary>
public class ChestsAnywhereMod : Mod
{
/*********
** Properties
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/****
** Version check
****/
/// <summary>The current semantic version.</summary>
private ISemanticVersion CurrentVersion;
/// <summary>The newer release to notify the user about.</summary>
private ISemanticVersion NewRelease;
/// <summary>Whether the update-available message has been shown since the game started.</summary>
private bool HasSeenUpdateWarning;
/****
** State
****/
/// <summary>The selected chest.</summary>
private Chest SelectedChest;
/// <summary>The menu overlay which lets the player navigate and edit chests.</summary>
private ManageChestOverlay ManageChestOverlay;
/*********
** 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<RawModConfig>().GetParsed();
this.CurrentVersion = this.ModManifest.Version;
// hook UI
GameEvents.GameLoaded += (sender, e) => this.ReceiveGameLoaded();
GraphicsEvents.OnPostRenderHudEvent += (sender, e) => this.ReceiveHudRendered();
MenuEvents.MenuChanged += (sender, e) => this.ReceiveMenuChanged(e.PriorMenu, e.NewMenu);
MenuEvents.MenuClosed += (sender, e) => this.ReceiveMenuClosed(e.PriorMenu);
// hook input
if (this.Config.Keyboard.HasAny())
ControlEvents.KeyPressed += (sender, e) => this.ReceiveKeyPress(e.KeyPressed, this.Config.Keyboard);
if (this.Config.Controller.HasAny())
{
ControlEvents.ControllerButtonPressed += (sender, e) => this.ReceiveKeyPress(e.ButtonPressed, this.Config.Controller);
ControlEvents.ControllerTriggerPressed += (sender, e) => this.ReceiveKeyPress(e.ButtonPressed, this.Config.Controller);
}
}
/*********
** Private methods
*********/
/// <summary>The method invoked when the player loads the game.</summary>
private void ReceiveGameLoaded()
{
// validate game version
string versionError = this.ValidateGameVersion();
if (versionError != null)
{
this.Monitor.Log(versionError, LogLevel.Error);
CommonHelper.ShowErrorMessage(versionError);
}
// check for mod update
if (this.Config.CheckForUpdates)
{
try
{
Task.Factory.StartNew(() =>
{
Task.Factory.StartNew(() =>
{
ISemanticVersion latest = UpdateHelper.LogVersionCheck(this.Monitor, this.ModManifest.Version, "ChestsAnywhere").Result;
if (latest.IsNewerThan(this.CurrentVersion))
this.NewRelease = latest;
});
});
}
catch (Exception ex)
{
this.HandleError(ex, "checking for a new version");
}
}
}
/// <summary>The method invoked when the interface has finished rendering.</summary>
private void ReceiveHudRendered()
{
// render update warning
if (this.Config.CheckForUpdates && !this.HasSeenUpdateWarning && this.NewRelease != null)
{
try
{
this.HasSeenUpdateWarning = true;
CommonHelper.ShowInfoMessage($"You can update Chests Anywhere from {this.CurrentVersion} to {this.NewRelease}.");
}
catch (Exception ex)
{
this.HandleError(ex, "showing the new version available");
}
}
// show chest label
if (this.Config.ShowHoverTooltips)
{
ManagedChest cursorChest = ChestFactory.GetChestFromTile(Game1.currentCursorTile);
if (cursorChest != null)
{
Vector2 tooltipPosition = new Vector2(Game1.getMouseX(), Game1.getMouseY()) + new Vector2(Game1.tileSize / 2f);
CommonHelper.DrawHoverBox(Game1.spriteBatch, cursorChest.Name, tooltipPosition, Game1.viewport.Width - tooltipPosition.X - Game1.tileSize / 2f);
}
}
}
/// <summary>The method invoked when the active menu changes.</summary>
/// <param name="previousMenu">The previous menu (if any)</param>
/// <param name="newMenu">The new menu (if any).</param>
private void ReceiveMenuChanged(IClickableMenu previousMenu, IClickableMenu newMenu)
{
// remove overlay
if (previousMenu is ItemGrabMenu)
{
this.ManageChestOverlay?.Dispose();
this.ManageChestOverlay = null;
}
// add overlay
if (newMenu is ItemGrabMenu)
{
// get open chest
ItemGrabMenu chestMenu = (ItemGrabMenu)newMenu;
ManagedChest chest = ChestFactory.GetChestFromMenu(chestMenu);
if (chest == null)
return;
// add overlay
ManagedChest[] chests = ChestFactory.GetChestsForDisplay(selectedChest: chest.Chest).ToArray();
this.ManageChestOverlay = new ManageChestOverlay(chestMenu, chest, chests, this.Config);
this.ManageChestOverlay.OnChestSelected += selected =>
{
this.SelectedChest = selected.Chest;
Game1.activeClickableMenu = selected.OpenMenu();
};
}
}
/// <summary>The method invoked when a menu is closed.</summary>
/// <param name="closedMenu">The menu that was closed.</param>
private void ReceiveMenuClosed(IClickableMenu closedMenu)
{
this.ReceiveMenuChanged(closedMenu, null);
}
/// <summary>The method invoked when the player presses an input button.</summary>
/// <typeparam name="TKey">The input type.</typeparam>
/// <param name="key">The pressed input.</param>
/// <param name="map">The configured input mapping.</param>
private void ReceiveKeyPress<TKey>(TKey key, InputMapConfiguration<TKey> map)
{
try
{
if (!map.IsValidKey(key))
return;
// open menu
if (key.Equals(map.Toggle) && (Game1.activeClickableMenu == null || (Game1.activeClickableMenu as GameMenu)?.currentTab == 0))
this.OpenMenu();
}
catch (Exception ex)
{
this.HandleError(ex, "handling key input");
}
}
/// <summary>Open the menu UI.</summary>
private void OpenMenu()
{
// get chests
ManagedChest[] chests = ChestFactory.GetChestsForDisplay().ToArray();
ManagedChest selectedChest = chests.FirstOrDefault(p => p.Chest == this.SelectedChest) ?? chests.FirstOrDefault();
// render menu
if (selectedChest != null)
Game1.activeClickableMenu = selectedChest.OpenMenu();
else
CommonHelper.ShowInfoMessage("You don't have any chests yet. :)", duration: 1000);
}
/// <summary>Validate that the game versions match the minimum requirements, and return an appropriate error message if not.</summary>
private string ValidateGameVersion()
{
if (Constant.MinimumApiVersion.IsNewerThan(Constants.ApiVersion))
return $"The Chests Anywhere mod requires a newer version of SMAPI. Please update SMAPI from {Constants.ApiVersion} to {Constant.MinimumApiVersion}.";
return null;
}
/// <summary>Log an error and warn the user.</summary>
/// <param name="ex">The exception to handle.</param>
/// <param name="verb">The verb describing where the error occurred (e.g. "looking that up").</param>
private void HandleError(Exception ex, string verb)
{
this.Monitor.Log($"Something went wrong {verb}:\n{ex}", LogLevel.Error);
CommonHelper.ShowErrorMessage($"Huh. Something went wrong {verb}. The error log has the technical details.");
}
}
}