+48
-2
lines changedFilter options
+48
-2
lines changed Original file line number Diff line number Diff line change
@@ -82,10 +82,14 @@ class Inventory<T>(
82
82
}
83
83
84
84
/**
85
-
* @return the total sum of all [item] stack quantities
85
+
* @return the total sum of all [item] stack quantities or 0 if [item] not present
86
86
*/
87
87
fun getItemQuantity(item: T): Int
88
-
= itemQuantityProperty(item).value
88
+
= try {
89
+
itemQuantityProperty(item).value
90
+
} catch (e: IllegalArgumentException) {
91
+
0
92
+
}
89
93
90
94
@Deprecated("Use add(ItemConfig)", ReplaceWith("add(com.almasb.fxgl.inventory.ItemConfig)"))
91
95
fun add(item: T, name: String, description: String, view: Node, quantity: Int): Boolean {
@@ -202,6 +206,23 @@ class Inventory<T>(
202
206
return true
203
207
}
204
208
209
+
/**
210
+
* Transfer all items from [other] to this inventory.
211
+
*
212
+
* @return true if operation was (at least partially) successful, if returns false then no modifications were made to either inventory objects
213
+
*/
214
+
fun transferAllFrom(other: Inventory<T>): Boolean {
215
+
var isPartiallySuccess = false
216
+
217
+
// toList() to create a copy since [transferFrom] modifies [other.items]
218
+
other.items.toList().forEach {
219
+
if (transferFrom(other, it.userItem, it.quantity))
220
+
isPartiallySuccess = true
221
+
}
222
+
223
+
return isPartiallySuccess
224
+
}
225
+
205
226
/**
206
227
* Transfer [item] with [quantity] amount from [other] to this inventory.
207
228
*
Original file line number Diff line number Diff line change
@@ -286,6 +286,31 @@ class InventoryTest {
286
286
assertTrue(!inventory.hasItem("Hello"))
287
287
}
288
288
289
+
@Test
290
+
fun `Transfer all from inventory`() {
291
+
inventory = Inventory(5)
292
+
293
+
inventory.add("Hello", quantity = 5)
294
+
inventory.add("Hi", quantity = 3)
295
+
296
+
val other = Inventory<String>(5)
297
+
298
+
assertTrue(other.transferAllFrom(inventory))
299
+
assertThat(other.getItemQuantity("Hello"), `is`(5))
300
+
assertThat(other.getItemQuantity("Hi"), `is`(3))
301
+
302
+
val other2 = Inventory<String>(1)
303
+
304
+
// true because [other2] only has 1 space, so partially succeeds
305
+
assertTrue(other2.transferAllFrom(other))
306
+
assertThat(other2.getItemQuantity("Hello"), `is`(5))
307
+
assertThat(other2.getItemQuantity("Hi"), `is`(0))
308
+
309
+
310
+
// false because [other2] now has no space, so op fails
311
+
assertFalse(other2.transferAllFrom(other))
312
+
}
313
+
289
314
@Test
290
315
fun `Transfer from inventory does not work if no item in inventory`() {
291
316
inventory = Inventory(5)
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4