[Easy] Creating icon menus

Discussion in 'Resources' started by MCForger, Jun 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    MCForger

    Hello everyone!
    So I have been helping lots of new bukkit developers and some not so new recently with creating interactive menus that have submenus or other functionality. So I decided to release some code from a plugin I made a while ago to hope it helps those bukkit members trying to look for a solution/figure out how it works. Keep in mind this is how I decided to go about making an interactive menu and I am sure there is plenty of different solutions to this idea.
    Sorry for rambling but here is the classes you will need.
    Menu:
    https://gist.github.com/anonymous/93cfe1803e2be9adb0ed
    Menu Item:
    https://gist.github.com/anonymous/3173707c7ee8135269e6
    IMenuItemClickHandler:
    https://gist.github.com/anonymous/1114ff22fff83ae9c38b
    GoToSubMenuHandler:
    https://gist.github.com/anonymous/3d3fab335ff279d358cd
    MenuListener (This class was modified by adding in TestInteractivePlugin instance so testing could be done through fake command):
    Code:java
    1. import org.bukkit.ChatColor;
    2. import org.bukkit.entity.HumanEntity;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.inventory.InventoryAction;
    7. import org.bukkit.event.inventory.InventoryClickEvent;
    8. import org.bukkit.event.inventory.InventoryType.SlotType;
    9. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    10. import org.bukkit.inventory.Inventory;
    11.  
    12. public class MenuListener implements Listener
    13. {
    14. private TestInteractiveMenuPlugin plugin;
    15.  
    16. public MenuListener(TestInteractiveMenuPlugin plugin)
    17. {
    18. this.plugin = plugin;
    19. }
    20.  
    21. @EventHandler
    22. public void onInventoryClick(InventoryClickEvent event)
    23. {
    24. // Get the inventory we are working with
    25. Inventory inventory = event.getInventory();
    26. // Get the type of action that occurred
    27. InventoryAction actionType = event.getAction();
    28. // Check to make sure its our Menu has the holder of the inventory
    29. if (!(inventory.getHolder() instanceof Menu))
    30. return;
    31. // Make sure the human does not try and shift click
    32. if (actionType.equals(InventoryAction.MOVE_TO_OTHER_INVENTORY))
    33. {
    34. event.setCancelled(true);
    35. return;
    36. }
    37. HumanEntity human = event.getWhoClicked();
    38. // Get the menu we are working with from inventory holder through
    39. // casting
    40. Menu menu = (Menu) inventory.getHolder();
    41. // Check if this menu should be closed when clicking outside inventory
    42. // frame
    43. if (menu.closeOnClickOutside()
    44. && event.getSlotType().equals(SlotType.OUTSIDE))
    45. {
    46. menu.close(human);
    47. return;
    48. }
    49. // Now lets check to make sure the player is not clicking outside our
    50. // interactive menu by comparing the raw slot to the actual size of the
    51. // inventory.
    52. if (menu.getSize() <= event.getRawSlot())
    53. return;
    54. // We only cancel after the check above to make sure the player clicked
    55. // in our inventory
    56. event.setCancelled(true);
    57. // Lets make sure for the onClick we are working with a player
    58. if (!(human instanceof Player))
    59. return;
    60. // We are all set to call the onClick method for menu!
    61. Player player = (Player) human;
    62. menu.onClick(player, actionType, event.getSlot());
    63. }
    64.  
    65. // This event below is for opening the menu for testing purposes. The event
    66. // is not actually needed for the menu to work properly.
    67.  
    68. @EventHandler
    69. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
    70. {
    71. Player player = event.getPlayer();
    72. String message = event.getMessage();
    73. if (message.startsWith("/"))
    74. {
    75. message = message.replaceFirst("/", "");
    76. if (message.equalsIgnoreCase("testmenu"))
    77. {
    78. event.setCancelled(true);
    79. plugin.getMenu().open(player);
    80. player.sendMessage(ChatColor.GREEN + "Opening test menu...");
    81. return;
    82. }
    83. }
    84. }
    85. }
    86.  

    Now that all of that code is out of the way, we can finally make some menus! Sorry for not typing up a tutorial through the post but I rather tried to do it through comments in this test plugin class file:
    Code:java
    1. import java.util.Arrays;
    2. import java.util.List;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.HandlerList;
    8. import org.bukkit.event.inventory.InventoryAction;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.meta.ItemMeta;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class TestInteractiveMenuPlugin extends JavaPlugin
    14. {
    15. private Menu menu;
    16.  
    17. public Menu getMenu()
    18. {
    19. return menu;
    20. }
    21.  
    22. @Override
    23. public void onDisable()
    24. {
    25. // We check all the players online to make sure they do not have an open
    26. // menu or else after a reload they could steal items
    27. for (Player online : getServer().getOnlinePlayers())
    28. {
    29. if (online.getOpenInventory().getTopInventory().getHolder() instanceof Menu)
    30. {
    31. online.closeInventory();
    32. }
    33. }
    34. menu.cleanUp();
    35. menu = null;
    36. HandlerList.unregisterAll(this);
    37. }
    38.  
    39. @Override
    40. public void onEnable()
    41. {
    42. getServer().getPluginManager().registerEvents(new MenuListener(this),
    43. this);
    44.  
    45. // Initiates our Menu object
    46. this.menu = new Menu(ChatColor.GREEN + "Interactive Menu", 2);
    47. menu.setCloseOnClickOutside(true);
    48.  
    49. // Creating our menu item to add to the menu we just made. This menu
    50. // item will give the players diamonds every time the diamond is
    51. // clicked.
    52. ItemStack freeDiamondsIcon = createItemStack(Material.DIAMOND, 64,
    53. ChatColor.AQUA + "Click 4 Diamonds",
    54. Arrays.asList(ChatColor.YELLOW + "Its so....shinny!"));
    55. MenuItem freeDiamondsMenuItem = new MenuItem(freeDiamondsIcon);
    56. freeDiamondsMenuItem.setCloseOnSelection(true);
    57. freeDiamondsMenuItem.setClickHandler(new IMenuItemClickHandler()
    58. {
    59. @Override
    60. public void onClick(MenuItem menuItem, Player player,
    61. InventoryAction action)
    62. {
    63. ItemStack diamonds = new ItemStack(Material.DIAMOND);
    64. diamonds.setAmount(64);
    65. player.getInventory().addItem(diamonds);
    66. player.sendMessage(ChatColor.GREEN
    67. + "For being a nice tester here is some diamonds!");
    68. }
    69. });
    70.  
    71. menu.addItem(0, freeDiamondsMenuItem);
    72.  
    73. // Have you ever wondered how to make sub-categories for interactive
    74. // menus?
    75. // Well, today is your lucky day. We are going to make a sub-menu that
    76. // can give us other itemstacks like dirt! There will also be buttons to
    77. // get to the sub menu and back.
    78. Menu subMenu = new Menu(menu, ChatColor.GREEN + "Sub Menu", 2);
    79. subMenu.setCloseOnClickOutside(true);
    80.  
    81. ItemStack subMenuIcon = createItemStack(
    82. Material.GOLD_BLOCK,
    83. 1,
    84. ChatColor.GOLD + "Rares Category",
    85. Arrays.asList(ChatColor.LIGHT_PURPLE
    86. + "All the best gems in Minecraft!"));
    87. MenuItem goToSubMenuItem = new MenuItem(subMenuIcon);
    88. goToSubMenuItem.setClickHandler(new GoToSubMenuHandler(subMenu));
    89.  
    90. // Adding the go to sub menu item to the parent menu
    91. menu.addItem(1, goToSubMenuItem);
    92.  
    93. ItemStack backToParentIcon = createItemStack(Material.REDSTONE_BLOCK,
    94. 1, ChatColor.RED + "Back to parent",
    95. Arrays.asList(ChatColor.DARK_AQUA + "Leaves current menu!"));
    96. MenuItem backToParentMenuItem = new MenuItem(backToParentIcon);
    97. backToParentMenuItem.setClickHandler(new IMenuItemClickHandler()
    98. {
    99. @Override
    100. public void onClick(MenuItem item, Player player,
    101. InventoryAction action)
    102. {
    103. if (item.getMenu().hasParent())
    104. {
    105. player.closeInventory();
    106. item.getMenu().getParent().open(player);
    107. }
    108. }
    109. });
    110.  
    111. // Adding the menu item to get back to the original menu
    112. subMenu.addItem(17, backToParentMenuItem);
    113.  
    114. ItemStack freeDirtIcon = createItemStack(Material.DIRT, 64,
    115. ChatColor.GREEN + "Free Dirt",
    116. Arrays.asList(ChatColor.GOLD + "My favorite!"));
    117. MenuItem freeDirtMenuItem = new MenuItem(freeDirtIcon);
    118. freeDirtMenuItem.setCloseOnSelection(true);
    119. freeDirtMenuItem.setClickHandler(new IMenuItemClickHandler()
    120. {
    121. @Override
    122. public void onClick(MenuItem item, Player player,
    123. InventoryAction action)
    124. {
    125. ItemStack dirt = new ItemStack(Material.DIRT);
    126. dirt.setAmount(64);
    127. player.getInventory().addItem(dirt);
    128. player.sendMessage(ChatColor.GREEN
    129. + "You have recieved from free dirt! Awesome :D");
    130. }
    131. });
    132.  
    133. subMenu.addItem(0, freeDirtMenuItem);
    134. }
    135.  
    136. public ItemStack createItemStack(Material material, int amount,
    137. String displayName, List<String> lore)
    138. {
    139. ItemStack itemstack = new ItemStack(material);
    140. itemstack.setAmount(amount);
    141. ItemMeta itemMeta = itemstack.getItemMeta();
    142. itemMeta.setDisplayName(displayName);
    143. itemMeta.setLore(lore);
    144. itemstack.setItemMeta(itemMeta);
    145. return itemstack;
    146. }
    147. }
    148.  

    I hope this helps you guys and here is some pictures from the menu in this example!
    (Just tag me and I will be happy to answer questions)
    Please leave a like! :)
    [​IMG][​IMG]
     
    AoH_Ruthless likes this.
  2. Offline

    BungeeTheCookie

    I have not gone through your code, but are you basically just giving the player a menu, and when he right clicks on an item in the menu, it closes it and opens another one? If so, this can easily be done with IconMenu. Nice work though.
     
  3. Offline

    MCForger

    BungeeTheCookie
    IconMenu is also a way of doing interactive menus. IconMenu basically accomplishes the same thing just a little different. I wanted to be able to show other functionality like closing when clicking outside of the inventory and the simplicity of making a sub-menu (Forgot to mention the unregister all is not needed on disable. I honestly do not know how I accidentally put that code there).
     
  4. Offline

    BungeeTheCookie

    I can see where you are coming from, but IconMenu can do the same thing. I am not trying to put your resource down, but try putting your time into something more unique than this. Great job though ;)
     
  5. Offline

    AoH_Ruthless

    MCForger
    This is a pretty nice resource. I might use this is one of my plugins!
     
  6. Offline

    MCForger

  7. Offline

    Muhammadazka

    IconMenu can do submenu? how?
     
  8. Offline

    MCForger

    Muhammadazka
    You make it so on click of a specific item you define you close the player's current inventory and then display another menu. I show how to do sub-menus with my classes above.
     
  9. Offline

    Muhammadazka

  10. Offline

    MrDplugins

    MCForger How do I set the position of a item in the menu?

    EDIT: lol scrolled down and saw where to do it xD Thanks!
     
  11. Offline

    MCForger

Thread Status:
Not open for further replies.

Share This Page