Search code examples
javaminecraftbukkit

Create a custom inventory in bukkit


This is my code for a new Inventory in Bukkit.

package com;

import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftInventoryCustom;
import org.bukkit.inventory.*;

public class Server_Doc extends CraftInventoryCustom implements CraftingInventory, Inventory {
    InventoryHolder IH;

public Server_Doc(InventoryHolder owner, int size) {
    super(owner, size);
    ItemStack items = new ItemStack(278);
    ((Inventory) owner).addItem(items);
    // TODO Auto-generated constructor stub
}

@Override
public ItemStack[] getMatrix() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Recipe getRecipe() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public ItemStack getResult() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void setMatrix(ItemStack[] contents) {
    // TODO Auto-generated method stub

}
@Override
public void setResult(ItemStack newResult) {
    // TODO Auto-generated method stub

}
//Inventory inv = Server_Doc(IH,8); 
}

How could I open the inventory, once created?


Solution

  • If you would like to open a 3x3 crafting table for a player, you can simply call player.openWorkbench(). Creating a custom GUI menu is a little more difficult, though. For example, using

    public Inventory inv;
    
    public void openGUI(Player p){
        //format: null, size of inventory (must be divisible by 9), "GUI name"
        inv = Bukkit.createInventory(null, 9, "GUI Name");
        inv.setItem(0, new ItemStack(Material.DIAMOND);
        p.openInventory(inv);
    }
    

    would open a 1x9 inventory, containing a diamond in the first slot. If you wanted to add more items, you could use

    inv.setItem(space, ItemStack);
    

    but remember, counting starts from 0, so 0 must be used to get slot 1, and 1 must be used to get slot 2.

    To open the GUI using the above code, simply call openGUI(player), where player is the player that you want to open it for.

    If you would like to do something when a player clicks an item, for example let's say the diamond that we created in slot 0 (Slot 1) above, you could do this

    @EventHandler //MAKE SURE YOU HAVE THIS
    public void InventoryClick(InventoryClickEvent e){
        Player p = (Player) e.getWhoClicked();  
    
        if(e.getInventory().getTitle().contains("put the name of the GUI here (CAsE SEnsITivE)")){
            //Cancel the event so they can't take items out of the GUI
            e.setCancelled(true);
    
            if(e.getCurrentItem() == null){
                return;
            }
            //gets called when the current item's type (The item the player clicked) is a diamond
            else if(e.getCurrentItem().getType() == Material.DIAMOND){
                //send the player a message when they click it
                p.sendMessage("You clicked the diamond!");
    
                //call this if you want to close the inventory when they click it
                p.closeInventory();
            }
        }
    }
    

    Now you only have to register events in your Main file in your onEnable() like so

    public void onEnable(){
        //if the code above is in your main file, use this:
        this.getServer().getPluginManager().registerEvents(this, this);
    
        //if it's in another class, use this:
        this.getServer().getPluginManager().registerEvents(new myClassNameHere(), this);
    }
    

    then just make the class that has your inventoryClick method in it implement Listener

    public class myClassNameHere implements Listener{
    

    now you have a fully functioning GUI, that when you call openGUI(player) player being the player you want to open the GUI for, it will open a GUI that is 1x9, having a diamond in slot 0 (Slot 1), that when clicked messages the player "You clicked the diamond!" Good luck!