How to block item give and show message?
Posted: 21 Sep 2018, 14:26
Hello,
How we can block the give item code in
The idea is that in specific condition we must ignore the cost and show whatever we need.
First you will need to edit your ze_items_manager.sma to be like:
Example: In the Weapons Ammo extra item we need to block user from buying weapon ammo for knife and grenades and show a message that he can't buy when he holding knife or grenades.
We will do the check in two places, in the pre buy we will check if he carry knife/grenades we will set ignorecost to true. Then in the post buy we will check again same conditions and if he holding knife/grenades we will show message and return.
Here is the final code:
A general example:
How we can block the give item code in
ze_select_item_post()
forward and show message. If you tried do something like:
- #include <zombie_escape>
- // Default Sound
- new const g_szBuyAmmoSound[] = "items/9mmclip1.wav"
- // Variables
- new g_iItemID
- public plugin_init()
- {
- register_plugin("[ZE] Items: Fire Nade", ZE_VERSION, AUTHORS)
- // Register our item
- g_iItemID = ze_register_item("Frost Nade", 50, 0)
- }
- public ze_select_item_pre(id, itemid)
- {
- // Return Available and we will block it in Post, So it dosen't affect other plugins
- if (itemid != g_iItemID)
- return ZE_ITEM_AVAILABLE
- // Available for Humans only, So don't show it for zombies
- if (ze_is_user_zombie(id))
- return ZE_ITEM_DONT_SHOW
- return ZE_ITEM_AVAILABLE
- }
- public ze_select_item_post(id, itemid)
- {
- // This is not our item, Block it here
- if (itemid != g_iItemID)
- return
- // Attempt to block the give item code, and show message
- if (Anything)
- {
- show message here
- return
- }
- // Get Weapon ID
- new iWpnID = get_weaponid("weapon_smokegrenade")
- // Player Don't have Frost Grenade then give him
- if (rg_get_user_bpammo(id, WeaponIdType:iWpnID) == 0)
- {
- rg_give_item(id, "weapon_smokegrenade", GT_APPEND)
- }
- else
- {
- // Player have, Increase his Back Pack Ammo, And play buy BP sound + Hud Flash
- rg_set_user_bpammo(id, WeaponIdType:iWpnID, rg_get_user_bpammo(id, WeaponIdType:iWpnID) + 1)
- emit_sound(id, CHAN_ITEM, g_szBuyAmmoSound, 1.0, ATTN_NORM, 0, PITCH_NORM)
- Show_Given_BPAmmo(id, 13, 1) // Smoke Grenade AmmoType Const = 13
- }
- }
The idea is that in specific condition we must ignore the cost and show whatever we need.
First you will need to edit your ze_items_manager.sma to be like:
- #include <zombie_escape>
- // Setting File
- new const ZE_EXTRAITEM_FILE[] = "ze_extraitems.ini"
- // Defines
- #define MENU_PAGE_ITEMS g_iMenuData[id]
- // Const
- const OFFSET_CSMENUCODE = 205
- // Forwards
- enum _:TOTAL_FORWARDS
- {
- FW_ITEM_SELECT_PRE = 0,
- FW_ITEM_SELECT_POST
- }
- new g_iForwards[TOTAL_FORWARDS],
- g_iForwardReturn
- // Variables
- new Array:g_szItemRealName,
- Array:g_szItemName,
- Array:g_iItemCost,
- Array:g_iItemLimit
- new g_iItemCount,
- g_szAdditionalMenuText[32],
- g_iMenuData[33],
- bool:g_bIgnoreCost[MAX_EXTRA_ITEMS],
- bool:g_bSetUsed[MAX_EXTRA_ITEMS]
- public plugin_init()
- {
- register_plugin("[ZE] Items Manager", ZE_VERSION, AUTHORS)
- // Commands
- register_clcmd("say /items", "Cmd_Items")
- // Forwards (In Pre Return Values important)
- g_iForwards[FW_ITEM_SELECT_PRE] = CreateMultiForward("ze_select_item_pre", ET_CONTINUE, FP_CELL, FP_CELL, FP_CELL)
- g_iForwards[FW_ITEM_SELECT_POST] = CreateMultiForward("ze_select_item_post", ET_IGNORE, FP_CELL, FP_CELL, FP_CELL)
- }
- public plugin_natives()
- {
- register_native("ze_register_item", "native_ze_register_item")
- register_native("ze_show_items_menu", "native_ze_show_items_menu")
- register_native("ze_force_buy_item", "native_ze_force_buy_item")
- register_native("ze_get_item_id", "native_ze_get_item_id")
- register_native("ze_get_item_cost", "native_ze_get_item_cost")
- register_native("ze_add_text_to_item", "native_ze_add_text_to_item")
- register_native("ze_get_item_limit", "native_ze_get_item_limit")
- register_native("ze_is_valid_itemid", "native_ze_is_valid_itemid")
- register_native("ze_get_item_name", "native_ze_get_item_name")
- register_native("ze_set_ignorecost", "native_ze_set_ignorecost", 1)
- register_native("ze_get_ignorecost", "native_ze_get_ignorecost", 1)
- g_szItemRealName = ArrayCreate(32, 1)
- g_szItemName = ArrayCreate(32, 1)
- g_iItemCost = ArrayCreate(1, 1)
- g_iItemLimit = ArrayCreate(1, 1)
- }
- public client_disconnected(id)
- {
- MENU_PAGE_ITEMS = 0
- }
- public Cmd_Items(id)
- {
- if (!is_user_alive(id))
- return
- Show_Items_Menu(id)
- }
- // Items Menu
- Show_Items_Menu(id)
- {
- static menu[128], name[32], cost, transkey[64]
- new menuid, index, itemdata[2]
- // Title
- formatex(menu, charsmax(menu), "%L:\r", id, "BUY_EXTRAITEM")
- menuid = menu_create(menu, "Extra_Items_Menu")
- // Item List
- for (index = 0; index < g_iItemCount; index++)
- {
- // Additional text to display
- g_szAdditionalMenuText[0] = 0
- // Execute item select attempt forward
- ExecuteForward(g_iForwards[FW_ITEM_SELECT_PRE], g_iForwardReturn, id, index, 0)
- // Show item to player?
- if (g_iForwardReturn >= ZE_ITEM_DONT_SHOW)
- continue;
- // Add Item Name and Cost
- ArrayGetString(g_szItemName, index, name, charsmax(name))
- cost = ArrayGetCell(g_iItemCost, index)
- // ML support for item name
- formatex(transkey, charsmax(transkey), "ITEMNAME %s", name)
- if (GetLangTransKey(transkey) != TransKey_Bad) formatex(name, charsmax(name), "%L", id, transkey)
- // Item available to player?
- if (g_iForwardReturn >= ZE_ITEM_UNAVAILABLE)
- formatex(menu, charsmax(menu), "\d%s %d %s", name, cost, g_szAdditionalMenuText)
- else
- formatex(menu, charsmax(menu), "%s \y%d \w%s", name, cost, g_szAdditionalMenuText)
- itemdata[0] = index
- itemdata[1] = 0
- menu_additem(menuid, menu, itemdata)
- }
- // No items to display?
- if (menu_items(menuid) <= 0)
- {
- ze_colored_print(id, "%L", id, "NO_EXTRA_ITEMS")
- menu_destroy(menuid)
- return;
- }
- // Back - Next - Exit
- formatex(menu, charsmax(menu), "%L", id, "BACK")
- menu_setprop(menuid, MPROP_BACKNAME, menu)
- formatex(menu, charsmax(menu), "%L", id, "NEXT")
- menu_setprop(menuid, MPROP_NEXTNAME, menu)
- formatex(menu, charsmax(menu), "%L", id, "EXIT")
- menu_setprop(menuid, MPROP_EXITNAME, menu)
- // If remembered page is greater than number of pages, clamp down the value
- MENU_PAGE_ITEMS = min(MENU_PAGE_ITEMS, menu_pages(menuid)-1)
- // Fix for AMXX custom menus
- set_pdata_int(id, OFFSET_CSMENUCODE, 0)
- menu_display(id, menuid, MENU_PAGE_ITEMS)
- }
- // Items Menu
- public Extra_Items_Menu(id, menuid, item)
- {
- // Menu was closed
- if (item == MENU_EXIT)
- {
- MENU_PAGE_ITEMS = 0
- menu_destroy(menuid)
- return PLUGIN_HANDLED;
- }
- // Remember items menu page
- MENU_PAGE_ITEMS = item / 7
- // Dead players are not allowed to buy items
- if (!is_user_alive(id))
- {
- menu_destroy(menuid)
- return PLUGIN_HANDLED;
- }
- // Retrieve item id
- new itemdata[2], dummy, itemid
- menu_item_getinfo(menuid, item, dummy, itemdata, charsmax(itemdata), _, _, dummy)
- itemid = itemdata[0]
- // Attempt to buy the item
- Buy_Item(id, itemid)
- menu_destroy(menuid)
- return PLUGIN_HANDLED;
- }
- // Buy Item
- Buy_Item(id, itemid, ignorecost = 0)
- {
- if (g_bSetUsed[itemid])
- {
- ignorecost = g_bIgnoreCost[itemid]
- }
- // Execute item select attempt forward
- ExecuteForward(g_iForwards[FW_ITEM_SELECT_PRE], g_iForwardReturn, id, itemid, ignorecost)
- // Item available to player?
- if (g_iForwardReturn >= ZE_ITEM_UNAVAILABLE)
- return;
- // Execute item selected forward
- ExecuteForward(g_iForwards[FW_ITEM_SELECT_POST], g_iForwardReturn, id, itemid, ignorecost)
- }
- // Natives
- public native_ze_register_item(plugin_id, num_params)
- {
- new szItem_Name[32], iItem_Cost, iItem_Limit
- // Get the Data from first Parameter in the native (Item Name)
- get_string(1, szItem_Name, charsmax(szItem_Name))
- // Get the Second Parameter (Item Cost)
- iItem_Cost = get_param(2)
- // Get limit third parameter
- iItem_Limit = get_param(3)
- if (strlen(szItem_Name) < 1)
- {
- // Can't leave item name empty
- log_error(AMX_ERR_NATIVE, "[ZE] Can't register item with an empty name")
- return ZE_WRONG_ITEM // Same as return -1
- }
- new iIndex, szItemName[32]
- // Loop from 0 to max items amount
- for (iIndex = 0; iIndex < g_iItemCount; iIndex++)
- {
- ArrayGetString(g_szItemRealName, iIndex, szItemName, charsmax(szItemName))
- if (equali(szItem_Name, szItemName))
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Item already registered (%s)", szItemName)
- return ZE_WRONG_ITEM; // Return -1
- }
- }
- // Load settings from extra items file
- new szItemRealName[32]
- copy(szItemRealName, charsmax(szItemRealName), szItem_Name)
- ArrayPushString(g_szItemRealName, szItemRealName)
- // Name
- if (!amx_load_setting_string(ZE_EXTRAITEM_FILE, szItemRealName, "NAME", szItem_Name, charsmax(szItem_Name)))
- amx_save_setting_string(ZE_EXTRAITEM_FILE, szItemRealName, "NAME", szItem_Name)
- ArrayPushString(g_szItemName, szItem_Name)
- // Cost
- if (!amx_load_setting_int(ZE_EXTRAITEM_FILE, szItemRealName, "COST", iItem_Cost))
- amx_save_setting_int(ZE_EXTRAITEM_FILE, szItemRealName, "COST", iItem_Cost)
- ArrayPushCell(g_iItemCost, iItem_Cost)
- // Limit
- if (!amx_load_setting_int(ZE_EXTRAITEM_FILE, szItemRealName, "LIMIT", iItem_Limit))
- amx_save_setting_int(ZE_EXTRAITEM_FILE, szItemRealName, "LIMIT", iItem_Limit)
- ArrayPushCell(g_iItemLimit, iItem_Limit)
- g_iItemCount++
- return g_iItemCount - 1
- }
- public native_ze_show_items_menu(plugin_id, num_params)
- {
- new id = get_param(1)
- if (!is_user_connected(id))
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player (%d)", id)
- return false;
- }
- Cmd_Items(id)
- return true
- }
- public native_ze_force_buy_item(plugin_id, num_params)
- {
- new id = get_param(1)
- if (!is_user_connected(id))
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player (%d)", id)
- return false;
- }
- new item_id = get_param(2)
- if (item_id < 0 || item_id >= g_iItemCount)
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid item id (%d)", item_id)
- return false;
- }
- new ignorecost = get_param(3)
- g_bIgnoreCost[item_id] = ignorecost;
- Buy_Item(id, item_id, ignorecost)
- return true;
- }
- public native_ze_get_item_id(plugin_id, num_params)
- {
- new szRealName[32]
- get_string(1, szRealName, charsmax(szRealName))
- new index, szItemName[32]
- for (index = 0; index < g_iItemCount; index++)
- {
- ArrayGetString(g_szItemRealName, index, szItemName, charsmax(szItemName))
- if (equali(szRealName, szItemName))
- return index
- }
- return ZE_WRONG_ITEM
- }
- public native_ze_get_item_cost(plugin_id, num_params)
- {
- new item_id = get_param(1)
- if (item_id < 0 || item_id >= g_iItemCount)
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid item id (%d)", item_id)
- return ZE_WRONG_ITEM;
- }
- return ArrayGetCell(g_iItemCost, item_id);
- }
- public native_ze_add_text_to_item(plugin_id, num_params)
- {
- new szText[32]
- get_string(1, szText, charsmax(szText))
- format(g_szAdditionalMenuText, charsmax(g_szAdditionalMenuText), "%s%s", g_szAdditionalMenuText, szText)
- }
- public native_ze_get_item_limit(plugin_id, num_params)
- {
- new item_id = get_param(1)
- if (item_id < 0 || item_id >= g_iItemCount)
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid item id (%d)", item_id)
- return ZE_WRONG_ITEM;
- }
- return ArrayGetCell(g_iItemLimit, item_id);
- }
- public native_ze_is_valid_itemid(plugin_id, num_params)
- {
- new item_id = get_param(1)
- if (item_id < 0 || item_id >= g_iItemCount)
- {
- return false;
- }
- return true;
- }
- public native_ze_get_item_name(plugin_id, num_params)
- {
- new item_id = get_param(1)
- if (item_id < 0 || item_id >= g_iItemCount)
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid item id (%d)", item_id)
- return ZE_WRONG_ITEM;
- }
- new szName[32]
- ArrayGetString(g_szItemName, item_id, szName, charsmax(szName))
- new iLen = get_param(3)
- set_string(2, szName, iLen)
- return true;
- }
- public native_ze_set_ignorecost(iItemid, bool:bSet)
- {
- g_bSetUsed[iItemid] = true;
- g_bIgnoreCost[iItemid] = bSet;
- }
- public native_ze_get_ignorecost(iItemid)
- {
- return g_bIgnoreCost[iItemid];
- }
ze_set_ignorecost(iItemid, bool:bSet)
so we ignore the cost in some cases.Example: In the Weapons Ammo extra item we need to block user from buying weapon ammo for knife and grenades and show a message that he can't buy when he holding knife or grenades.
We will do the check in two places, in the pre buy we will check if he carry knife/grenades we will set ignorecost to true. Then in the post buy we will check again same conditions and if he holding knife/grenades we will show message and return.
Here is the final code:
- #include <zombie_escape>
- native ze_set_ignorecost(iItemid, bool:bSet)
- new const g_iClipSize[] =
- {
- 0,
- 13, //CSW_P228
- 0,
- 10, //CSW_SCOUT
- 0, //CSW_HEGRENADE
- 7, //CSW_XM1014
- 0, //CSW_C4
- 30, //CSW_MAC10
- 30, //CSW_AUG
- 0, //CSW_SMOKEGRENADE
- 30, //CSW_ELITE
- 20, //CSW_FIVESEVEN
- 25, //CSW_UMP45
- 30, //CSW_SG550
- 35, //CSW_GALIL
- 25, //CSW_FAMAS
- 12, //CSW_USP
- 20, //CSW_GLOCK18
- 10, //CSW_AWP
- 30, //CSW_MP5NAVY
- 100, //CSW_M249
- 8, //CSW_M3
- 30, //CSW_M4A1
- 30, //CSW_TMP
- 20, //CSW_G3SG1
- 0, //CSW_FLASHBANG
- 7, //CSW_DEAGLE
- 30, //CSW_SG552
- 30, //CSW_AK47
- 0, //CSW_KNIFE
- 50 //CSW_P90
- }
- new const g_iAmmoType[] =
- {
- 0,
- 9, //CSW_P228
- 0,
- 2, //CSW_SCOUT
- 12, //CSW_HEGRENADE
- 5, //CSW_XM1014
- 14, //CSW_C4
- 6, //CSW_MAC10
- 4, //CSW_AUG
- 13, //CSW_SMOKEGRENADE
- 10, //CSW_ELITE
- 7, //CSW_FIVESEVEN
- 6, //CSW_UMP45
- 4, //CSW_SG550
- 4, //CSW_GALIL
- 4, //CSW_FAMAS
- 6, //CSW_USP
- 10, //CSW_GLOCK18
- 1, //CSW_AWP
- 10, //CSW_MP5NAVY
- 3, //CSW_M249
- 5, //CSW_M3
- 4, //CSW_M4A1
- 10, //CSW_TMP
- 2, //CSW_G3SG1
- 11, //CSW_FLASHBANG
- 8, //CSW_DEAGLE
- 4, //CSW_SG552
- 2, //CSW_AK47
- 0, //CSW_KNIFE
- 7 //CSW_P90
- }
- // Default Sound
- new const g_szBuyAmmoSound[] = "items/9mmclip1.wav"
- // Variables
- new g_iItemID
- public plugin_init()
- {
- register_plugin("[ZE] Items: Weapon Ammo", ZE_VERSION, AUTHORS)
- // Register our item
- g_iItemID = ze_register_item("Weapon Ammo", 5, 0)
- }
- public ze_select_item_pre(id, itemid)
- {
- // Return Available and we will block it in Post, So it dosen't affect other plugins
- if (itemid != g_iItemID)
- return ZE_ITEM_AVAILABLE
- // Available for Humans only, So don't show it for zombies
- if (ze_is_user_zombie(id))
- return ZE_ITEM_DONT_SHOW
- // Itemid is our item?
- if (itemid == g_iItemID)
- {
- // Get Weapon ID
- new iWpnID, iAmmo
- iWpnID = get_user_weapon(id, _, iAmmo)
- // Here if player weapon is any of this we will ignore cost, so we can show message in post and no coins reduced from player
- if (iWpnID == CSW_KNIFE || iWpnID == CSW_FLASHBANG || iWpnID == CSW_HEGRENADE || iWpnID == CSW_SMOKEGRENADE)
- {
- ze_set_ignorecost(g_iItemID, true)
- }
- else
- {
- // If not don't ignore cost - normal proccess
- ze_set_ignorecost(g_iItemID, false)
- }
- }
- return ZE_ITEM_AVAILABLE
- }
- public ze_select_item_post(id, itemid)
- {
- // This is not our item, Block it here
- if (itemid != g_iItemID)
- return
- // Get Weapon ID
- new iWpnID, iAmmo
- iWpnID = get_user_weapon(id, _, iAmmo)
- // Here we send any messages, and use return to block item give and no coins will be reduced as we ignored cost in this case
- if (iWpnID == CSW_KNIFE)
- {
- ze_colored_print(id, "!tYou can't buy ammo for knife!y!")
- return
- }
- if (iWpnID == CSW_FLASHBANG || iWpnID == CSW_HEGRENADE || iWpnID == CSW_SMOKEGRENADE)
- {
- ze_colored_print(id, "!tYou can't buy ammo for grenades!y!")
- return
- }
- // Increase his Back Pack Ammo, And play buy BP sound + Hud Flash
- rg_set_user_bpammo(id, WeaponIdType:iWpnID, iAmmo + g_iClipSize[iWpnID])
- emit_sound(id, CHAN_ITEM, g_szBuyAmmoSound, 1.0, ATTN_NORM, 0, PITCH_NORM)
- Show_Given_BPAmmo(id, g_iAmmoType[iWpnID], g_iClipSize[iWpnID])
- }
- You need to ignorecost in ze_select_item_pre() in specific condition
- In ze_select_item_post() you will block whatever you need in same specific condition as pre.
A general example:
- #include <zombie_escape>
- // Default Sound
- new const g_szBuyAmmoSound[] = "items/9mmclip1.wav"
- // Variables
- new g_iItemID
- public plugin_init()
- {
- register_plugin("[ZE] Items: Fire Nade", ZE_VERSION, AUTHORS)
- // Register our item
- g_iItemID = ze_register_item("Frost Nade", 50, 0)
- }
- public ze_select_item_pre(id, itemid)
- {
- // Return Available and we will block it in Post, So it dosen't affect other plugins
- if (itemid != g_iItemID)
- return ZE_ITEM_AVAILABLE
- // Verify first it's our item
- if (itemid == g_iItemID)
- {
- if (Your condition == true)
- {
- // Ignore the cost
- ze_set_ignorecost(g_iItemID, true)
- }
- else
- {
- // Here your condition not true, so we will not ignore the cost
- ze_set_ignorecost(g_iItemID, false)
- }
- }
- // Available for Humans only, So don't show it for zombies
- if (ze_is_user_zombie(id))
- return ZE_ITEM_DONT_SHOW
- return ZE_ITEM_AVAILABLE
- }
- public ze_select_item_post(id, itemid)
- {
- // This is not our item, Block it here
- if (itemid != g_iItemID)
- return
- // Attempt to block the give item code, and show message
- if (Your condition == true)
- {
- // This is the right way
- show message here
- return
- }
- // Get Weapon ID
- new iWpnID = get_weaponid("weapon_smokegrenade")
- // Player Don't have Frost Grenade then give him
- if (rg_get_user_bpammo(id, WeaponIdType:iWpnID) == 0)
- {
- rg_give_item(id, "weapon_smokegrenade", GT_APPEND)
- }
- else
- {
- // Player have, Increase his Back Pack Ammo, And play buy BP sound + Hud Flash
- rg_set_user_bpammo(id, WeaponIdType:iWpnID, rg_get_user_bpammo(id, WeaponIdType:iWpnID) + 1)
- emit_sound(id, CHAN_ITEM, g_szBuyAmmoSound, 1.0, ATTN_NORM, 0, PITCH_NORM)
- Show_Given_BPAmmo(id, 13, 1) // Smoke Grenade AmmoType Const = 13
- }
- }]