Repository navigation
Expand file tree
/
Copy pathModEntry.cs
More file actions
239 lines (207 loc) · 10.3 KB
/
Copy pathModEntry.cs
File metadata and controls
239 lines (207 loc) · 10.3 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
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
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Pathoschild.Stardew.ChestsAnywhere.Framework;
using Pathoschild.Stardew.ChestsAnywhere.Framework.Containers;
using Pathoschild.Stardew.ChestsAnywhere.Menus.Overlays;
using Pathoschild.Stardew.Common;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
namespace Pathoschild.Stardew.ChestsAnywhere
{
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Properties
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/// <summary>The internal mod settings.</summary>
private ModData Data;
/// <summary>Encapsulates logic for finding chests.</summary>
private ChestFactory ChestFactory;
/****
** State
****/
/// <summary>The selected in-game inventory.</summary>
private List<Item> SelectedInventory;
/// <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)
{
// initialise
this.Config = helper.ReadConfig<ModConfig>();
this.Data = helper.ReadJsonFile<ModData>("data.json") ?? new ModData();
this.ChestFactory = new ChestFactory(helper.Translation, helper.Reflection, this.Config.EnableShippingBin);
// hook UI
GraphicsEvents.OnPostRenderHudEvent += this.GraphicsEvents_OnPostRenderHudEvent;
MenuEvents.MenuChanged += this.MenuEvents_MenuChanged;
MenuEvents.MenuClosed += this.MenuEvents_MenuClosed;
// hook input
InputEvents.ButtonPressed += this.InputEvents_ButtonPressed;
// hook game events
SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
// validate translations
if (!helper.Translation.GetTranslations().Any())
this.Monitor.Log("The translation files in this mod's i18n folder seem to be missing. The mod will still work, but you'll see 'missing translation' messages. Try reinstalling the mod to fix this.", LogLevel.Warn);
}
/*********
** Private methods
*********/
/// <summary>The method invoked after the player loads a saved game.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void SaveEvents_AfterLoad(object sender, EventArgs e)
{
// validate game version
string versionError = this.ValidateGameVersion();
if (versionError != null)
{
this.Monitor.Log(versionError, LogLevel.Error);
CommonHelper.ShowErrorMessage(versionError);
}
}
/// <summary>The method invoked when the interface has finished rendering.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void GraphicsEvents_OnPostRenderHudEvent(object sender, EventArgs e)
{
// show chest label
if (this.Config.ShowHoverTooltips)
{
ManagedChest cursorChest = this.ChestFactory.GetChestFromTile(Game1.currentCursorTile);
if (cursorChest != null && !cursorChest.HasDefaultName())
{
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="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void MenuEvents_MenuChanged(object sender, EventArgsClickableMenuChanged e)
{
// remove overlay
if (e.PriorMenu is ItemGrabMenu)
{
this.ManageChestOverlay?.Dispose();
this.ManageChestOverlay = null;
}
// add overlay
if (e.NewMenu is ItemGrabMenu chestMenu)
{
// get open chest
ManagedChest chest = this.ChestFactory.GetChestFromMenu(chestMenu);
if (chest == null)
return;
// reopen shipping box in standard chest UI if needed
// This is called in two cases:
// - When the player opens the shipping bin directly, it opens the shipping bin view instead of the full chest view.
// - When the player changes the items in the chest view, it reopens itself but loses the constructor args (e.g. highlight function).
if (this.Config.EnableShippingBin && chest.Container is ShippingBinContainer && (!chestMenu.showReceivingMenu || !(chestMenu.inventory.highlightMethod?.Target is ShippingBinContainer)))
{
chestMenu = chest.OpenMenu();
Game1.activeClickableMenu = chestMenu;
}
// add overlay
RangeHandler range = this.GetCurrentRange();
ManagedChest[] chests = this.ChestFactory.GetChests(range, excludeHidden: true, alwaysIncludeContainer: chest.Container).ToArray();
this.ManageChestOverlay = new ManageChestOverlay(chestMenu, chest, chests, this.Config, this.Helper.Translation);
this.ManageChestOverlay.OnChestSelected += selected =>
{
this.SelectedInventory = selected.Container.Inventory;
Game1.activeClickableMenu = selected.OpenMenu();
};
}
}
/// <summary>The method invoked when a menu is closed.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void MenuEvents_MenuClosed(object sender, EventArgsClickableMenuClosed e)
{
this.MenuEvents_MenuChanged(sender, new EventArgsClickableMenuChanged(e.PriorMenu, null));
}
/// <summary>The method invoked when the player presses a button.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void InputEvents_ButtonPressed(object sender, EventArgsInput e)
{
try
{
var controls = this.Config.Controls;
// open menu
if (controls.Toggle.Contains(e.Button) && (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()
{
if (this.Config.Range == ChestRange.None)
return;
// handle disabled location
if (this.IsDisabledLocation(Game1.currentLocation))
{
CommonHelper.ShowInfoMessage("Remote chest access is disabled here. :)", duration: 1000);
return;
}
// get chests
RangeHandler range = this.GetCurrentRange();
ManagedChest[] chests = this.ChestFactory.GetChests(range, excludeHidden: true).ToArray();
ManagedChest selectedChest = chests.FirstOrDefault(p => p.Container.IsSameAs(this.SelectedInventory)) ?? chests.FirstOrDefault();
// render menu
if (selectedChest != null)
Game1.activeClickableMenu = selectedChest.OpenMenu();
else
{
CommonHelper.ShowInfoMessage(
"You don't have any chests " + (this.Config.Range == ChestRange.Unlimited ? "yet" : "in range") + ". :)",
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.");
}
/// <summary>Get whether remote access is disabled from the given location.</summary>
/// <param name="location">The game location.</param>
private bool IsDisabledLocation(GameLocation location)
{
return this.Config.DisabledInLocations != null && this.Config.DisabledInLocations.Contains(location.Name, StringComparer.InvariantCultureIgnoreCase);
}
/// <summary>Get the range for the current context.</summary>
private RangeHandler GetCurrentRange()
{
ChestRange range = this.IsDisabledLocation(Game1.currentLocation)
? ChestRange.None
: this.Config.Range;
return new RangeHandler(this.Data.WorldAreas, range, Game1.currentLocation);
}
}
}