@@ -96,7 +96,7 @@ class Inventory<T>(
96
96
* Add [item] to inventory, dynamically creating stacks based on [config].
97
97
* If [item] (as checked by hashCode()) is already present, then its quantity is increased by [quantity].
98
98
*
99
- * @return false if could not add item, otherwise true
99
+ * @return false if could not add item and no modifications were made , otherwise true
100
100
*/
101
101
@JvmOverloads fun add (
102
102
item : T ,
@@ -105,13 +105,14 @@ class Inventory<T>(
105
105
): Boolean {
106
106
107
107
// already in inventory, just increment quantity
108
- if (item in itemsData )
108
+ if (hasItem( item) )
109
109
return incrementQuantity(item, quantity)
110
110
111
111
// can't add because inventory is full
112
112
if (isFull)
113
113
return false
114
114
115
+ // TODO: if == 0?
115
116
// adding a new item, so check if quantity > 0
116
117
if (quantity < 0 ) {
117
118
log.warning(" Attempted to add a new item with negative quantity. Ignoring" )
@@ -124,14 +125,15 @@ class Inventory<T>(
124
125
if (numStacksNeeded > numFreeStacks)
125
126
return false
126
127
127
- // capacity-wise we are good, add new item
128
+ // capacity-wise we are good, add new item will succeed
128
129
129
130
val data = ItemData (item).also {
130
131
it.name = config.name
131
132
it.description = config.description
132
133
it.view = config.view
133
134
it.maxStackQuantity = config.maxStackQuantity
134
135
136
+ // TODO: should we delegate to incrementQuantity(item, amount)
135
137
it.incrementQuantity(quantity)
136
138
}
137
139
@@ -153,10 +155,14 @@ class Inventory<T>(
153
155
}
154
156
155
157
/* *
156
- * @return true if operation was successful
158
+ * @return true if operation was successful, if returns false then no modifications were made
157
159
*/
158
160
fun incrementQuantity (item : T , amount : Int ): Boolean {
159
- if (item !in itemsData) {
161
+ // meaningless call
162
+ if (amount == 0 )
163
+ return false
164
+
165
+ if (! hasItem(item)) {
160
166
log.warning(" Attempted to increment qty of item that is not in inventory. Ignoring" )
161
167
return false
162
168
}
@@ -172,47 +178,50 @@ class Inventory<T>(
172
178
}
173
179
}
174
180
175
- val isOK = data.incrementQuantity(amount)
181
+ // if decrementing, check that we have enough
182
+ if (amount < 0 && - amount > data.quantity)
183
+ return false
176
184
177
- // if all good, update stacks
178
- if (isOK) {
179
- // remove non-existent stacks
180
- items.removeIf { it.userItem == item && it !in data.stacks }
185
+ // all checks passed, we will modify this inventory
186
+ data.incrementQuantity(amount)
181
187
182
- // add newly created stacks
183
- data.stacks.forEach {
184
- if (it !in items) {
185
- items + = it
186
- }
187
- }
188
+ // remove non-existent stacks
189
+ items.removeIf { it.userItem == item && it !in data.stacks }
188
190
189
- if (data.quantity == 0 ) {
190
- itemsData - = item
191
+ // add newly created stacks
192
+ data.stacks.forEach {
193
+ if (it !in items) {
194
+ items + = it
191
195
}
192
196
}
193
197
194
- return isOK
198
+ // if we reached 0, remove item from inventory
199
+ if (data.quantity == 0 ) {
200
+ itemsData - = item
201
+ }
202
+
203
+ return true
195
204
}
196
205
197
206
/* *
198
207
* Transfer [item] with [quantity] amount from [other] to this inventory.
199
208
*
200
- * @return true if operation was successful
209
+ * @return true if operation was successful, if returns false then no modifications were made to either inventory objects
201
210
*/
202
211
@JvmOverloads fun transferFrom (other : Inventory <T >, item : T , quantity : Int = 1): Boolean {
203
- if (isFull)
204
- return false
205
-
206
212
if (! other.hasItem(item))
207
213
return false
208
214
209
215
if (other.getItemQuantity(item) < quantity)
210
216
return false
211
217
212
- add(item, quantity = quantity)
213
- other.incrementQuantity(item, - quantity)
218
+ val isOK = add(item, quantity = quantity)
214
219
215
- return true
220
+ if (isOK) {
221
+ return other.incrementQuantity(item, - quantity)
222
+ }
223
+
224
+ return false
216
225
}
217
226
}
218
227
@@ -249,20 +258,20 @@ class ItemData<T> internal constructor(var userItem: T) {
249
258
val quantity: Int
250
259
get() = quantityProperty.value
251
260
252
- fun incrementQuantity (amount : Int ): Boolean {
261
+ /* *
262
+ * This operation is meant to always succeed since checks should be done before the call.
263
+ */
264
+ internal fun incrementQuantity (amount : Int ) {
253
265
if (amount > 0 )
254
- return fill(amount)
255
-
256
- if (amount < 0 )
257
- return deplete(- amount)
258
-
259
- return true
266
+ fill(amount)
267
+ else if (amount < 0 )
268
+ deplete(- amount)
260
269
}
261
270
262
271
/* *
263
272
* @param amount - a positive value to increment
264
273
*/
265
- private fun fill (amount : Int ): Boolean {
274
+ private fun fill (amount : Int ) {
266
275
var left = amount
267
276
268
277
// fill existing stacks
@@ -300,18 +309,12 @@ class ItemData<T> internal constructor(var userItem: T) {
300
309
quantityProperty.value + = remainder
301
310
}
302
311
}
303
-
304
- return true
305
312
}
306
313
307
314
/* *
308
315
* @param amount - a positive value to decrement
309
316
*/
310
- private fun deplete (amount : Int ): Boolean {
311
- // tried to decrease by more than we have
312
- if (amount > quantityProperty.value)
313
- return false
314
-
317
+ private fun deplete (amount : Int ) {
315
318
var left = amount
316
319
317
320
while (left > 0 ) {
@@ -330,8 +333,6 @@ class ItemData<T> internal constructor(var userItem: T) {
330
333
left = 0
331
334
}
332
335
}
333
-
334
- return true
335
336
}
336
337
}
337
338
0 commit comments