Solved setItemInHand with name

Discussion in 'Plugin Development' started by Anrza, Jul 8, 2014.

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

    Anrza

    I want to put a named stick in someone's inventory. It all works fine, except I don't know how to set the name.
    Code:
    player.getInventory().setItemInHand(new ItemStack(Material.STICK,1));
     
    jflory7 likes this.
  2. Offline

    JasonDL13

    Take a look at ItemMeta

    See: http://jd.bukkit.org/rb/apidocs/org/bukkit/inventory/meta/ItemMeta.html

    Instead of setting the item in hand to a new ItemStack. Make an ItemStack variable.

    You can get the ItemMeta from an ItemStack by calling getItemMeta()

    When you are done modifying the ItemMeta do ItemStack.setItemMeta(ItemMeta);

    Example:

    Code:java
    1. ItemStack is = new ItemStack(Material.STICK);
    2. //Makes an ItemStack
    3.  
    4. ItemMeta im = is.getItemMeta();
    5. //Gets the ItemMeta
    6.  
    7. //Modify the ItemMeta here, I'm leaving that up to you
    8.  
    9. is.setItemMeta(im);
    10.  
    11. p.setItemInHand(is);
     
    jflory7 likes this.
  3. Offline

    Anrza

    Ok, I did see that page before, and I wasn't able to make something working from that.
    The problem with this code you gave me, is that I don't think it specifies which slot I want the item to go on. It is essential that it is the slot which the player is holding his hand in.
     
  4. Offline

    JasonDL13

    Anrza If you use setItemInHand it always goes into your hand slot. But I want you to learn how to use JavaDoc's, it will help you a lot when you code. So under "Method Summary" It has to columns. First one being what the method returns (or void). The second one being the name and comments. So if you see Method Detail don't go down any farther, look at Method Summary. If you still can't figure it out I'll tell you but it's better if you can get it from the JavaDoc.
     
  5. Offline

    Anrza

    JasonDL13 Where do I find all this "Method Summary" and "Method Detail"?
     
  6. Offline

    JasonDL13

    Anrza On the link I sent you
     
  7. Offline

    Anrza

    Oh, ok, thanks a lot!
     
  8. Offline

    macboinc

    Please marked as solved.
     
  9. Offline

    Anrza

    That is what I will do when it is solved.

    Sorry, can't make it work. Tried some stuff that should have worked, removed some stuff, and this is the code I have now:
    Code:
        public void onPlayerInteract(PlayerInteractEvent event) {
     
            if (event.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("lol")) {
                if (event.getPlayer().getItemInHand().getType().equals(Material.STICK)){
                    ItemStack is = new ItemStack(Material.STICK);
                    //Makes an ItemStack
                   
                    ItemMeta im = is.getItemMeta();
                    //Gets the ItemMeta
                   
                    //Modify the ItemMeta here, I'm leaving that up to you
                   
                    is.setItemMeta(im);
                   
                    p.setItemInHand(is);
    I would like to know exactly where to type what.
     
  10. Offline

    Gater12

    Anrza
    If you want to modify the stick in the player's hand, getItemInHand returns the ItemStack in which you can get the ItemMeta and do whatever then set it back.

    Methods for ItemMeta are here as previously stated: http://jd.bukkit.org/rb/apidocs/org/bukkit/inventory/meta/ItemMeta.html

    You also should check if the Item has a display name first before getting or it'll throw an NPE.
     
  11. Offline

    AoH_Ruthless

    Anrza
    Perhaps you should learn what you are doing instead of copying and pasting code without even reading it or understanding it....

    You honestly just copied it verbatim. You never modify the item meta (i.e displayname), you just get it and set the itemstack's meta to its default meta.
     
  12. Offline

    Anrza

    Who said I'd copy and paste without even reading it or understanding it? I ALWAYS read and I usually understand the code. Otherwise I would never learn anything.

    And, oh, that's not the code I'm using. Must've failed copying and pasting.
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
     
            if (event.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("firstName")) {
                if (event.getPlayer().getItemInHand().getType().equals(Material.STICK)){
                    event.getPlayer().getItemInHand().getItemMeta().setDisplayName("secondName");
                    event.getPlayer().updateInventory();
                }
           
            }
           
        }
    That's the code I have.
     
  13. Offline

    Necrodoom

    Anrza then what do you expect your code to do currently?
     
  14. Offline

    Anrza

    Necrodoom To put a stick named secondName in my hand whenever I right click any amount of sticks named firstName in my hand.
     
  15. Offline

    Necrodoom

    Anrza Where is your code that is supposed to do that?
     
  16. Offline

    Anrza

    Necrodoom Detects right clicking with onPlayerInteract, checks that the player is holding the correct item in their hand, renames the item with the setDisplayName-line. You are being over-pedagogic. I just want the answer. If I wanted riddles, I would go to another forum.
     
  17. Offline

    Necrodoom

    Anrza oh, you silently edited your post.
     
  18. Offline

    Anrza

    Necrodoom Yea, I thought there was a mistake but then I saw there wasn't a mistake so I removed the "I made a mistake"-part.

    Bump. What line should I write to put a named item in a players hand?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  19. Offline

    Zupsub

    You have to use #setItemMeta(...);
     
  20. Offline

    Anrza


    Zupsub So, do I do setItemMeta(setDisplayName("name"))?

    Bump.

    Solved like this:
    ItemStack is = new ItemStack(Material.STICK);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName("newName");
    is.setItemMeta(im);
    event.getPlayer().setItemInHand(is);

    event.getPlayer().updateInventory();

    Thanks to this thread and 1SmallVille1.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  21. Offline

    unrealdesign

    Sorry not many people answered, but this question is literally asked twice a week if not more.
     
Thread Status:
Not open for further replies.

Share This Page