forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageGUI.cs
More file actions
675 lines (577 loc) · 20.8 KB
/
StorageGUI.cs
File metadata and controls
675 lines (577 loc) · 20.8 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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
using System;
using System.Collections.Generic;
using System.Linq;
using MagicStorage.Components;
using MagicStorage.Sorting;
using MagicStorage.UI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ReLogic.Content;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.Localization;
using Terraria.UI;
namespace MagicStorage
{
public static class StorageGUI
{
private const int padding = 4;
private const int numColumns = 10;
private const float inventoryScale = 0.85f;
private const int startMaxRightClickTimer = 20;
public static MouseState curMouse;
public static MouseState oldMouse;
private static UIPanel basePanel;
private static float panelTop;
private static float panelLeft;
private static float panelWidth;
private static float panelHeight;
private static UIElement topBar;
// TODO consider Terraria's UISearchBar
internal static UI.UISearchBar searchBar;
private static UIButtonChoice sortButtons;
private static UIToggleButton favoritedOnlyButton;
internal static UITextPanel<LocalizedText> depositButton;
private static UIElement topBar2;
private static UIButtonChoice filterButtons;
public static readonly ModSearchBox modSearchBox = new(RefreshItems);
private static readonly UISlotZone slotZone = new(HoverItemSlot, GetItem, inventoryScale);
private static int slotFocus = -1;
private static int rightClickTimer;
private static int maxRightClickTimer = startMaxRightClickTimer;
internal static UIScrollbar scrollBar = new();
private static bool scrollBarFocus;
private static int scrollBarFocusMouseStart;
private static float scrollBarFocusPositionStart;
private static readonly float scrollBarViewSize = 1f;
private static float scrollBarMaxViewSize = 2f;
private static readonly List<Item> items = new();
private static readonly List<bool> didMatCheck = new();
private static int numRows;
private static int displayRows;
private static UIElement bottomBar;
private static UIText capacityText;
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 void Initialize()
{
InitLangStuff();
float itemSlotWidth = TextureAssets.InventoryBack.Value.Width * inventoryScale;
float itemSlotHeight = TextureAssets.InventoryBack.Value.Height * inventoryScale;
panelTop = Main.instance.invBottom + 60;
panelLeft = 20f;
basePanel = new UIPanel();
float innerPanelWidth = numColumns * (itemSlotWidth + padding) + 20f + padding;
panelWidth = basePanel.PaddingLeft + innerPanelWidth + basePanel.PaddingRight;
panelHeight = Main.screenHeight - panelTop - 40f;
basePanel.Left.Set(panelLeft, 0f);
basePanel.Top.Set(panelTop, 0f);
basePanel.Width.Set(panelWidth, 0f);
basePanel.Height.Set(panelHeight, 0f);
basePanel.Recalculate();
topBar = new UIElement();
topBar.Width.Set(0f, 1f);
topBar.Height.Set(32f, 0f);
basePanel.Append(topBar);
InitSortButtons();
topBar.Append(sortButtons);
float x = sortButtons.GetDimensions().Width + 2 * padding;
favoritedOnlyButton.Left.Set(x, 0f);
topBar.Append(favoritedOnlyButton);
// TODO make this a new variable
x += favoritedOnlyButton.GetDimensions().Width + 2 * padding;
depositButton.Left.Set(x, 0f);
depositButton.Width.Set(128f, 0f);
depositButton.Height.Set(-2 * padding, 1f);
depositButton.PaddingTop = 8f;
depositButton.PaddingBottom = 8f;
topBar.Append(depositButton);
// TODO also new variable
x += depositButton.GetDimensions().Width;
float depositButtonRight = x;
searchBar.Left.Set(depositButtonRight + padding, 0f);
searchBar.Width.Set(-depositButtonRight - 2 * padding, 1f);
searchBar.Height.Set(0f, 1f);
topBar.Append(searchBar);
topBar2 = new UIElement();
topBar2.Width.Set(0f, 1f);
topBar2.Height.Set(32f, 0f);
topBar2.Top.Set(36f, 0f);
basePanel.Append(topBar2);
InitFilterButtons();
topBar2.Append(filterButtons);
float filterButtonsRight = filterButtons.GetDimensions().Width + padding;
modSearchBox.Button.Left.Set(filterButtonsRight + padding, 0f);
modSearchBox.Button.Width.Set(-filterButtonsRight - 2 * padding, 1f);
modSearchBox.Button.Height.Set(0f, 1f);
modSearchBox.Button.OverflowHidden = true;
topBar2.Append(modSearchBox.Button);
slotZone.Width.Set(0f, 1f);
slotZone.Top.Set(76f, 0f);
slotZone.Height.Set(-116f, 1f);
basePanel.Append(slotZone);
numRows = (items.Count + numColumns - 1) / numColumns;
displayRows = (int)slotZone.GetDimensions().Height / ((int)itemSlotHeight + padding);
slotZone.SetDimensions(numColumns, displayRows);
int noDisplayRows = numRows - displayRows;
if (noDisplayRows < 0)
noDisplayRows = 0;
scrollBarMaxViewSize = 1 + noDisplayRows;
scrollBar.Height.Set(displayRows * (itemSlotHeight + padding), 0f);
scrollBar.Left.Set(-20f, 1f);
scrollBar.SetView(scrollBarViewSize, scrollBarMaxViewSize);
slotZone.Append(scrollBar);
bottomBar = new UIElement();
bottomBar.Width.Set(0f, 1f);
bottomBar.Height.Set(32f, 0f);
bottomBar.Top.Set(-32f, 1f);
basePanel.Append(bottomBar);
capacityText.Left.Set(6f, 0f);
capacityText.Top.Set(6f, 0f);
TEStorageHeart heart = GetHeart();
int numItems = 0;
int capacity = 0;
if (heart is not null)
foreach (TEAbstractStorageUnit abstractStorageUnit in heart.GetStorageUnits())
if (abstractStorageUnit is TEStorageUnit storageUnit)
{
numItems += storageUnit.NumItems;
capacity += storageUnit.Capacity;
}
capacityText.SetText(Language.GetTextValue("Mods.MagicStorage.Capacity", numItems, capacity));
bottomBar.Append(capacityText);
basePanel.Activate();
}
private static void InitLangStuff()
{
depositButton ??= new UITextPanel<LocalizedText>(Language.GetText("Mods.MagicStorage.DepositAll"));
searchBar ??= new UI.UISearchBar(Language.GetText("Mods.MagicStorage.SearchName"), RefreshItems);
modSearchBox.InitLangStuff();
capacityText ??= new UIText("Items");
}
internal static void Unload()
{
sortButtons = null;
filterButtons = null;
favoritedOnlyButton = null;
}
private static void InitSortButtons()
{
sortButtons ??= GUIHelpers.MakeSortButtons(RefreshItems);
favoritedOnlyButton ??= new UIToggleButton(RefreshItems,
MagicStorage.Instance.Assets.Request<Texture2D>("Assets/FilterMisc", AssetRequestMode.ImmediateLoad),
Language.GetText("Mods.MagicStorage.ShowOnlyFavorited"));
}
private static void InitFilterButtons()
{
filterButtons ??= GUIHelpers.MakeFilterButtons(true, RefreshItems);
}
public static void Update(GameTime gameTime)
{
oldMouse = curMouse;
curMouse = Mouse.GetState();
if (Main.playerInventory && StoragePlayer.LocalPlayer.ViewingStorage().X >= 0 && !StoragePlayer.IsStorageCrafting() && !StoragePlayer.IsStorageEnvironment())
{
if (curMouse.RightButton == ButtonState.Released)
ResetSlotFocus();
basePanel?.Update(gameTime);
UpdateScrollBar();
UpdateDepositButton();
modSearchBox.Update(curMouse, oldMouse);
}
else
{
scrollBarFocus = false;
scrollBar.ViewPosition = 0f;
ResetSlotFocus();
}
}
public static void Draw()
{
Player player = Main.LocalPlayer;
Initialize();
if (Main.mouseX > panelLeft && Main.mouseX < panelLeft + panelWidth && Main.mouseY > panelTop && Main.mouseY < panelTop + panelHeight)
{
player.mouseInterface = true;
player.cursorItemIconEnabled = false;
InterfaceHelper.HideItemIconCache();
}
basePanel.Draw(Main.spriteBatch);
slotZone.DrawText();
sortButtons.DrawText();
favoritedOnlyButton.DrawText();
filterButtons.DrawText();
DrawDepositButton();
}
private static Item GetItem(int slot, ref int context)
{
int index = slot + numColumns * (int)Math.Round(scrollBar.ViewPosition);
Item item = index < items.Count ? items[index] : new Item();
if (!item.IsAir && !didMatCheck[index])
{
item.checkMat();
didMatCheck[index] = true;
}
return item;
}
private static void UpdateScrollBar()
{
if (slotFocus >= 0)
{
scrollBarFocus = false;
return;
}
Rectangle dim = scrollBar.GetClippingRectangle(Main.spriteBatch);
Vector2 boxPos = new(dim.X, dim.Y + dim.Height * (scrollBar.ViewPosition / scrollBarMaxViewSize));
float boxWidth = 20f * Main.UIScale;
float boxHeight = dim.Height * (scrollBarViewSize / scrollBarMaxViewSize);
if (scrollBarFocus)
{
if (curMouse.LeftButton == ButtonState.Released)
{
scrollBarFocus = false;
}
else
{
int difference = curMouse.Y - scrollBarFocusMouseStart;
scrollBar.ViewPosition = scrollBarFocusPositionStart + difference / boxHeight;
}
}
else if (MouseClicked)
{
if (curMouse.X > boxPos.X && curMouse.X < boxPos.X + boxWidth && curMouse.Y > boxPos.Y - 3f && curMouse.Y < boxPos.Y + boxHeight + 4f)
{
scrollBarFocus = true;
scrollBarFocusMouseStart = curMouse.Y;
scrollBarFocusPositionStart = scrollBar.ViewPosition;
}
}
if (!scrollBarFocus)
{
int difference = oldMouse.ScrollWheelValue / 250 - curMouse.ScrollWheelValue / 250;
scrollBar.ViewPosition += difference;
}
}
public static TEStorageHeart GetHeart() => StoragePlayer.LocalPlayer.GetStorageHeart();
public static void RefreshItems()
{
if (StoragePlayer.IsStorageCrafting())
{
CraftingGUI.RefreshItems();
return;
}
if (StoragePlayer.IsStorageEnvironment())
return;
items.Clear();
didMatCheck.Clear();
TEStorageHeart heart = GetHeart();
if (heart == null)
return;
NetHelper.Report(true, "Refreshing storage items");
InitLangStuff();
InitSortButtons();
InitFilterButtons();
SortMode sortMode = (SortMode)sortButtons.Choice;
FilterMode filterMode = ItemFilter.GetFilter(filterButtons.Choice);
int modFilterIndex = modSearchBox.ModIndex;
void DoFiltering()
{
IEnumerable<Item> itemsLocal;
if (filterMode == FilterMode.Recent)
{
Dictionary<int, Item> stored = heart.GetStoredItems().GroupBy(x => x.type).ToDictionary(x => x.Key, x => x.First());
IEnumerable<Item> toFilter = heart.UniqueItemsPutHistory.Reverse().Where(x => stored.ContainsKey(x.type)).Select(x => stored[x.type]);
itemsLocal = ItemSorter.SortAndFilter(toFilter, sortMode == SortMode.Default ? SortMode.AsIs : sortMode, FilterMode.All, modFilterIndex, searchBar.Text, 100);
}
else
{
itemsLocal = ItemSorter.SortAndFilter(heart.GetStoredItems(), sortMode, filterMode, modFilterIndex, searchBar.Text).OrderBy(x => x.favorited ? 0 : 1);
}
items.AddRange(itemsLocal.Where(x => !favoritedOnlyButton.Value || x.favorited));
NetHelper.Report(false, "Filtering applied. Item count: " + items.Count);
}
DoFiltering();
// now if nothing found we disable filters one by one
if (searchBar.Text.Trim().Length > 0)
{
if (items.Count == 0 && filterMode != FilterMode.All)
{
// search all categories
filterMode = FilterMode.All;
NetHelper.Report(false, "No items displayed even though items exist in storage. Defaulting to \"All\" filter mode");
DoFiltering();
}
if (items.Count == 0 && modFilterIndex != ModSearchBox.ModIndexAll)
{
// search all mods
modFilterIndex = ModSearchBox.ModIndexAll;
NetHelper.Report(false, $"No items displayed even though items exist in storage. Defaulting to \"{Language.GetTextValue("Mods.MagicStorage.FilterAllMods")}\" mod filter");
DoFiltering();
}
}
for (int k = 0; k < items.Count; k++)
didMatCheck.Add(false);
}
private static void UpdateDepositButton()
{
Rectangle dim = InterfaceHelper.GetFullRectangle(depositButton);
if (curMouse.X > dim.X && curMouse.X < dim.X + dim.Width && curMouse.Y > dim.Y && curMouse.Y < dim.Y + dim.Height)
{
depositButton.BackgroundColor = new Color(73, 94, 171);
if (MouseClicked)
{
bool ctrlDown = Main.keyState.IsKeyDown(Keys.LeftControl) || Main.keyState.IsKeyDown(Keys.RightControl);
if (TryDepositAll(ctrlDown == MagicStorageConfig.QuickStackDepositMode))
{
RefreshItems();
SoundEngine.PlaySound(SoundID.Grab);
}
}
else if (CraftingGUI.RightMouseClicked)
{
if (TryRestock())
{
RefreshItems();
SoundEngine.PlaySound(SoundID.Grab);
}
}
}
else
{
depositButton.BackgroundColor = new Color(63, 82, 151) * 0.7f;
}
}
private static void DrawDepositButton()
{
Rectangle dim = InterfaceHelper.GetFullRectangle(depositButton);
if (curMouse.X > dim.X && curMouse.X < dim.X + dim.Width && curMouse.Y > dim.Y && curMouse.Y < dim.Y + dim.Height)
{
string alt = MagicStorageConfig.QuickStackDepositMode ? "Alt" : "";
Main.instance.MouseText(Language.GetText($"Mods.MagicStorage.DepositTooltip{alt}").Value);
}
}
private static void ResetSlotFocus()
{
slotFocus = -1;
rightClickTimer = 0;
maxRightClickTimer = startMaxRightClickTimer;
}
private static void HoverItemSlot(int slot, ref int hoverSlot)
{
Player player = Main.LocalPlayer;
int visualSlot = slot;
slot += numColumns * (int)Math.Round(scrollBar.ViewPosition);
if (MouseClicked)
{
bool changed = false;
if (!Main.mouseItem.IsAir && player.itemAnimation == 0 && player.itemTime == 0)
{
if (TryDeposit(Main.mouseItem))
changed = true;
}
else if (Main.mouseItem.IsAir && slot < items.Count && !items[slot].IsAir)
{
if (Main.keyState.IsKeyDown(Keys.LeftAlt))
{
if (Main.netMode == NetmodeID.SinglePlayer)
items[slot].favorited = !items[slot].favorited;
else
Main.NewTextMultiline(
"Toggling item as favorite is not implemented in multiplayer but you can withdraw this item, toggle it in inventory and deposit again",
c: Color.White);
// there is no item instance id and there is no concept of slot # in heart so we can't send this in operation
// a workaropund would be to withdraw and deposit it back with changed favorite flag
// but it still might look ugly for the player that initiates operation
}
else
{
Item toWithdraw = items[slot].Clone();
if (toWithdraw.stack > toWithdraw.maxStack)
toWithdraw.stack = toWithdraw.maxStack;
Main.mouseItem = DoWithdraw(toWithdraw, ItemSlot.ShiftInUse);
if (ItemSlot.ShiftInUse)
Main.mouseItem = player.GetItem(Main.myPlayer, Main.mouseItem, GetItemSettings.InventoryEntityToPlayerInventorySettings);
changed = true;
}
}
RefreshItems();
if (changed)
{
RefreshItems();
SoundEngine.PlaySound(SoundID.Grab);
}
}
if (RightMouseClicked &&
slot < items.Count &&
(Main.mouseItem.IsAir || ItemData.Matches(Main.mouseItem, items[slot]) && Main.mouseItem.stack < Main.mouseItem.maxStack))
slotFocus = slot;
if (slot < items.Count && !items[slot].IsAir)
{
hoverSlot = visualSlot;
items[slot].newAndShiny = false;
}
if (slotFocus >= 0)
SlotFocusLogic();
}
private static void SlotFocusLogic()
{
if (slotFocus >= items.Count ||
!Main.mouseItem.IsAir && (!ItemData.Matches(Main.mouseItem, items[slotFocus]) || Main.mouseItem.stack >= Main.mouseItem.maxStack))
{
ResetSlotFocus();
}
else
{
if (rightClickTimer <= 0)
{
rightClickTimer = maxRightClickTimer;
maxRightClickTimer = maxRightClickTimer * 3 / 4;
if (maxRightClickTimer <= 0)
maxRightClickTimer = 1;
Item toWithdraw = items[slotFocus].Clone();
toWithdraw.stack = 1;
Item result = DoWithdraw(toWithdraw);
if (Main.mouseItem.IsAir)
Main.mouseItem = result;
else
Main.mouseItem.stack += result.stack;
RefreshItems();
SoundEngine.PlaySound(SoundID.MenuTick);
}
rightClickTimer--;
}
}
/// <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;
TEStorageHeart heart = wrapper.Heart;
heart.TryDeposit(item);
return oldStack != item.stack;
}
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;
TEStorageHeart heart = GetHeart();
heart.TryDeposit(item);
return oldStack != item.stack;
}
/// <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;
return heart.TryDeposit(items);
}
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)
{
TEStorageHeart heart = GetHeart();
return heart.TryDeposit(items);
}
private 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);
}
return heart.TryDeposit(items);
}
private 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;
return heart.TryWithdraw(item, keepOneIfFavorite, toInventory);
}
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();
return heart.TryWithdraw(item, keepOneIfFavorite, toInventory);
}
}
}