Starting a player with items in inventory

Discussion in 'Spigot Plugin Development' started by spinnekoppie, May 14, 2016.

  1. I hope I manage to explain what I'm trying to do.

    How do I go about putting blocks of a certain type in a player's inventory in specific slots when the player is spawned. It can't be that difficult because lots of games do this but I think my thinking is off on a tangent which is why I can't figure this out.

    I wanted to, when the player joins (PlayerJoinEvent) create a block and then put it in an ItemStack and then put the ItemStack into the Inventory. But then, because Block is an interface, I would have to create a class of the type block. I'm sure there is a simpler way of doing this. Can someone perhaps help me out of my misery?
     
  2. player.getInventory.setItem(int slot, ItemStack item). The JavaDocs for it say what slots correspond to what, or google Bukkit inventory slot numbers
     
  3. That much I know ...
    Code (Text):

        Player player = e.getPlayer();
         player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
         PlayerInventory inventory = player.getInventory();
         inventory.setItem(coalslot, new ItemStack('HOW AND WHERE DO I GET THE BLOCK I WANT TO PUT IN HERE'));
     
     
    • Informative Informative x 1
  4. Material# enum is amazing for this, new ItemStack(Material.{whatever}); and setting the item display-name / lore / etc is a bit more difficult

    #SNIPED ouch...
     
  5. Code (Text):
    public void addItemtoInventory(Player p){
    Inventory inv = p.getInventory(); //gets player inventory
    ItemStack item = new ItemStack(Material.STONE); //creates an itemstack with stone item
    ItemMeta itemmeta = item.getItemMeta(); //gets the meta for the itemstack
    itemmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&6Example Item"); //change the itemstacks name
    item.setItemMeta(itemmeta); //set the meta of the itemstack to itemmeta for display name to be "&6Example Item"
    inv.setItem(1, item); //set the item to slot # 1 in inventory
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e){
    Player p = e.getPlayer();
    addItemtoInventory(p);
    }
     
     
  6. Spoonfeed game is strong with this one, you've just spoonfed an entire method with no pseudo-code to a user, how do you feel?
     
  7. i feel awesome :) XD
     
  8. You sir, are making the human race collectively dumber.
     
    • Agree Agree x 1
    • Winner Winner x 1
  9. you sir, mind your own business!
     
  10. Spoonfeeding to an entire community and everyone who tries to google search a fix to their problem could find this thread, copy the code, and use it as their own without actually learning how to do it, I am apart of the community so it is my business. Everyone here is apart of the community and you should be looking to help the entire community into learning how to do things not how to copy code and call it their own.
     
    • Winner Winner x 1
  11. i placed some explanation.
    if he doesn't read them, he is the one loosing not me...
     
  12. Yes, he is the one losing but you're the one setting him up to fail, sure you put in explanation but who knows if it will be read or not. If you're setting another person up to fail then you are not doing good as a citizen of this community. This forum is for learning and growing into better developers and learning how to fix / problem solve the issues and learning to know what our code does and why. You are disabling that growth, this is getting off-topic so I'll stop my rant here but please stop spoonfeeding to the community...
     
  13. Hi andrew28. Thanks for this. Yes, I was off on a tangent. For some reason I was trying to pass a Block in stead of just the material type. I'm not sure what set me off onto that track. I was obviously making things much more complicated than what it is.
     
  14. Interesting, yeah always use the Material enum when creating an ItemStack, if you use a block then just do new ItemStack() and your IDE should give you options on how to fix the line of code, as in it will give you constructor options, telling you how to make an itemstack or construct one and it should give you the option of new ItemStack(Material arg0);
     
  15. Work for me... thanks