Solved Error by InteractionEvent

Discussion in 'Plugin Development' started by DSCxSander, May 6, 2015.

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

    DSCxSander

    Help me, If i do Right Click whit the itemstacks, its work. But if i rightclick, or build there is an error in the console, How do i remove that?

    Interaction event:

    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
                if(e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§9Navigatie")){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR){
                        e.getPlayer().sendMessage("test");
                }
            } else {
                return;
            }
        }
    ItemStack:

    Code:
    public static ItemStack Navigate(){
            ItemStack i = new ItemStack(Material.NETHER_STAR);
            ItemMeta m = i.getItemMeta();
            m.setDisplayName("§9Navigatie");
            i.setItemMeta(m);
            return i;
        }
    Error:

    Error (Spoiler) (open)

    [20:02:03 ERROR]: Could not pass event PlayerInteractEvent to Eftelaars v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:299) ~[spigot.jar:git-Spigot-1571]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1571]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-1571]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-1571]
    at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:242) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.PlayerInteractManager.interact(PlayerInt
    eractManager.java:375) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :659) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.a(SourceFile:60)
    [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.handle(SourceFile
    :9) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184
    ) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java
    :81) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    31) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1571]
    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1571]
    Caused by: java.lang.NullPointerException
    at me.dsnxsander.e.events.JoinQuit.onPlayerInteract(JoinQuit.java:34) ~[
    ?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_60]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_60]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:296) ~[spigot.jar:git-Spigot-1571]
    ... 15 more
    >



    Line 34 is the line: if(e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§9Navigatie")){
     
  2. @DSCxSander What happens if the player is holding nothing?
     
  3. Offline

    Signatured

    Make sure the item has a meta.
    Code:java
    1. if (item.hasItemMeta()) {
    2. //do stuff
    3. }
     
  4. Offline

    DSCxSander

    @DJSkepter Nothing, only 1000n errors codes in the console ;)

    @Signatured Its has, look the itemstack on the top ;)
     
  5. Offline

    Konato_K

    @Signatured getItemMeta will return a new ItemMeta if the item doesn't have one, the exception for this is if the type of the ItemStack is AIR, in which case you have to check that first.

    Also, even if the item has a meta, calling getDisplayName().equals() will throw a null pointer if the item has no display name, checking hasDisplayName() is needed or at least handle the null that getDisplayName can return.

    Also, I recall some inventory methods returning null if there was no item, which is also something to be worried in some cases.
     
  6. Offline

    DSCxSander

  7. Offline

    87pen

    Code:
        public void onPlayerInteract(PlayerInteractEvent event){
            if(event.getItem() != null){
                if(event.getItem().equals(new ItemStack(Material.BOOK))){
                   //Code Here
                }
            }
        }
    You need to check if the Item is null because Minecraft does not return air if your holding nothing it returns null.
     
  8. Offline

    DSCxSander

    @87pen Thnxs! The error is solved!
     
Thread Status:
Not open for further replies.

Share This Page