-
Notifications
You must be signed in to change notification settings - Fork 1
/
ninventory.inc
987 lines (830 loc) · 27.5 KB
/
ninventory.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/************************************************************
LICENSE
Script: NInventory
Type: Include
Version: Dev 1.1
Author: Nickk888
Date: 08.05.2022
************************************************************
Social Media
*************************************************************
YouTube: https://www.youtube.com/channel/UCvjbpjJXET1QS9Wa0_XVCuw
Facebook: https://www.facebook.com/Nickk888FP/
************************************************************
When using a Database eg. MySQL
*************************************************************
Simply create 2 tables in your Database, one for the "Server" Items and the second one
for the player Items. Look at the Example:
For Server Items:
CREATE TABLE `items` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(128) NULL DEFAULT NULL,
`description` VARCHAR(128) NULL DEFAULT NULL,
`stacksize` INT(11) NULL DEFAULT '1',
`category` INT(11) NULL DEFAULT '0',
`objectmodelid` INT(11) NULL DEFAULT '0',
`iconname` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`));
Use the AUTO INCREMENTed ID as the Item ID parameter so every item gets a unique Item ID.
For Player Items:
CREATE TABLE `player_items` (
`id` INT(11) NULL DEFAULT '0',
`userid` INT(11) NULL DEFAULT '0',
`itemid` INT(11) NULL DEFAULT '0',
`amount` INT(11) NULL DEFAULT '1',
PRIMARY KEY (`id`)
)
************************************************************
Functions
*************************************************************
************************************************************
Callbacks
*************************************************************
OnPlayerItemGet(playerid, Item:item, amount, remains);
************************************************************
Loops
*************************************************************
LOOP_ITEMS(variableName) - Loops through the server items, returns item as tag "Item".
LOOP_PLAYER_ITEMS(playerid, variableName) - Loops through the player items, returns item as tag "PlayerItem".
*************************************************************/
#if defined _nitems_included
#endinput
#endif
#define _nitems_included
/************************************************************
INCLUDES
*************************************************************/
#include <YSI_Data\y_foreach>
#include <YSI_Coding\y_hooks>
/************************************************************
FORWARDS
*************************************************************/
forward OnPlayerItemGet(playerid, Item:item, amount, remains, PlayerItem:updatedItems[], updatedItemsAmount);
forward OnPlayerItemRemoved(playerid, PlayerItem:pitem);
forward Item:CreateItem(const name[], const description[] = "", stacksize = 1, category = 0, objectmodelid = 0, dbid = 0, itemid = -1, const iconname[] = "");
forward Item:GetPlayerItemItem(playerid, PlayerItem:pitem);
forward Item:GetRandomItem();
forward Item:GetItemFromItemID(itemid);
forward Item:GetItemFromName(const name[], bool:ignorecase = true);
forward Item:GetItemFromIndex(index);
forward PlayerItem:SetPlayerItem(playerid, Item:item, slot, amount);
forward PlayerItem:GetPlayerItemFromItemID(playerid, itemid);
forward PlayerItem:GetPlayerItemFromIndex(playerid, index);
forward PlayerItem:GetRandomPlayerItem(playerid);
forward PlayerItem:GetPlayerItemFromName(playerid, const name[], bool:ignorecase = true);
forward Float:GetItemFloatData(Item:item, arrayId);
/************************************************************
DEFINITIONS
*************************************************************/
#if !defined MAX_SERVER_ITEMS
#define MAX_SERVER_ITEMS 100
#endif
#if !defined MAX_PLAYER_ITEMS
#define MAX_PLAYER_ITEMS 20
#endif
/* Loops through all Items returning the Item object. */
#define LOOP_ITEMS(%1) foreach(new Item:%1 : NI_I_ITEMS)
/* Loops through all player Items returning the PlayerItem object. */
#define LOOP_PLAYER_ITEMS(%1,%2) foreach(new PlayerItem:%2 : NI_I_PITEMS[%1])
/************************************************************
INTERNALS
*************************************************************/
#define NI_MAX_STRING_SIZE 128
#define NI_MAX_DATA_AMOUNT 20
#define NI_VALID_ITER_FREE(%1,%2) %2 >= 0 && %2 < Iter_End(%1)
/************************************************************
ITERATORS
*************************************************************/
new Iterator:NI_I_ITEMS<Item:MAX_SERVER_ITEMS>;
new Iterator:NI_I_PITEMS[MAX_PLAYERS]<PlayerItem:MAX_PLAYER_ITEMS>;
/************************************************************
ENUMERATORS
*************************************************************/
enum NI_E_ITEM // Stores Server Items
{
bool:ni_i_valid,
ni_i_itemid,
ni_i_dbId,
ni_i_name[NI_MAX_STRING_SIZE],
ni_i_desc[NI_MAX_STRING_SIZE],
ni_i_category,
ni_i_intData[NI_MAX_DATA_AMOUNT],
ni_i_objModelId,
ni_i_iconName[NI_MAX_STRING_SIZE],
ni_i_stackSize,
Float:ni_i_floatData[NI_MAX_DATA_AMOUNT]
};
new NI_Item[Item:MAX_SERVER_ITEMS][NI_E_ITEM];
enum NI_E_PLAYERITEM // Stores Player Items
{
bool:ni_pi_valid,
ni_pi_dbId,
ni_pi_itemId,
Item:ni_pi_item,
ni_pi_amount
};
new NI_PlayerItem[MAX_PLAYERS][PlayerItem:MAX_PLAYER_ITEMS][NI_E_PLAYERITEM];
/************************************************************
CALLBACKS
*************************************************************/
hook OnScriptInit()
{
Iter_Init(PlayerItem:NI_I_PITEMS);
return 1;
}
/************************************************************
FUNCTIONS
*************************************************************/
/* Creates new item returning the item object */
stock Item:CreateItem(const name[], const description[] = "", stacksize = 1, category = 0, objectmodelid = 0, dbid = 0, itemid = -1, const iconname[] = "")
{
new Item:item = Item:Iter_Free(NI_I_ITEMS);
if(NI_VALID_ITER_FREE(NI_I_ITEMS, _:item))
{
Iter_Add(NI_I_ITEMS, item);
NI_Item[item][ni_i_valid] = true;
NI_Item[item][ni_i_itemid] = ((itemid == -1) ? (_:item + 1) : (itemid));
NI_Item[item][ni_i_dbId] = dbid;
NI_Item[item][ni_i_category] = category;
NI_Item[item][ni_i_objModelId] = objectmodelid;
NI_Item[item][ni_i_stackSize] = ((stacksize > 0) ? (stacksize) : (1));
strpack(NI_Item[item][ni_i_name], name, 128);
strpack(NI_Item[item][ni_i_desc], description, 128);
strpack(NI_Item[item][ni_i_iconName], iconname, 128);
for(new i; i < NI_MAX_DATA_AMOUNT; i++)
{
NI_Item[item][ni_i_intData][i] = 0;
NI_Item[item][ni_i_floatData][i] = 0.0;
}
#if defined NI_DEBUG
printf("[NINVENTORY - DEBUG] Created Item '%s', Description '%s', ItemID '%i', Stacksize '%i', Index '%i'",
name,
description,
NI_Item[item][ni_i_itemid],
NI_Item[item][ni_i_stackSize],
_:item);
#endif
return item;
}
return Item:-1;
}
/* Gets item float data inside the Item */
stock Float:GetItemFloatData(Item:item, arrayId)
{
if(!IsValidItem(item) || arrayId < 0 || arrayId >= NI_MAX_DATA_AMOUNT)
return Float:0.0;
return NI_Item[item][ni_i_floatData][arrayId];
}
/* Gets item intiger data inside the Item */
stock GetItemIntData(Item:item, arrayId)
{
if(!IsValidItem(item) || arrayId < 0 || arrayId >= NI_MAX_DATA_AMOUNT)
return 0;
return NI_Item[item][ni_i_intData][arrayId];
}
/* Gets item icon name inside the Item */
stock GetItemIconName(Item:item)
{
new unpackedItemStr[128];
if(!IsValidItem(item))
return unpackedItemStr;
strunpack(unpackedItemStr, NI_Item[_:item][ni_i_iconName], 128);
return unpackedItemStr;
}
/* Gets item description inside the Item */
stock GetItemDescription(Item:item)
{
new unpackedItemStr[128];
if(!IsValidItem(item))
return unpackedItemStr;
strunpack(unpackedItemStr, NI_Item[item][ni_i_desc], 128);
return unpackedItemStr;
}
/* Gets item name inside the Item */
stock GetItemName(Item:item)
{
new unpackedItemStr[128];
if(!IsValidItem(item))
return unpackedItemStr;
strunpack(unpackedItemStr, NI_Item[item][ni_i_name], 128);
return unpackedItemStr;
}
/* Gets item category ID inside the Item */
stock GetItemCategory(Item:item)
{
if(!IsValidItem(item))
return 0;
return NI_Item[item][ni_i_category];
}
/* Gets item stack size inside the Item */
stock GetItemStackSize(Item:item)
{
if(!IsValidItem(item))
return 0;
return NI_Item[item][ni_i_stackSize];
}
/* Sets new item icon name inside the Item */
stock SetPlayerItemDatabaseID(playerid, PlayerItem:pitem, dbid)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
NI_PlayerItem[playerid][pitem][ni_pi_dbId] = dbid;
return 1;
}
/* Gets the item ID from player inventory */
stock GetPlayerItemID(playerid, PlayerItem:pitem)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
return NI_PlayerItem[playerid][pitem][ni_pi_itemId];
}
/* Returns the Player items icon name. */
stock GetPlayerItemIconName(playerid, PlayerItem:pitem)
{
new unpackedItemStr[128];
new Item:item = GetPlayerItemItem(playerid, pitem);
if(!IsValidItem(item))
return unpackedItemStr;
strunpack(unpackedItemStr, NI_Item[item][ni_i_iconName], 128);
return unpackedItemStr;
}
/* Returns the Player items name. */
stock GetPlayerItemName(playerid, PlayerItem:pitem)
{
new unpackedItemStr[128];
new Item:item = GetPlayerItemItem(playerid, pitem);
if(!IsValidItem(item))
return unpackedItemStr;
strunpack(unpackedItemStr, NI_Item[item][ni_i_name], 128);
return unpackedItemStr;
}
/* Returns the Player items description. */
stock GetPlayerItemDescription(playerid, PlayerItem:pitem)
{
new unpackedItemStr[128];
new Item:item = GetPlayerItemItem(playerid, pitem);
if(!IsValidItem(item))
return unpackedItemStr;
strunpack(unpackedItemStr, NI_Item[item][ni_i_desc], 128);
return unpackedItemStr;
}
/* Returns the player items database ID, good for storing and reading in a MySQL Database. */
stock GetPlayerItemDatabaseID(playerid, PlayerItem:pitem)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
return NI_PlayerItem[playerid][pitem][ni_pi_dbId];
}
/* Returns the player items integer data from the array. */
stock GetPlayerItemIntData(playerid, PlayerItem:pitem, arrayId)
{
return GetItemIntData(GetPlayerItemItem(playerid, pitem), arrayId);
}
/* Returns the player items floating point data from the array. */
stock Float:GetPlayerItemFloatData(playerid, PlayerItem:pitem, arrayId)
{
return GetItemFloatData(GetPlayerItemItem(playerid, pitem), arrayId);
}
/* Returns the player items category. */
stock GetPlayerItemCategory(playerid, PlayerItem:pitem)
{
return GetItemCategory(GetPlayerItemItem(playerid, pitem));
}
/* Returns the Player items stack size. */
stock GetPlayerItemStackSize(playerid, PlayerItem:pitem)
{
return GetItemStackSize(GetPlayerItemItem(playerid, pitem));
}
/* Returns the amount of server items created. */
stock CountItems()
{
return Iter_Count(NI_I_ITEMS);
}
/* Returns the amount of items in the Player Inventory. */
stock CountPlayerItems(playerid)
{
if(!IsPlayerConnected(playerid))
return 0;
return Iter_Count(NI_I_PITEMS[playerid]);
}
/* Gets the item amount from player inventory. */
stock GetPlayerItemAmount(playerid, PlayerItem:pitem)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
return NI_PlayerItem[playerid][pitem][ni_pi_amount];
}
/* Gets the item object from the player item object */
stock Item:GetPlayerItemItem(playerid, PlayerItem:pitem)
{
if(!IsValidPlayerItem(playerid, pitem))
return Item:-1;
if(!IsValidItem(NI_PlayerItem[playerid][pitem][ni_pi_item]))
return Item:-1;
return NI_PlayerItem[playerid][pitem][ni_pi_item];
}
/* Gets Object Model ID inside the Item */
stock GetItemModelID(Item:item)
{
if(!IsValidItem(item))
return 0;
return NI_Item[item][ni_i_objModelId];
}
/* Gets Object Model ID inside the Player Item. */
stock GetPlayerItemModelID(playerid, PlayerItem:pitem)
{
return GetItemModelID(GetPlayerItemItem(playerid, pitem));
}
/* Gets Database ID inside the Item */
stock GetItemDatabaseID(Item:item)
{
if(!IsValidItem(item))
return 0;
return NI_Item[item][ni_i_dbId];
}
/* Gets Item ID inside the Item */
stock GetItemItemID(Item:item)
{
if(!IsValidItem(item))
return 0;
return NI_Item[item][ni_i_itemid];
}
/* Sets new item float data inside the Item */
stock SetItemFloatData(Item:item, arrayId, Float:floatData)
{
if(!IsValidItem(item) || arrayId < 0 || arrayId >= NI_MAX_DATA_AMOUNT)
return 0;
NI_Item[item][ni_i_floatData][arrayId] = floatData;
return 1;
}
/* Sets new item intiger data inside the Item */
stock SetItemIntData(Item:item, arrayId, intData)
{
if(!IsValidItem(item) || arrayId < 0 || arrayId >= NI_MAX_DATA_AMOUNT)
return 0;
NI_Item[item][ni_i_intData][arrayId] = intData;
return 1;
}
/* Sets new item icon name inside the Item */
stock SetItemIconName(Item:item, const newiconname[])
{
if(!IsValidItem(item))
return 0;
strpack(NI_Item[item][ni_i_iconName], newiconname, 128);
return 1;
}
/* Sets new item description inside the Item */
stock SetItemDescription(Item:item, const newdescription[])
{
if(!IsValidItem(item))
return 0;
strpack(NI_Item[item][ni_i_desc], newdescription, 128);
return 1;
}
/* Sets new item name inside the Item */
stock SetItemName(Item:item, const newname[])
{
if(!IsValidItem(item))
return 0;
strpack(NI_Item[item][ni_i_name], newname, 128);
return 1;
}
/* Sets new item category inside the Item */
stock SetItemCategory(Item:item, categoryid)
{
if(!IsValidItem(item))
return 0;
NI_Item[item][ni_i_category] = categoryid;
return 1;
}
/* Sets new item stack size inside the Item */
stock SetItemStackSize(Item:item, newstacksize)
{
if(!IsValidItem(item))
return 0;
NI_Item[item][ni_i_stackSize] = newstacksize;
return 1;
}
/* Sets new Object Model ID inside the Item */
stock SetItemModelID(Item:item, newmodelid)
{
if(!IsValidItem(item))
return 0;
NI_Item[item][ni_i_objModelId] = newmodelid;
return 1;
}
/* Sets new Database ID inside the Item */
stock SetItemDatabaseID(Item:item, newdbid)
{
if(!IsValidItem(item))
return 0;
NI_Item[item][ni_i_dbId] = newdbid;
return 1;
}
/* Sets new Item ID inside the Item */
stock SetItemItemID(Item:item, newItemID)
{
if(!IsValidItem(item))
return 0;
NI_Item[item][ni_i_itemid] = newItemID;
return 1;
}
/* Checks if item exists and is valid */
stock IsValidItem(Item:item)
{
if(_:item >= 0 && _:item < MAX_SERVER_ITEMS && NI_Item[item][ni_i_valid])
{
return 1;
}
return 0;
}
/* Gets Item object from itemid */
stock Item:GetItemFromItemID(itemid)
{
LOOP_ITEMS(i)
{
if(NI_Item[i][ni_i_valid] && NI_Item[i][ni_i_itemid] == itemid)
{
return Item:i;
}
}
return Item:-1;
}
/* Gets Item object from name */
stock Item:GetItemFromName(const name[], bool:ignorecase = true)
{
LOOP_ITEMS(i)
{
if(!strcmp(GetItemName(i), name, ignorecase))
{
return i;
}
}
return Item:-1;
}
/* Checks if item ID exists and is valid */
stock IsValidItemID(itemid)
{
LOOP_ITEMS(i)
{
if(NI_Item[i][ni_i_valid] && NI_Item[i][ni_i_itemid] == itemid)
{
return 1;
}
}
return 0;
}
/* Checks if a Player Item exists passing the name. */
stock PlayerItemExistsByName(playerid, const name[], bool:ignorecase = true)
{
LOOP_PLAYER_ITEMS(playerid, i)
{
if(NI_PlayerItem[playerid][i][ni_pi_valid] && !strcmp(GetPlayerItemName(playerid, i), name, ignorecase))
{
return 1;
}
}
return 0
}
/* Checks if a Player Item exists passing the item ID. */
stock PlayerItemExistsByItemID(playerid, itemid)
{
LOOP_PLAYER_ITEMS(playerid, i)
{
if(NI_PlayerItem[playerid][i][ni_pi_valid] && NI_PlayerItem[playerid][i][ni_pi_itemId] == itemid)
{
return 1;
}
}
return 0
}
/* Gets Player Item object from itemid */
stock PlayerItem:GetPlayerItemFromItemID(playerid, itemid)
{
LOOP_PLAYER_ITEMS(playerid, i)
{
if(NI_PlayerItem[playerid][i][ni_pi_valid] && NI_PlayerItem[playerid][i][ni_pi_itemId] == itemid)
{
return i;
}
}
return PlayerItem:-1;
}
/* Gets a Player Item object from the name. */
stock PlayerItem:GetPlayerItemFromName(playerid, const name[], bool:ignorecase = true)
{
LOOP_PLAYER_ITEMS(playerid, i)
{
if(NI_PlayerItem[playerid][i][ni_pi_valid] && !strcmp(GetPlayerItemName(playerid, i), name, ignorecase))
{
return i;
}
}
return PlayerItem:-1;
}
/* Checks if player item ID exists and is valid */
stock IsValidPlayerItem(playerid, PlayerItem:item)
{
if(_:item >= 0 && _:item < MAX_PLAYER_ITEMS && NI_PlayerItem[playerid][item][ni_pi_valid])
{
return 1;
}
return 0;
}
/* Checks if item exists using the name */
stock ItemNameExists(const name[], bool:ignorecase = true)
{
LOOP_ITEMS(i)
{
if(!strcmp(GetItemName(i), name, ignorecase))
{
return 1;
}
}
return 0;
}
/* Removes server item */
stock RemoveItem(Item:item)
{
if(!IsValidItem(item))
return 0;
if(Iter_Remove(NI_I_ITEMS, item))
{
NI_Item[item][ni_i_valid] = false;
return 1;
}
return 0;
}
/* Safely removes server item, mostly used in iterators */
stock SafeRemoveItem(Item:item, & Item:nextItem)
{
if(!IsValidItem(item))
return 0;
new next;
if(Iter_SafeRemove(NI_I_ITEMS, item, next))
{
nextItem = Item:next;
NI_Item[item][ni_i_valid] = false;
return 1;
}
return 0;
}
/* Removes player item */
stock RemovePlayerItem(playerid, PlayerItem:pitem, bool:callCallback = true)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
if(Iter_Remove(NI_I_PITEMS[playerid], pitem))
{
if(callCallback)
CallLocalFunction("OnPlayerItemRemoved", "ii", playerid, _:pitem);
NI_PlayerItem[playerid][pitem][ni_pi_valid] = false;
return 1;
}
return 0;
}
/* Safely removes player item, mostly used in iterators */
stock SafeRemovePlayerItem(playerid, PlayerItem:pitem, & PlayerItem:nextItem, bool:callCallback = true)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
new next;
if(Iter_SafeRemove(NI_I_PITEMS[playerid], pitem, next))
{
nextItem = PlayerItem:next;
if(callCallback)
CallLocalFunction("OnPlayerItemRemoved", "ii", playerid, _:pitem);
NI_PlayerItem[playerid][pitem][ni_pi_valid] = false;
return 1;
}
return 0;
}
/* Transfers Player Item from player A to player B */
stock TransferPlayerItemToPlayer(playeridA, playeridB, PlayerItem:item, amount)
{
if(amount <= 0 || !IsPlayerConnected(playeridA) || !IsPlayerConnected(playeridB))
return 0;
if(!IsValidPlayerItem(playeridA, item))
return 0;
if(GetPlayerItemAmount(playeridA, item) >= amount)
{
if(TakePlayerItem(playeridA, item, amount))
{
GivePlayerItem(playeridB, GetPlayerItemItem(playeridA, item), amount);
}
return 1;
}
return 0;
}
/* Gets the PlayerItem from the Iterator from the given Index. */
stock Item:GetItemFromIndex(index)
{
new Item:item = Item:Iter_Index(NI_I_ITEMS, Item:index);
if(!IsValidItem(item))
return Item:-1;
return item;
}
/* Gets the PlayerItem from the Iterator from the given Index. */
stock PlayerItem:GetPlayerItemFromIndex(playerid, index)
{
new PlayerItem:pitem = PlayerItem:Iter_Index(NI_I_PITEMS[playerid], PlayerItem:index);
if(!IsValidPlayerItem(playerid, pitem))
return PlayerItem:-1;
return pitem;
}
/* Take player item with amount */
stock TakePlayerItem(playerid, PlayerItem:pitem, amount)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
if(GetPlayerItemAmount(playerid, pitem) >= amount)
{
NI_PlayerItem[playerid][pitem][ni_pi_amount] -= amount;
if(NI_PlayerItem[playerid][pitem][ni_pi_amount] == 0)
{
RemovePlayerItem(playerid, pitem);
}
return 1;
}
return 0;
}
/* Savely takes player item with amount. Used in loops. */
stock SafeTakePlayerItem(playerid, PlayerItem:pitem, amount, & PlayerItem:nextItem)
{
if(!IsValidPlayerItem(playerid, pitem))
return 0;
if(GetPlayerItemAmount(playerid, pitem) >= amount)
{
NI_PlayerItem[playerid][pitem][ni_pi_amount] -= amount;
if(NI_PlayerItem[playerid][pitem][ni_pi_amount] == 0)
{
SafeRemovePlayerItem(playerid, pitem, nextItem);
}
else nextItem = PlayerItem:Iter_Next(NI_I_PITEMS[playerid], pitem);
return 1;
}
return 0;
}
/* Returns a random player Item */
stock PlayerItem:GetRandomPlayerItem(playerid)
{
if(!IsPlayerConnected(playerid))
return PlayerItem:-1;
return PlayerItem:Iter_Random(NI_I_PITEMS[playerid]);
}
/* Returns a random Item */
stock Item:GetRandomItem() {
return Item:Iter_Random(NI_I_ITEMS);
}
/* Clears player inventory */
stock ClearPlayerItems(playerid)
{
LOOP_PLAYER_ITEMS(playerid, i)
{
NI_PlayerItem[playerid][i][ni_pi_valid] = false;
}
Iter_Clear(NI_I_PITEMS[playerid]);
return 1;
}
stock PlayerItem:SetPlayerItem(playerid, Item:item, slot, amount)
{
if(!IsValidItem(item))
return PlayerItem:-1;
// Gets the item ID from the Item object.
new itemid = GetItemItemID(item);
// Gets the stacksize from the Item object.
new stacksize = GetItemStackSize(item);
new PlayerItem:pitem = PlayerItem:slot;
if(NI_VALID_ITER_FREE(NI_I_PITEMS[playerid], _:pitem))
{
if(NI_PlayerItem[playerid][pitem][ni_pi_valid] == false)
{
// Create the new inventory slot using the free slot ID.
Iter_Add(NI_I_PITEMS[playerid], PlayerItem:pitem);
// Insert data to the inventory slot.
NI_PlayerItem[playerid][pitem][ni_pi_valid] = true;
NI_PlayerItem[playerid][pitem][ni_pi_item] = item;
NI_PlayerItem[playerid][pitem][ni_pi_amount] = clamp(amount, 0, stacksize);
NI_PlayerItem[playerid][pitem][ni_pi_itemId] = itemid;
NI_PlayerItem[playerid][pitem][ni_pi_dbId] = -1;
return PlayerItem:pitem;
}
}
return PlayerItem:-1;
}
/* Gives player an item with amount */
stock GivePlayerItem(playerid, Item:item, amount, bool:callCallback = true)
{
// Check if the item object is valid.
if(!IsValidItem(item))
return 0;
// Save the amount to insert in cache.
new amountCache = amount;
// Amount counts how much items could be added to inventory.
new amountGot = 0;
// Gets the item ID from the Item object.
new itemid = GetItemItemID(item);
// Gets the stacksize from the Item object.
new stacksize = GetItemStackSize(item);
// Check if stacksize is valid.
if(stacksize <= 0)
return 0;
// Variables for identifying updated items returned in the callback
// Good for a saving system like MySQL
new PlayerItem:currentItem = PlayerItem:-1;
new updatedItemsAmount = 0;
new PlayerItem:updatedItems[MAX_PLAYER_ITEMS];
/******************************************************
Try adding item to existing items in the inventory
*******************************************************/
LOOP_PLAYER_ITEMS(playerid, i)
{
// When item has been found.
if(NI_PlayerItem[playerid][i][ni_pi_itemId] == itemid)
{
// Try inserting item to current slot.
while(amountCache)
{
// Check if there is place to insert the item in current slot.
if(NI_PlayerItem[playerid][i][ni_pi_amount] < stacksize)
{
// Add the item amount to current slot.
NI_PlayerItem[playerid][i][ni_pi_amount]++;
amountCache--;
amountGot++;
if(currentItem != i)
{
updatedItemsAmount++;
currentItem = i;
}
updatedItems[updatedItemsAmount - 1] = i;
}
// Exit loop when not possible to insert.
else
{
break;
}
}
}
}
/******************************************************
Add item as new when there's no free slot available.
*******************************************************/
// Creates an invalid PlayerItem object.
new PlayerItem:pitem = PlayerItem:-1;
// Try adding the amount to new slot/s
while(amountCache)
{
// Check if PlayerItem object is Invalid
if(pitem == PlayerItem:-1)
{
// Try getting a free inventory slot.
pitem = PlayerItem:Iter_Free(NI_I_PITEMS[playerid]);
}
// Check if the free inventory slot is valid (Eg. The player has full inventory.)
if(NI_VALID_ITER_FREE(NI_I_PITEMS[playerid], _:pitem))
{
// Create the new inventory slot using the free slot ID.
Iter_Add(NI_I_PITEMS[playerid], PlayerItem:pitem);
// Insert data to the inventory slot.
NI_PlayerItem[playerid][pitem][ni_pi_valid] = true;
NI_PlayerItem[playerid][pitem][ni_pi_item] = item;
NI_PlayerItem[playerid][pitem][ni_pi_amount] = 0;
NI_PlayerItem[playerid][pitem][ni_pi_itemId] = itemid;
NI_PlayerItem[playerid][pitem][ni_pi_dbId] = -1;
// Try inserting how much you can to the current slot.
while(amountCache)
{
// Is there room to insert the item?
if(NI_PlayerItem[playerid][pitem][ni_pi_amount] < stacksize)
{
// Insert the amount to the current slot.
NI_PlayerItem[playerid][pitem][ni_pi_amount]++;
amountCache--;
amountGot++;
if(currentItem != pitem)
{
updatedItemsAmount++;
currentItem = pitem;
}
updatedItems[updatedItemsAmount - 1] = pitem;
}
// Exit loop when no room inside the slot.
else
{
break;
}
}
// Resetting the object variable to be invalid so the loop can repeat creating a new slot
// when current slot is full
pitem = PlayerItem:-1;
}
// Exit insert loop when no new slot could be created.
else break;
}
// Call "OnPlayerItemGet" callback inserting the ID of the player, the Item object, the amount
// which has been added and the remainig amount which couldn't be added.
if(callCallback)
CallLocalFunction("OnPlayerItemGet", "iiiiad", playerid, _:item, amountGot, amountCache, _:updatedItems, updatedItemsAmount);
return 1;
}