Skip to content

Commit

Permalink
Fix empty category. Issue #77 (#85)
Browse files Browse the repository at this point in the history
* fix Should draw category if not exist items in it

* added a check for the presence of items in a category before displaying it

* optimized, only need one approval

* Fix menu drop when category is empty
  • Loading branch information
IL0co authored Jun 23, 2022
1 parent aaba3ba commit b54b086
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 140 deletions.
115 changes: 27 additions & 88 deletions addons/sourcemod/scripting/shop.sp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public int Native_ShowItemsOfCategory(Handle plugin, int params)
{
ThrowNativeError(1, "Category id %d is invalid!", category_id);
}
return ShowItemsOfCategory(client, category_id, GetNativeCell(3));
return ShowItemsOfCategory(client, category_id, GetNativeCell(3) ? Menu_Inventory : Menu_Buy);
}

public void OnPluginStart()
Expand Down Expand Up @@ -598,11 +598,7 @@ public int OnInventorySelect(Menu menu, MenuAction action, int param1, int param
return 0;
}

if (!ShowItemsOfCategory(param1, StringToInt(info), true) && !ShowInventory(param1))
{
ShowMainMenu(param1);
CPrintToChat(param1, "%t", "EmptyInventory");
}
ShowItemsOfCategory(param1, StringToInt(info), Menu_Inventory);
}
case MenuAction_Cancel:
{
Expand All @@ -623,7 +619,7 @@ public int OnInventorySelect(Menu menu, MenuAction action, int param1, int param
bool ShowCategories(int client)
{
Menu menu = new Menu(OnCategorySelect);

if (!ItemManager_FillCategories(menu, client, Menu_Buy))
{
delete menu;
Expand Down Expand Up @@ -661,11 +657,7 @@ public int OnCategorySelect(Menu menu, MenuAction action, int param1, int param2
return 0;
}

if (!ShowItemsOfCategory(param1, category_id, false) && !ShowCategories(param1))
{
ShowMainMenu(param1);
CPrintToChat(param1, "%t", "EmptyShop");
}
ShowItemsOfCategory(param1, category_id, Menu_Buy);
}
case MenuAction_Cancel :
{
Expand All @@ -680,40 +672,25 @@ public int OnCategorySelect(Menu menu, MenuAction action, int param1, int param2
return 0;
}

bool ShowItemsOfCategory(int client, int category_id, bool inventory, int pos = 0)
bool ShowItemsOfCategory(int client, int category_id, ShopMenu shop_menu, int pos = 0)
{
Menu menu = new Menu(OnItemSelect, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
if (!ItemManager_FillItemsOfCategory(menu, client, client, category_id, inventory))
{
delete menu;
return false;
}
Menu menu = new Menu(OnItemSelect, MENU_ACTIONS_DEFAULT|MenuAction_DisplayItem);
bool result = ItemManager_FillItemsOfCategory(menu, client, client, category_id, shop_menu);

char title[128];
if (inventory)
{
FormatEx(title, sizeof(title), "%T\n%T", "inventory", client, "credits", client, PlayerManager_GetCredits(client));
OnMenuTitle(client, Menu_Inventory, title, title, sizeof(title));
iClMenuId[client] = Menu_Inventory;
}
else
{
FormatEx(title, sizeof(title), "%T\n%T", "Shop", client, "credits", client, PlayerManager_GetCredits(client));
OnMenuTitle(client, Menu_Buy, title, title, sizeof(title));
iClMenuId[client] = Menu_Buy;
}

FormatEx(title, sizeof(title), "%T\n%T", (shop_menu == Menu_Inventory) ? "inventory" : "Shop", client, "credits", client, PlayerManager_GetCredits(client));
OnMenuTitle(client, shop_menu, title, title, sizeof(title));
menu.SetTitle(title);

bInv[client] = inventory;
bInv[client] = (shop_menu == Menu_Inventory);
iClCategoryId[client] = category_id;
iClMenuId[client] = shop_menu;

menu.ExitButton = true;
menu.ExitBackButton = true;

iClCategoryId[client] = category_id;
menu.ExitButton = true;

menu.DisplayAt(client, pos, MENU_TIME_FOREVER);

return true;
return result;
}

public int OnItemSelect(Menu menu, MenuAction action, int param1, int param2)
Expand All @@ -726,16 +703,14 @@ public int OnItemSelect(Menu menu, MenuAction action, int param1, int param2)
menu.GetItem(param2, info, sizeof(info));
iPos[param1] = GetMenuSelectionPosition();

ShopMenu shop_menu = (bInv[param1] ? Menu_Inventory : Menu_Buy);
int value = StringToInt(info);

Action result = Forward_OnItemSelect(param1, shop_menu, iClCategoryId[param1], value);
Action result = Forward_OnItemSelect(param1, iClMenuId[param1], iClCategoryId[param1], value);

if (result == Plugin_Handled ||
((result == Plugin_Changed || result == Plugin_Continue) && !ShowItemInfo(param1, value)))
{
ShowItemsOfCategory(param1, iClCategoryId[param1], bInv[param1], iPos[param1]);
Forward_OnItemSelected(param1, shop_menu, iClCategoryId[param1], value);
Forward_OnItemSelected(param1, iClMenuId[param1], iClCategoryId[param1], value);
ShowItemsOfCategory(param1, iClCategoryId[param1], iClMenuId[param1], iPos[param1]);
}
}
case MenuAction_Cancel :
Expand All @@ -758,29 +733,15 @@ public int OnItemSelect(Menu menu, MenuAction action, int param1, int param2)
}
}
case MenuAction_End : delete menu;
case MenuAction_DrawItem :
{
char info[16];
int style;
menu.GetItem(param2, info, sizeof(info), style);

bool disabled;
Action result = Forward_OnItemDraw(param1, bInv[param1] ? Menu_Inventory : Menu_Buy, iClCategoryId[param1], StringToInt(info), disabled);

if(result >= Plugin_Handled)
{
return ITEMDRAW_IGNORE;
}
else if (result == Plugin_Changed && disabled)
{
return ITEMDRAW_DISABLED;
}
return style;
}
case MenuAction_DisplayItem:
{
char info[16], sBuffer[SHOP_MAX_STRING_LENGTH];
menu.GetItem(param2, info, sizeof(info), _, sBuffer, sizeof(sBuffer));

if(!info[0])
{
return 0;
}

bool result = Forward_OnItemDisplay(param1, bInv[param1] ? Menu_Inventory : Menu_Buy, iClCategoryId[param1], StringToInt(info), sBuffer, sBuffer, sizeof(sBuffer));

Expand Down Expand Up @@ -1176,11 +1137,7 @@ public int ItemPanel_Handler(Menu menu, MenuAction action, int param1, int param
}
if (bInv[param1] && PlayerManager_GetItemCount(param1, iClItemId[param1]) < 1)
{
if (!ShowItemsOfCategory(param1, iClCategoryId[param1], true, iPos[param1]) && !ShowInventory(param1))
{
ShowMainMenu(param1);
CPrintToChat(param1, "%t", "EmptyInventory");
}
ShowItemsOfCategory(param1, iClCategoryId[param1], iClMenuId[param1], iPos[param1]);
}
}
case BUTTON_PREVIEW :
Expand Down Expand Up @@ -1226,25 +1183,7 @@ public int ItemPanel_Handler(Menu menu, MenuAction action, int param1, int param
{
if(param2 == g_iMaxPageItems-2)
{
switch (iClMenuId[param1])
{
case Menu_Buy :
{
if (!ShowItemsOfCategory(param1, iClCategoryId[param1], false, iPos[param1]) && !ShowCategories(param1))
{
ShowMainMenu(param1);
CPrintToChat(param1, "%t", "EmptyShop");
}
}
case Menu_Inventory :
{
if (!ShowItemsOfCategory(param1, iClCategoryId[param1], true, iPos[param1]) && !ShowInventory(param1))
{
ShowMainMenu(param1);
CPrintToChat(param1, "%t", "EmptyInventory");
}
}
}
ShowItemsOfCategory(param1, iClCategoryId[param1], iClMenuId[param1], iPos[param1]);
}
}
}
Expand Down Expand Up @@ -2068,9 +2007,9 @@ bool FillCategories(Menu menu, int source_client, ShopMenu shop_menu, bool showA
return ItemManager_FillCategories(menu, source_client, shop_menu, showAll);
}

bool FillItemsOfCategory(Menu menu, int client, int source_client, int category_id, bool showAll = false)
bool FillItemsOfCategory(Menu menu, int client, int source_client, int category_id, ShopMenu shop_menu, bool showAll = false)
{
return ItemManager_FillItemsOfCategory(menu, client, source_client, category_id, _, showAll);
return ItemManager_FillItemsOfCategory(menu, client, source_client, category_id, shop_menu, showAll);
}

int GetItemCategoryId(int item_id)
Expand Down
22 changes: 14 additions & 8 deletions addons/sourcemod/scripting/shop/admin.sp
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,8 @@ bool Admin_ShowItemsOfCategory(int client, int category_id, int pos = 0)
SetGlobalTransTarget(client);

Menu menu = new Menu(Admin_ItemsMenu_Handler, MENU_ACTIONS_DEFAULT|MenuAction_Display|MenuAction_DrawItem|MenuAction_DisplayItem);
if (!FillItemsOfCategory(menu, client, client, category_id, true))
{
CPrintToChat(client, "%t", "EmptyCategory");
delete menu;
return false;
}

FillItemsOfCategory(menu, client, client, category_id, Menu_AdminPanel, true);

menu.ExitButton = true;
menu.ExitBackButton = true;

Expand Down Expand Up @@ -524,7 +519,13 @@ public int Admin_ItemsMenu_Handler(Menu menu, MenuAction action, int param1, int
case MenuAction_DrawItem :
{
char info[16];
menu.GetItem(param2, info, sizeof(info));
int style;
menu.GetItem(param2, info, sizeof(info), style);

if(!info[0])
{
return style;
}

int target = GetClientOfUserId(g_iOpt[param1].AdminTarget);

Expand Down Expand Up @@ -555,6 +556,11 @@ public int Admin_ItemsMenu_Handler(Menu menu, MenuAction action, int param1, int
{
char info[16], buffer[SHOP_MAX_STRING_LENGTH];
menu.GetItem(param2, info, sizeof(info), _, buffer, sizeof(buffer));

if(!info[0])
{
return 0;
}

ItemType type = GetItemTypeEx(info);

Expand Down
8 changes: 7 additions & 1 deletion addons/sourcemod/scripting/shop/forwards.sp
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,20 @@ void Forward_OnClientItemLucked(int client, int item_id)
Action Forward_OnItemDraw(int client, ShopMenu menu_action, int category_id, int item_id, bool &disabled)
{
Action result = Plugin_Continue;
bool call_disable = disabled;

Call_StartForward(h_fwdOnItemDraw);
Call_PushCell(client);
Call_PushCell(menu_action);
Call_PushCell(category_id);
Call_PushCell(item_id);
Call_PushCellRef(disabled);
Call_PushCellRef(call_disable);
Call_Finish(result);

if(result == Plugin_Changed)
{
disabled = call_disable;
}

return result;
}
Expand Down
Loading

0 comments on commit b54b086

Please sign in to comment.