AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED] Multiple menus with same array, function and handler (https://forums.alliedmods.net/showthread.php?t=282952)

OciXCrom 05-21-2016 14:05

[SOLVED] Multiple menus with same array, function and handler
 
I'm trying to make a plugin which will allow you to create menus through a file, so I need suggestions on how to do that. For now I made the plugin for a single menu only, but I want to make something that will look like [New Menu = "Menu Title"] in the file and add the options for it beneath this line. That's the easy part. I don't know how to make more menus use the same array, function and handler.

I have this:

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <colorchat>

#define PLUGIN_VERSION "1.0"

enum
{
    
SECTION_SETTINGS,
    
SECTION_MENUITEMS
}

enum _:Settings
{
    
stgTitle[128],
    
stgPrefix[32],
    
stgBack[32],
    
stgNext[32],
    
stgExit[32],
    
stgFlag[2],
    
stgAliveOnly,
    
stgPerPage
}

enum _:Items
{
    
iName[64],
    
iCommand[32],
    
iFlag[2]
}

new 
g_eSettings[Settings]
new Array:
g_aMenuItems
new g_iTotalItems

public plugin_init()
{
    
register_plugin("Simple Menu"PLUGIN_VERSION"OciXCrom")
    
register_cvar("SimpleMenu"PLUGIN_VERSIONFCVAR_SERVER|FCVAR_SPONLY|FCVAR_UNLOGGED)
    
g_aMenuItems ArrayCreate(Items)
    
fileRead()
}

public 
plugin_end()
    
ArrayDestroy(g_aMenuItems)

fileRead()
{
    new 
szConfigsName[256], szFilename[256]
    
get_configsdir(szConfigsNamecharsmax(szConfigsName))
    
formatex(szFilenamecharsmax(szFilename), "%s/SimpleMenu.ini"szConfigsName)
    new 
iFilePointer fopen(szFilename"rt")
    
    if(
iFilePointer)
    {
        new 
szData[160], szKey[32], szValue[128], iSection
        
new eItem[Items]
        
        while(!
feof(iFilePointer))
        {
            
fgets(iFilePointerszDatacharsmax(szData))
            
trim(szData)
            
            switch(
szData[0])
            {
                case 
EOS';': continue
                case 
'[':
                {
                    if(
szData[strlen(szData) - 1] == ']')
                    {
                        if(
containi(szData"settings") != -1)
                            
iSection SECTION_SETTINGS
                        
else if(containi(szData"items") != -1)
                            
iSection SECTION_MENUITEMS
                    
}
                    else continue
                }
                default:
                {
                    switch(
iSection)
                    {
                        case 
SECTION_SETTINGS:
                        {
                            
strtok(szDataszKeycharsmax(szKey), szValuecharsmax(szValue), '=')
                            
trim(szKey); trim(szValue)
                            
                            if(
equal(szKey"MENU_TITLE"))
                                
copy(g_eSettings[stgTitle], charsmax(g_eSettings[stgTitle]), szValue)
                            else if(
equal(szKey"MENU_PREFIX"))
                                
copy(g_eSettings[stgPrefix], charsmax(g_eSettings[stgPrefix]), szValue)
                            else if(
equal(szKey"MENU_BACK"))
                                
copy(g_eSettings[stgBack], charsmax(g_eSettings[stgBack]), szValue)
                            else if(
equal(szKey"MENU_NEXT"))
                                
copy(g_eSettings[stgNext], charsmax(g_eSettings[stgNext]), szValue)
                            else if(
equal(szKey"MENU_EXIT"))
                                
copy(g_eSettings[stgExit], charsmax(g_eSettings[stgExit]), szValue)
                            else if(
equal(szKey"MENU_FLAG"))
                                
copy(g_eSettings[stgFlag], charsmax(g_eSettings[stgFlag]), szValue)
                            else if(
equal(szKey"MENU_ALIVEONLY"))
                                
g_eSettings[stgAliveOnly] = str_to_num(szValue)
                            else if(
equal(szKey"MENU_ITEMS_PER_PAGE"))
                                
g_eSettings[stgPerPage] = str_to_num(szValue)
                            else if(
equal(szKey"MENU_OPEN"))
                            {
                                while(
szValue[0] != && strtok(szValueszKeycharsmax(szKey), szValuecharsmax(szValue), ','))
                                {
                                    
trim(szKey); trim(szValue)
                                    
register_clcmd(szKey"menuMain")
                                }
                            }
                        }
                        case 
SECTION_MENUITEMS:
                        {
                            
parse(szDataeItem[iName], charsmax(eItem[iName]), eItem[iCommand], charsmax(eItem[iCommand]), eItem[iFlag], charsmax(eItem[iFlag]))
                            
ArrayPushArray(g_aMenuItemseItem)
                            
eItem[iFlag] = EOS
                            g_iTotalItems
++
                        }
                    }
                }
            }            
        }
        
        
fclose(iFilePointer)
    }
}

public 
menuMain(id)
{
    if(
g_eSettings[stgFlag][0] != '0' && !(get_user_flags(id) & read_flags(g_eSettings[stgFlag])))
    {
        
ColorChat(idRED"^3%s ^1You have no access to this menu."g_eSettings[stgPrefix])
        return 
PLUGIN_HANDLED
    
}
    
    if(!
get_alive_access(id))
    {
        
ColorChat(idRED"^3%s ^1You need to be %s to use this menu."g_eSettings[stgPrefix], g_eSettings[stgAliveOnly] == "alive" "dead")
        return 
PLUGIN_HANDLED
    
}
    
    new 
eItem[Items], iMenu menu_create(g_eSettings[stgTitle], "handlerMain")
    
    for(new 
ig_iTotalItemsi++)
    {
        
ArrayGetArray(g_aMenuItemsieItem)
        
menu_additem(iMenueItem[iName], ""read_flags(eItem[iFlag]))
    }
    
    
menu_setprop(iMenuMPROP_BACKNAMEg_eSettings[stgBack])
    
menu_setprop(iMenuMPROP_NEXTNAMEg_eSettings[stgNext])
    
menu_setprop(iMenuMPROP_EXITNAMEg_eSettings[stgExit])
    
menu_setprop(iMenuMPROP_PERPAGEg_eSettings[stgPerPage])
    
menu_display(idiMenu0)
    return 
PLUGIN_HANDLED
}

public 
handlerMain(idiMenuiItem)
{
    if(!
get_alive_access(id) || iItem == MENU_EXIT) {}
    else
    {
        new 
eItem[Items]
        
ArrayGetArray(g_aMenuItemsiItemeItem)
        
client_cmd(ideItem[iCommand])
    }
    
    
menu_destroy(iMenu)
    return 
PLUGIN_HANDLED
}

get_alive_access(id)
    return ((
g_eSettings[stgAliveOnly] == && !is_user_alive(id)) || (g_eSettings[stgAliveOnly] == && is_user_alive(id))) ? false true 

And an example file:

PHP Code:

[Menu Settings]
MENU_TITLE My Simple Menu
MENU_BACK 
= \yPrevious Page
MENU_NEXT 
= \yNext Page
MENU_EXIT 
= \rClose
MENU_OPEN 
say /menu say_team /menu
MENU_FLAG 
0
MENU_ALIVEONLY 
0
MENU_ITEMS_PER_PAGE 
7
MENU_PREFIX 
= [Menu]

[
Menu Items]
;<
Name> <Command> [Flag]

"Show Rules" "say /rules"
"Hats Menu" "say /hats" 
b
"Turn the Radio On" "say /radio"
"Leave the Server" "disconnect"
"Command #5" ""
"Command #6" ""
"Command #7" "" 
d
"Command #8" "" d
"Command #9" ""
"Command #10" "" 

So I want to get something like this:

PHP Code:

[New Menu "My Simple Menu"]

[
Menu Settings]
MENU_BACK = \yPrevious Page
MENU_NEXT 
= \yNext Page
MENU_EXIT 
= \rClose
MENU_OPEN 
say /menu say_team /menu
MENU_FLAG 
0
MENU_ALIVEONLY 
0
MENU_ITEMS_PER_PAGE 
7
MENU_PREFIX 
= [Menu]

[
Menu Items]
;<
Name> <Command> [Flag]

"Show Rules" "say /rules"
"Hats Menu" "say /hats" 
b
"Turn the Radio On" "say /radio"
"Leave the Server" "disconnect"
"Command #5" ""
"Command #6" ""
"Command #7" "" 
d
"Command #8" "" d
"Command #9" ""
"Command #10" ""

[New Menu "My Simple Menu 2"]

[
Menu Settings]
MENU_BACK = \yPrevious Page
MENU_NEXT 
= \yNext Page
MENU_EXIT 
= \rClose
MENU_OPEN 
say /menu2 say_team /menu2
MENU_FLAG 
0
MENU_ALIVEONLY 
0
MENU_ITEMS_PER_PAGE 
7
MENU_PREFIX 
= [Menu]

[
Menu Items]
;<
Name> <Command> [Flag]

"Show Rules" "say /rules"
"Hats Menu" "say /hats" 
b
"Turn the Radio On" "say /radio"
"Leave the Server" "disconnect"
"Command #5" ""
"Command #6" ""
"Command #7" "" 
d
"Command #8" "" d
"Command #9" ""
"Command #10" "" 

Any ideas?

fysiks 05-21-2016 14:30

Re: Multiple menus with same array, function and handler
 
The easiest way is to simply put the command (e.g. "disconnect") in the info argument of menu_additem() the you have everything you need in the handler without even knowing what menu was chosen.

FYI, prefixing a variable with "i" means that it is an integer. The variables in your "items" enum should be prefixed with "sz" since you are using them as strings.

OciXCrom 05-21-2016 17:39

Re: Multiple menus with same array, function and handler
 
What about storing the items in an array and displaying different ones with different menu commands? Same goes for different menu settings.

PS: I like using different prefixes in arrays, so the "i" means "item" in that one.

OciXCrom 06-27-2016 17:45

Re: Multiple menus with same array, function and handler
 
Solved - https://forums.alliedmods.net/showthread.php?t=284324
I assigned a menu ID to every registered command.


All times are GMT -4. The time now is 18:38.

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