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

Solved Combine the menus


Post New Thread Reply   
 
Thread Tools Display Modes
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 05-23-2021 , 08:22   Re: Combine the menus
Reply With Quote #11

You will need a global variable to determine which menu should be displayed.

Code:
#include <amxmodx>

enum
{
	CustomMenu1,
	CustomMenu2,
	CustomMenu3
}

new gPlayerMenu[33]

public plugin_init()
{
	register_plugin("Plugin", "Version", "Author")

	register_clcmd("say", "CommandSay")
}

public CommandSay(index)
{
	new szArgs[192]
	read_args(szArgs, charsmax(szArgs))
	remove_quotes(szArgs)
	trim(szArgs)

	new szCmd[35]
	parse(szArgs, szCmd, charsmax(szCmd))
	trim(szCmd)
	
	if (szCmd[0] != '/')
		return PLUGIN_CONTINUE

	new iMenu = -1
	if (equali(szCmd[1], "cmd1"))
		iMenu = CustomMenu1
	else if (equali(szCmd[1], "cmd2"))
		iMenu = CustomMenu2
	else if (equali(szCmd[1], "cmd3"))
		iMenu = CustomMenu3

	if (iMenu == -1)
		return PLUGIN_CONTINUE

	gPlayerMenu[index] = iMenu
	ShowCustomMenu(index)
	return PLUGIN_HANDLED
}

ShowCustomMenu(index)
{
	new szBuf[128]

	switch (gPlayerMenu[index])
	{
		case CustomMenu2: copy(szBuf, charsmax(szBuf), "Custom Menu 2")
		case CustomMenu3: copy(szBuf, charsmax(szBuf), "Custom Menu 3")
		default: copy(szBuf, charsmax(szBuf), "Custom Menu 1")
	}

	new iMenu = menu_create(szBuf, "HandleCustomMenu")

	menu_additem(iMenu, "Item")

	menu_display(index, iMenu)
}

public HandleCustomMenu(index, iMenu, iItem)
{
	if (iItem == MENU_EXIT)
	{
		menu_destroy(iMenu)
		return PLUGIN_HANDLED
	}

	switch (gPlayerMenu[index])
	{
		case CustomMenu2:
		{
			// do x
		}
		case CustomMenu3:
		{
			// do y
		}
		default:
		{
			// do z
		}
	}

	menu_destroy(iMenu)
	return PLUGIN_HANDLED
}
__________________









Last edited by CrazY.; 05-23-2021 at 08:23.
CrazY. is offline
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 05-23-2021 , 09:08   Re: Combine the menus
Reply With Quote #12

