Skip to content

Commit b4640ce

Browse files
committed
Add comments
1 parent 93ac022 commit b4640ce

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

src/main/kotlin/client/inventory/Inventory.kt

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,69 @@ package client.inventory
22

33
import constants.ItemConstants
44

5+
/**
6+
* Represents an inventory in the game.
7+
*
8+
* @property type The type of the inventory. This can be any value from the `InventoryType` enum.
9+
* @property slotLimit The maximum number of slots in the inventory. Default value is 96.
10+
* @constructor Creates an instance of Inventory which is also an Iterable of `Item`.
11+
*/
512
class Inventory(val type: InventoryType, var slotLimit: Byte = 96) : Iterable<Item> {
13+
// Represents the items in the inventory. The key is the slot number and the value is the item in that slot.
614
val inventory = mutableMapOf<Byte, Item>()
15+
16+
// A flag to indicate whether the inventory has been checked or not.
717
var checked = false
818

19+
/**
20+
* Checks if the inventory is extendable.
21+
*
22+
* An inventory is considered extendable if its type is not UNDEFINED, EQUIPPED, or CASH.
23+
*
24+
* @return true if the inventory is extendable, false otherwise.
25+
*/
926
fun isExtendableInventory() = when (type) {
1027
InventoryType.UNDEFINED, InventoryType.EQUIPPED, InventoryType.CASH -> false
1128
else -> true
1229
}
1330

31+
/**
32+
* Checks if the inventory is of type EQUIP or EQUIPPED.
33+
*
34+
* @return true if the inventory is of type EQUIP or EQUIPPED, false otherwise.
35+
*/
1436
fun isEquipInventory() = type == InventoryType.EQUIP || type == InventoryType.EQUIPPED
1537

38+
/**
39+
* Finds an item in the inventory by its ID.
40+
*
41+
* @param itemId The ID of the item to find.
42+
* @return The item if found, null otherwise.
43+
*/
1644
fun findById(itemId: Int) = inventory.values.find { it.itemId == itemId }
1745

46+
/**
47+
* Counts the number of items in the inventory with a specific ID.
48+
*
49+
* @param itemId The ID of the item to count.
50+
* @return The count of items with the specified ID.
51+
*/
1852
fun countById(itemId: Int) = inventory.values.count { it.itemId == itemId }
1953

54+
/**
55+
* Lists all items in the inventory with a specific ID.
56+
*
57+
* @param itemId The ID of the item to list.
58+
* @return A sorted list of items with the specified ID.
59+
*/
2060
fun listById(itemId: Int) = inventory.values.filter { it.itemId == itemId }.toList().sorted()
2161

62+
/**
63+
* Lists all items in the inventory.
64+
*
65+
* @return A list of all items in the inventory.
66+
*/
2267
fun list() = inventory.values.toList()
23-
2468
fun addItem(item: Item?): Byte {
2569
val slotId = getNextFreeSlot()
2670
if (slotId < 0 || item == null) return -1
@@ -29,6 +73,13 @@ class Inventory(val type: InventoryType, var slotLimit: Byte = 96) : Iterable<It
2973
return slotId
3074
}
3175

76+
/**
77+
* Adds an item to the inventory from the database.
78+
*
79+
* This function is typically used when loading the inventory from the database. It checks if the item's position is not negative and the inventory type is not EQUIPPED. If these conditions are met, the item is added to the inventory at its position.
80+
*
81+
* @param item The item to be added to the inventory.
82+
*/
3283
fun addFromDatabase(item: Item) {
3384
if (item.position < 0 && type != InventoryType.EQUIPPED) return
3485
inventory[item.position] = item
@@ -105,15 +156,34 @@ class Inventory(val type: InventoryType, var slotLimit: Byte = 96) : Iterable<It
105156

106157
fun allInventories() = listOf(this)
107158

159+
/**
160+
* Finds an item in the inventory by its cash ID.
161+
*
162+
* This function is used to find an item in the inventory by its cash ID.
163+
* It first checks if the item is of type `Equip`. If it is, it checks if the item is a ring.
164+
* If the item is a ring, it uses the ring ID. If the item is not a ring, it uses the cash ID.
165+
* If the item is not of type `Equip`, it checks if the item is a pet. If it is, it uses the pet ID.
166+
* If it is not, it uses the cash ID.
167+
*
168+
* @param cashId The cash ID of the item to find.
169+
* @return The item if found, null otherwise.
170+
*/
108171
fun findByCashId(cashId: Int) = inventory.values.find {
109-
var isRing = false
110-
var equip: Equip? = null
111-
if (it.getType() == 1) {
112-
equip = it as Equip
113-
isRing = equip.ringId > -1
114-
}
115-
(if (it.petId > -1) it.petId else if (isRing) equip?.ringId else it.cashId) == cashId
172+
var isRing = false
173+
var equip: Equip? = null
174+
if (it.getType() == 1) {
175+
equip = it as Equip
176+
isRing = equip.ringId > -1
116177
}
178+
(if (it.petId > -1) it.petId else if (isRing) equip?.ringId else it.cashId) == cashId
179+
}
117180

181+
/**
182+
* Provides an iterator over the items in the inventory.
183+
*
184+
* This function is used to iterate over the items in the inventory. It returns an iterator over the values in the inventory map, which are the items in the inventory.
185+
*
186+
* @return An iterator over the items in the inventory.
187+
*/
118188
override fun iterator() = inventory.values.iterator()
119189
}

0 commit comments

Comments
 (0)