-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathChestFactory.cs
507 lines (451 loc) · 20.8 KB
/
ChestFactory.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.Xna.Framework;
using Pathoschild.Stardew.ChestsAnywhere.Framework;
using Pathoschild.Stardew.ChestsAnywhere.Framework.Containers;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Utilities;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Locations;
using StardewValley.Menus;
using StardewValley.Objects;
using SObject = StardewValley.Object;
namespace Pathoschild.Stardew.ChestsAnywhere;
/// <summary>Encapsulates logic for finding chests.</summary>
internal class ChestFactory
{
/*********
** Fields
*********/
/// <summary>The qualified item ID for auto-grabbers.</summary>
private readonly string AutoGrabberID = "(BC)165";
/// <summary>Provides multiplayer utilities.</summary>
private readonly IMultiplayerHelper Multiplayer;
/// <summary>The mod settings to apply.</summary>
private readonly Func<ModConfig> Config;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="multiplayer">Provides multiplayer utilities.</param>
/// <param name="config">The mod settings to apply.</param>
public ChestFactory(IMultiplayerHelper multiplayer, Func<ModConfig> config)
{
this.Multiplayer = multiplayer;
this.Config = config;
}
/// <summary>Get all player chests.</summary>
/// <param name="range">Determines whether given locations are in range of the player for remote chest access.</param>
/// <param name="excludeHidden">Whether to exclude chests marked as hidden.</param>
/// <param name="alwaysInclude">A chest to include even if it would normally be hidden.</param>
public IEnumerable<ManagedChest> GetChests(RangeHandler range, bool excludeHidden = false, ManagedChest? alwaysInclude = null)
{
ModConfig config = this.Config();
IEnumerable<ManagedChest> Search()
{
// get location info
var locations =
(
from GameLocation location in this.GetAccessibleLocations()
select new
{
Location = location,
Category = this.GetCategory(location)
}
)
.ToArray();
IDictionary<string, int> defaultCategories = locations
.GroupBy(p => p.Category)
.Where(p => p.Count() > 1)
.ToDictionary(p => p.Key, _ => 0);
// find chests
foreach (var entry in locations)
{
IDictionary<string, int> nameCounts = new Dictionary<string, int>();
// get info
GameLocation location = entry.Location;
string category = defaultCategories.ContainsKey(entry.Category)
? I18n.DefaultCategory_Duplicate(locationName: entry.Category, number: ++defaultCategories[entry.Category])
: entry.Category;
// chests in location
foreach (KeyValuePair<Vector2, SObject> pair in location.Objects.Pairs)
{
Vector2 tile = pair.Key;
SObject obj = pair.Value;
// chests
if (obj is Chest chest && chest.playerChest.Value)
{
yield return new ManagedChest(
container: new ChestContainer(chest, context: chest, showColorPicker: this.CanShowColorPicker(chest, location)),
location: location,
tile: tile,
mapEntity: chest,
defaultDisplayName: this.GetDisambiguatedDefaultName(chest.DisplayName, nameCounts),
defaultCategory: category
);
}
// auto-grabbers
else if (obj.QualifiedItemId == this.AutoGrabberID && obj.heldObject.Value is Chest grabberChest)
{
yield return new ManagedChest(
container: new AutoGrabberContainer(obj, grabberChest, context: obj),
location: location,
tile: tile,
mapEntity: obj,
defaultDisplayName: this.GetDisambiguatedDefaultName(obj.DisplayName, nameCounts),
defaultCategory: category
);
}
// sprinkler attachments
else if (config.EnableSprinklerAttachments && obj.IsSprinkler() && obj.heldObject.Value is { } attachment)
{
Chest? attachmentChest = (attachment as Chest) ?? (attachment.heldObject.Value as Chest);
if (attachmentChest is not null)
{
string displayName = attachment.DisplayName;
if (displayName == ItemRegistry.GetDataOrErrorItem("(O)130").DisplayName) // if the display name is just "Chest", show the sprinkler name
displayName = obj.DisplayName;
yield return new ManagedChest(
container: new ChestContainer(attachmentChest, context: attachmentChest, showColorPicker: false),
location: location,
tile: tile,
mapEntity: obj,
defaultDisplayName: this.GetDisambiguatedDefaultName(displayName, nameCounts),
defaultCategory: category
);
}
}
}
// farmhouse fridge
{
Chest? fridge = this.GetStaticFridge(location);
if (fridge != null)
{
yield return new ManagedChest(
container: new ChestContainer(fridge, context: fridge, showColorPicker: false),
location: location,
tile: Vector2.Zero,
mapEntity: null,
defaultDisplayName: I18n.DefaultName_Fridge(),
defaultCategory: category
);
}
}
// dressers
foreach (Furniture rawFurniture in location.furniture)
{
if (rawFurniture is not StorageFurniture furniture)
continue;
if (furniture.QualifiedItemId == "(F)CCFishTank" && location is CommunityCenter)
continue; // temporary fish tank
var container = new StorageFurnitureContainer(furniture);
yield return new ManagedChest(
container: container,
location,
furniture.TileLocation,
mapEntity: furniture,
defaultDisplayName: this.GetDisambiguatedDefaultName(furniture.DisplayName, nameCounts),
defaultCategory: category
);
}
// buildings
foreach (Building building in location.buildings)
{
if (building is JunimoHut hut)
{
yield return new ManagedChest(
container: new JunimoHutContainer(hut),
location: location,
tile: new Vector2(hut.tileX.Value, hut.tileY.Value),
mapEntity: building,
defaultDisplayName: this.GetDisambiguatedDefaultName(GameI18n.GetString("Strings\\Buildings:JunimoHut_Name"), nameCounts),
defaultCategory: category
);
}
}
// shipping bin
if (this.HasShippingBin(location))
{
string shippingBinLabel = GameI18n.GetString("Strings\\Buildings:ShippingBin_Name");
if (Constants.TargetPlatform == GamePlatform.Android)
{
yield return new ManagedChest(
container: new ShippingBinContainer(location, ShippingBinMode.MobileStore),
location: location,
tile: Vector2.Zero,
mapEntity: null,
defaultDisplayName: $"{shippingBinLabel} ({I18n.DefaultName_ShippingBin_Store()})",
defaultCategory: category
);
yield return new ManagedChest(
container: new ShippingBinContainer(location, ShippingBinMode.MobileTake),
location: location,
tile: Vector2.Zero,
mapEntity: null,
defaultDisplayName: $"{shippingBinLabel} ({I18n.DefaultName_ShippingBin_Take()})",
defaultCategory: category
);
}
else
{
yield return new ManagedChest(
container: new ShippingBinContainer(location, ShippingBinMode.Normal),
location: location,
tile: Vector2.Zero,
mapEntity: null,
defaultDisplayName: shippingBinLabel,
defaultCategory: category
);
}
}
}
}
return Search()
.OrderBy(chest => chest.Order ?? int.MaxValue)
.ThenBy(chest => chest.DisplayName, HumanSortComparer.DefaultIgnoreCase)
.Where(chest =>
(
alwaysInclude != null
&& chest.Container.IsSameAs(alwaysInclude.Container)
&& object.ReferenceEquals(chest.Location, alwaysInclude.Location)
&& chest.Tile == alwaysInclude.Tile
)
|| (
(!excludeHidden || !chest.IsIgnored)
&& range.IsInRange(chest.Location)
)
);
}
/// <summary>Get the player chest on the specified tile (if any).</summary>
/// <param name="tile">The tile to check.</param>
public ManagedChest? GetChestFromTile(Vector2 tile)
{
if (!Game1.currentLocation.Objects.TryGetValue(tile, out SObject obj) || obj is not Chest chest)
return null;
return ChestFactory.GetBestMatch(
chests: this.GetChests(RangeHandler.CurrentLocation()),
inventory: this.GetChestInventory(chest),
location: Game1.currentLocation,
tile: tile,
mapEntity: chest
);
}
/// <summary>Get the player chest from the given menu, if any.</summary>
/// <param name="menu">The menu to check.</param>
public ManagedChest? GetChestFromMenu(IClickableMenu menu)
{
// get inventory from menu
IList<Item?>? inventory = null;
GameLocation? forLocation = null;
Vector2? tile = null;
SObject? chest = null;
switch (menu)
{
case ItemGrabMenu itemGrabMenu:
inventory = this.GetInventoryFromContext(itemGrabMenu.context);
forLocation = this.GetLocationFromContext(itemGrabMenu.context);
tile = this.GetTileFromContext(itemGrabMenu.context);
chest = itemGrabMenu.context as SObject;
break;
case ShopMenu shopMenu:
inventory = this.GetInventoryFromContext(shopMenu.source);
forLocation = this.GetLocationFromContext(shopMenu.source);
tile = this.GetTileFromContext(shopMenu.source);
chest = shopMenu.source as SObject;
break;
}
if (inventory == null)
return null;
// get chest from inventory
return ChestFactory.GetBestMatch(
chests: this.GetChests(RangeHandler.Unlimited()),
inventory: inventory,
location: forLocation,
tile: tile,
mapEntity: chest
);
}
/// <summary>Get the chest which contains the given inventory, prioritizing the closest match based on the available data.</summary>
/// <param name="chests">The available chests to filter.</param>
/// <param name="inventory">The chest inventory to match.</param>
/// <param name="location">The chest location, if known.</param>
/// <param name="tile">The chest tile, if known.</param>
/// <param name="mapEntity">The map entity equivalent to the container (e.g. the object or furniture instance), if applicable.</param>
public static ManagedChest? GetBestMatch(IEnumerable<ManagedChest> chests, IList<Item?> inventory, GameLocation? location, Vector2? tile, object? mapEntity)
{
if (inventory == null)
throw new ArgumentNullException(nameof(inventory));
return
(
from chest in chests
where chest.Container.IsSameAs(inventory)
orderby
mapEntity == null || object.ReferenceEquals(chest.MapEntity, mapEntity) descending,
location == null || object.ReferenceEquals(location, chest.Location) descending,
tile == null || chest.Tile == tile descending
select chest
)
.FirstOrDefault();
}
/// <summary>Get the chest which contains the given inventory, prioritizing the closest match for chests with shared inventory like Junimo chests.</summary>
/// <param name="chests">The available chests to filter.</param>
/// <param name="search">The chest to match.</param>
public static ManagedChest? GetBestMatch(IEnumerable<ManagedChest> chests, ManagedChest? search)
{
// We can't just return the search chest here, since it may be a previously created
// instance that's not in the list being searched.
return search != null
? ChestFactory.GetBestMatch(chests, search.Container.Inventory, search.Location, search.Tile, search.MapEntity)
: null;
}
/*********
** Private methods
*********/
/// <summary>Get the locations which are accessible to the current player (regardless of settings).</summary>
private IEnumerable<GameLocation> GetAccessibleLocations()
{
return Context.IsMainPlayer
? CommonHelper.GetLocations()
: this.Multiplayer.GetActiveLocations();
}
/// <summary>Get the inventory for a chest.</summary>
/// <param name="chest">The chest instance.</param>
[return: NotNullIfNotNull("chest")]
private IList<Item?>? GetChestInventory(Chest? chest)
{
return chest?.GetItemsForPlayer(Game1.player.UniqueMultiplayerID);
}
/// <summary>Get the container location from an <see cref="ItemGrabMenu.context"/>, if applicable.</summary>
/// <param name="context">The menu context.</param>
private GameLocation? GetLocationFromContext(object context)
{
return context switch
{
GameLocation location => location,
ShippingBin => Game1.getFarm(),
_ => null
};
}
/// <summary>Get the container's tile position from an <see cref="ItemGrabMenu.context"/>, if applicable.</summary>
/// <param name="context">The menu context.</param>
private Vector2? GetTileFromContext(object context)
{
return context switch
{
SObject obj => obj.TileLocation,
Building building => new Vector2(building.tileX.Value, building.tileY.Value),
_ => null
};
}
/// <summary>Get the underlying inventory for an <see cref="ItemGrabMenu.context"/> value.</summary>
/// <param name="context">The menu context.</param>
private IList<Item?>? GetInventoryFromContext(object context)
{
switch (context)
{
// chest
case Chest chest:
return this.GetChestInventory(chest);
// auto-grabber
case SObject obj when obj.QualifiedItemId == this.AutoGrabberID:
return this.GetChestInventory(obj.heldObject.Value as Chest);
// shipping bin
case Farm:
case IslandWest:
case ShippingBin:
return Game1.getFarm().getShippingBin(Game1.player);
// buildings
case JunimoHut hut:
return this.GetChestInventory(hut.GetOutputChest());
case Building building:
{
// 'output' convention (e.g. vanilla Mill)
Chest? output = building.GetBuildingChest("Output");
if (output != null)
return output.Items;
// else only chest
IList<Chest>? chests = building.buildingChests;
if (chests?.Count == 1)
return chests[0].Items;
// else no match
return null;
}
// dresser
case StorageFurniture furniture:
return furniture.heldItems;
// unsupported type
default:
return null;
}
}
/// <summary>Get the default category name for a location.</summary>
/// <param name="location">The in-game location.</param>
private string GetCategory(GameLocation location)
{
// cabin with owner
if (location is Cabin cabin)
{
return !string.IsNullOrWhiteSpace(cabin.owner?.Name)
? I18n.DefaultCategory_OwnedCabin(owner: cabin.owner?.Name ?? string.Empty)
: I18n.DefaultCategory_UnownedCabin();
}
// else location name
string name = location.GetDisplayName() ?? location.Name;
if (name.Length > Constant.MaxDefaultCategoryLength)
name = name[..(Constant.MaxDefaultCategoryLength - 3)] + "...";
return name;
}
/// <summary>Get whether it's safe to show a color picker for the given chest.</summary>
/// <param name="chest">The chest instance.</param>
/// <param name="location">The location containing the chest.</param>
/// <remarks>The game is hardcoded to exit the chest menu if this is enabled and the chest isn't present in the player's *current* location (see <see cref="ItemGrabMenu.update"/>), except if its tile location is (0, 0).</remarks>
private bool CanShowColorPicker(Chest chest, GameLocation location)
{
if (chest.TileLocation == Vector2.Zero)
return true;
return
object.ReferenceEquals(Game1.currentLocation, location)
&& Game1.currentLocation.objects.TryGetValue(chest.TileLocation, out SObject obj)
&& object.ReferenceEquals(obj, chest);
}
/// <summary>Get the static fridge for a location, if any.</summary>
/// <param name="location">The location to check.</param>
private Chest? GetStaticFridge(GameLocation location)
{
// main farmhouse or cabin
if (location is FarmHouse house && house.fridgePosition != Point.Zero)
return house.fridge.Value;
// island farmhouse
if (location is IslandFarmHouse islandHouse && islandHouse.visited.Value)
return islandHouse.fridge.Value;
return null;
}
/// <summary>Whether the location has a predefined shipping bin.</summary>
/// <param name="location">The location to check.</param>
private bool HasShippingBin(GameLocation location)
{
if (!this.Config().EnableShippingBin)
return false;
return location switch
{
Farm => true,
IslandWest islandFarm => islandFarm.farmhouseRestored.Value,
_ => false
};
}
/// <summary>Append an incrementing number to a default chest name.</summary>
/// <param name="name">The name without the suffix.</param>
/// <param name="nameCounts">The number of times names were previously disambiguated.</param>
private string GetDisambiguatedDefaultName(string name, IDictionary<string, int> nameCounts)
{
if (!nameCounts.TryGetValue(name, out int prevNumber))
prevNumber = 0;
int number = prevNumber + 1;
nameCounts[name] = number;
return I18n.DefaultName_Other(name, number);
}
}