DEADSOFTWARE

Drop items on Q and add empty hearts to health bar
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / model / item / InventoryItem.kt
index 5121ab3f9f778575af8131c553cbe07f035bdefc..765224334d1d29b8441e0f59d1a9b0bd07df503e 100644 (file)
@@ -12,9 +12,18 @@ import java.io.Serializable
 
 class InventoryItem @JvmOverloads constructor(
     val itemKey: String,
-    var amount: Int = 1,
+    _amount: Int = 1,
 ) : Serializable {
 
+    var amount = _amount
+        set(value) {
+            field = if (value < 0) {
+                0
+            } else {
+                value
+            }
+        }
+
     @Transient
     lateinit var item: Item
         private set
@@ -33,11 +42,19 @@ class InventoryItem @JvmOverloads constructor(
 
     @JvmOverloads
     fun add(count: Int = 1) {
+        if (count > 0 && Int.MAX_VALUE - count < amount) {
+            throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE")
+        }
+
         amount += count
     }
 
     @JvmOverloads
     fun subtract(count: Int = 1) {
+        if (count < 0) {
+            throw IllegalArgumentException("Can't subtract negative amount")
+        }
+
         add(-count)
     }