tiny-essentials 1.20.3 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -11
- package/dist/_/numbers.cjs +136 -0
- package/dist/_/numbers.d.mts +24 -0
- package/dist/_/numbers.mjs +102 -0
- package/dist/v1/TinyAdvancedRaffle.min.js +1 -0
- package/dist/v1/TinyArrayPaginator.min.js +1 -0
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyCookieConsent.min.js +1 -0
- package/dist/v1/TinyDayNightCycle.min.js +1 -0
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyGamepad.min.js +1 -0
- package/dist/v1/TinyI18.min.js +1 -0
- package/dist/v1/TinyInventory.min.js +1 -0
- package/dist/v1/TinyInventoryTrader.min.js +1 -0
- package/dist/v1/TinyLocalStorage.min.js +1 -1
- package/dist/v1/TinyRateLimiter.min.js +1 -1
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyToastNotify.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/basics/array.cjs +21 -0
- package/dist/v1/basics/array.d.mts +8 -0
- package/dist/v1/basics/array.mjs +18 -0
- package/dist/v1/basics/index.cjs +2 -8
- package/dist/v1/basics/index.d.mts +3 -8
- package/dist/v1/basics/index.mjs +2 -3
- package/dist/v1/basics/simpleMath.cjs +46 -0
- package/dist/v1/basics/simpleMath.d.mts +27 -0
- package/dist/v1/basics/simpleMath.mjs +42 -0
- package/dist/v1/build/TinyAdvancedRaffle.cjs +7 -0
- package/dist/v1/build/TinyAdvancedRaffle.d.mts +3 -0
- package/dist/v1/build/TinyAdvancedRaffle.mjs +2 -0
- package/dist/v1/build/TinyArrayPaginator.cjs +7 -0
- package/dist/v1/build/TinyArrayPaginator.d.mts +3 -0
- package/dist/v1/build/TinyArrayPaginator.mjs +2 -0
- package/dist/v1/build/TinyCookieConsent.cjs +7 -0
- package/dist/v1/build/TinyCookieConsent.d.mts +3 -0
- package/dist/v1/build/TinyCookieConsent.mjs +2 -0
- package/dist/v1/build/TinyDayNightCycle.cjs +7 -0
- package/dist/v1/build/TinyDayNightCycle.d.mts +3 -0
- package/dist/v1/build/TinyDayNightCycle.mjs +2 -0
- package/dist/v1/build/TinyGamepad.cjs +7 -0
- package/dist/v1/build/TinyGamepad.d.mts +3 -0
- package/dist/v1/build/TinyGamepad.mjs +2 -0
- package/dist/v1/build/TinyI18.cjs +7 -0
- package/dist/v1/build/TinyI18.d.mts +3 -0
- package/dist/v1/build/TinyI18.mjs +2 -0
- package/dist/v1/build/TinyInventory.cjs +7 -0
- package/dist/v1/build/TinyInventory.d.mts +3 -0
- package/dist/v1/build/TinyInventory.mjs +2 -0
- package/dist/v1/build/TinyInventoryTrader.cjs +7 -0
- package/dist/v1/build/TinyInventoryTrader.d.mts +3 -0
- package/dist/v1/build/TinyInventoryTrader.mjs +2 -0
- package/dist/v1/css/TinyCookieConsent.min.css +1 -0
- package/dist/v1/index.cjs +18 -8
- package/dist/v1/index.d.mts +11 -8
- package/dist/v1/index.mjs +10 -3
- package/dist/v1/libs/TinyAdvancedRaffle.cjs +1654 -0
- package/dist/v1/libs/TinyAdvancedRaffle.d.mts +851 -0
- package/dist/v1/libs/TinyAdvancedRaffle.mjs +1477 -0
- package/dist/v1/libs/TinyArrayPaginator.cjs +132 -0
- package/dist/v1/libs/TinyArrayPaginator.d.mts +78 -0
- package/dist/v1/libs/TinyArrayPaginator.mjs +119 -0
- package/dist/v1/libs/TinyCookieConsent.cjs +278 -0
- package/dist/v1/libs/TinyCookieConsent.d.mts +142 -0
- package/dist/v1/libs/TinyCookieConsent.mjs +248 -0
- package/dist/v1/libs/TinyDayNightCycle.cjs +1389 -0
- package/dist/v1/libs/TinyDayNightCycle.d.mts +673 -0
- package/dist/v1/libs/TinyDayNightCycle.mjs +1232 -0
- package/dist/v1/libs/TinyGamepad.cjs +2690 -0
- package/dist/v1/libs/TinyGamepad.d.mts +1286 -0
- package/dist/v1/libs/TinyGamepad.mjs +2386 -0
- package/dist/v1/libs/TinyHtml.d.mts +2 -2
- package/dist/v1/libs/TinyI18.cjs +946 -0
- package/dist/v1/libs/TinyI18.d.mts +418 -0
- package/dist/v1/libs/TinyI18.mjs +873 -0
- package/dist/v1/libs/TinyInventory.cjs +1788 -0
- package/dist/v1/libs/TinyInventory.d.mts +871 -0
- package/dist/v1/libs/TinyInventory.mjs +1650 -0
- package/dist/v1/libs/TinyInventoryTrader.cjs +242 -0
- package/dist/v1/libs/TinyInventoryTrader.d.mts +95 -0
- package/dist/v1/libs/TinyInventoryTrader.mjs +212 -0
- package/dist/v1/libs/TinyLocalStorage.cjs +14 -1
- package/dist/v1/libs/TinyLocalStorage.mjs +8 -1
- package/dist/v1/libs/TinyRateLimiter.cjs +0 -13
- package/dist/v1/libs/TinyRateLimiter.d.mts +0 -8
- package/dist/v1/libs/TinyRateLimiter.mjs +0 -12
- package/dist/v1/libs/TinySmartScroller.cjs +0 -51
- package/dist/v1/libs/TinySmartScroller.d.mts +0 -36
- package/dist/v1/libs/TinySmartScroller.mjs +0 -46
- package/dist/v1/libs/TinyToastNotify.cjs +2 -2
- package/dist/v1/libs/TinyToastNotify.mjs +2 -2
- package/dist/v1/libs/TinyUploadClicker.cjs +1 -1
- package/docs/v1/Ai-Tips.md +51 -0
- package/docs/v1/Personal-Ai-Prompts(portuguese).md +134 -0
- package/docs/v1/Personal-Ai-Prompts.md +113 -0
- package/docs/v1/README.md +23 -0
- package/docs/v1/basics/array.md +17 -0
- package/docs/v1/basics/simpleMath.md +73 -0
- package/docs/v1/libs/TinyAdvancedRaffle.md +674 -0
- package/docs/v1/libs/TinyArrayPaginator.md +150 -0
- package/docs/v1/libs/TinyCookieConsent.md +152 -0
- package/docs/v1/libs/TinyDayNightCycle.md +711 -0
- package/docs/v1/libs/TinyGamepad.md +980 -0
- package/docs/v1/libs/TinyI18.md +515 -0
- package/docs/v1/libs/TinyInventory.md +777 -0
- package/docs/v1/libs/TinyInventoryTrader.md +114 -0
- package/package.json +7 -5
- package/dist/v1/basics/html_deprecated.cjs +0 -124
- package/dist/v1/basics/html_deprecated.d.mts +0 -40
- package/dist/v1/basics/html_deprecated.mjs +0 -97
- package/dist/v1/css/TinyDraggerExample.css +0 -21
- package/dist/v1/css/TinyNotify.css +0 -350
- package/dist/v1/css/aiMarker.css +0 -4
- package/docs/v1/basics/html_deprecated.md +0 -127
|
@@ -0,0 +1,777 @@
|
|
|
1
|
+
# 📦 TinyInventory
|
|
2
|
+
|
|
3
|
+
**TinyInventory** is a lightweight yet powerful inventory management system designed for games, simulations, and applications that require structured item handling. It provides:
|
|
4
|
+
|
|
5
|
+
* **Stack management** (items merge up to configurable stack limits).
|
|
6
|
+
* **Slot management** (fixed, dynamic, and special equipment slots).
|
|
7
|
+
* **Weight and size constraints** for advanced gameplay mechanics.
|
|
8
|
+
* **Serialization & cloning** (save, load, and duplicate inventories safely).
|
|
9
|
+
* **Flexible item registry support** to integrate with custom game logic.
|
|
10
|
+
|
|
11
|
+
With a clean API and robust safeguards, TinyInventory ensures that every item transaction — whether adding, removing, moving, or trading — follows consistent rules and prevents invalid states.
|
|
12
|
+
|
|
13
|
+
It is designed to work standalone or as a foundation for higher-level systems, such as crafting, trading, or player equipment managers.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 📦 Core Types & Properties
|
|
18
|
+
|
|
19
|
+
### 🧩 ItemDef
|
|
20
|
+
|
|
21
|
+
Represents a registered **item definition** in the global registry.
|
|
22
|
+
|
|
23
|
+
| Property | Type | Description |
|
|
24
|
+
| ---------- | -------------------- | ------------------------------------ |
|
|
25
|
+
| `id` | `string` | Unique identifier for the item. |
|
|
26
|
+
| `weight` | `number` | Weight of a single unit. |
|
|
27
|
+
| `metadata` | `InventoryMetadata` | Default metadata for this item type. |
|
|
28
|
+
| `maxStack` | `number` | Maximum stack size per slot. |
|
|
29
|
+
| `onUse` | `OnUseEvent \| null` | Callback triggered when used. |
|
|
30
|
+
| `type` | `string \| null` | Optional category/type. |
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
### 🎒 InventoryItem
|
|
35
|
+
|
|
36
|
+
Represents a **stored item instance** inside the inventory.
|
|
37
|
+
|
|
38
|
+
| Property | Type | Description |
|
|
39
|
+
| ---------- | ------------------- | --------------------------- |
|
|
40
|
+
| `id` | `string` | Item identifier. |
|
|
41
|
+
| `metadata` | `InventoryMetadata` | Metadata for this instance. |
|
|
42
|
+
| `quantity` | `number` | Units in this stack. |
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
### 📑 InvSlots
|
|
47
|
+
|
|
48
|
+
An array of item stacks in the inventory.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
(InventoryItem | null)[]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### 🗂️ InventoryMetadata
|
|
57
|
+
|
|
58
|
+
Metadata object for **arbitrary key-value pairs**.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
Record<string | number | symbol, any>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
### 🛡️ SpecialSlot
|
|
67
|
+
|
|
68
|
+
Represents a **special slot** (e.g., equipment).
|
|
69
|
+
|
|
70
|
+
| Property | Type | Description |
|
|
71
|
+
| -------- | ----------------------- | -------------------------- |
|
|
72
|
+
| `type` | `string \| null` | Optional slot category. |
|
|
73
|
+
| `item` | `InventoryItem \| null` | Item equipped in the slot. |
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
### 🔔 Event System
|
|
78
|
+
|
|
79
|
+
* `OnEvent` → `(payload: EventPayload) => void`
|
|
80
|
+
* **AddItemEvent** → triggered when an item is added
|
|
81
|
+
* **SetItemEvent** → triggered when an item is set/overwritten
|
|
82
|
+
* **RemoveItemEvent** → triggered when an item is removed
|
|
83
|
+
* **UseItemEvent** → triggered when an item is used
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
### 🏷️ OnUseEvent
|
|
88
|
+
|
|
89
|
+
Special callback executed when using an item.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
(payload: EventPayload) => void
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### 🎯 EventsType
|
|
98
|
+
|
|
99
|
+
Supported event strings:
|
|
100
|
+
`"add" | "remove" | "use" | "set"`
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
### 🔍 Callbacks
|
|
105
|
+
|
|
106
|
+
* **GetItemsByMetadataCallback** → Filters items by metadata.
|
|
107
|
+
* **FindItemCallback** → Works like `Array.prototype.find()` to locate items.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
### 📄 SerializedInventory
|
|
112
|
+
|
|
113
|
+
Full JSON structure for saving/loading inventories.
|
|
114
|
+
|
|
115
|
+
| Property | Type | Description | |
|
|
116
|
+
| -------------- | ----------------------------- | -------------------------------- | ------------- |
|
|
117
|
+
| `__schema` | `"TinyInventory"` | Schema identifier. | |
|
|
118
|
+
| `version` | `number` | Format version. | |
|
|
119
|
+
| `maxWeight` | `number \| null` | Max weight allowed. | |
|
|
120
|
+
| `maxSlots` | `number \| null` | Max slots allowed. | |
|
|
121
|
+
| `maxSize` | `number \| null` | Max item amount. | |
|
|
122
|
+
| `maxStack` | `number` | Global stack limit. | |
|
|
123
|
+
| `items` | \`(InventoryItem | null)\[]\` | Stored items. |
|
|
124
|
+
| `specialSlots` | `Record<string, SpecialSlot>` | Reserved slots (e.g., "helmet"). | |
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
### 🗃️ ItemListData
|
|
129
|
+
|
|
130
|
+
Tuple representing an inventory entry:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
[InventoryItem, number] // [item, index]
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
### 📬 EventPayload
|
|
139
|
+
|
|
140
|
+
Payload object dispatched when an inventory action occurs.
|
|
141
|
+
|
|
142
|
+
| Property | Type | Description | |
|
|
143
|
+
| -------------- | ------------------------------- | ------------------------ | ---------------------------- |
|
|
144
|
+
| `index` | \`number | null\` | Item index in slots. |
|
|
145
|
+
| `item` | \`InventoryItem | null\` | Item affected. |
|
|
146
|
+
| `isCollection` | `boolean` | True if from collection. | |
|
|
147
|
+
| `specialSlot` | \`string | null\` | Special slot ID if relevant. |
|
|
148
|
+
| `remove` | `(forceSpace: boolean) => void` | Function to remove item. | |
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
### ➕ AddItemResult
|
|
153
|
+
|
|
154
|
+
Result object returned after adding an item.
|
|
155
|
+
|
|
156
|
+
| Property | Type | Description |
|
|
157
|
+
| ------------- | --------------------------------------- | --------------------------------------------- |
|
|
158
|
+
| `remaining` | `number` | Quantity not added due to space/stack limits. |
|
|
159
|
+
| `placesAdded` | `{ index: number; quantity: number }[]` | Slots where items were placed. |
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## 🏗️ TinyInventory Class
|
|
164
|
+
|
|
165
|
+
A **flexible inventory management system** providing:
|
|
166
|
+
|
|
167
|
+
* 📦 Standard slots with configurable **limits** (weight, size, stack).
|
|
168
|
+
* 🛡️ Special slots for **equipment & unique items**.
|
|
169
|
+
* ✏️ Full **CRUD** operations (add, remove, move, use, equip, unequip).
|
|
170
|
+
* 🧠 **Metadata-aware** handling (durability, enchantments, etc.).
|
|
171
|
+
* 💾 **Serialization/Deserialization** to JSON.
|
|
172
|
+
* 🔔 **Event triggers** for `"add"`, `"remove"`, `"use"`, `"set"`.
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
### 🏷️ Static: `#ItemRegistry`
|
|
177
|
+
|
|
178
|
+
TinyInventory comes with a **global item registry** that defines all possible item types before they are used in any inventory.
|
|
179
|
+
This ensures consistency in rules such as stack limits, metadata defaults, weight, and usage behavior (`id → ItemDef`).
|
|
180
|
+
|
|
181
|
+
#### 🔍 `TinyInventory.itemRegistry` (getter)
|
|
182
|
+
|
|
183
|
+
Returns a deep-cloned snapshot of the registered item definitions.
|
|
184
|
+
|
|
185
|
+
* ✅ Prevents accidental mutations of the internal registry.
|
|
186
|
+
* 📑 Each entry includes `id`, `weight`, `maxStack`, `metadata`, `type`, and `onUse`.
|
|
187
|
+
|
|
188
|
+
**Returns:**
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
Record<string, ItemDef>
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
#### ❌ `TinyInventory.removeItem(itemId)`
|
|
197
|
+
|
|
198
|
+
Removes an item type from the registry.
|
|
199
|
+
|
|
200
|
+
**Parameters:**
|
|
201
|
+
|
|
202
|
+
* `itemId` *(string)* → The unique identifier.
|
|
203
|
+
|
|
204
|
+
**Returns:**
|
|
205
|
+
|
|
206
|
+
* `true` if the item existed and was removed.
|
|
207
|
+
* `false` if the item was not found.
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
#### ✅ `TinyInventory.hasItem(itemId)`
|
|
212
|
+
|
|
213
|
+
Checks if an item type exists in the registry.
|
|
214
|
+
|
|
215
|
+
**Parameters:**
|
|
216
|
+
|
|
217
|
+
* `itemId` *(string)* → The unique identifier.
|
|
218
|
+
|
|
219
|
+
**Returns:**
|
|
220
|
+
|
|
221
|
+
* `true` if the item exists.
|
|
222
|
+
* `false` otherwise.
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
#### 📥 `TinyInventory.getItem(itemId)`
|
|
227
|
+
|
|
228
|
+
Retrieves the definition of a registered item.
|
|
229
|
+
|
|
230
|
+
**Parameters:**
|
|
231
|
+
|
|
232
|
+
* `itemId` *(string)* → The unique identifier.
|
|
233
|
+
|
|
234
|
+
**Returns:**
|
|
235
|
+
|
|
236
|
+
* `ItemDef` → The item definition.
|
|
237
|
+
|
|
238
|
+
⚠️ Throws `Error` if the item is not defined.
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
#### 🛠️ Static: `defineItem(config)`
|
|
243
|
+
|
|
244
|
+
Registers or updates an item in the global registry.
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
TinyInventory.defineItem({
|
|
248
|
+
id: "apple",
|
|
249
|
+
weight: 0.2,
|
|
250
|
+
maxStack: 10,
|
|
251
|
+
metadata: { edible: true },
|
|
252
|
+
type: "food",
|
|
253
|
+
onUse: (payload) => console.log("Apple eaten!", payload),
|
|
254
|
+
});
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
⚠️ Throws if `id` is missing or invalid.
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
### ⚙️ Constructor
|
|
262
|
+
|
|
263
|
+
Creates a new inventory instance with customizable constraints:
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
new TinyInventory({
|
|
267
|
+
maxWeight: 100,
|
|
268
|
+
maxSlots: 20,
|
|
269
|
+
maxSize: 200,
|
|
270
|
+
maxStack: 99,
|
|
271
|
+
specialSlots: { helmet: { type: "armor" }, weapon: { type: "sword" } }
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
### 📊 Properties (Getters & Setters)
|
|
278
|
+
|
|
279
|
+
* `maxStack` → Maximum stack size per slot.
|
|
280
|
+
* `maxSize` → Maximum total quantity of items.
|
|
281
|
+
* `maxSlots` → Maximum number of slots.
|
|
282
|
+
* `maxWeight` → Maximum total weight allowed.
|
|
283
|
+
* `events` → Returns registered event listeners.
|
|
284
|
+
* `items` → Returns cloned item list.
|
|
285
|
+
* `specialSlots` → Returns cloned special slots.
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
### 📏 Calculated Properties
|
|
290
|
+
|
|
291
|
+
* `size` → Total quantity of items.
|
|
292
|
+
* `slotsSize` → Number of occupied slots.
|
|
293
|
+
* `weight` → Total carried weight.
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## ⚖️ Space & Capacity Checks
|
|
298
|
+
|
|
299
|
+
### 🔍 `hasSpace(settings?)`
|
|
300
|
+
|
|
301
|
+
Checks if there is **available space** based on slot, size, and weight limits.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
hasSpace({
|
|
305
|
+
weight?: number,
|
|
306
|
+
sizeLength?: number,
|
|
307
|
+
slotsLength?: number
|
|
308
|
+
}): boolean
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
| Parameter | Type | Default | Description |
|
|
312
|
+
| ------------- | ------ | ------- | ------------------------------------------------------ |
|
|
313
|
+
| `weight` | number | `0` | Extra weight to preview (e.g., before adding an item). |
|
|
314
|
+
| `sizeLength` | number | `0` | Extra item count to preview. |
|
|
315
|
+
| `slotsLength` | number | `0` | Extra slot count to preview. |
|
|
316
|
+
|
|
317
|
+
✅ Returns `true` if the inventory can handle the addition.
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
### 🏋️ `isHeavy(extraWeight = 0)`
|
|
322
|
+
|
|
323
|
+
Checks if the **total weight** exceeds the max allowed.
|
|
324
|
+
|
|
325
|
+
* `true` → Overweight
|
|
326
|
+
* `false` → Within limits
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
### 📦 `areFull(extraLength = 0)`
|
|
331
|
+
|
|
332
|
+
Checks if the **item count** exceeds `maxSize`.
|
|
333
|
+
|
|
334
|
+
* Uses `>` check (strictly over the limit).
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
### 📦 `isFull(extraLength = 0)`
|
|
339
|
+
|
|
340
|
+
Checks if the **item count** has **reached** `maxSize`.
|
|
341
|
+
|
|
342
|
+
* Uses `>=` check (at limit or over).
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
### 🎯 `areFullSlots(extraLength = 0)`
|
|
347
|
+
|
|
348
|
+
Checks if the **slot count** exceeds `maxSlots`.
|
|
349
|
+
|
|
350
|
+
* Uses `>` check.
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
### 🎯 `isFullSlots(extraLength = 0)`
|
|
355
|
+
|
|
356
|
+
Checks if the **slot count** has **reached** `maxSlots`.
|
|
357
|
+
|
|
358
|
+
* Uses `>=` check.
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
## 🔔 Event System
|
|
363
|
+
|
|
364
|
+
### ⚡ `#triggerEvent(type, payload)`
|
|
365
|
+
|
|
366
|
+
Internal method to dispatch inventory events.
|
|
367
|
+
|
|
368
|
+
* `type` → `"add" | "remove" | "use" | "set"`
|
|
369
|
+
* `payload` → `EventPayload`
|
|
370
|
+
|
|
371
|
+
---
|
|
372
|
+
|
|
373
|
+
### ❌ `off(eventType, callback)`
|
|
374
|
+
|
|
375
|
+
Unregisters a specific event listener.
|
|
376
|
+
|
|
377
|
+
---
|
|
378
|
+
|
|
379
|
+
### 🧹 `offAll(eventType)`
|
|
380
|
+
|
|
381
|
+
Removes **all callbacks** for a given event type.
|
|
382
|
+
|
|
383
|
+
---
|
|
384
|
+
|
|
385
|
+
### 🪞 `cloneEventCallbacks(eventType)`
|
|
386
|
+
|
|
387
|
+
Returns a **shallow copy** of all listeners for an event type.
|
|
388
|
+
|
|
389
|
+
---
|
|
390
|
+
|
|
391
|
+
### 🎉 Event Registration Helpers
|
|
392
|
+
|
|
393
|
+
* `onAddItem(callback)` → Listen to item additions.
|
|
394
|
+
* `onSetItem(callback)` → Listen to item sets/replacements.
|
|
395
|
+
* `onRemoveItem(callback)` → Listen to removals.
|
|
396
|
+
* `onUseItem(callback)` → Listen to item usage.
|
|
397
|
+
|
|
398
|
+
---
|
|
399
|
+
|
|
400
|
+
## 🧹 Slot Management
|
|
401
|
+
|
|
402
|
+
### 🧽 `compactInventory()`
|
|
403
|
+
|
|
404
|
+
Removes unnecessary `null` values, **compacting slots**.
|
|
405
|
+
|
|
406
|
+
* Preserves item order.
|
|
407
|
+
* Triggers `"remove"` event for cleared slots.
|
|
408
|
+
|
|
409
|
+
---
|
|
410
|
+
|
|
411
|
+
## 🧹 Inventory Clearing
|
|
412
|
+
|
|
413
|
+
### 🧽 `clear()`
|
|
414
|
+
|
|
415
|
+
Clears **all items** and **event listeners** from the inventory.
|
|
416
|
+
|
|
417
|
+
* Resets normal and special slots.
|
|
418
|
+
* Clears all registered event listeners (`add`, `remove`, `use`, `set`).
|
|
419
|
+
* Preserves max limits and slot definitions.
|
|
420
|
+
|
|
421
|
+
---
|
|
422
|
+
|
|
423
|
+
### 🧹 `clearAllEvents()`
|
|
424
|
+
|
|
425
|
+
Removes **all registered event listeners** from the inventory.
|
|
426
|
+
|
|
427
|
+
* Event types affected: `"add"`, `"remove"`, `"use"`, `"set"`.
|
|
428
|
+
* Does not affect any items in inventory.
|
|
429
|
+
|
|
430
|
+
---
|
|
431
|
+
|
|
432
|
+
### 🗃️ `clearAllItems()`
|
|
433
|
+
|
|
434
|
+
Removes **all items** from the inventory, including normal and special slots.
|
|
435
|
+
|
|
436
|
+
* Calls `clearItems()` and `clearSpecialItems()` internally.
|
|
437
|
+
* Does not remove slots or event listeners.
|
|
438
|
+
|
|
439
|
+
---
|
|
440
|
+
|
|
441
|
+
### 📦 `clearItems()`
|
|
442
|
+
|
|
443
|
+
Clears all items in **normal inventory slots**.
|
|
444
|
+
|
|
445
|
+
* Resets the `#items` array to empty.
|
|
446
|
+
* Does not affect special slots or event listeners.
|
|
447
|
+
|
|
448
|
+
---
|
|
449
|
+
|
|
450
|
+
### 🎯 `clearSpecialItems()`
|
|
451
|
+
|
|
452
|
+
Clears all items in **special slots**, without removing the slots themselves.
|
|
453
|
+
|
|
454
|
+
* Each slot remains defined, but its `item` property is set to `null`.
|
|
455
|
+
* Does not affect normal slots or event listeners.
|
|
456
|
+
|
|
457
|
+
---
|
|
458
|
+
|
|
459
|
+
## ➕ Item Management
|
|
460
|
+
|
|
461
|
+
### 📥 `addItem(options)`
|
|
462
|
+
|
|
463
|
+
Adds an item to the inventory, respecting:
|
|
464
|
+
|
|
465
|
+
* ✅ Stack limits
|
|
466
|
+
* ✅ Metadata equality
|
|
467
|
+
* ✅ Capacity (size, slots, weight)
|
|
468
|
+
|
|
469
|
+
```ts
|
|
470
|
+
addItem({
|
|
471
|
+
itemId: string,
|
|
472
|
+
quantity?: number,
|
|
473
|
+
metadata?: InventoryMetadata,
|
|
474
|
+
forceSpace?: boolean
|
|
475
|
+
}): AddItemResult
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
#### Behavior:
|
|
479
|
+
|
|
480
|
+
1. 🔄 **Fills existing stacks** first.
|
|
481
|
+
2. 📤 **Uses empty slots** if possible.
|
|
482
|
+
3. 🆕 **Pushes new stacks** at the end if needed.
|
|
483
|
+
|
|
484
|
+
📦 Returns `{ remaining, placesAdded }`.
|
|
485
|
+
⚠️ Throws if the item is not registered.
|
|
486
|
+
|
|
487
|
+
---
|
|
488
|
+
|
|
489
|
+
### 📤 `getItemFrom(slotIndex)`
|
|
490
|
+
|
|
491
|
+
Retrieves item from a slot.
|
|
492
|
+
|
|
493
|
+
* Returns a **clone** (safe copy).
|
|
494
|
+
* Throws if index is out of bounds.
|
|
495
|
+
|
|
496
|
+
---
|
|
497
|
+
|
|
498
|
+
### ✏️ `setItem(options)`
|
|
499
|
+
|
|
500
|
+
Directly sets or clears an item at a specific slot.
|
|
501
|
+
|
|
502
|
+
* Validates stack size & registry.
|
|
503
|
+
* Supports `null` for deletion.
|
|
504
|
+
* Triggers `"set"` event.
|
|
505
|
+
* Throws if invalid index or type.
|
|
506
|
+
|
|
507
|
+
---
|
|
508
|
+
|
|
509
|
+
### ❌ `deleteItem(slotIndex, forceSpace?)`
|
|
510
|
+
|
|
511
|
+
Shortcut for clearing a slot (`setItem({ item: null })`).
|
|
512
|
+
|
|
513
|
+
---
|
|
514
|
+
|
|
515
|
+
### 🔄 `moveItem(fromIndex, toIndex, forceSpace?)`
|
|
516
|
+
|
|
517
|
+
Moves an item from one slot to another.
|
|
518
|
+
|
|
519
|
+
* Throws if source is empty.
|
|
520
|
+
* Replaces destination if valid.
|
|
521
|
+
|
|
522
|
+
---
|
|
523
|
+
|
|
524
|
+
### 🗑️ `removeItem(options)`
|
|
525
|
+
|
|
526
|
+
Removes a quantity of a given item (normal slots + special slots).
|
|
527
|
+
|
|
528
|
+
```ts
|
|
529
|
+
removeItem({
|
|
530
|
+
itemId: string,
|
|
531
|
+
metadata?: InventoryMetadata | null,
|
|
532
|
+
quantity?: number
|
|
533
|
+
}): boolean
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
* ✅ Matches by `id` and optional `metadata`.
|
|
537
|
+
* ✅ Works across both normal slots and special slots.
|
|
538
|
+
* ✅ Triggers `"remove"` events.
|
|
539
|
+
* Returns `true` if fully removed, `false` if not enough items.
|
|
540
|
+
|
|
541
|
+
---
|
|
542
|
+
|
|
543
|
+
## 🎮 Item Usage & Special Slots
|
|
544
|
+
|
|
545
|
+
### 🧩 `#removeItemCallback(config)`
|
|
546
|
+
|
|
547
|
+
Creates a **removal callback** for normal or special slots.
|
|
548
|
+
|
|
549
|
+
```ts
|
|
550
|
+
#removeItemCallback({
|
|
551
|
+
locationType: "special" | "normal",
|
|
552
|
+
item: InventoryItem,
|
|
553
|
+
specialSlot?: string,
|
|
554
|
+
slotIndex?: number,
|
|
555
|
+
forceSpace?: boolean
|
|
556
|
+
}): (forceSpace?: boolean) => void
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
* Decrements item quantity.
|
|
560
|
+
* Clears slot if quantity reaches `0`.
|
|
561
|
+
* Returns a function that executes the removal.
|
|
562
|
+
|
|
563
|
+
---
|
|
564
|
+
|
|
565
|
+
### 🎯 `useItem({ slotIndex, specialSlot, forceSpace }, ...args)`
|
|
566
|
+
|
|
567
|
+
Uses an item from either a **normal slot** or a **special slot**.
|
|
568
|
+
|
|
569
|
+
✨ Behavior:
|
|
570
|
+
|
|
571
|
+
* Calls the item’s `onUse` callback if defined.
|
|
572
|
+
* Provides a `remove()` function inside the `onUse` context.
|
|
573
|
+
* Triggers the `"use"` event.
|
|
574
|
+
|
|
575
|
+
⚠️ Throws if the item doesn’t exist.
|
|
576
|
+
|
|
577
|
+
---
|
|
578
|
+
|
|
579
|
+
## 🛡️ Special Slot Management
|
|
580
|
+
|
|
581
|
+
### 🔍 `hasSpecialSlot(slotId)`
|
|
582
|
+
|
|
583
|
+
Checks if a special slot exists.
|
|
584
|
+
|
|
585
|
+
---
|
|
586
|
+
|
|
587
|
+
### 📦 `getSpecialItem(slotId)`
|
|
588
|
+
|
|
589
|
+
Gets the item from a special slot.
|
|
590
|
+
|
|
591
|
+
* Returns `null` if empty.
|
|
592
|
+
* Throws if slot doesn’t exist.
|
|
593
|
+
|
|
594
|
+
---
|
|
595
|
+
|
|
596
|
+
### 🏷️ `getSpecialSlotType(slotId)`
|
|
597
|
+
|
|
598
|
+
Gets the type/category of a special slot.
|
|
599
|
+
|
|
600
|
+
* Returns `null` if unrestricted.
|
|
601
|
+
|
|
602
|
+
---
|
|
603
|
+
|
|
604
|
+
### ✏️ `setSpecialSlot({ slotId, item, forceSpace })`
|
|
605
|
+
|
|
606
|
+
Sets or clears an item in a special slot.
|
|
607
|
+
|
|
608
|
+
* Validates item structure.
|
|
609
|
+
* Ensures registry + weight/space limits.
|
|
610
|
+
* Triggers `"set"` event.
|
|
611
|
+
|
|
612
|
+
---
|
|
613
|
+
|
|
614
|
+
### ❌ `deleteSpecialItem(slotId, forceSpace?)`
|
|
615
|
+
|
|
616
|
+
Clears a special slot (sets item to `null`).
|
|
617
|
+
|
|
618
|
+
---
|
|
619
|
+
|
|
620
|
+
### ⚔️ `equipItem({ slotId, slotIndex, quantity, forceSpace })`
|
|
621
|
+
|
|
622
|
+
Equips an item from the inventory into a special slot.
|
|
623
|
+
|
|
624
|
+
✨ Behavior:
|
|
625
|
+
|
|
626
|
+
1. **Same item equipped + stackable** → merge up to `maxStack`.
|
|
627
|
+
2. **Different item equipped** → unequips old item and equips the new one.
|
|
628
|
+
3. Returns leftover quantity (not equipped).
|
|
629
|
+
|
|
630
|
+
⚠️ Throws if slot does not exist, types mismatch, or insufficient quantity.
|
|
631
|
+
|
|
632
|
+
---
|
|
633
|
+
|
|
634
|
+
### 🛡️ `unequipItem({ slotId, quantity, forceSpace })`
|
|
635
|
+
|
|
636
|
+
Unequips an item from a special slot back to the inventory.
|
|
637
|
+
|
|
638
|
+
* If `quantity` is omitted → unequips the whole stack.
|
|
639
|
+
* Returns `true` if successful, `false` if empty.
|
|
640
|
+
* Throws if invalid quantity.
|
|
641
|
+
|
|
642
|
+
---
|
|
643
|
+
|
|
644
|
+
## 🛠️ Utility Methods
|
|
645
|
+
|
|
646
|
+
### 🧬 `#cloneItemData(item)`
|
|
647
|
+
|
|
648
|
+
Creates a **deep clone** of an item (copies metadata).
|
|
649
|
+
|
|
650
|
+
---
|
|
651
|
+
|
|
652
|
+
### 📋 `getItemList()`
|
|
653
|
+
|
|
654
|
+
Returns an array of all items with their slot indexes:
|
|
655
|
+
|
|
656
|
+
```ts
|
|
657
|
+
[ InventoryItem, index ][]
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
---
|
|
661
|
+
|
|
662
|
+
### 📦 `getAllItems()`
|
|
663
|
+
|
|
664
|
+
Returns all items in the inventory,
|
|
665
|
+
including **special slots**, excluding `null`s.
|
|
666
|
+
|
|
667
|
+
---
|
|
668
|
+
|
|
669
|
+
### 🔍 `getItemsByMetadata(filterFn)`
|
|
670
|
+
|
|
671
|
+
Finds items by metadata.
|
|
672
|
+
|
|
673
|
+
* `filterFn(itemTypeMetadata, itemInstance)` must return `true` to include item.
|
|
674
|
+
|
|
675
|
+
---
|
|
676
|
+
|
|
677
|
+
### 🔍 `findItem(predicate)`
|
|
678
|
+
|
|
679
|
+
Finds the **first item** matching the predicate.
|
|
680
|
+
|
|
681
|
+
---
|
|
682
|
+
|
|
683
|
+
### 🔍 `findItems(predicate)`
|
|
684
|
+
|
|
685
|
+
Finds **all items** matching the predicate.
|
|
686
|
+
|
|
687
|
+
---
|
|
688
|
+
|
|
689
|
+
### 🔢 `getItemCount(itemId)`
|
|
690
|
+
|
|
691
|
+
Counts the **total quantity** of a given item across the whole inventory.
|
|
692
|
+
|
|
693
|
+
---
|
|
694
|
+
|
|
695
|
+
### ✅ `hasItem(itemId, quantity = 1)`
|
|
696
|
+
|
|
697
|
+
Checks if the inventory has at least `quantity` of a given item.
|
|
698
|
+
|
|
699
|
+
---
|
|
700
|
+
|
|
701
|
+
### 📍 `existsItemAt(slotIndex)`
|
|
702
|
+
|
|
703
|
+
Checks if there is an item at a given **normal slot index**.
|
|
704
|
+
|
|
705
|
+
---
|
|
706
|
+
|
|
707
|
+
## 📝 Serialization & Cloning
|
|
708
|
+
|
|
709
|
+
### 🧬 `clone()`
|
|
710
|
+
|
|
711
|
+
Creates a **deep copy** of the entire inventory.
|
|
712
|
+
|
|
713
|
+
✨ Behavior:
|
|
714
|
+
|
|
715
|
+
* Copies all items, slots, and metadata.
|
|
716
|
+
* The clone is fully independent (changes won’t affect the original).
|
|
717
|
+
|
|
718
|
+
🔄 Returns:
|
|
719
|
+
|
|
720
|
+
* A new `TinyInventory` instance.
|
|
721
|
+
|
|
722
|
+
---
|
|
723
|
+
|
|
724
|
+
### 📦 `toObject()`
|
|
725
|
+
|
|
726
|
+
Creates a **plain JSON-safe object** representing the current inventory state.
|
|
727
|
+
|
|
728
|
+
✨ Behavior:
|
|
729
|
+
|
|
730
|
+
* Functions (like `onUse`) are **not serialized**.
|
|
731
|
+
* Only item data, slots, and metadata are saved.
|
|
732
|
+
|
|
733
|
+
🔄 Returns:
|
|
734
|
+
|
|
735
|
+
* A `SerializedInventory` object (safe to `JSON.stringify`).
|
|
736
|
+
|
|
737
|
+
---
|
|
738
|
+
|
|
739
|
+
### 📜 `toJSON(space = 0)`
|
|
740
|
+
|
|
741
|
+
Serializes the inventory into a **JSON string**.
|
|
742
|
+
|
|
743
|
+
* `space` controls pretty-print indentation (e.g., `2`).
|
|
744
|
+
|
|
745
|
+
🔄 Returns:
|
|
746
|
+
|
|
747
|
+
* A JSON string representing the inventory.
|
|
748
|
+
|
|
749
|
+
---
|
|
750
|
+
|
|
751
|
+
## 📥 Deserialization
|
|
752
|
+
|
|
753
|
+
### 🛠️ `static fromObject(obj)`
|
|
754
|
+
|
|
755
|
+
Rebuilds a `TinyInventory` instance from a plain object produced by `toObject()`.
|
|
756
|
+
|
|
757
|
+
✨ Behavior:
|
|
758
|
+
|
|
759
|
+
* Requires **item definitions** to already exist in the external registry.
|
|
760
|
+
* Validates schema (`__schema: "TinyInventory"`, `version: 1`).
|
|
761
|
+
* Restores normal slots and special slots.
|
|
762
|
+
|
|
763
|
+
⚠️ Throws if validation fails or schema is invalid.
|
|
764
|
+
|
|
765
|
+
🔄 Returns:
|
|
766
|
+
|
|
767
|
+
* A populated `TinyInventory` instance.
|
|
768
|
+
|
|
769
|
+
---
|
|
770
|
+
|
|
771
|
+
### 🔄 `static fromJSON(json)`
|
|
772
|
+
|
|
773
|
+
Rebuilds a `TinyInventory` from a JSON string produced by `toJSON()`.
|
|
774
|
+
|
|
775
|
+
🔄 Returns:
|
|
776
|
+
|
|
777
|
+
* A populated `TinyInventory` instance.
|