Re-adjusting the IDs. -
I am having a hard time on this Inventory System I am using (a heavily - Maybe?? - modified of Joe Staff's Inventory System v2)
The /inventory works perfectly fine, If you remove an inventory item (ID 0 -> listitem ID 0) It will get fucked up. /inventory still lists the items properly, The only problem is the OnDialogResponse. On Joe's Inventory System, OnPlayerUseItem is originally scripted to be called by CallLocalFunction (and the MAX_ITEMS id for it are assigned by listitem) But since I removed the first item which is ID 0 (MAX_ITEMS) , OnDialogResponse will think that the remaining items that are in the first listitem will be ID 0 - That is why I wanted to re-adjust the IDs like for instance an item has been removed, I will loop all the items and assign the existing slots to the none-existing ones (I am having a hard time on scripting it)
Any help would be appreciated - Post down below if you are still having a hard time understanding my situation (probably because of my grammar)
* An invalid item will always be named as "none".
* An invalid item will always have a zero item quantity.
Here's an example.
-> I have 5 items, ID 0/1/2/3/4. I took ID 2 and ID 4 away from the item menu. The ID 2 and ID 4 will be now unoccupied, The thing I wanted to do is to re-fresh the Inventory & make the IDs re-adjusted like ID 3 to ID 2.
-> I have 10 items, ID 0/1/2/3/4/5/6/7/8/9, I took ID 2, ID 4, ID 6 and ID 8 away from the item menu, The mentioned IDs will be now occupied. Unoccupied IDs must be re-occupied by an existing item. ID 3 will took over ID 2, ID 5 will took over ID 4, ID 7 will took over ID 6 and ID 9 will took over ID 8 - The IDs that will replace the unoccupied IDs will be then re-occupied by their above itemIDs if it's occupied.
PHP Code:
// OnDialogResponse
case DIALOG_INVENTORY:
{
if(response)
{
CallLocalFunction("OnPlayerUseItem", "ds", playerid, InventoryInfo[playerid][listitem][inventoryName]);
}
else
{
DeletePVar(playerid, "item_name");
}
}
case DIALOG_INVID:
{
new itemname[32];
GetPVarString(playerid, "item_name", itemname, 32);
if(response)
{
if(!isnumeric(inputtext))
{
ShowPlayerDialog(playerid, DIALOG_INVID, DIALOG_STYLE_INPUT, "Give to Player", "Type in the playerID you wanted to give the item to.", "Give", "Cancel");
return 1;
}
if(strval(inputtext) == playerid)
{
ShowPlayerDialog(playerid, DIALOG_INVID, DIALOG_STYLE_INPUT, "Give to Player", "* You cannot give the item to yourself!\n\nType in the playerID you wanted to give the item to.", "Give", "Cancel");
return 1;
}
if(!IsPlayerConnected(strval(inputtext)))
{
ShowPlayerDialog(playerid, DIALOG_INVID, DIALOG_STYLE_INPUT, "Give to Player", "* Player not connected!\n\nType in the playerID you wanted to give the item to.", "Give", "Cancel");
return 1;
}
new id = strval(inputtext);
if (!IsPlayerNearPlayer(playerid, id, 5.0))
{
ShowPlayerDialog(playerid, DIALOG_INVID, DIALOG_STYLE_INPUT, "Give to Player", "* You are not near to that player!\n\nType in the playerID you wanted to give the item to.", "Give", "Cancel");
return 1;
}
if(strcmp(itemname, "Walkie Talkie", true) == 0)
{
if(!PlayerHasItem(playerid, itemname))
return SendError(playerid, "You do not have the Walkie Talkie.") && DeletePVar(playerid, "item_name");
if(PlayerHasItem(id, itemname))
return SendError(playerid, "Player already have a walkie talkie.") && DeletePVar(playerid, "item_name");
AddItem(id, itemname, 1);
RemoveItem(playerid, itemname, 1);
PlayerInfo[id][pChannel] = PlayerInfo[playerid][pChannel];
PlayerInfo[playerid][pChannel] = 0;
format(string, sizeof(string), "hands their Walkie Talkie to %s.", GetPlayerNameEx(id));
cmd_me(playerid, string);
format(string, sizeof(string), "* You hand your Walkie Talkie to %s.", GetPlayerNameEx(id));
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "* %s hands their Walkie Talkie to you.", GetPlayerNameEx(playerid));
SendClientMessage(id, -1, string);
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Repair Kit", true) == 0)
{
if(!PlayerHasItem(playerid, itemname))
return SendError(playerid, "You do not have a Repair Kit.") && DeletePVar(playerid, "item_name");
if(GetItemQuantity(id, itemname) >= 3)
return SendError(playerid, "Player has already enough repair kit.");
AddItem(id, itemname, 1);
RemoveItem(playerid, itemname, 1);
format(string, sizeof(string), "hands their Repair Kit to %s.", GetPlayerNameEx(id));
cmd_me(playerid, string);
format(string, sizeof(string), "* You hand your Repair Kit to %s.", GetPlayerNameEx(id));
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "* %s hands their Repair Kit to you.", GetPlayerNameEx(playerid));
SendClientMessage(id, -1, string);
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Brass Knuckles", true) == 0)
{
if(!PlayerHasItem(playerid, itemname))
return SendError(playerid, "You do not have a Brass Knuckles.") && DeletePVar(playerid, "item_name");
if(GetWeapon(playerid) == 1)
return SendError(playerid, "You cannot give the player the weapon (They already have one)!");
AddItem(id, itemname, GetItemQuantity(playerid, itemname));
GiveGun(playerid, 1, GetItemQuantity(playerid, itemname));
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
format(string, sizeof(string), "hands their Brass Knuckles to %s.", GetPlayerNameEx(id));
cmd_me(playerid, string);
format(string, sizeof(string), "* You hand your Brass Knuckles to %s.", GetPlayerNameEx(id));
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "* %s hands their Brass Knuckles to you.", GetPlayerNameEx(playerid));
SendClientMessage(id, -1, string);
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Colt 45", true) == 0)
{
if(!PlayerHasItem(playerid, itemname))
return SendError(playerid, "You do not have a Colt 45.") && DeletePVar(playerid, "item_name");
if(GetWeapon(playerid) == 23 || GetWeapon(playerid) == 24 || GetWeapon(playerid) == 25)
return SendError(playerid, "You cannot give the player the weapon (They already have one)!");
AddItem(id, itemname, GetItemQuantity(playerid, itemname));
GiveGun(playerid, 22, GetItemQuantity(playerid, itemname));
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
format(string, sizeof(string), "hands their Colt 45 to %s.", GetPlayerNameEx(id));
cmd_me(playerid, string);
format(string, sizeof(string), "* You hand your Colt 45 to %s.", GetPlayerNameEx(id));
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "* %s hands their Colt 45 to you.", GetPlayerNameEx(playerid));
SendClientMessage(id, -1, string);
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Desert Eagle", true) == 0)
{
if(!PlayerHasItem(playerid, itemname))
return SendError(playerid, "You do not have a Desert Eagle.") && DeletePVar(playerid, "item_name");
if(GetWeapon(playerid) == 23 || GetWeapon(playerid) == 24 || GetWeapon(playerid) == 25)
return SendError(playerid, "You cannot give the player the weapon (They already have one)!");
AddItem(id, itemname, GetItemQuantity(playerid, itemname));
GiveGun(playerid, 24, GetItemQuantity(playerid, itemname));
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
format(string, sizeof(string), "hands their Desert Eagle to %s.", GetPlayerNameEx(id));
cmd_me(playerid, string);
format(string, sizeof(string), "* You hand your Desert Eagle to %s.", GetPlayerNameEx(id));
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "* %s hands their Desert Eagle to you.", GetPlayerNameEx(playerid));
SendClientMessage(id, -1, string);
DeletePVar(playerid, "item_name");
}
}
else
{
DeletePVar(playerid, "item_name");
}
}
case DIALOG_INVOPTION:
{
new itemname[32];
GetPVarString(playerid, "item_name", itemname, 32);
if(response)
{
switch(listitem)
{
case 0:
{
ShowPlayerDialog(playerid, DIALOG_INVID, DIALOG_STYLE_INPUT, "Give to Player", "Type in the playerID you wanted to give the item to.", "Give", "Cancel");
}
case 1:
{
if(strcmp(itemname, "Walkie Talkie", true) == 0)
{
RemoveItem(playerid, itemname, 1);
PlayerInfo[playerid][pChannel] = 0;
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You dropped your walkie talkie.");
cmd_me(playerid, "has dropped their walkie talkie.");
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Repair Kit", true) == 0)
{
RemoveItem(playerid, itemname, 1);
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You dropped a repair kit.");
cmd_me(playerid, "has dropped a repair kit.");
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Brass Knuckles", true) == 0)
{
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You dropped your Brass Knuckles.");
cmd_me(playerid, "has dropped their Brass Knuckles.");
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Colt 45", true) == 0)
{
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You dropped your Colt 45.");
cmd_me(playerid, "has dropped their Colt 45.");
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Desert Eagle", true) == 0)
{
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You dropped your Desert Eagle.");
cmd_me(playerid, "has dropped their Desert Eagle.");
DeletePVar(playerid, "item_name");
}
}
case 2:
{
if(strcmp(itemname, "Brass Knuckles", true) == 0)
{
if(GetWeapon(playerid) == 1)
return SendError(playerid, "You cannot stack up your weapons!");
GiveGun(playerid, 1, GetItemQuantity(playerid, itemname));
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You equipped your Brass Knuckles.");
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Colt 45", true) == 0)
{
if(GetWeapon(playerid) == 23 || GetWeapon(playerid) == 24 || GetWeapon(playerid) == 25)
return SendError(playerid, "You cannot stack up your weapons!");
GiveGun(playerid, 22, GetItemQuantity(playerid, itemname));
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You equipped your Colt 45.");
DeletePVar(playerid, "item_name");
}
else if(strcmp(itemname, "Desert Eagle", true) == 0)
{
if(GetWeapon(playerid) == 23 || GetWeapon(playerid) == 24 || GetWeapon(playerid) == 25)
return SendError(playerid, "You cannot stack up your weapons!");
GiveGun(playerid, 24, GetItemQuantity(playerid, itemname));
RemoveItem(playerid, itemname, GetItemQuantity(playerid, itemname));
SendClientMessage(playerid, COLOR_CLIENT, "**{FFFFFF} You equipped your Desert Eagle.");
DeletePVar(playerid, "item_name");
}
}
}
}
else
{
DeletePVar(playerid, "item_name");
}
}
// Stocks
stock AddItem(playerid, name[], amount)
{
new slot = -1;
for(new item; item < MAX_ITEMS; item++)
{
if(!InventoryInfo[playerid][item][inventoryExist])
{
if(slot == -1) slot = item;
continue;
}
if(!strcmp(InventoryInfo[playerid][item][inventoryName], name, true))
{
InventoryInfo[playerid][item][inventoryQuantity] += amount;
if(InventoryInfo[playerid][item][inventoryQuantity] <= 0)
{
format(InventoryInfo[playerid][item][inventoryName], 32, "none");
InventoryInfo[playerid][item][inventoryQuantity] = 0;
InventoryInfo[playerid][item][inventoryExist] = false;
}
return 1;
}
}
if(slot > -1)
{
format(InventoryInfo[playerid][slot][inventoryName], 32, name);
InventoryInfo[playerid][slot][inventoryQuantity] = amount;
InventoryInfo[playerid][slot][inventoryExist] = true;
return 1;
}
return 0;
}
stock RemoveItem(playerid, name[], amount)
{
for(new item; item < MAX_ITEMS; item++)
{
if(!InventoryInfo[playerid][item][inventoryExist]) continue;
if(!strcmp(InventoryInfo[playerid][item][inventoryName], name, true))
{
InventoryInfo[playerid][item][inventoryQuantity] -= amount;
if(InventoryInfo[playerid][item][inventoryQuantity] <= 0)
{
format(InventoryInfo[playerid][item][inventoryName], 32, "none");
InventoryInfo[playerid][item][inventoryQuantity] = 0;
InventoryInfo[playerid][item][inventoryExist] = false;
}
return 1;
}
}
return 0;
}
stock PlayerHasItem(playerid, name[])
{
for(new item; item < MAX_ITEMS; item++)
{
if(!InventoryInfo[playerid][item][inventoryExist]) continue;
if(!strcmp(InventoryInfo[playerid][item][inventoryName], name, false)) return 1;
}
return 0;
}
stock ResetPlayerInventorys(playerid)
{
for(new item; item < MAX_ITEMS; item++)
{
InventoryInfo[playerid][item][inventoryExist] = false;
format(InventoryInfo[playerid][item][inventoryName], 32, "none");
InventoryInfo[playerid][item][inventoryQuantity] = 0;
}
}
stock ResetPlayerInventory(playerid, item)
{
InventoryInfo[playerid][item][inventoryExist] = false;
format(InventoryInfo[playerid][item][inventoryName], 32, "none");
InventoryInfo[playerid][item][inventoryQuantity] = 0;
return true;
}
stock ShowInventory(playerid)
{
new gItemLists[(MAX_ITEMS+1)*(32+3)];
for(new item; item < MAX_ITEMS; item++)
{
if(!InventoryInfo[playerid][item][inventoryExist]) continue;
format(gItemLists, sizeof(gItemLists), "%s\n%s\t%s", gItemLists, InventoryInfo[playerid][item][inventoryName], AddComma(InventoryInfo[playerid][item][inventoryQuantity]));
}
format(gItemLists, sizeof(gItemLists), "Name\tQuantity%s", gItemLists);
ShowPlayerDialog(playerid, DIALOG_INVENTORY, DIALOG_STYLE_TABLIST_HEADERS, "Your Inventory", gItemLists, "Select", "Close");
return 1;
}
stock GetItemQuantity(playerid, itemname[])
{
for(new item; item < MAX_ITEMS; item++)
{
if(!InventoryInfo[playerid][item][inventoryExist]) continue;
if(!strcmp(InventoryInfo[playerid][item][inventoryName], itemname, false))
{
return InventoryInfo[playerid][item][inventoryQuantity];
}
}
return -1;
}
stock GetAllItems(playerid)
{
new count = 0;
for(new i; i < MAX_ITEMS; i++)
{
if(InventoryInfo[playerid][i][inventoryExist])
{
count++;
}
}
return count++;
}
// OnPlayerUseItem
public OnPlayerUseItem(playerid, itemname[])
{
if(strcmp(itemname, "Walkie Talkie", true) == 0)
{
DeletePVar(playerid, "item_name");
SetPVarString(playerid, "item_name", itemname);
ShowPlayerDialog(playerid, DIALOG_INVOPTION, DIALOG_STYLE_LIST, "Inventory Options", "Give\nDrop", "Select", "Cancel");
}
else if(strcmp(itemname, "Repair Kit", true) == 0)
{
DeletePVar(playerid, "item_name");
SetPVarString(playerid, "item_name", itemname);
ShowPlayerDialog(playerid, DIALOG_INVOPTION, DIALOG_STYLE_LIST, "Inventory Options", "Give\nDrop", "Select", "Cancel");
}
else if(strcmp(itemname, "Colt 45", true) == 0 || strcmp(itemname, "Desert Eagle", true) == 0 || strcmp(itemname, "Brass Knuckles", true) == 0)
{
DeletePVar(playerid, "item_name");
SetPVarString(playerid, "item_name", itemname);
ShowPlayerDialog(playerid, DIALOG_INVOPTION, DIALOG_STYLE_LIST, "Inventory Options", "Give\nDrop\nUse", "Select", "Cancel");
}
return 1;
}
Re: Re-adjusting the IDs. -
What you basically do is directly use listitem. Listitem values are according to the dialog not the array you use to store player's inventory. So you should loop in the dialog response and start an own counter (
) which will side by be added +1 if the inventory item is valid with the loop's index. And then you should check if the count is equals the listitem id which makes sense as it was the listitem shown at that specific index.
Re: Re-adjusting the IDs. -
Gonna try it out, I was too dumb to figure that out xD. I am gonna edit this post & let chu know if it's working.