Repository navigation
Expand file tree
/
Copy pathChestFactory.cs
More file actions
138 lines (123 loc) · 6.08 KB
/
Copy pathChestFactory.cs
File metadata and controls
138 lines (123 loc) · 6.08 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
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.Common;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Locations;
using StardewValley.Menus;
using StardewValley.Objects;
namespace Pathoschild.Stardew.ChestsAnywhere
{
/// <summary>Encapsulates logic for finding chests.</summary>
internal class ChestFactory
{
/*********
** Properties
*********/
/// <summary>Provides translations stored in the mod's folder.</summary>
private readonly ITranslationHelper Translations;
/// <summary>Simplifies access to private game data.</summary>
private readonly IReflectionHelper Reflection;
/// <summary>Whether to support access to the shipping bin.</summary>
private readonly bool EnableShippingBin;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="translations">Provides translations stored in the mod's folder.</param>
/// <param name="reflection">Simplifies access to private game data.</param>
/// <param name="enableShippingBin">Whether to support access to the shipping bin.</param>
public ChestFactory(ITranslationHelper translations, IReflectionHelper reflection, bool enableShippingBin)
{
this.Translations = translations;
this.Reflection = reflection;
this.EnableShippingBin = enableShippingBin;
}
/// <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="alwaysIncludeContainer">A container to include even if it would normally be hidden.</param>
public IEnumerable<ManagedChest> GetChests(RangeHandler range, bool excludeHidden = false, IContainer alwaysIncludeContainer = null)
{
IEnumerable<ManagedChest> Search()
{
foreach (GameLocation location in CommonHelper.GetLocations())
{
// chests in location
{
int namelessChests = 0;
foreach (KeyValuePair<Vector2, Object> pair in location.Objects)
{
Vector2 tile = pair.Key;
if (pair.Value is Chest chest && chest.playerChest)
yield return new ManagedChest(new ChestContainer(chest), location, tile, this.Translations.Get("default-name.chest", new { number = ++namelessChests }));
}
}
// farmhouse containers
if (location is FarmHouse house && Game1.player.HouseUpgradeLevel > 0)
{
Chest fridge = house.fridge;
if (fridge != null)
yield return new ManagedChest(new ChestContainer(fridge), location, Vector2.Zero, this.Translations.Get("default-name.fridge"));
}
// buildings
if (location is BuildableGameLocation buildableLocation)
{
int namelessHuts = 0;
foreach (Building building in buildableLocation.buildings)
{
if (building is JunimoHut hut)
yield return new ManagedChest(new JunimoHutContainer(hut), location, new Vector2(hut.tileX, hut.tileY), this.Translations.Get("default-name.junimo-hut", new { number = ++namelessHuts }));
}
}
// shipping bin
if (this.EnableShippingBin && location is Farm farm)
yield return new ManagedChest(new ShippingBinContainer(farm, this.Reflection), farm, Vector2.Zero, this.Translations.Get("default-name.shipping-bin"));
}
}
return (
from chest in Search()
orderby chest.Order ?? int.MaxValue, chest.Name
where
chest.Container.IsSameAs(alwaysIncludeContainer)
|| (
(!excludeHidden || !chest.IsIgnored)
&& range.IsInRange(chest.Location)
)
select chest
);
}
/// <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 Object obj) || !(obj is Chest chest))
return null;
RangeHandler range = RangeHandler.CurrentLocation();
return this.GetChests(range).FirstOrDefault(p => p.Container.IsSameAs(chest.items));
}
/// <summary>Get the player chest from the specified menu (if any).</summary>
/// <param name="menu">The menu to check.</param>
public ManagedChest GetChestFromMenu(ItemGrabMenu menu)
{
RangeHandler range = RangeHandler.Unlimited();
// get from opened inventory
{
object target = menu.behaviorOnItemGrab?.Target;
List<Item> inventory = (target as Chest)?.items ?? (target as IContainer)?.Inventory;
if (inventory != null)
{
ManagedChest chest = this.GetChests(range).FirstOrDefault(p => p.Container.IsSameAs(inventory));
if (chest != null)
return chest;
}
}
// fallback to open chest
return this.GetChests(range).FirstOrDefault(p => p.Container.IsOpen());
}
}
}