Give Players Items

Discussion in 'Plugin Development' started by ComputerTurtle, Sep 7, 2013.

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

    ComputerTurtle

    Hey, I am sitting here, trying to figure out the code to give a player an item, and have it custom named. if you can help please let me know!
     
  2. Offline

    adam753

    Code:java
    1.  
    2. //Creates a new ItemStack of 1 dirt
    3. ItemStack stack = new ItemStack(Material.DIRT, 1);
    4.  
    5. //Gets the dirt's metadata so we can name it
    6. ItemMeta meta = stack.getItemMeta();
    7.  
    8. //Name the dirt "Special Dirt"
    9. meta.setDisplayName("Special Dirt");
    10.  
    11. //Assign the modified ItemMeta to the dirt
    12. stack.setItemMeta(meta);
    13.  
    14. //Give the special dirt to player
    15. player.getInventory().add(stack);
    16.  
     
  3. Offline

    adam753

    Datdenkikniet Would you need to do that? In Java, variables are references, so changing meta should also change stack's meta because they're the same instance.
     
  4. Yes, you do need to do that, since .getMetaData() will only get you a copy of the meta data object (similar to .getLocation() etc)
     
    adam753 likes this.

  5. Yep, ya need to ;)
     
  6. Offline

    adam753

Thread Status:
Not open for further replies.

Share This Page