@@ -88,6 +88,11 @@ await inventory.LoadItems(
88
88
89
89
_inventories . Add ( inventoryType , inventory ) ;
90
90
}
91
+
92
+ // Finally load all the sub items
93
+ if ( this [ InventoryType . Hidden ] is { } hiddenInventory )
94
+ await hiddenInventory . LoadItems ( playerCharacter . Items . Where ( i => i . ParentId != ObjectId . Invalid
95
+ && ( InventoryType ) i . InventoryType == InventoryType . Hidden ) ) ;
91
96
} ) ;
92
97
93
98
Listen ( OnDestroyed , ( ) =>
@@ -106,11 +111,7 @@ await inventory.LoadItems(
106
111
public async Task SaveAsync ( UchuContext context )
107
112
{
108
113
var itemsToSave = Items ;
109
-
110
- // If the inventory is empty something went terribly wrong, do NOT save
111
- if ( itemsToSave . Length == 0 )
112
- return ;
113
-
114
+
114
115
var character = await context . Characters . Where ( c => c . Id == GameObject . Id )
115
116
. Include ( c => c . Items )
116
117
. FirstAsync ( ) ;
@@ -193,6 +194,19 @@ public Item FindItem(Lot lot, InventoryType inventoryType)
193
194
return _inventories [ inventoryType ] . Items . FirstOrDefault ( i => i . Lot == lot ) ;
194
195
}
195
196
197
+ /// <summary>
198
+ /// Finds and returns the first item of a certain lot in a certain inventory given that the provided argument
199
+ /// item is its parent
200
+ /// </summary>
201
+ /// <param name="lot">The lot of the item to look for</param>
202
+ /// <param name="inventoryType">The inventory type to look in for the item</param>
203
+ /// <param name="rootItem">The root item that this item needs to belong to</param>
204
+ /// <returns>The item retrieved from the given inventory</returns>
205
+ public Item FindItem ( Lot lot , InventoryType inventoryType , Item rootItem )
206
+ {
207
+ return _inventories [ inventoryType ] . Items . FirstOrDefault ( i => i . Lot == lot && i . RootItem ? . Id == rootItem . Id ) ;
208
+ }
209
+
196
210
/// <summary>
197
211
/// Finds an item by looking for the provided lot in the given inventory type and ensuring that said item has a
198
212
/// count of at least minimumCount
@@ -223,14 +237,7 @@ public Item[] FindItems(Lot lot) => _inventories.Values.SelectMany(
223
237
/// <returns>List of all found items</returns>
224
238
public Item [ ] FindItems ( Lot lot , InventoryType inventoryType ) =>
225
239
_inventories [ inventoryType ] . Items . Where ( i => i . Lot == lot ) . ToArray ( ) ;
226
-
227
- /// <summary>
228
- /// If this is a sub item, it returns the root item of this item. If this item is a root item, it returns itself.
229
- /// </summary>
230
- /// <param name="item">The item to find the root item for</param>
231
- /// <returns>The root item of this item if it is a sub item, the same item otherwise</returns>
232
- public Item GetRootItem ( Item item ) => Items . FirstOrDefault ( i => i . Id == item . RootItem ? . Id ) ?? item ;
233
-
240
+
234
241
#endregion finditem
235
242
236
243
/// <summary>
@@ -297,19 +304,26 @@ private static async Task<ItemComponent> GetItemComponentForLotAysnc(Lot lot)
297
304
298
305
return itemComponent ;
299
306
}
300
-
307
+
301
308
/// <summary>
302
309
/// Adds a new lot to the inventory. The lot will automatically instantiated as the required amount of items
303
310
/// based on the provided count and the stack size of that item.
304
311
/// </summary>
312
+ /// <remarks>
313
+ /// The returning list may be empty if it was possible to distribute over already existing stacks
314
+ /// </remarks>
305
315
/// <param name="lot">The lot to add to the inventory</param>
306
316
/// <param name="count">The count of the lot we want to add to the inventory</param>
307
- /// <param name="inventoryType">Optional explicit inventory type to add this lot to, if not provided this will be
308
- /// implicitly retrieved from the item lot, generally used for vendor buy back</param>
309
317
/// <param name="settings">Optional <c>LegoDataDictionary</c> to instantiate the item with</param>
310
- public async Task AddLotAsync ( Lot lot , uint count , LegoDataDictionary settings = default ,
311
- InventoryType inventoryType = default )
318
+ /// <param name="inventoryType">Optional explicit inventory type to add this lot to, if not provided this will be
319
+ /// implicitly retrieved from the item lot, generally used for vendor buy back</param>
320
+ /// <param name="rootItem">An optional parent item</param>
321
+ /// <returns>The list of items that were created while adding the lot</returns>
322
+ public async Task AddLotAsync ( Lot lot , uint count , LegoDataDictionary settings = default ,
323
+ InventoryType inventoryType = default , Item rootItem = default )
312
324
{
325
+ var createdItems = new List < Item > ( ) ;
326
+
313
327
// For players, the faction token proxy needs to be replaced with an actual faction token
314
328
// If this wasn't possible, exit
315
329
if ( ! HandleFactionToken ( ref lot ) )
@@ -363,14 +377,15 @@ public async Task AddLotAsync(Lot lot, uint count, LegoDataDictionary settings =
363
377
while ( totalToAdd > 0 )
364
378
{
365
379
var toAdd = ( uint ) Min ( stackSize , ( int ) totalToAdd ) ;
366
- var item = await Item . Instantiate ( GameObject , lot , inventory , toAdd , extraInfo : settings ) ;
380
+ var item = await Item . Instantiate ( GameObject , lot , inventory , toAdd , extraInfo : settings , rootItem : rootItem ) ;
367
381
368
382
// Might occur if the inventory is full or an error occured during slot claiming
369
383
if ( item == null )
370
384
return ; // TODO: Message item to player with mail
371
385
372
386
Start ( item ) ;
373
387
item . MessageCreation ( ) ;
388
+ createdItems . Add ( item ) ;
374
389
375
390
totalToAdd -= toAdd ;
376
391
totalAdded += toAdd ;
@@ -394,6 +409,12 @@ public async Task AddLotAsync(Lot lot, uint count, LegoDataDictionary settings =
394
409
await OnLotAdded . InvokeAsync ( lot , ( uint ) totalAdded ) ;
395
410
} ) ;
396
411
}
412
+
413
+ // Create sub items if this was a root item
414
+ if ( rootItem == null )
415
+ foreach ( var createdItem in createdItems )
416
+ foreach ( var subItemLot in createdItem . SubItemLots )
417
+ await AddLotAsync ( subItemLot , 1 , default , InventoryType . Hidden , createdItem ) ;
397
418
}
398
419
399
420
/// <summary>
@@ -463,7 +484,7 @@ public async Task RemoveLotAsync(Lot lot, uint count, InventoryType inventoryTyp
463
484
{
464
485
var amountToRemove = ( uint ) Min ( ( int ) count , ( int ) itemToRemove . Count ) ;
465
486
await itemToRemove . DecrementCountAsync ( amountToRemove , silent ) ;
466
-
487
+
467
488
count -= amountToRemove ;
468
489
if ( count == 0 )
469
490
return ;
0 commit comments