I cant get this code to work properly.

Discussion in 'Plugin Development' started by xXRobbie21Xx, May 8, 2014.

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

    xXRobbie21Xx

    Hey guys its Robbie again and i was wondering if anyone can tell me whats wrong with this code!

    -Its suppose to send you a message depending on the item in hand
    (if you are holding "Item" then it should say "hey") (If you are holding "Item2" it should say "Hey There)

    -Instead when i click "Item" it works correctly, but the second i click "Item2" nothing happens.

    -The console is telling me there is something wrong with the "if else" section.

    Code:
    package me.xXRobbie21Xx.TcEssentials;
     
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    public class PlayerListener implements Listener{
     
       
     
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent event){
        Player p = event.getPlayer();
        if (p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item"))
            p.sendMessage("Hey");
           
        else if (p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item2"))
            p.sendMessage("Hey There"); }}
    
    So if anyone can see an immediate error within this, i would appreciate it if you were to let me know! Thanks
     
  2. Offline

    MrAwellstein

    Post your error.

    Edit:
    Also explain more. Like what items were you holding, and were they named?
     
  3. Offline

    xXRobbie21Xx


    Im clicking items with the name, Item, or Item2.
    And i have no clue how to copy the console text D:

    Any ideas?

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

    Mayoz

    Post the error please.
     
  5. xXRobbie21Xx You can take a screenshot of the console text... or go to the log file and copy it from there...
     
  6. Offline

    xXRobbie21Xx

    error.PNG

    DJSkepter Mayoz ok i got it

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

    Mayoz

    You need to check if the item in their hand isn't null
    if(p.getItemInHand != null)
     
  8. Offline

    xXRobbie21Xx

    Mayoz Ok i understand, still not working, i probably still set it up wrong. Is this what you are suggesting?
    Code:
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent event){
        Player p = event.getPlayer();
        if (p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item"))
            p.sendMessage("Hey");
           
        else if(p.getItemInHand() != null)
            if(p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item2"))
                p.sendMessage("Hey There"); }}
    
     
  9. Offline

    Mayoz

    No, I mean like
    Code:
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent event){
        Player p = event.getPlayer();
        if(getItemInHand() != null)
            if (p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item"))
                p.sendMessage("Hey");
            else if (p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item2"))
                p.sendMessage("Hey There"); }}
    alternatively, you could also do
    Code:
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent event){
        Player p = event.getPlayer();
        if(getItemInHand() == null)
            return;
        if (p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item"))
            p.sendMessage("Hey");
        else if (p.getItemInHand().getItemMeta().getDisplayName().startsWith("Item2"))
            p.sendMessage("Hey There"); }}
    I would not recommend this second one though, because if you want to do other things in this then it won't happen if the item in hand is null.
     
  10. Offline

    AoH_Ruthless

    Mayoz xXRobbie21Xx
    Uhm, if the item is named "Item2", both if statements will execute seeing as it starts with "Item" and "Item2"...

    In the first item displayname check (where you check if the name starts with item only), return at the end of it.
     
    Mayoz likes this.
  11. Offline

    Mayoz

    AoH_Ruthless Jeez, I completely overlooked that, but returning after it would only make it display the first message, even if its name is Item2. If he really wants to do the startsWith checks he should put the Item2 check first, otherwise if you don't need it then just do .equals.
     
    AoH_Ruthless likes this.
  12. Offline

    AoH_Ruthless

    Mayoz
    Woops, you are right.
     
  13. Offline

    xXRobbie21Xx

    Mayoz okay okay, that makes sence thanks guys
     
Thread Status:
Not open for further replies.

Share This Page