From b54b086ede7b11cbbc352fb6da275d7771f64349 Mon Sep 17 00:00:00 2001 From: iLoco <50996383+IL0co@users.noreply.github.com> Date: Thu, 23 Jun 2022 13:45:05 +0300 Subject: [PATCH] Fix empty category. Issue #77 (#85) * 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 --- addons/sourcemod/scripting/shop.sp | 115 ++++-------------- addons/sourcemod/scripting/shop/admin.sp | 22 ++-- addons/sourcemod/scripting/shop/forwards.sp | 8 +- .../sourcemod/scripting/shop/item_manager.sp | 108 +++++++++------- .../translations/pt_p/shop.phrases.txt | 5 + .../translations/ru/shop.phrases.txt | 5 + .../sourcemod/translations/shop.phrases.txt | 5 + .../translations/ua/shop.phrases.txt | 5 + 8 files changed, 133 insertions(+), 140 deletions(-) diff --git a/addons/sourcemod/scripting/shop.sp b/addons/sourcemod/scripting/shop.sp index c77985d..c7d1260 100644 --- a/addons/sourcemod/scripting/shop.sp +++ b/addons/sourcemod/scripting/shop.sp @@ -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() @@ -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: { @@ -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; @@ -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 : { @@ -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) @@ -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 : @@ -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)); @@ -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 : @@ -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]); } } } @@ -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) diff --git a/addons/sourcemod/scripting/shop/admin.sp b/addons/sourcemod/scripting/shop/admin.sp index c12e69b..7b855e6 100644 --- a/addons/sourcemod/scripting/shop/admin.sp +++ b/addons/sourcemod/scripting/shop/admin.sp @@ -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; @@ -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); @@ -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); diff --git a/addons/sourcemod/scripting/shop/forwards.sp b/addons/sourcemod/scripting/shop/forwards.sp index 82f2874..ef4adfc 100644 --- a/addons/sourcemod/scripting/shop/forwards.sp +++ b/addons/sourcemod/scripting/shop/forwards.sp @@ -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; } diff --git a/addons/sourcemod/scripting/shop/item_manager.sp b/addons/sourcemod/scripting/shop/item_manager.sp index fcc403d..29ab540 100644 --- a/addons/sourcemod/scripting/shop/item_manager.sp +++ b/addons/sourcemod/scripting/shop/item_manager.sp @@ -1484,7 +1484,7 @@ bool ItemManager_FillCategories(Menu menu, int source_client, ShopMenu shop_menu { continue; } - + trie.GetString("name", buffer, sizeof(buffer)); ItemManager_OnCategoryDisplay(on_display_hndl, on_display_func, source_client, index, category, buffer, display, sizeof(display), shop_menu); @@ -1584,65 +1584,82 @@ bool ItemManager_GetItemDisplay(int item_id, int source_client, char[] buffer, i return true; } -bool ItemManager_FillItemsOfCategory(Menu menu, int client, int source_client, int category_id, bool inventory = false, bool showAll = false) +bool ItemManager_FillItemsOfCategory(Menu menu, int client, int source_client, int category_id, ShopMenu shop_menu, bool showAll = false) { - bool result = false; h_KvItems.Rewind(); - if (h_KvItems.GotoFirstSubKey()) + KeyValues kv = new KeyValues("Temp"); + kv.Import(h_KvItems); + + char display[SHOP_MAX_STRING_LENGTH]; + + if(kv.GotoFirstSubKey()) { - char sItemId[16], display[SHOP_MAX_STRING_LENGTH], category[SHOP_MAX_STRING_LENGTH], item[SHOP_MAX_STRING_LENGTH]; + int item_id; + Handle plugin; + DataPack dpCallback; + Action aShouldDisplay; + bool bShouldDisplay, disabled; + char sItemId[16], category[SHOP_MAX_STRING_LENGTH], item[SHOP_MAX_STRING_LENGTH]; h_arCategories.GetString(category_id, category, sizeof(category)); + do { - bool isHidden = view_as(h_KvItems.GetNum("hide", 0)); - if (h_KvItems.GetNum("category_id", -1) != category_id || !h_KvItems.GetSectionName(sItemId, sizeof(sItemId)) || (inventory && !ClientHasItemEx(client, sItemId))) + if((shop_menu != Menu_Inventory || shop_menu != Menu_ItemTransfer) && !showAll && kv.GetNum("hide", 0)) { continue; } + + if (kv.GetNum("category_id", -1) != category_id || !kv.GetSectionName(sItemId, sizeof(sItemId))) + { + continue; + } + + if((shop_menu == Menu_Inventory || shop_menu == Menu_ItemTransfer) && !ClientHasItemEx(client, sItemId)) + { + continue; + } + + kv.GetString("item", item, sizeof(item)); - if (!inventory && !showAll && isHidden) continue; - - h_KvItems.GetString("item", item, sizeof(item)); - - int item_id = StringToInt(sItemId); - - Handle plugin = view_as(h_KvItems.GetNum("plugin")); - - h_KvItems.GetString("name", display, sizeof(display)); - - DataPack dpCallback = view_as(h_KvItems.GetNum("callbacks", 0)); - if (dpCallback == null) - ThrowNativeError(SP_ERROR_NATIVE, "Callbacks for this item not found"); - - dpCallback.Position = ITEM_DATAPACKPOS_DISPLAY; - Function callback_display = dpCallback.ReadFunction(); + item_id = StringToInt(sItemId); + plugin = view_as(kv.GetNum("plugin")); + + if ((dpCallback = view_as(kv.GetNum("callbacks", 0))) == null) + { + ThrowNativeError(SP_ERROR_NATIVE, "Callbacks for this item not found"); + } + disabled = false; + aShouldDisplay = Forward_OnItemDraw(client, shop_menu, category_id, item_id, disabled); + dpCallback.Position = ITEM_DATAPACKPOS_SHOULD_DISPLAY; - Function callback_should = dpCallback.ReadFunction(); - - h_KvItems.Rewind(); - - bool bShouldDisplay = ItemManager_OnItemShouldDisplay(plugin, callback_should, source_client, category_id, category, item_id, item, inventory ? Menu_Inventory : Menu_Buy); - - bool disabled = false; - if (!ItemManager_OnItemDisplay(plugin, callback_display, source_client, category_id, category, item_id, item, inventory ? Menu_Inventory : Menu_Buy, disabled, display, display, sizeof(display))) + bShouldDisplay = ItemManager_OnItemShouldDisplay(plugin, dpCallback.ReadFunction(), source_client, category_id, category, item_id, item, shop_menu); + + if(aShouldDisplay >= Plugin_Handled || !bShouldDisplay) { - disabled = false; + continue; } - h_KvItems.JumpToKey(sItemId); - - if (bShouldDisplay) - menu.AddItem(sItemId, display, disabled ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT); + kv.GetString("name", display, sizeof(display)); + + dpCallback.Position = ITEM_DATAPACKPOS_DISPLAY; + ItemManager_OnItemDisplay(plugin, dpCallback.ReadFunction(), source_client, category_id, category, item_id, item, shop_menu, disabled, display, display, sizeof(display)); - result = true; + menu.AddItem(sItemId, display, disabled ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT); } - while (h_KvItems.GotoNextKey()); - - h_KvItems.Rewind(); + while (kv.GotoNextKey()); } - - return result; + + delete kv; + + if(!menu.ItemCount) + { + FormatEx(display, sizeof(display), "%T", "MenuEmptyCategory", client); + menu.AddItem("", display, ITEMDRAW_DISABLED); + return false; + } + + return true; } Panel ItemManager_CreateItemPanelInfo(int source_client, int item_id, ShopMenu menu_act) @@ -2344,6 +2361,7 @@ bool ItemManager_OnItemShouldDisplay(Handle plugin, Function callback, int clien bool ItemManager_OnItemDisplay(Handle plugin, Function callback, int client, int category_id, const char[] category, int item_id, const char[] item, ShopMenu menu, bool &disabled = false, const char[] name, char[] buffer, int maxlen) { bool result = false; + bool call_disabled = disabled; if (IsCallValid(plugin, callback)) { @@ -2354,7 +2372,7 @@ bool ItemManager_OnItemDisplay(Handle plugin, Function callback, int client, int Call_PushCell(item_id); Call_PushString(item); Call_PushCell(menu); - Call_PushCellRef(disabled); + Call_PushCellRef(call_disabled); Call_PushString(name); Call_PushStringEx(buffer, maxlen, SM_PARAM_STRING_UTF8|SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK); Call_PushCell(maxlen); @@ -2364,6 +2382,10 @@ bool ItemManager_OnItemDisplay(Handle plugin, Function callback, int client, int if (!result) { strcopy(buffer, maxlen, name); + } + else + { + disabled = call_disabled; } return result; diff --git a/addons/sourcemod/translations/pt_p/shop.phrases.txt b/addons/sourcemod/translations/pt_p/shop.phrases.txt index 29427f1..4565489 100644 --- a/addons/sourcemod/translations/pt_p/shop.phrases.txt +++ b/addons/sourcemod/translations/pt_p/shop.phrases.txt @@ -229,6 +229,11 @@ "pt_p" "{green}[Loja]{default} Categoria vazia!" } + "MenuEmptyCategory" + { + "pt_p" "Categoria vazia!" + } + "NothingToLuck" { "pt_p" "{green}[Loja]{default} Não há nada que te possa dar sorte!" diff --git a/addons/sourcemod/translations/ru/shop.phrases.txt b/addons/sourcemod/translations/ru/shop.phrases.txt index 37b38d6..db35d0a 100644 --- a/addons/sourcemod/translations/ru/shop.phrases.txt +++ b/addons/sourcemod/translations/ru/shop.phrases.txt @@ -209,6 +209,11 @@ "ru" "{green}[Shop]{default} Категория пуста!" } + "MenuEmptyCategory" + { + "ru" "Категория пуста!" + } + "NothingToLuck" { "ru" "{green}[Shop]{default} Нет ничего, что вы могли бы выйграть!" diff --git a/addons/sourcemod/translations/shop.phrases.txt b/addons/sourcemod/translations/shop.phrases.txt index 171230a..f7b46db 100644 --- a/addons/sourcemod/translations/shop.phrases.txt +++ b/addons/sourcemod/translations/shop.phrases.txt @@ -229,6 +229,11 @@ "en" "{green}[Shop]{default} The category is empty!" } + "MenuEmptyCategory" + { + "en" "The category is empty!" + } + "NothingToLuck" { "en" "{green}[Shop]{default} There is nothing that you can luck!" diff --git a/addons/sourcemod/translations/ua/shop.phrases.txt b/addons/sourcemod/translations/ua/shop.phrases.txt index 9253749..87e68c6 100644 --- a/addons/sourcemod/translations/ua/shop.phrases.txt +++ b/addons/sourcemod/translations/ua/shop.phrases.txt @@ -209,6 +209,11 @@ "ua" "{green}[Shop]{default} Категорія порожня!" } + "MenuEmptyCategory" + { + "en" "Категорія порожня!" + } + "NothingToLuck" { "ua" "{green}[Shop]{default} Немає нічого, що ви могли б виграти!"