menus

This page contains tutorials about themenus package.

Creating PagedMenu

This is a simple example to create amenus.PagedMenu, which shows 20 options.After selecting an option the menu will be shuffled and shown again.

You can use paged menus if you have many menu entries and want Source.Pythonto take care about the pagination. If you need more control about the menutake a look atmenus.SimpleMenu.

importrandomfromcommands.sayimportSayCommandfrommenusimportPagedMenufrommenusimportPagedOptionfromplayers.entityimportPlayer# Register menu command@SayCommand(['menu','/menu','!menu'])defsay_command(command,index,teamonly):# Send the menu to the player who issued the say command.menu.send(index)returnFalsedefmy_select_callback(menu,index,option):"""Called whenever a selection was made."""# Shuffle the menu : Drandom.shuffle(menu)# Make it sticky. If you return a menu, it will be shown immediately.returnmenumenu=PagedMenu(title='Welcome menu',description='Choose an option:',select_callback=my_select_callback)# Add options from 1 to 20foriinrange(1,20):menu.append(PagedOption(f'Option{i}',i))# Instead of passing the select, build and close callbacks to the constructor# of PagedMenu, you can also use a decorator to register the callbacks.@menu.register_close_callbackdef_on_close_menu(menu,index):"""Called when the player closes the menu."""# Send the menu againmenu.send(index)

Creating SimpleMenu

This is an example to createmenus.SimpleMenu which asks a player to accept the server rules.If he declines, he will be kicked from the server.

importtimefromcommands.sayimportSayCommandfrommenusimportSimpleMenufrommenusimportSimpleOptionfrommessagesimportSayText2fromplayers.entityimportPlayer# Register menu command@SayCommand(['menus','/menus','!menus'])defsay_menus_command(command,index,teamonly):# Send the menumenu.send(index)returnFalsedefmy_menu_select_callback(menu,index,option):"""Called whenever a selection was made."""ifoption.value=='yes':SayText2('Thank you for accepting the rules!').send(index)# Player has selected no optionelse:# Kick player for selecting no optionPlayer(index).kick('You have to accept the rules!')menu=SimpleMenu()# Tell the current timemenu.append(f"Current Time:{time.strftime('%H:%M:%S')}")# Add empty linemenu.append(' ')menu.append('Do you accept the rules?')menu.append(' ')# Add menu optionsmenu.append(SimpleOption(1,'Yes','yes'))menu.append(SimpleOption(2,'No','no'))# Another way to define a select callback.menu.select_callback=my_menu_select_callback

Creating ListMenu

This example shows how to create amenus.ListMenu.The goal of a list menu is to show a lot of data, but the menu entries are not selectable.

fromcommands.sayimportSayCommandfrommenusimportListMenu# Register menu command@SayCommand(['menus','/menus','!menus'])defsay_menus_command(command,index,teamonly):# Send the menumenu.send(index)returnFalsemenu=ListMenu()# Add menu textmenu.append('This is an example text')