-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinventory.inc
296 lines (220 loc) · 7.72 KB
/
inventory.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// built-in include guard removal
// just in case the user has a local dependency with the same file name
#if defined _inc_inventory
#undef _inc_inventory
#endif
// custom include-guard to ensure we don't duplicate
#if defined _inventory_included
#endinput
#endif
#define _inventory_included
#include <a_samp>
#include <logger>
#include <item>
#include <container>
#include <YSI_Coding\y_timers>
#include <YSI_Coding\y_hooks>
// Maximum amount of item slots available in a player inventory.
#if !defined MAX_INVENTORY_SLOTS
#define MAX_INVENTORY_SLOTS (4)
#endif
// Adds specified item to players inventory and removes item from world. `call`
// determines whether or not to call OnItemAddToInventory. Returns 0 on success.
// If item doesn't fit, returns a positive integer representing required item
// slots, otherwise -1 if itemid invalid, -2 if item is already in container, -4
// if OnItemAddToInventory returned non-zero, -5 if item is in the inventory of
// another player, -6 if item is inside a container from the container package.
forward AddItemToInventory(playerid, Item:itemid, call = 1);
// Removes the item from the specified slot if there is one. If `call` is true,
// will call OnItemRemoveFromInventory.
forward RemoveItemFromInventory(playerid, slotid, call = 1);
// GetInventorySlotItem returns the ID handle of the item stored in the
// specified slot.
forward GetInventorySlotItem(playerid, slotid, &Item:itemid);
// IsInventorySlotUsed checks if the specified inventory slot contains an item.
forward bool:IsInventorySlotUsed(playerid, slotid);
// IsPlayerInventoryFull checks if a players inventory is full.
forward bool:IsPlayerInventoryFull(playerid);
// IsPlayerInventoryEmpty checks if a players inventory is empty.
forward bool:IsPlayerInventoryEmpty(playerid);
// InventoryFitsItemType checks if an item type will fit into a player's
// inventory.
forward InventoryFitsItemType(playerid, ItemType:itemtype, &bool:result);
// GetInventoryFreeSlots returns the amount of free slots in a player's
// inventory.
forward GetInventoryFreeSlots(playerid, &slots);
// GetItemInventoryPlayer returns the ID of a player if itemid is stored in
// their inventory.
forward GetItemInventoryPlayer(Item:itemid, &playerid);
// GetItemPlayerInventorySlot returns inventory slot of an item if it's stored
// inside a player's inventory.
forward GetItemPlayerInventorySlot(Item:itemid, &slot);
// SetPlayerInventorySize sets the maximum amount of slots a player's inventory
// has.
forward SetPlayerInventorySize(playerid, size);
// GetPlayerInventorySize returns the capacity of a player's inventory.
forward GetPlayerInventorySize(playerid, &size);
// OnItemAddToInventory is called Before an item is added to a player's
// inventory by the function `AddItemToInventory`. Return 1 to cancel.
forward OnItemAddToInventory(playerid, itemid);
// After an item is added to a player's inventory by the function
// `AddItemToInventory`.
forward OnItemAddedToInventory(playerid, itemid);
// Before an item is removed from a player's inventory by the function
// `RemoveItemFromInventory`. Return 1 to cancel.
forward OnItemRemoveFromInventory(playerid, slot, Item:itemid);
// Called after an item is removed from a player's inventory by the function
// `RemoveItemFromInventory`.
forward OnItemRemovedFromInventory(playerid, slot, Item:itemid);
static
Container:inv_Container[MAX_PLAYERS] = {INVALID_CONTAINER_ID, ...},
inv_ContainerPlayer[MAX_CONTAINER] = {INVALID_PLAYER_ID, ...};
stock AddItemToInventory(playerid, Item:itemid, call = 1) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(call) {
if(CallLocalFunction("OnItemAddToInventory", "dd", playerid, _:itemid)) {
return 2;
}
}
new ret = AddItemToContainer(inv_Container[playerid], itemid, playerid, call);
if(ret) {
return ret;
}
if(call) {
CallLocalFunction("OnItemAddedToInventory", "dd", playerid, _:itemid);
}
return 0;
}
stock RemoveItemFromInventory(playerid, slotid, call = 1) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
new Item:itemid;
GetInventorySlotItem(playerid, slotid, itemid);
if(call) {
if(CallLocalFunction("OnItemRemoveFromInventory", "ddd", playerid, slotid, _:itemid)) {
return 0;
}
}
new ret = RemoveItemFromContainer(inv_Container[playerid], slotid, playerid, call);
if(ret) {
return ret;
}
if(call) {
CallLocalFunction("OnItemRemovedFromInventory", "ddd", playerid, slotid, _:itemid);
}
return 1;
}
stock GetInventorySlotItem(playerid, slotid, &Item:itemid) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
return GetContainerSlotItem(inv_Container[playerid], slotid, itemid);
}
stock bool:IsInventorySlotUsed(playerid, slotid) {
if(!IsPlayerConnected(playerid)) {
return false;
}
return IsContainerSlotUsed(inv_Container[playerid], slotid);
}
stock bool:IsPlayerInventoryFull(playerid) {
if(!IsPlayerConnected(playerid)) {
return false;
}
return IsContainerFull(inv_Container[playerid]);
}
stock bool:IsPlayerInventoryEmpty(playerid) {
if(!IsPlayerConnected(playerid)) {
return false;
}
return IsContainerEmpty(inv_Container[playerid]);
}
stock InventoryFitsItemType(playerid, ItemType:itemtype, &bool:result) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
return ContainerFitsItemType(inv_Container[playerid], itemtype, result);
}
stock GetInventoryFreeSlots(playerid, &slots) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
return GetContainerFreeSlots(inv_Container[playerid], slots);
}
stock GetItemInventoryPlayer(Item:itemid, &playerid) {
if(!IsValidItem(itemid)) {
return 1;
}
new Container:containerid;
new ret = GetItemContainer(itemid, containerid);
if(ret) {
return ret;
}
if(!IsValidContainer(containerid)) {
return 2;
}
playerid = inv_ContainerPlayer[containerid];
return 0;
}
stock GetItemPlayerInventorySlot(Item:itemid, &slot) {
return GetItemContainerSlot(itemid, slot);
}
stock SetPlayerInventorySize(playerid, size) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
return SetContainerSize(inv_Container[playerid], size);
}
stock GetPlayerInventorySize(playerid, &size) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
return GetContainerSize(inv_Container[playerid], size);
}
/*==============================================================================
Internal Functions and Hooks
==============================================================================*/
_inventory_playerSetup(playerid) {
if(IsValidContainer(inv_Container[playerid])) {
Logger_Err("attempted to create inventory container for player and container already exists",
Logger_I("playerid", playerid),
Logger_I("containerid", _:inv_Container[playerid]));
}
new Container:containerid = CreateContainer("Inventory", MAX_INVENTORY_SLOTS);
if(!IsValidContainer(containerid)) {
Logger_Err("failed to create inventory container for player",
Logger_I("playerid", playerid),
Logger_I("containerid", _:containerid));
return;
}
inv_Container[playerid] = containerid;
inv_ContainerPlayer[containerid] = playerid;
}
timer _inventory_tearDown[1](playerid) {
if(!IsValidContainer(inv_Container[playerid])) {
Logger_Err("attempted to delete inventory container for player and container does not exist",
Logger_I("playerid", playerid));
}
new ret = DestroyContainer(inv_Container[playerid]);
if(ret != 0) {
Logger_Err("failed to destroy inventory container for player",
Logger_I("playerid", playerid),
Logger_I("return", ret));
}
inv_ContainerPlayer[inv_Container[playerid]] = INVALID_PLAYER_ID;
inv_Container[playerid] = INVALID_CONTAINER_ID;
}
hook OnFilterScriptInit() {
foreach(new i : Player) {
_inventory_playerSetup(i);
}
}
hook OnPlayerConnect(playerid) {
_inventory_playerSetup(playerid);
return;
}
hook OnPlayerDisconnect(playerid) {
defer _inventory_tearDown(playerid);
}