Solved Cant get this to work(playerInteract event)

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

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

    xXRobbie21Xx

    Hey, iv been testing this code forever but i cant seem to get it to work. Everything seems just fine but this is what i get.
    (When a player interacts with a sand block, it should send them a message)

    [​IMG]

    Here is my code.
    (Main class)
    Code:
    package me.xXRobbie21Xx.TcEssentials;
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class TcEssentials extends JavaPlugin {
       
     
        @Override
        public void onEnable() {
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(new PlayerListener(), this);
       
     
           
        }
       
        @Override
        public void onDisable() {
           
        }
    }
    
    Here is my player listener class.

    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("Sand"))
                p.sendMessage("Hey");
        }
    }
    
    I made this as simple as possible for testing reasons.
     

    Attached Files:

  2. Offline

    xXRobbie21Xx

    another error.PNG

    Sorry about that, here it looks a bit clearer :p
     
  3. Offline

    Konkz

    I can't even read the error... I even squinted my eyes.

    I'm guessing it's because you are not checking if them item has meta but your code
    presumes it does. Just add a check if it has an item meta.

    Also, you should do p.getItemInHand().getItemMeta().getDisplayName().equals("Sand") - I hope that it's a custom item that you named. :p

    If it's Normal sand you should do p.getItemInHand().getItemStack() == Material.SAND;
     
  4. Offline

    xXRobbie21Xx

    Konkz Allrighty, thanks for the advice, it means alot. I posted a clearer error message if you want to take a look btw. :p

    Edit: It says that there is something wrong with line 13 on the listener class. This is confusing me because i personally dont see something wrong. By any chance do you see the problem?

    Line 13 is Player p = event.getPlayer();
     
  5. Offline

    aninsanellama

    I believe the code is throwing a NullPointerException because you are assuming that the ItemStack has an ItemMeta and a display name. To solve this problem simply add two more if statements checking whether the ItemStack has an ItemMeta or a display name.

    ItemMeta check:
    Code:java
    1. if(p.getItemInHand().hasItemMeta())
    2. {
    3. // Your Code
    4. }


    Display Name check (after item meta check):
    Code:java
    1. if(p.getItemInHand().getItemMeta().hasDisplayName())
    2. {
    3. // Your Code
    4. }
     
  6. Offline

    xXRobbie21Xx

    aninsanellama ok thank you so much for the advice, however it seems like line 13 (Player p = event.getPlayer(); ) is giving me some trouble. Its really strange, did i do something wrong with that?
     
  7. Offline

    aninsanellama

    I can't see a reason that would be causing an error. Did you try the other solutions that were given?
     
  8. Offline

    xXRobbie21Xx

    aninsanellama yeah absolutely, thats why im so stuck on this. Thanks for your time btw.
     
  9. Offline

    Xyplo

  10. Offline

    TGRHavoc

    Xyplo
    The error refers to line 13 of the class "PlayerListenerHandler" not his main class..
     
  11. Offline

    xXRobbie21Xx

    TGRHavoc Xyplo line 13 in my player listener (not main class) is Player p = event.getPlayer(); and i have no clue why that is giving me problems. If you see the problem please let me know, it would be extremely appreciated.
     
  12. Offline

    TGRHavoc

  13. Offline

    xXRobbie21Xx

    TGRHavoc yeah here it is blah.PNG

    for the whole error i posted an image above.
     
  14. Offline

    TGRHavoc

    xXRobbie21Xx
    Above where you declared the player, put:
    Code:java
    1. Bukkit.broadcastMessage(event.getPlayer().getName() + " has triggered the event");

    and see if the message gets broadcasted.. It seems that the player is null for some reason...
     
  15. Offline

    xXRobbie21Xx

    TGRHavoc hmm, that is extremely wierd, i carefully took everyones advice and changed a fiew things and somehow i got it to work correctly. Thanks for your time, its people like you that inspire people to do things like this.
    Appreciate it.
     
  16. Offline

    TGRHavoc

    xXRobbie21Xx
    How did you get it to work?
    And thanks, I appreciate it :D
     
  17. Offline

    xXRobbie21Xx

    TGRHavoc i changed it back to the first code i had (where it checked for the meta data) and then i changed the name of the block it will check for, and it worked!
     
  18. Offline

    TGRHavoc

    xXRobbie21Xx
    Nice! Don't forget to mark this thread as "Solved" :p
     
  19. Offline

    Xyplo

    It was his code to begin with... Line 13 was HIS code...
     
Thread Status:
Not open for further replies.

Share This Page