Items will be grouped by names and amount (x1, x2, x3).
I want to be this every one under one.
For example, added item 'Colt'.
After gave player two should be Colt and second Colt under leatest.
Код:
#include a_samp.inc
#if defined _itemstreamer_included
#endinput
#endif
#define _itemstreamer_included
/*
native AddItem(itemid, name[]);
native DeleteItem(itemid);
native GetItemName(itemid);
native
native GivePlayerItem(playerid, itemid, amount);
native SetPlayerItem(playerid, itemid, amount);
native ResetPlayerItem(playerid, itemid);
native IsValidItem(itemid);
native GetPlayerItemAmount(playerid, itemid);
native
native GetPlayerItems(playerid, &index, &item, &amount);
native
native RemoveNewLine(string[]);
native NickToFilename(nick[]);
native str_replace(needle[], replace[], haystack[])
*/
#if !defined MAX_ITEM_ID
#define MAX_ITEM_ID (500)
#endif
#define MAX_ITEM_NAME (100)
#define INVALID_ITEM_ID (-1)
static item_string[128];
static item_playername[MAX_PLAYERS][MAX_PLAYER_NAME + 26];
static File:item_file;
public OnPlayerConnect(playerid)
{
GetPlayerName(playerid, item_playername[playerid], MAX_PLAYER_NAME + 26);
NickToFilename(item_playername[playerid]);
CallLocalFunction("ITEMSYSTEM_OnPlayerConnect", "d", playerid);
return 1;
}
#if defined _ALS_OnPlayerConnect
#undef OnPlayerConnect
#else
#define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect ITEMSYSTEM_OnPlayerConnect
forward ITEMSYSTEM_OnPlayerConnect(playerid);
stock GetPlayerItems(playerid, &index, &item, &amount)
{
if(index >= MAX_ITEM_ID || index < 0) return 0;
for(new i = index; i < MAX_ITEM_ID; i++)
{
format(item_string, sizeof item_string, "item/name/%d", i);
if(!fexist(item_string)) continue;
format(item_string, sizeof item_string, "item/players/%s.%d", item_playername[playerid], i);
if(!fexist(item_string)) continue;
item_file = fopen(item_string, io_read);
item_string[0] = EOS;
fread(item_file, item_string);
fclose(item_file);
RemoveNewLine(item_string);
if(strval(item_string) <= 0) continue;
item = i;
amount = strval(item_string);
index = i + 1;
return 1;
}
return 0;
}
stock GetPlayerItemAmount(playerid, itemid)
{
format(item_string, sizeof item_string, "item/players/%s.%d", item_playername[playerid], itemid);
if(fexist(item_string))
{
item_file = fopen(item_string, io_read);
fread(item_file, item_string);
fclose(item_file);
RemoveNewLine(item_string);
if(strval(item_string) > 0) return strval(item_string);
}
return 0;
}
stock ResetPlayerItem(playerid, itemid)
{
format(item_string, sizeof item_string, "item/players/%s.%d", item_playername[playerid], itemid);
if(fexist(item_string))
{
fremove(item_string);
return 1;
}
return 0;
}
stock SetPlayerItem(playerid, itemid, amount)
{
if(itemid >= MAX_ITEM_ID || itemid < 0) return -1;
format(item_string, sizeof item_string, "item/players/%s.%d", item_playername[playerid], itemid);
item_file = fopen(item_string, io_write);
valstr(item_string, amount);
fwrite(item_file, item_string);
fclose(item_file);
return 1;
}
stock GivePlayerItem(playerid, itemid, amount)
{
if(itemid >= MAX_ITEM_ID || itemid < 0) return -1;
static am;
am = amount + GetPlayerItemAmount(playerid, itemid);
format(item_string, sizeof item_string, "item/players/%s.%d", item_playername[playerid], itemid);
if(am <= 0)
{
ResetPlayerItem(playerid, itemid);
}
else
{
item_file = fopen(item_string, io_write);
valstr(item_string, am);
fwrite(item_file, item_string);
fclose(item_file);
}
return am;
}
stock AddItem(itemid, name[])
{
if(itemid >= MAX_ITEM_ID || itemid < 0) return 0;
format(item_string, sizeof item_string, "item/name/%d", itemid);
item_file = fopen(item_string, io_write);
fwrite(item_file, name);
fclose(item_file);
return 1;
}
stock DeleteItem(itemid)
{
format(item_string, sizeof item_string, "item/name/%d", itemid);
if(fexist(item_string))
{
fremove(item_string);
return 1;
}
return 0;
}
stock GetItemName(itemid)
{
format(item_string, sizeof item_string, "item/name/%d", itemid);
if(fexist(item_string))
{
item_file = fopen(item_string, io_read);
item_string[0] = EOS;
fread(item_file, item_string);
fclose(item_file);
RemoveNewLine(item_string);
}
else
{
item_string[0] = EOS;
}
return item_string;
}
stock IsValidItem(itemid)
{
format(item_string, sizeof item_string, "item/name/%d", itemid);
if(fexist(item_string)) return 1;
return 0;
}
stock NickToFilename(nick[])
{
item_string[0] = EOS;
strcat(item_string, nick);
str_replace("/", "&01&", item_string);
str_replace("\\", "&02&", item_string);
str_replace("?", "&03&", item_string);
str_replace("*", "&04&", item_string);
str_replace("<", "&05&", item_string);
str_replace(">", "&06&", item_string);
str_replace(":", "&07&", item_string);
str_replace("|", "&08&", item_string);
return item_string;
}
stock str_replace(needle[], replace[], haystack[])
{
new index;
for(new i; i < strlen(haystack); i++)
{
index = strfind(haystack, needle, true);
if(index == -1) return false;
strdel(haystack, index, index + strlen(needle));
strins(haystack, replace, index, strlen(haystack));
}
return true;
}
stock RemoveNewLine(string[])
{
new pos = strfind(string, "\r\n");
if(pos != -1)
{
strdel(string, pos, pos+2);
return 1;
}
return 0;
}