[Tutorial] Create an inventory

Discussion in 'Resources' started by BaconStripzMan, Jan 14, 2014.

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

    BaconStripzMan

    Before you read this thread remember: This is not a tutorial on how to create a teleportation compass -something but it will show you how to create the required stuff to make it.

    First make a new class, make it a listener class.

    [​IMG]

    Now we're gonna create the inventory
    Code:java
    1. public static Inventory inventoryname = Bukkit.createInventory(null, 54, "Inventory Name");
    2. // ^ the amount of slots (Must be multiple of 9)


    Now let's define some items, in this case: 1 Diamond.

    Code:java
    1. static{
    2. ItemStack diamond = new ItemStack(Material.DIAMOND, 1);
    3. }


    So let's add this item to the inventory now

    Code:java
    1. inventory.setItem(diamond, 0);
    2. // ^ inventory name ^ item ^ slot in inv (slot 1 = 0)


    OPTIONAL: Now let's make so the player can't take these items

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event) {
    3. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    4. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    5. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    6. if (inventory.getName().equals(diamond.getName())) {
    7. if (clicked.getType() == Material.DIAMOND) { // The item that the player clicked it (diamond)
    8. event.setCancelled(true);// Make it so the diamond is back in it's spot
    9. player.closeInventory();
    10. }
    11. }


    Open the inventory
    Code:java
    1.  
    2. player.openInventory(inventory);


    If you know how, you can use ItemMeta's to set Displaynames and lores.

    Class made up for you (It makes you so when you right click a diamond it opens)

    http://pastebin.com/WLYziSvW
     
  2. Offline

    metalhedd

    This line of code is not only syntactically invalid, but it also doesn't do what you're trying to.
    Code:java
    1.  
    2. if (inventory.getName().equals(inventory.getName())) {Inventory
    3.  

    Also you should rename this tutorial, it's not about creating an inventory, (which is a one-line api call) it's about creating an inventory MENU.
     
    BaconStripzMan likes this.
  3. Offline

    BaconStripzMan

    metalhedd Okay I suck at this... I said you were wrong, I wasn't in the pastebin but I was here. I shall fix it now, and how do I change the title? It's not letting me.
     
  4. Offline

    metalhedd


    I don't know, never had to do it before, but it can be done because people constantly add [solved] to their titles.
     
  5. Offline

    xTrollxDudex

    BaconStripzMan
    There's no reason to use a static initializer to add items to the inventory
     
  6. Offline

    BaconStripzMan

    Doesn't work for me without it.
     
  7. Offline

    BaconStripzMan

  8. Offline

    xTrollxDudex

    The static initializer is called when the class is loaded. Put that in a constructor.
    Why bump a resource?
     
  9. Offline

    Captain Dory

    This is almost exactly the same to JPG2000 's custom inventory...
     
Thread Status:
Not open for further replies.

Share This Page