How would i check the metadata of a item

Discussion in 'Plugin Development' started by ChromeHD__, Mar 3, 2014.

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

    ChromeHD__

    Okay so i am making a gun plugin and i need to check the name of the item first before i shoot anything but i have tried everything to get the metadata but it does not work so how would i go around about doing this
     
  2. Offline

    DogeDev

    Code:java
    1. ItemStack item = player.getItemInhand();
    2. ItemMeta meta = item.getItemMeta();
    3. String name = meta.getDisplayName();
    4.  

    ^That will get the item meta and display name of the item in the player's hand. From there just test if(name.equalsIgnoreCase("whatever")){

    Example of Usage:
    Code:java
    1. public void onClick(PlayerInteractEvent e){
    2. Player p = e.getPlayer()
    3. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    4. if(p.getItemInHand().getType() == Material.APPLE){
    5. ItemStack item = p.getItemInHand;
    6. if(item.hasItemMeta()){
    7. ItemMeta meta = item.getItemMeta();
    8. if(meta.getDisplayName().equalsIgnoreCase("Doge")){
    9. p.sendMessage("You have a Doge Apple");
    10. }
    11. }
    12. }
    13. }
    14. }

    Sends a player a message when they right click with an apple named "doge"
     
  3. Offline

    calebbfmv

    Code:
    //coded in bukkit
    ItemStack itemInHandOnEventOrSomething = player.getItemInHand();
    if(itemInHandOnEventOrSomething.hasMteaData()){
      String name = itemInHandOnEventOrSomething.getItemMeta().getDisplayName();
      if(name.equalsIgnoreCase(ChatColor.stripColor("calebbfmv"))){
        System.out.println("calebbfmv is amazing");
      }
    }
     
  4. Offline

    ChromeHD__

    Code:java
    1. @EventHandler
    2. public void RightClick(PlayerInteractEvent e){
    3. Player p = (Player) e.getPlayer();
    4. ItemStack i = new ItemStack(Material.STICK);
    5. ItemMeta meta = i.getItemMeta();
    6. meta.setDisplayName("§5§l[§bBor Rifle§5§l]");
    7. Player player = e.getPlayer();
    8. if(e.getAction().equals(Action.LEFT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
    9. if(p.getItemInHand().getType() == Material.APPLE){
    10. ItemStack item = p.getItemInHand();
    11. if(item.hasItemMeta()){
    12. if(meta.getDisplayName().equalsIgnoreCase("§5§l[§bBor Rifle§5§l]")){
    13. p.getPlayer().launchProjectile(Snowball.class);
    14. }else{
    15.  
    16. }
    17. }
    18. }
    19. }else{
    20.  
    21. }
    22. }
    23.  
    24. }


    Now it wont do anything on left click
     
  5. Offline

    calebbfmv

    ChromeHD__
    Please for the love of God do ChatColor.stripColor("name");
    Also, try doing a .contains instead of .equals
     
  6. Offline

    ChromeHD__

    calebbfmv
    Code:
    @EventHandler
        public void RightClick(PlayerInteractEvent  e){
            Player p = (Player) e.getPlayer();
            ItemStack i = new ItemStack(Material.STICK);
            ItemMeta meta = i.getItemMeta();
            meta.setDisplayName("§5§l[§bBor Rifle§5§l]");
              Player player = e.getPlayer();
              if(e.getAction().equals(Action.LEFT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
                  if(p.getItemInHand().getType() == Material.APPLE){
                      ItemStack item = p.getItemInHand();
                      if(item.hasItemMeta()){
                      if(meta.getDisplayName().contains((ChatColor.stripColor(("§5§l[§bBor Rifle§5§l]"))))){
                          p.getPlayer().launchProjectile(Snowball.class);
                      }else{
                         
                      }
                      }
                      }
                      }else{
                         
                      }
                    }
                 
        }
    i tried that dosent work
     
  7. Offline

    calebbfmv

    You don't need all of the color codes inside of the ChatColor.stripColor(), just do the String.
     
  8. Offline

    ChromeHD__

    Code:java
    1. @EventHandler
    2. public void RightClick(PlayerInteractEvent e){
    3. Player p = (Player) e.getPlayer();
    4. ItemStack i = new ItemStack(Material.STICK);
    5. ItemMeta meta = i.getItemMeta();
    6. meta.setDisplayName("§5§l[§bBor Rifle§5§l]");
    7. Player player = e.getPlayer();
    8. if(e.getAction().equals(Action.LEFT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
    9. if(p.getItemInHand().getType() == Material.APPLE){
    10. ItemStack item = p.getItemInHand();
    11. if(i.hasItemMeta()){
    12. if(meta.getDisplayName().contains((ChatColor.stripColor(("[Bor Rifle]"))))){
    13. p.getPlayer().launchProjectile(Snowball.class);
    14. }else{
    15.  
    16. }
    17. }
    18. }
    19. }else{
    20.  
    21. }
    22. }
    23.  
    24. }

    this dosent work either
     
  9. Offline

    calebbfmv

    Try adding some Debug lines
     
  10. Offline

    ChromeHD__

    calebbfmv does it look like there is anything wrong with the code
     
  11. Offline

    calebbfmv

    ChromeHD__
    Then there is something wrong with the item in your hand.
    EDIT:
    You never set the ItemMeta of the Itemstack
     
  12. Offline

    Wolfey

    Well, it looks like on line 8, you're checking to see if the type of item in the player's hand is a stick, then on line 9 you're checking if it is a apple. So it's definitely gonna fail there.
     
    calebbfmv likes this.
  13. Offline

    ChromeHD__

    Omg I can't believe I didn't see that haha I will check it out in the morning and post back Wolfey calebbfmv

    DogeDev Wolfey i have tried everything it dosent work is there any working code out there

    Here is my stacktrace

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

    Alshain01

    ChromeHD__ getItemInHand can return null if there is no item in the hand.

    Code:java
    1. if(e.getAction().equals(Action.LEFT_CLICK_AIR) && player.getItemInHand() != null && player.getItemInHand().getType() == Material.STICK) {
     
  15. Offline

    ChromeHD__

    Code:java
    1. @EventHandler
    2. public void RightClick(PlayerInteractEvent e){
    3. Player p = (Player) e.getPlayer();
    4. ItemStack i = new ItemStack(Material.STICK);
    5. ItemMeta meta = i.getItemMeta();
    6. if(p.getItemInHand().getType() == Material.STICK){
    7. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
    8.  
    9. ItemStack item = p.getItemInHand();
    10. if(item.hasItemMeta()){
    11. if(meta.getDisplayName().equalsIgnoreCase((ChatColor.stripColor(("[Bor Rifle]"))))){
    12.  
    13. e.getPlayer().launchProjectile(EnderPearl.class);
    14. }else{
    15. p.sendMessage("debug");
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }


    new stack trace it creates it on right click

     
  16. Offline

    Alshain01

    Read my last post, your still running getType() on null.

    EDIT: Also, there is no need to cast (Player) event.getPlayer(). It's already a player.
     
  17. Offline

    ChromeHD__

    Alshain01
    Code:java
    1. if(meta.getDisplayName().equalsIgnoreCase((ChatColor.stripColor(("[Bor Rifle]"))))){


    it appears on that line
     
  18. Offline

    Alshain01

    Ok, you have more than one error then. You still need to check that they have something in your hand or your going to get an NPE anyway.

    As for THAT line, getDisplayName can also be null.
     
  19. Offline

    ChromeHD__

    Code:java
    1. if(meta.getDisplayName().equalsIgnoreCase((ChatColor.stripColor(("[Bor Rifle]")))) && meta.getDisplayName() != null ){


    i put the !=null on the getitem in hand and that above and its still saying that line Alshain01

    Someone please help this has been annoying me for ages

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  20. ChromeHD__ hmm.. Define it as a single color, and just try to get that ChatColor.
     
  21. Offline

    Alshain01

    You put it in the wrong place, your order of operations is using it prior to checking it.
    Code:java
    1. if(meta.getDisplayName() != null && meta.getDisplayName().equalsIgnoreCase(ChatColor.stripColor("[Bor Rifle]"))){


    && is a short circuit operator, meaning if the first condition is false, the second one never gets evaluated. That prevents the NPE.
     
  22. Offline

    ChromeHD__

    Alshain01 Now i am not getting anything no stack trace no error
     
  23. Offline

    Alshain01

    Right, because there is no display name (hence the reason you were getting a Null Pointer Exception).
     
  24. Offline

    Garris0n

    Teaching people to do this probably isn't the best, as they might get confused if the colors happen to matter for what they're doing.

    That said, use the ChatColor enum instead of the selection symbol codes.
     
  25. Offline

    ChromeHD__

    Alshain01 but it still wont shoot the enderperal
     
  26. Offline

    Alshain01

    Because you designed it so that if it did not have that Display Name, it wouldn't shoot the ender pearl. Does your stick have that display name set?
     
  27. Offline

    ChromeHD__

    Alshain01 Yes when they use the command to spawn it in it sets the display name
     
  28. Offline

    Alshain01

    ChromeHD__ Ok, I need the full code. Both a re-post of this updated method AND that command handler.
     
  29. Offline

    ChromeHD__

  30. Offline

    Alshain01

    Just post it here.
     
Thread Status:
Not open for further replies.

Share This Page