-
Notifications
You must be signed in to change notification settings - Fork 101
/
StorageGUI.cs
291 lines (233 loc) · 8.77 KB
/
StorageGUI.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
using System.Collections.Generic;
using System.Linq;
using MagicStorage.Common.Systems;
using MagicStorage.Components;
using Microsoft.Xna.Framework.Input;
using Terraria;
using Terraria.ID;
namespace MagicStorage
{
// Method implementations can also be found in UI/GUIs/StorageGUI.X.cs
public static partial class StorageGUI
{
public const int padding = 4;
public const int numColumns = 10;
public const float inventoryScale = 0.85f;
public const int startMaxRightClickTimer = 20;
public static MouseState curMouse;
public static MouseState oldMouse;
internal static readonly float scrollBarViewSize = 1f;
internal static float scrollBarMaxViewSize = 2f;
public const int WAIT_PANEL_MINIMUM_TICKS = 45;
//Legacy properties required for UISearchBar to function properly
public static bool MouseClicked => curMouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released;
public static bool RightMouseClicked => curMouse.RightButton == ButtonState.Pressed && oldMouse.RightButton == ButtonState.Released;
public static TEStorageHeart GetHeart() => StoragePlayer.LocalPlayer.GetStorageHeart();
internal static void InvokeOnRefresh() => OnRefresh?.Invoke();
internal static void Unload() => ClearAllCollections();
private static void ClearAllCollections() {
itemTypesToUpdate?.Clear();
itemTypesToUpdate = null;
items.Clear();
sourceItems.Clear();
didMatCheck.Clear();
ResetRefreshCache();
}
internal static void FavoriteItem(int slot) {
if (slot < 0 || slot >= items.Count)
return;
//Favorite all of the source items
bool doFavorite = !sourceItems[slot][0].favorited;
foreach (var item in sourceItems[slot])
item.favorited = doFavorite;
MagicUI.SetNextCollectionsToRefresh(sourceItems[slot][0].type);
}
/// <summary>
/// Simulates a deposit attempt into a storage center (Storage Heart, Storage Access, etc.).
/// </summary>
/// <param name="center">The tile entity used to attempt to retrieve a Storage Heart</param>
/// <param name="item">The item to deposit</param>
/// <returns>Whether the deposit was successful</returns>
public static bool TryDespoit(TEStorageCenter center, Item item) {
if (center is null)
return false;
StoragePlayer.StorageHeartAccessWrapper wrapper = new(center);
if (wrapper.Valid) {
int oldStack = item.stack;
int oldType = item.type;
TEStorageHeart heart = wrapper.Heart;
heart.TryDeposit(item);
if (oldStack != item.stack) {
if (GetHeart()?.Position == heart.Position)
MagicUI.SetNextCollectionsToRefresh(oldType);
return true;
}
}
return false;
}
/// <summary>
/// Simulates a deposit attempt into the currently assigned Storage Heart.
/// </summary>
/// <param name="item">The item to deposit</param>
/// <returns>Whether the deposit was successful</returns>
public static bool TryDeposit(Item item)
{
int oldStack = item.stack;
int oldType = item.type;
TEStorageHeart heart = GetHeart();
heart.TryDeposit(item);
if (oldStack != item.stack) {
MagicUI.SetNextCollectionsToRefresh(oldType);
return true;
}
return false;
}
/// <summary>
/// Simulates a deposit attempt into a storage center (Storage Heart, Storage Access, etc.).
/// </summary>
/// <param name="center">The tile entity used to attempt to retrieve a Storage Heart</param>
/// <param name="items">The items to deposit</param>
/// <returns>Whether the deposit was successful</returns>
public static bool TryDeposit(TEStorageCenter center, List<Item> items)
{
if (center is null)
return false;
StoragePlayer.StorageHeartAccessWrapper wrapper = new(center);
if (wrapper.Valid) {
TEStorageHeart heart = wrapper.Heart;
int[] types = items.Select(static i => i.type).ToArray();
if (heart.TryDeposit(items)) {
if (GetHeart()?.Position == heart.Position)
MagicUI.SetNextCollectionsToRefresh(types);
return true;
}
return false;
}
return false;
}
/// <summary>
/// Simulates a deposit attempt into a storage center (Storage Heart, Storage Access, etc.).
/// </summary>
/// <param name="center">The tile entity used to attempt to retrieve a Storage Heart</param>
/// <param name="items">The items to deposit</param>
/// <param name="quickStack">Whether the operation is a quick stack</param>
/// <returns>Whether the deposit was successful</returns>
public static bool TryDeposit(TEStorageCenter center, List<Item> items, bool quickStack = false)
{
if (center is null)
return false;
StoragePlayer.StorageHeartAccessWrapper wrapper = new(center);
if (wrapper.Valid) {
TEStorageHeart heart = wrapper.Heart;
if (quickStack)
items = new(items.Where(i => heart.HasItem(i, ignorePrefix: true)));
int[] types = items.Select(static i => i.type).ToArray();
if (heart.TryDeposit(items)) {
if (GetHeart()?.Position == heart.Position)
MagicUI.SetNextCollectionsToRefresh(types);
return true;
}
return false;
}
return false;
}
/// <summary>
/// Simulates a deposit attempt into the currently assigned Storage Heart.
/// </summary>
/// <param name="items">The items to deposit</param>
/// <returns>Whether the deposit was successful</returns>
public static bool TryDeposit(List<Item> items)
{
if (GetHeart() is not TEStorageHeart heart)
return false;
int[] types = items.Select(static i => i.type).ToArray();
if (heart.TryDeposit(items)) {
MagicUI.SetNextCollectionsToRefresh(types);
return true;
}
return false;
}
internal static bool TryDepositAll(bool quickStack)
{
Player player = Main.LocalPlayer;
TEStorageHeart heart = GetHeart();
bool filter(Item item) => !item.IsAir && !item.favorited && (!quickStack || heart.HasItem(item, true));
var items = new List<Item>();
for (int k = 10; k < 50; k++)
{
Item item = player.inventory[k];
if (filter(item))
items.Add(item);
}
int[] types = items.Select(static i => i.type).ToArray();
if (heart.TryDeposit(items)) {
SetNextItemTypesToRefresh(types);
return true;
}
return false;
}
internal static bool TryRestock()
{
Player player = Main.LocalPlayer;
GetHeart();
bool changed = false;
foreach (Item item in player.inventory)
if (item is not null && !item.IsAir && item.stack < item.maxStack)
{
Item toWithdraw = item.Clone();
toWithdraw.stack = item.maxStack - item.stack;
toWithdraw = DoWithdraw(toWithdraw, true, true);
if (!toWithdraw.IsAir)
{
item.stack += toWithdraw.stack;
toWithdraw.TurnToAir();
changed = true;
}
}
return changed;
}
/// <summary>
/// Attempts to withdraw an item from a storage center (Storage Heart, Storage Access, etc.).
/// </summary>
/// <param name="center">The tile entity used to attempt to retrieve a Storage Heart</param>
/// <param name="item">The item to withdraw</param>
/// <param name="toInventory">
/// Whether the item goes into the player inventory.
/// This parameter is only used if <see cref="Main.netMode"/> is <see cref="NetmodeID.MultiplayerClient"/>
/// </param>
/// <param name="keepOneIfFavorite">Whether at least one item should remain in the storage if it's favourited</param>
/// <returns>A valid item instance if the withdrawal was succesful, an air item otherwise.</returns>
public static Item DoWithdraw(TEStorageCenter center, Item item, bool toInventory = false, bool keepOneIfFavorite = false)
{
if (center is null)
return new Item();
StoragePlayer.StorageHeartAccessWrapper wrapper = new(center);
if (wrapper.Valid) {
TEStorageHeart heart = wrapper.Heart;
Item withdrawn = heart.TryWithdraw(item, keepOneIfFavorite, toInventory);
if (!withdrawn.IsAir && GetHeart()?.Position == heart.Position)
SetNextItemTypeToRefresh(withdrawn.type);
return withdrawn;
}
return new Item();
}
/// <summary>
/// Attempts to withdraw an item from the currently assigned Storage Heart.
/// </summary>
/// <param name="item">The item to withdraw</param>
/// <param name="toInventory">
/// Whether the item goes into the player inventory.
/// This parameter is only used if <see cref="Main.netMode"/> is <see cref="NetmodeID.MultiplayerClient"/>
/// </param>
/// <param name="keepOneIfFavorite">Whether at least one item should remain in the storage if it's favourited</param>
/// <returns>A valid item instance if the withdrawal was succesful, an air item otherwise.</returns>
public static Item DoWithdraw(Item item, bool toInventory = false, bool keepOneIfFavorite = false)
{
TEStorageHeart heart = GetHeart();
Item withdrawn = heart.TryWithdraw(item, keepOneIfFavorite, toInventory);
if (!withdrawn.IsAir)
SetNextItemTypeToRefresh(withdrawn.type);
return withdrawn;
}
}
}