Quote:
Originally Posted by fysiks View Post
If you wanted to make it "easy" for beginners (not a good goal) then you would make three completely separate menus. Just implement something that works (you've been given several options) and then you can modify it later.

Even if it is possible to do what you want to do (still not really sure what you're talking about) it will most likely be more complex anyways.
but this idea will be more helpful and will fix hard coding, if you still don't understand my code and what i want to do check this code by CraZy.

Quote:
Originally Posted by jimaway View Post
pass your menu type to the handler in the item info field along with the userid
I will do this if i used three menu for one handler, But i want to make three commands for one menu!

Quote:
Originally Posted by Natsheh View Post
Destroy the menu right after retrieving the info there is no need to keep it alive.
I dont understand !
Do you mean i have to do this ! ?
Code:
public CustomMenu(id, iMenu, iItem) {     if (iItem == MENU_EXIT)     {         menu_destroy(iMenu)         return PLUGIN_HANDLED     }                        // items !!     menu_destroy(iMenu)     return PLUGIN_HANDLED }

Quote:
Originally Posted by Natsheh View Post
Also dont be shy to post the whole code no ones gonna steal anything its not that well coded either.
I already did !!, also i will share my plugin on public, I doing optimizing the code and fixing hard code.

Quote:
Originally Posted by CrazY. View Post
You will need a global variable to determine which menu should be displayed.
Thanks a lot, I will test it
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.
Supremache is offline
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 05-23-2021 , 13:40   Re: Combine the menus
Reply With Quote #13

I Did it thanks all and thanks a lot CrazY.

Code:
enum _:eCommandSettings {     iMenuType,     iCommands[32],     iFlags }; new const g_iCommandSettings[][eCommandSettings] = {     /*     * Dont touch this, Commands , Cmd Flags     */     { 1, "xplist", ADMIN_ALL},     { 2, "resetdata" , ADMIN_RCON },     { 3, "manage" , ADMIN_RCON } } public CommandSay(id) {     new szArgs[192]         read_args(szArgs, charsmax(szArgs))     remove_quotes(szArgs)     trim(szArgs)     new szCmd[35]     parse(szArgs, szCmd, charsmax(szCmd))     trim(szCmd)         if (szCmd[0] != '/')         return PLUGIN_CONTINUE         new iMenu = -1         for(new  i = 0; i <= charsmax(g_iCommandSettings); i++)     {         if (equali(szCmd[1], g_iCommandSettings[i][iCommands]))         {             iMenu = g_iCommandSettings[i][iMenuType]                         if (iMenu == -1)                 return PLUGIN_CONTINUE                         g_iPlayerMenu[id] = iMenu                         PlayerRank(id, g_iCommandSettings[i][iFlags])         }     }         return PLUGIN_HANDLED } public PlayerRank(id, iFlag) {     if(!access(id, iFlag))     {         Print(id, "!g[RS]!n You dont have access.");         return PLUGIN_HANDLED     }                 new iPlayers[MAX_PLAYERS], iPnum, szUserID[32], szTitle[128], iMenu         switch ( g_iPlayerMenu[id] )     {         case 2: MenuTitle("\yRank System: \rReset Data")         case 3: MenuTitle("\yRank System: \rManage Players")         default: MenuTitle("\yRank System: \rXP List")     }         iMenu = menu_create(szTitle, "Handler")     get_players(iPlayers, iPnum, "ch"); SortCustom1D(iPlayers, iPnum, "sort_players_by_xp")         for(new szItem[1024], iPlayer, i; i < iPnum; i++)     {         iPlayer = iPlayers[i]                 MenuInfo("\d[%i XP] \w%s \r[\yLevel %i: %s\r]")         formatex( szUserID, charsmax(szUserID), "%d", get_user_userid(iPlayer))         menu_additem(iMenu, szItem, szUserID)     }         menu_setprop(iMenu, MPROP_BACKNAME, "Previous page")     menu_setprop(iMenu, MPROP_NEXTNAME, "Next page")     menu_setprop(iMenu, MPROP_EXITNAME, "\rClose")     menu_display(id, iMenu)     return PLUGIN_HANDLED } public Handler(id, iMenu, iItem) {     if(iItem != MENU_EXIT)     {         new iAccess, iCallback, iData[6],iName[64];         menu_item_getinfo(iMenu, iItem, iAccess, iData, charsmax(iData), iName, charsmax(iName), iCallback);             new iPlayer = str_to_num(iData);                 switch ( g_iPlayerMenu[id] )         {             case 2: ResetPlayerRank(id, iPlayer);             case 3: ManagePlayerRank(id, iPlayer);             default: return PLUGIN_HANDLED         }     }     menu_destroy(iMenu)     return PLUGIN_HANDLED }
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.

Last edited by Supremache; 05-23-2021 at 13:45.
Supremache is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-23-2021 , 14:49   Re: Combine the menus
Reply With Quote #14

You just literally did exactly what I said to do originally . . .

A couple other suggestions:
  • You need to remove the return in the switch because the menu will not get properly destroyed and will cause a memory leak.
  • You should break out of the for loop in your conditional because you no longer need to check for the other commands.
__________________

Last edited by fysiks; 05-23-2021 at 15:15.
fysiks is offline
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 05-23-2021 , 15:45   Re: Combine the menus
Reply With Quote #15

Quote:
Originally Posted by fysiks View Post
You just literally did exactly what I said to do originally . .
Really ?!!!, I don't think that or I didn't understand..

Quote:
Originally Posted by fysiks View Post
  • You need to remove the return in the switch because the menu will not get properly destroyed and will cause a memory leak.
PHP Code:
public Handler(idiMenuiItem)
{
    if(
iItem != MENU_EXIT)
    {
        new 
iAccessiCallbackiData[6],iName[64];
        
menu_item_getinfo(iMenuiItemiAccessiDatacharsmax(iData), iNamecharsmax(iName), iCallback);
    
        new 
iPlayer str_to_num(iData);
        
        switch ( 
g_iPlayerMenu[id] )
        {
            case 
2ResetPlayerRank(idiPlayer);
            case 
3ManagePlayerRank(idiPlayer);
            default: 
menu_destroy(iMenu)
        }
    }
    
//menu_destroy(iMenu)
    
return PLUGIN_HANDLED

Quote:
Originally Posted by fysiks View Post
  • You should break out of the for loop in your conditional because you no longer need to check for the other commands.
I don't understand and which "for" loop ?
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.
Supremache is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-23-2021 , 16:02   Re: Combine the menus
Reply With Quote #16

Quote:
Originally Posted by Supremache View Post

PHP Code:
public Handler(idiMenuiItem)
{
    if(
iItem != MENU_EXIT)
    {
        new 
iAccessiCallbackiData[6],iName[64];
        
menu_item_getinfo(iMenuiItemiAccessiDatacharsmax(iData), iNamecharsmax(iName), iCallback);
    
        new 
iPlayer str_to_num(iData);
        
        switch ( 
g_iPlayerMenu[id] )
        {
            case 
2ResetPlayerRank(idiPlayer);
            case 
3ManagePlayerRank(idiPlayer);
            default: 
menu_destroy(iMenu)
        }
    }
    
//menu_destroy(iMenu)
    
return PLUGIN_HANDLED

No, from your original version, just delete the default case.

Quote:
Originally Posted by Supremache View Post
I don't understand and which "for" loop ?
The for loop in CommandSay(). Add the break after calling PlayerRank() because you don't need to process the reset of the cases since they are mutually exclusive.
__________________
fysiks is offline
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 05-23-2021 , 16:21   Re: Combine the menus
Reply With Quote #17

Ops i understand , thanks for suggests

EDIT:
Quote:
Originally Posted by fysiks View Post
The for loop in CommandSay(). Add the break after calling PlayerRank() because you don't need to process the reset of the cases since they are mutually exclusive.
After i added break I get bug on commands, i type /xplist and it show reset data menu
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.

Last edited by Supremache; 05-23-2021 at 16:37.
Supremache is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-23-2021 , 18:44   Re: Combine the menus
Reply With Quote #18

Quote:
Originally Posted by Supremache View Post

After i added break I get bug on commands, i type /xplist and it show reset data menu
Where did you put it? Show your code. It should be after the call to the function that shows the menu.
__________________

Last edited by fysiks; 05-23-2021 at 18:44.
fysiks is offline
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 05-23-2021 , 19:13   Re: Combine the menus
Reply With Quote #19

Quote:
Originally Posted by fysiks View Post
Where did you put it? Show your code. It should be after the call to the function that shows the menu.
PHP Code:
.     

for(new  
0<= charsmax(g_iCommandSettings); i++)
    {
        if (
equali(szCmd[1], g_iCommandSettings[i][iCommands]))
        {
            
iMenu g_iCommandSettings[i][iMenuType]
            
            if (
iMenu == -1)
                return 
PLUGIN_CONTINUE
            
            g_iPlayerMenu
[id] = iMenu
            
            PlayerRank
(idg_iCommandSettings[i][iFlags])
            break;
        }
    } 
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.
Supremache is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-23-2021 , 19:35   Re: Combine the menus
Reply With Quote #20

That is done correctly.
__________________
fysiks 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 22:40.


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