[Snippet] Creating a new ItemStack and giving that ItemStack to a player.

Discussion in 'Spigot Plugin Development' started by Coltll, May 8, 2015.

  1. A snippet for @jflory7

    So, you have been a server owner for a while now and you have always wanted to be able to make your very own minecraft plugins?

    You finally stop waiting and delaying yourself and you dedicate your summer break to learning Java.
    After a some good months of reading books, watching online tutorials and having somehands on work, you believe you're ready to start learning the Bukkit/Spigot API.

    Okay, making your very own plugin

    well, I am sure you and most of us developers all got up and started learning Java and the Bukkit API because we had an idea, a plugin idea that no other developer would do for us.

    So, do you remember in minecraft and essentials how you can simply do "/give <player> DIAMOND_BLOCK 64"?
    Yea, well what that is doing is simply getting the args (<player>, DIAMOND_BLOCK, 64) and using those args to build an itemstack.

    Today, I am going to teach you how to build your own itemstack containing whatever item you want.
    I will then teach you how to give that itemstack to a player (put it in the players inventory)

    Let's begin.

    We are going to use the PlayerJoinEvent to create the itemstack and then give it to the player.
    PHP:
    package com.me.MyPlugin;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MyPlugin extends JavaPlugin implements Listener {

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {

        }
    }
    Okay now lets create a new ItemStack and declare it as an ItemStack variable called "is"
    PHP:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            ItemStack is = new ItemStack(Material.DIAMOND, 64); // Create a new ItemStack called "is" and then define it as equal to a new ItemStack containing 64 Diamonds.
        }
    Now let's get the player instance from the PlayerJoinEvent and store it in a Player variable.
    PHP:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer(); // define the Player variable "p" as equal to the Player instance of the player joining.
     
            ItemStack is = new ItemStack(Material.DIAMOND, 64); // Create a new ItemStack called "is" and then define it as nequal to a new ItemStack containing 64 Diamonds.
        }
    Now let's access the players inventory and add our ItemStack to it.
    PHP:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer(); // define the Player variable "p" as equal to the Player instance of the player joining.
     
            ItemStack is = new ItemStack(Material.DIAMOND, 64); // Create a new ItemStack called "is" and then define it as nequal to a new ItemStack containing 64 Diamonds.
     
            p.getInventory().addItem(is); // access the players inventory then use #addItem(ItemStack) to add our ItemStack which is declared as "is"
        }
    Thats about all there is to it.

    now I am going to post a few more snippets of other things you can do with this ItemStack.
    PHP:
    package com.me.MyPlugin;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MyPlugin extends JavaPlugin implements Listener {

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer(); // define the Player variable "p" as equal to the Player instance of the player joining.
     
            ItemStack is = new ItemStack(Material.DIAMOND, 64); // Create a new ItemStack called "is" and then define it as nequal to a new ItemStack containing 64 Diamonds.
     
            for(Player pl : Bukkit.getOnlinePlayers()) { // use a for-loop to loop through every single player currently online the server.  
                pl.getInventory().addItem(is); // access the players inventory and use #addItem(ItemStack) to add our ItemStack ("is") to the players inventory.
            }
        }
    }
    I know that some of this is unclear and a little messy, I wanted to get this thread up so I can fix it up for being posted to the wiki and so if someone else has an idea on how I can better explain what I am attempting to teach.
     
    #1 Coltll, May 8, 2015
    Last edited: May 9, 2015
    • Like Like x 1
    • Informative Informative x 1
    • Useful Useful x 1
  2. Bad idea putting the player's inventory as a variable there. Why have a variable at all when you can just use pl.getInventory().addItem(is) ?
     
    • Agree Agree x 2
    • Like Like x 1
  3. oh right, was thinking about keep the code looking as clean and understandable as possible.
    I'll fix it now, thanks for pointing it out.

    @fujiboy4 ..?
     
  4. What's the disadvantage to including an extra line of code to store it in a variable besides that it's one extra line? Might be more clear to new programmers.
     
  5. Um, if we are using the inventory for only once, we should not actually need to put it into a variable.
    BUT for demonstration (for newbies), we shall put it in a new line.
     
    • Agree Agree x 4
  6. Ya, that's the point I was trying to make - my post was lazily written
     
  7. Kinda not relevant to the tutorial but you forgot to register events :)
     
    • Funny Funny x 3
    • Winner Winner x 1
  8. haha, maybe I shoulda waited atleast a few hours after I woke up before writing this :p
     
    • Agree Agree x 1
  9. Haha that would prob be a good idea xD
     
    • Agree Agree x 2
  10. Ya lol. :p
     
    • Funny Funny x 1
  11. Hi guys I 've just started to code in java and learning curve as been interesting however, I really want to thank "Cewit" and his tutorial, as I have been putting in major hours the past couple of days on the internet and his tutorial pulls it all together. I finally got my Plugin off the ground and by putting the listener in and Instead of the join event I put it into a command and it is all working and now it gives a full suit of diamond armor and sword and anything else I can think of lol. It is not elegant or efficient from a coding standpoint but it works , much thanks and gratitude again. It will come in handy for the pvp part of the server I'm working on ..:cool: :D
     
    • Like Like x 1
  12. Please don't necro post.

    Only bump old threads if 110% necessary
     
  13. How to create a global item stack? Because i want to use this item stack on 2 events
     
    • Optimistic Optimistic x 1
  14. Code (Text):
      public static final ItemStack is = new ItemStack(Material.DIAMOND, 64); // Put this at the top of your class before the onCommand or event listener.
    If there's a better way to do this someone can correct me.
     
  15. Or you can simply create a singleton in the class and access the variable from there (you have to create a field for that object tho).
     
  16. A constant is nice, but constants should follow naming conventions.
    Code (Text):
    UPPERCASE_WITH_UNDERSCORES
    Other than that, solid advice. Although, you shouldn't be so willing to help someone with a very basic portion of Java.
     
    • Agree Agree x 1
  17. Maybe you're right there, just trying to help though. :)
    What were you saying about naming conventions? Would you have used IS instead of is? When my variables are one word I usually name them in lowercase. Eg. "colorCode" and "color." Is there different naming conventions for constants? (Sorry, I haven't been developing for a super long time, but I like to think I know a pretty intermediate amount of Java.)
     
  18. http://stackoverflow.com/questions/12519475/java-constant-variable-naming-convention
    Remove the static, and it's named like a normal instance variable. I like to think of (class) constant's conventions as that of an enum, just easy to remember.
     
    • Informative Informative x 1
  19. @--TOMAS-- gj, you necroposted on a year old thread that was necroposted nearly 3 months ago.
     
  20. Well, but, how to get or set meta of this item?