Raised This Month: $51 Target: $400
 12% 

Solved menu_additem flag


Post New Thread Reply   
 
Thread Tools Display Modes
lexzor
Veteran Member
Join Date: Nov 2020
Old 03-19-2023 , 11:19   Re: menu_additem flag
Reply With Quote #11

i made some changes to your code and explained them

PHP Code:
#include <amxmodx>
#include <cstrike>
#include <hamsandwich>

#define DEFAULT_MODEL 1

enum _:Models
{
    
Name[20],
    
NameModel[20],
    
RUTA[126],
    
FlagAdmin[6]
}

// creating a global variable to store user selected model
// here you must put the g_eModels model id starting from 0 to how many models do you have
new g_iUserModel[MAX_PLAYERS 1];

new const 
g_eModels[][Models] =
{
    
//changed the flag as a string (you must use read_flags to transform it in bytes and use get_user_flags)
    
"hutao""hutao""models/player/hutao/hutao.mdl" "a" },
    { 
"spiderman""admin_spiderman""models/player/donald/admin_spiderman.mdl""b"},
    { 
"woailuo""flash_woailuo""models/player/donald/flash_woailuo.mdl""c"}
}

public 
plugin_init()
{
    
register_clcmd("say /test""ModelMenu")
    
RegisterHam(Ham_Spawn"player""fwPlayerSpawn"true//true means the forward will be called after the player is spawned
}

public 
plugin_precache()
{
    for(new 
isizeof(g_eModels); i++)
    {
        
//better check if the model exists so the server won't crash
        //doesn't make any sense here but it s a good practice
        
if(file_exists(g_eModels[i][RUTA]))
        {
            
precache_generic(g_eModels[i][RUTA])
        }
        else 
        {
            
log_amx("Model ^"%s^" does not exists"g_eModels[i][RUTA])
        }
    }
}

public 
client_connect(id)
{
    
// reset user model to -1 that means no model or default_model id on user client id so the player that join the server won't have the previous user selected model
    
g_iUserModel[id] = DEFAULT_MODEL
}

public 
fwPlayerSpawn(id)
{
    
//check if player is alive and has a selected model
    //you must check if user is alive even if you create the forward where the post parameter is true
    
if(is_user_alive(id) && g_iUserModel[id] > -1)
    {
        static 
iModel
        iModel 
g_iUserModel[id]
        
cs_set_user_model(idg_eModels[iModel])
    }
}

public 
ModelMenu(id)
{
    new 
iMenu menu_create("Models Menu""ModelsHandler")
    new 
szMenuItem[128];

    for(new 
isizeof(g_eModels); i++)
    {
        
//this is just to add [#] to the selected item so the player will know what skin he's using
        
formatex(szMenuItemcharsmax(szMenuItem), "%s%s"g_eModels[i][Name], (g_iUserModel[id] == i) ? "\y[\d#\y]" "")

        
// didn't event need to pass the flag access
        
menu_additem(iMenuszMenuItem)
    }

    
//i'm checking if the user is connected to display the menu because without you will get an console error if the player leaves the server and the menu is opened
    
if(is_user_connected(id))
    {
        
menu_display(idiMenu0)
    }
}

public 
ModelsHandler(idiMenuiItem){
    if(
iItem == MENU_EXIT)
    {
        
menu_destroy(iMenu);
        return 
PLUGIN_HANDLED;
    }

    
//doesn't even need to pass the flag

    
if(!(get_user_flags(id) & read_flags(g_eModels[iItem][FlagAdmin])))
    {
        
client_print(idprint_chat"You don't have the access to use ^"%s^" model"g_eModels[iItem][Name])
        
ModelMenu(id)
        return 
PLUGIN_HANDLED
    
}

    
cs_set_user_model(idg_eModels[iItem][NameModel])
    
client_print(idprint_chat"Selected Model: %s"g_eModels[iItem][Name])
    
g_iUserModel[id] = iItem
    
    
return PLUGIN_CONTINUE


Last edited by lexzor; 03-19-2023 at 11:32.
lexzor is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 03-19-2023 , 13:54   Re: menu_additem flag
Reply With Quote #12

anything at this point works
imo using return too many times it can make the code hard to read and complex because the function doesnt properly execute and it will be harder to debug later on
that doesnt mean in any way that you should stop using return but it's smarter to do so when necessary
here's a proper example
Quote:
Originally Posted by Bugsy View Post
Hardly tested
PHP Code:

#include <amxmodx>
#include <fakemeta>
#include <cstrike>
#include <engine>
#include <hamsandwich>

new const Version[] = "0.1";

enum WeaponSlots
{
    
Slot1 1,
    
Slot2
}
    
public 
plugin_init() 
{
    
register_plugin"Drop BP Ammo with Weapon" Version "bugsy" );
   
    
register_forwardFM_SetModel "SetModel" );
    
RegisterHamHam_AddPlayerItem "player" "AddPlayerItem" );
}

public 
SetModeliEntity , const szModel[] )
{
    new 
szClassname32 ] , iWeaponEntID iWeaponSlot iBPAmmo;
    
    
peviEntity pev_classname szClassname charsmaxszClassname ) );
    
    if ( 
equal szClassname "weaponbox" ) )
    {
        if ( 
pev_valid( ( iWeaponEntID GetWeaponEntityFromWeaponboxiEntity iWeaponSlot ) ) ) )
        {
            if ( ( 
iBPAmmo cs_get_user_bpammopeviEntity pev_owner ) , cs_get_weapon_idiWeaponEntID ) ) ) )
                
set_peviWeaponEntID pev_iuser2 iBPAmmo );
        }
    }


public 
AddPlayerItemiPlayer iEntity )
{
    new 
iBPAmmo peviEntity pev_iuser2 );
    
    if ( 
iBPAmmo )
        
cs_set_user_bpammoiPlayer cs_get_weapon_idiEntity ) , iBPAmmo );
}

GetWeaponEntityFromWeaponboxiWeaponBox , &iWeaponSlot )
{
    new const 
m_rgpPlayerItems_Weaponbox] = { 34 35 , ... };
    new 
iWeaponEntID;

    for ( new 
WeaponSlots:iSlot Slot1 iSlot <= Slot2 iSlot++ )
    {
        if ( ( 
iWeaponEntID get_pdata_cbaseiWeaponBox m_rgpPlayerItems_Weaponbox] + _:iSlot ) ) > )
        {
            
iWeaponSlot _:iSlot;
            break;
        }
    }    
    
    return ( 
iWeaponEntID ) ? iWeaponEntID 0;

__________________

Last edited by bigdaddy424; 03-19-2023 at 14:23.
bigdaddy424 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:12.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode