Get name of an item

Discussion in 'Plugin Development' started by legostarwarszach, Jun 28, 2013.

Thread Status:
Not open for further replies.
  1. How would I get the name of an item clicked in a inventory? My code so far for getting it:
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent click){
            if(click.getCurrentItem().getType() == Material.SKULL_ITEM){
     
                   
            }
        }
     
  2. Offline

    caseif

    If you want the display name, call getItemMeta().getDisplayName() on the ItemStack.
     
    legostarwarszach likes this.
  3. AngryNerd
    Can you please give me an example?
     
  4. Offline

    inventorman101 Retired Staff

    I think this would work

    Code:java
    1.  
    2. @EventHandler
    3. public void onInventoryClick(InventoryClickEvent click){
    4. if(click.getCurrentItem().getType() == Material.SKULL_ITEM) && click.getCurrentItem.getItemMeta().getDisplayName.equalsIgnoreCase("Skull Head"){
    5.  
    6.  
    7. }
    8. }
    9.  
     
    legostarwarszach likes this.
  5. Offline

    caseif

    Don't forget null checks, though. :D
     
  6. Okay :D

    AngryNerd
    How would I get what's after that?
    Like Player: Legostarwarszach
    How would I get info about the player?

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

    caseif

    The owner of the skull? Cast the ItemMeta to SkullMeta and call .getOwner().
     
  8. Offline

    inventorman101 Retired Staff

    legostarwarszach

    Code:java
    1.  
    2. Player p = (Player) click;
    3. String name = p.getDisplayName(); //Gets the player's name
    4.  

    That will give you the name of the player who clicked the item.
     
  9. inventorman101
    Is there any way I can get the name of the player name in the name of the item?

    inventorman101
    For example, if the name of the skull is: Player: Notch
    If the player clicks on that skull it will broadcast: Notch was selected!

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

    iextasey

    Code:java
    1.  
    2. String name = "Player: Notch";
    3. String afterName = name.substring(name.indexOf(":")+2);
    4.  
    5. this.getServer().broadcastMessage(afterName+" was selected!");
    6.  
     
  11. iextasey
    How would I get the location of the player? (EX: Notch)
     
  12. Offline

    iextasey

    Code:java
    1. Bukkit.getPlayer(afterName).getLocation()
     
  13. iextasey
    Code:
    Type mismatch: cannot convert from String to int
     
  14. Offline

    iextasey

    Please paste your code.
     
  15. Offline

    iextasey

    Why is there a "+" after the name1?
     
  16. iextasey
    Umm........dope
    It works now, thx :D
     
  17. Offline

    iextasey

    yw, change Prefix to [Solved] ;)
     
Thread Status:
Not open for further replies.

Share This Page