Raised This Month: $ Target: $400
 0% 

[SOLVED] Multiple menus with same array, function and handler


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 05-21-2016 , 14:05   [SOLVED] Multiple menus with same array, function and handler
Reply With Quote #1

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?

Last edited by OciXCrom; 06-27-2016 at 17:46.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
 



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 18:38.


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