Sueta
IN

InventoryAPI

1.5

Simple API for Inventory GUI's

·Plugin

InventoryAPI

Images

Description

  • With InventoryAPI you can create UI's very easy.
  • All it takes is couple lines of code and you will have a working UI in seconds.

Commands

  • /InventoryAPI
    • Show's you a simple UI that has some info about the plugin.

Usage

  • First we need to make some variables.
  • We need IInventory & InventoryHandler
  • Make sure that you have InventoryAPI added as a dependency
  • And make sure that you have Spigot added as a dependency aswell
private IInventory inventory;
 private InventoryHandler inventoryHandler;
  • Inventory handler is just a new Java class that has nothing in it for now
  • Then in we are gonna init some variables and create some new variables.
  • Im going to do it in in the onEnable(); method as that get's called at the plugin start.
@Override
    public void onEnable() {
        ItemStack filler = this.getItemStack(ChatColor.BLACK + " ", null, Material.STAINED_GLASS_PANE);
        List<InventoryItem> inventoryItems = new ArrayList<InventoryItem>();
  • So i have created 2 new fields.
  • It's an ItemStack (filler) that will fill in the slots that are not used.
  • Then i created an empty list of InventoryItem's.
  • Now we need to populate that arraylist with the items that we want in the UI itself.
inventoryItems.add(new InventoryItem(0, 3, this.getItemStack("Hi", Arrays.asList("World!"), Material.WATER_BUCKET)));
        inventoryItems.add(new InventoryItem(1, 5, this.getItemStack("Quit", Arrays.asList("awww dont leave!"), Material.BARRIER)));
  • Constructor for InventoryItem takes in an <id>, <slot> and an <ItemStack>.
  • So for the first InventoryItem i give it an id of 0 and put it in the thrid slot.
  • For the second InventoryItem i give it an id of 1 and put it in the slot five.
  • And the method for getting an ItemStack is very simple.
public ItemStack getItemStack(String name, List<String> lore, Material material) {
        ItemStack stack = new ItemStack(material);
        ItemMeta meta = stack.getItemMeta();
        if (name != null) meta.setDisplayName(name);
        if (lore != null) meta.setLore(lore);
        stack.setItemMeta(meta);
        return stack;
    }
  • After that we need to create some more stuff.
  • Now we are gonna init InventoryHandler & the IInventory.
this.inventoryHandler = new InventoryHandler();
  this.inventory = new IInventory(ChatColor.BOLD + "Test UI", this.inventoryHandler, inventoryItems, 9, filler);
  • InventoryHandler doesn't need anything in the constructor so we dont need to give it anything.
  • But for IInventory we need to give it few things.
  • We need to give it <name>, <InventoryHandler>, <list of inventory items>, <inventory size> and the <filler itemstack>.
  • After doing that we just need to register the IInventory as a listener.
Bukkit.getPluginManager().registerEvents(this.inventory, this);
@Override
    public void onEnable() {

        ItemStack filler = this.getItemStack(ChatColor.BLACK + " ", null, Material.STAINED_GLASS_PANE);
        List<InventoryItem> inventoryItems = new ArrayList<InventoryItem>();

        inventoryItems.add(new InventoryItem(0, 3, this.getItemStack("Hi", Arrays.asList("World!"), Material.WATER_BUCKET)));
        inventoryItems.add(new InventoryItem(1, 5, this.getItemStack("Quit", Arrays.asList("awww dont leave!"), Material.BARRIER)));

        this.inventoryHandler = new InventoryHandler();
        this.inventory = new IInventory(ChatColor.BOLD + "Test UI", this.inventoryHandler, inventoryItems, 9, filler);

        Bukkit.getPluginManager().registerEvents(this.inventory, this);

    }
  • Now lets setup our InventoryHandler class.
  • that class is very small and doesn't take much to setup.
  • for that class we have a maximum of 3 events that can be called
  • and in this tutorial we are gonna be using only one as the other 2 are pointless to explain so i will just leave their names in here.
  • Okey so, in InventoryHandler we are gonna create a method called
    onInventoryAction that takes in a
    EventInventoryAction as a parameter with @EventHandler annotation.
public class InventoryHandler {
    @EventHandler
    public void onInventoryAction(EventInventoryAction eventInventoryAction) {
    }
}
  • In onInventoryAction method we are gonna add a switch case for our InventoryItem id.
@EventHandler
    public void onInventoryAction(EventInventoryAction eventInventoryAction) {
        switch (eventInventoryAction.getClickedItem().getId()) {
            case 0:
                break;
            case 1:
                break;
        }
    }
  • Now we are gonna populate the switch case with stuff that we want to happen on the click of the item.
@EventHandler
    public void onInventoryAction(EventInventoryAction eventInventoryAction) {
        switch (eventInventoryAction.getClickedItem().getId()) {
            case 0:
                eventInventoryAction.getPlayer().sendMessage("Hello World!");
                eventInventoryAction.getPlayer().closeInventory();
                break;
            case 1:
                eventInventoryAction.getPlayer().sendMessage("Byeeeeee!");
                eventInventoryAction.getPlayer().closeInventory();
                break;
        }
    }
  • Now let's setup a very simple command to make our inventory open. (In the class that the onEnable() is).
@Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player))
            return false;
        if (command.getName().equalsIgnoreCase("Test")) {
            inventory.openInventory((Player) sender);
            return true;
        }
        return super.onCommand(sender, command, label, args);
    }
  • And for the last thing its the plugin.yml
name: Test
version: 1
main: <your path to main class>
description: Show's you a simple UI.
depend: [InventoryAPI]

commands:
   Test:
     usage: /<command>
     description: Show's you an UI.
  • If you have any trouble and dont want to bother and write the code out you can download the compiled code from this tutorial here.

Support/Bugs

  • For any bugs that you find feel free to report them as an issue on my Github.

Similar projects

Plugin →