[Include] [INC]Player Inventory[v2] -- Now with Saving/Loading functions
#36

Here the code for the one who cannot download it:

pawn Code:
#define INV_DIALOG_ID 13337
#define MAX_ITEMS 99
#define MAX_ITEM_STACK 99
#define MAX_ITEM_NAME 128
forward OnPlayerUseItem(playerid,ItemName[]);
new gItemList[(MAX_ITEMS+1)*(MAX_ITEM_NAME+3)];
/*
native AddItem(playerid,ItemName[],Amount);
native RemoveItem(playerid,ItemName[],Amount);
native PlayerHasItem(playerid,ItemName[]);
native GetPlayerItemInfo(playerid,&idx,const ItemName[],len=sizeof(ItemName),&Amount);
native ResetPlayerInventory(playerid);
native ShowInventory(playerid);
native InventoryOnDialogResponse(playerid, dialogid, response, inputtext[]);
native SaveInventory(playerid);
native LoadInventory(playerid);
*/

stock _GetItemNamePVar(playerid,item)
{
    new tmp[32];
    new tmp2[MAX_ITEM_NAME];
    format(tmp,32,"PITEMNAME%d",item);
    GetPVarString(playerid,tmp,tmp2,MAX_ITEM_NAME);
    return tmp2;
}
stock _SetItemNamePVar(playerid,item,ItemName[])
{
    new tmp[MAX_ITEM_NAME];
    format(tmp,MAX_ITEM_NAME,"PITEMNAME%d",item);
    SetPVarString(playerid,tmp,ItemName);
}
stock _GetItemAmountPVar(playerid,item)
{
    new tmp[16];
    format(tmp,16,"PITEMAMOUNT%d",item);
    return GetPVarInt(playerid,tmp);
}
stock _SetItemAmountPVar(playerid,item,Amount)
{
    new tmp[16];
    format(tmp,16,"PITEMAMOUNT%d",item);
    SetPVarInt(playerid,tmp,Amount);
}
stock AddItem(playerid,ItemName[],Amount)
{
    new slot=-1;
    for(new item;item<MAX_ITEMS;item++)
    {
        if(!_GetItemAmountPVar(playerid,item))
        {
            if(slot==-1)slot=item;
            continue;
        }
        if(!strcmp(_GetItemNamePVar(playerid,item),ItemName,true))
        {
            _SetItemAmountPVar(playerid,item,_GetItemAmountPVar(playerid,item)+Amount);
            if(_GetItemAmountPVar(playerid,item)<=0)_SetItemAmountPVar(playerid,item,0);
            if(_GetItemAmountPVar(playerid,item)>MAX_ITEM_STACK)
            {
                _SetItemAmountPVar(playerid,item,MAX_ITEM_STACK);
                return 2;
            }
            return 1;
        }
    }
    if(slot>-1)
    {
        _SetItemNamePVar(playerid,slot,ItemName);
        _SetItemAmountPVar(playerid,slot,Amount);
        if(_GetItemAmountPVar(playerid,slot)>MAX_ITEM_STACK)
        {
            _SetItemAmountPVar(playerid,slot,MAX_ITEM_STACK);
            return 2;
        }
        return 1;
    }
    return 0;
}
stock RemoveItem(playerid,ItemName[],Amount)
{
    for(new item;item<MAX_ITEMS;item++)
    {
        if(!_GetItemAmountPVar(playerid,item))continue;
        if(!strcmp(_GetItemNamePVar(playerid,item),ItemName,true))
        {
            _SetItemAmountPVar(playerid,item,_GetItemAmountPVar(playerid,item)-Amount);
            if(_GetItemAmountPVar(playerid,item)<=0)_SetItemAmountPVar(playerid,item,0);
            if(_GetItemAmountPVar(playerid,item)>MAX_ITEM_STACK)
            {
                _SetItemAmountPVar(playerid,item,MAX_ITEM_STACK);
                return 2;
            }
            return 1;
        }
    }
    return 0;
}
stock PlayerHasItem(playerid,ItemName[])
{
    for(new item;item<MAX_ITEMS;item++)
    {
        if(!_GetItemAmountPVar(playerid,item))continue;
        if(!strcmp(_GetItemNamePVar(playerid,item),ItemName,false))return _GetItemAmountPVar(playerid,item);
    }
    return 0;
}
stock GetPlayerItemInfo(playerid,&idx,ItemName[],len=sizeof(ItemName),&Amount)
{
    if(idx>=MAX_ITEMS)return 0;
    format(ItemName,len,_GetItemNamePVar(playerid,idx));
    Amount=_GetItemAmountPVar(playerid,idx);
    idx++;
    return 1;
}
stock ResetPlayerInventory(playerid)
{
    for(new item;item<MAX_ITEMS;item++)_SetItemAmountPVar(playerid,item,0);
}
stock ShowInventory(playerid)
{
    gItemList="";
    for(new item;item<MAX_ITEMS;item++)
    {
        if(!strlen(_GetItemNamePVar(playerid,item))||!_GetItemAmountPVar(playerid,item))continue;
        format(gItemList,sizeof(gItemList),"%s\n%d\t\t%s",gItemList,_GetItemAmountPVar(playerid,item),_GetItemNamePVar(playerid,item));
    }
    format(gItemList,sizeof(gItemList),"Amount\t\tItem Name%s",gItemList);
    ShowPlayerDialog(playerid,INV_DIALOG_ID,DIALOG_STYLE_LIST,"Inventory",gItemList,"Use","Close");
    SetPVarInt(playerid,"PUSINGDIALOG",1);
}
stock SaveInventory(playerid)
{
    gItemList="";
    new filename[48];
    GetPlayerName(playerid,filename,24);
    format(filename,48,"Inventory/%s.inv",filename);
    new File:file=fopen(filename,io_write);
    for(new item;item<MAX_ITEMS;item++)
    {
        if(!strlen(_GetItemNamePVar(playerid,item))||!_GetItemAmountPVar(playerid,item))continue;
        format(gItemList,sizeof(gItemList),"%s%s\n%d\n",gItemList,_GetItemNamePVar(playerid,item),_GetItemAmountPVar(playerid,item));
    }
    fwrite(file,gItemList);
    fclose(file);
    GetPlayerName(playerid,filename,24);
    printf("[INV] %s[%d]'s inventory saved.",filename,playerid);
}
stock LoadInventory(playerid)
{
    new tstring[48];
    new tstring2[12];
    GetPlayerName(playerid,tstring,48);
    format(tstring,48,"Inventory/%s.inv",tstring);
    if(!fexist(tstring))return 0;
    new File:file=fopen(tstring,io_read);
    fread(file,tstring);
    while(tstring[0])
    {
        format(tstring,strlen(tstring),"%s",tstring); //Delete last character
        fread(file,tstring2);
        AddItem(playerid,tstring,strval(tstring2));
        fread(file,tstring);
    }
    fclose(file);
    GetPlayerName(playerid,tstring,24);

    return 1;
}
InventoryOnDialogResponse(playerid, dialogid, response, inputtext[])
{
    if(dialogid!=INV_DIALOG_ID)return 1;
    if(!GetPVarInt(playerid,"PUSINGDIALOG"))return 1;
    if(!response)return 1;
    if(!strcmp(inputtext,"Amount",true,6))
    {
        ShowInventory(playerid);
        return 1;
    }
    format(gItemList,MAX_ITEM_NAME,inputtext[strfind(inputtext,"\t")+2]);
    if(CallLocalFunction("OnPlayerUseItem","is",playerid,gItemList))ShowInventory(playerid);
    else SetPVarInt(playerid,"PUSINGDIALOG",0);
    return 1;
}
Reply


