Detecting Item Names

Discussion in 'Plugin Development' started by TheChugBug, May 11, 2014.

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

    TheChugBug

    I'm trying to detect whenever the player eats a pumpkin pie named "Apple Pie." This is my code:

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGH)
    3. public void PlayerConsumeItemEvent(PlayerItemConsumeEvent e) {
    4. final Player p = e.getPlayer();
    5. final ItemStack i = e.getItem();
    6. if(i.getType() == Material.PUMPKIN_PIE && i.getItemMeta().getDisplayName() == "Apple Pie") {
    7. p.sendMessage("yes");
    8. p.setFoodLevel(p.getFoodLevel() + 2);
    9. } else {
    10. p.sendMessage("no");
    11. }
    12. }
    13.  


    The i.getType() works fine, but the item meta never returns true. I tried tons of things to get this to work. Please help!
     
  2. Offline

    GeorgeeeHD

  3. Offline

    Gater12

    TheChugBug
    Use .equals or .equalsIgnoreCase

    You should check if it has an ItemMeta and display name before trying to get it or it will throw an exception.
     
    Krotass likes this.
  4. Offline

    MineStein

  5. Offline

    Krotass

    Code:
    i.getItemMeta().getDisplayName().contains("Apple Pie")
     
  6. Offline

    TheChugBug


    I'm kind of new with java, what's the difference between == and .equals()?

    (BTW, it works)
     
  7. Offline

    Gater12

    TheChugBug
    .equals() checks the content of the String == checks if it references the same object in memory

    Code:java
    1. /* I think this is the right example */
    2. String s1 = "Bob";
    3. String s2 = "Bob";
    4. if(s1 == s2)
    5. /* false because it doesn't s2 doesn't reference the same object as s1 */


    Code:java
    1.  
    2. String s1 = "Bob";
    3. String s2 = s1;
    4. if(s1 == s2)
    5. /* true because s2 references the same object as s1 */
     
  8. Offline

    hubeb

    TheChugBug also if you have the display name colored make sure you add the color to it or it won't detect the name.
     
Thread Status:
Not open for further replies.

Share This Page