[Tutorial] Create a per-player inventory

Discussion in 'Resources' started by jusjus112, Sep 21, 2014.

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

    jusjus112

    Hello guys, and welcome to my tutorial for how to create a inventory for each player.
    At the end of this tutorial, ill show you a class will look like.
    I'm explane how to create it, and how to set items in it. It's not as difficult as it sounds, but
    you must know how and when you use it. Soo, this tutorial is for people who dont know how to do this.
    Soo, i hope you like it.

    If you dont know how to make a inventory?
    Take a look at this tut: http://forums.bukkit.org/threads/tutorial-create-a-inventory-menu.173571/

    First step:
    We are not making 1 inventory, we create the inventory when the event is called.
    So, as a example. When i player right click a clock, a inventory will created.
    Code:java
    1. @EventHandler
    2. public void onPlayerClockClick(PlayerInteractEvent e) {
    3. if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
    4. if (e.getPlayer().getItemInHand().equals(new ItemStack(Material.WATCH))) {
    5.  
    6. //Here we creating the inventory
    7. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "" + ChatColor.BOLD + e.getPlayer().getName() + " stat's");
    8. }
    9. }
    10. }


    Second step:
    We willl put items in this inventory for the player.
    This will not set the items for everyone, its just for the player itself.
    We can set a item, or add a item to the inventory.
    Code:java
    1. int levels = e.getPlayer().getLevel();
    2. inv.setItem(0,new ItemStack(Material.EXP_BOTTLE, levels));


    Third step:
    This is basicly it, you can set all the items for each player.
    Now i show you how this small class will look like. Its just a
    small peace of code, but some poeple find it quite difficult to do.
    Code:java
    1. @EventHandler
    2. public void onPlayerClockClick(PlayerInteractEvent e) {
    3. if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
    4. if (e.getPlayer().getItemInHand().equals(new ItemStack(Material.WATCH))) {
    5.  
    6. //Here we creating the inventory
    7. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "" + ChatColor.BOLD + e.getPlayer().getName() + " stat's");
    8.  
    9. //Now we put a item in it
    10. int kills = e.getPlayer().getLevel();
    11. inv.setItem(0,new ItemStack(Material.EXP_BOTTLE, kills));
    12. }
    13. }
    14. }


    Soo, now we have made a inventory for each player. thats shows different items in it.
    You can do more then just the levels, such as health or deaths or kills.
    Some poeple find it quite difficult to do, but now you know how :D
    I hope i have learn you the basic of this system.

    Leave at the comments below, if you have more to add in this tutorial.
    Oke, now i see.

    GoodBye and GG (Good Coding)
     
  2. Offline

    Dragonphase

    jusjus112

    This is not a very useful tutorial, nor do you make it clear how this would be beneficial to use compared to using the player's actual inventory window. At the most, this tutorial teaches nothing that can otherwise be learned by reading the JavaDocs.

    In addition to this, you link to another tutorial which employs bad coding practices, namely the use of an unnecessary static field and the adding of items to an inventory in a static block.
     
Thread Status:
Not open for further replies.

Share This Page