Messages In This Thread
[INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Joe Staff - 26.02.2010, 22:39
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by LuxurioN™ - 26.02.2010, 22:48
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by aspire5630 - 26.02.2010, 22:49
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 26.02.2010, 23:03
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Torran - 26.02.2010, 23:21
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MaykoX - 27.02.2010, 03:26
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by ViruZZzZ_ChiLLL - 27.02.2010, 06:12
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 27.02.2010, 06:56
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by bydraq10 - 27.02.2010, 07:24
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by armyoftwo - 05.03.2010, 14:32
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by aircombat - 05.03.2010, 15:17
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by cyber_punk - 07.03.2010, 04:02
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 07.03.2010, 06:54
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by cyber_punk - 08.03.2010, 12:31
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 08.03.2010, 15:45
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by king4you - 04.04.2010, 02:49
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MisterTickle - 27.04.2010, 03:04
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Peep - 27.04.2010, 13:58
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by yezizhu - 28.04.2010, 03:20
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MisterTickle - 01.05.2010, 06:37
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Aur0nX390 - 01.05.2010, 06:53
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 15.05.2010, 05:16
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 15.05.2010, 16:43
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MisterTickle - 19.05.2010, 03:56
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Stas92 - 19.05.2010, 13:08
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by agd555 - 20.05.2010, 15:56
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Her - 22.05.2010, 15:07
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 23.05.2010, 10:38
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Her - 23.05.2010, 12:54
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Owenlishious - 25.05.2010, 12:39
AW: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by 'Pawno. - 20.09.2012, 03:42
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Nexis - 20.09.2012, 09:42
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by NoahF - 20.09.2012, 10:06
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Joe Staff - 20.09.2012, 10:18
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Nexis - 20.09.2012, 12:22
Re : [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Naruto_Emilio - 20.09.2012, 12:29
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Nexis - 20.09.2012, 12:33
Re : [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Amine_Mejrhirrou - 20.10.2012, 01:19
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by ZayanImran - 29.12.2012, 11:51
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Sramm - 12.01.2013, 10:09
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by kyoto - 28.01.2013, 16:24
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Admigo - 17.03.2013, 13:59
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by por12802 - 29.03.2013, 05:50
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Juanxz - 15.06.2013, 15:22
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Supermaxultraswag - 28.12.2014, 17:31
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by MasonSFW - 22.07.2015, 08:20
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by SQLite - 28.12.2015, 23:56
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by jojo5528 - 01.09.2018, 14:20

Forum Jump:


Users browsing this thread: 50 Guest(s)