[API] InventoryMenuAPI

Discussion in 'Resources' started by amhokies, Mar 29, 2014.

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

    amhokies

    I spend a good amount of my time making private plugins for a server that I'm staff on, and I found myself writing the same code for Inventory Menus over and over, so I decided to create something that will help streamline this process. I'm sure you have a few questions, so here we go:

    What is it?
    InventoryMenuAPI will serve as a tool for developers to easily create interactive menus in all of their plugins. Using this API, you will be able to populate the menu with clickable options (represented by items in an inventory slot). Each of these slots will be able to perform virtually any function.

    Why do I need it?
    Inventory menus serve as an easy, interactive way for players to chose between different options. For instance, when choosing kits, it's a lot easier to click something in an InventoryMenu than type a command to spawn the kit. By using this API, you will save yourself a good bit of time and energy when creating these menus. To give you an idea, I rewrote an old plugin called ServerSelector which let players choose a server by using an inventory menu and by using InventoryMenuAPI, the plugin went from 276 lines to 164 lines of code (Only 5 lines were calls to the API).

    How do I use it?
    The biggest thing is creating your custom slot class(es). Say for instance I want to create a type of slot that will give a player a kit when they click on it.
    Code:java
    1. public class KitSlot extends AbstractSlot {
    2. //However you implement your kits
    3. private Kit kit;
    4.  
    5. /**
    6.   The default constructor you need to use will take
    7.   two parameters, a String id and Material mat.
    8.  
    9.   The id is the display name of the slot when a player
    10.   hovers over it. The material is the item type that
    11.   players will see and click in the inventory slot.
    12.  
    13.   In this case, we are adding an extra parameter in
    14.   order to pass in our kit object for each slot.
    15.   */
    16. public KitSlot(String id, Material mat, Kit kit) {
    17. super(id, mat);
    18. this.kit = kit;
    19. }
    20.  
    21. /**
    22.   You must implement this method in all classes
    23.   that extends AbstractSlot. This is the method
    24.   that is called when the slot is clicked.
    25.   */
    26. public void execute(Player player) {
    27. kit.giveToPlayer(player);
    28. }
    29. }


    Next is building your menu and showing it to the player.

    First, to get the InventoryMenuAPI plugin instance, call the static method:
    Code:java
    1. InventoryMenuAPI menuAPI = InventoryMenuAPI.getInstance();


    To build your menu, you need to create a MenuBuilder object
    Code:java
    1. MenuBuilder menuBuilder = new MenuBuilder(Plugin owner, String menuTitle);


    Then you can use the MenuBuilder to build your menu.
    Code:java
    1. menuBuilder.addSlot(new KitSlot("Warrior", Material.DIAMOND_SWORD, warriorKit))
    2. .setDescription("Click to choose the warrior kit!");
    3. menuBuilder.addSlot(new KitSlot("Archer", Material.BOW, archerKit))
    4. .setDescription("Click to choose the archer kit!");
    5. menuBuilder.addSlot(new KitSlot("Farmer", Material.DIAMOND_HOE, farmerKit))
    6. .setDescription("Click to choose the farmer kit!");


    The MenuBuilder.addSlot(AbstractSlot slot) method will return the AbstractSlot that you add. This allows for the stringing together of the AbstractSlot.setDescription(String description) and AbstractSlot.setCloseOnClick(boolean closeOnClick) methods onto the end of the addSlot method. By default, the menus close when a slot is clicked, and have no description.

    After adding all of the slot that you want, you can then build the menu by invoking the MenuBuilder.getMenu() method on the MenuBuilder object from before.
    Code:java
    1. InventoryMenu menu = menuBuilder.getMenu();


    Lastly, to show the InventoryMenu to player(s), you need to use either of the InventoryMenuAPI (non-static) methods:

    InventoryMenuAPI.showInventoryMenu(Player player, InventoryMenu menu)
    -OR-
    InventoryMenuAPI.showInventoryMenu(Collection<Player> players, InventoryMenu menu)

    Where's the source code?
    Here's the source, JavaDocs, and BukkitDev page

    If you have any questions, please don't hesitate to ask below. Enjoy!
     
  2. Offline

    stonebloodtv

    Oh nice ! :D
     
  3. Offline

    amhokies

    The file is now available on BukkitDev, so go check it out! :) (The BukkitDev link is at the bottom of the first post)
     
Thread Status:
Not open for further replies.

Share This Page