Solved RIGHT_CLICK_BLOCK error in console

Discussion in 'Plugin Development' started by sebafudi, Oct 21, 2016.

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

    sebafudi

    Hello!

    I have this code:
    Code:
        public void dowod(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
    
            player.sendMessage("asdf: " + event.getAction());
    
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK));
            if(event.getItem().getType() == Material.PAPER) {
                player.sendMessage(event.getItem().getItemMeta() + "asdf");
            }
        }
    And whenever i click on air it works, but when i click on block it gives me error in console:
    Code:
    [14:44:50] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to HexPlay v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:231) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.a(PlayerInteractManager.java:496) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:910) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_102]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_102]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_102]
    Caused by: java.lang.NullPointerException
        at me.sebafudi.PlayerListener.dowod(PlayerListener.java:22) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_102]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.10.2.jar:git-Spigot-047f737-744e1a1]
        ... 17 more
    I can see text from player.sendMessage but it gives that error.

    Another thing is when i type
    Code:
    player.sendMessage(event.getItem().getItemMeta() + "");
    
    - The method sendMessage(String) in the type CommandSender is not applicable for the arguments (ItemMeta)
    How can i do this without ""?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @sebafudi 1. The click if statement is not doing anything.
    2. That causes the code below to run and there is no itemmeta or no item.
     
  3. Offline

    sebafudi

    I have a paper in hand, and as i said, it don't give that error when i click on air, only when i click on block.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @sebafudi Please fix the if statement and post the updated code.
     
  5. Offline

    sebafudi

    @timtower
    Code:
        public void dowod(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
           
            player.sendMessage("asdf: " + event.getAction());
           
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if(event.getItem().getType() == Material.PAPER) {
                    player.sendMessage(event.getItem().getItemMeta() + "asdf");
                }
            }
        }
    I forgot that damn {} :p

    But still this same error.
     
  6. Offline

    ipodtouch0218

    Code:
    event.getItem()
    ItemStack can be null
    Code:
    event.getItem().getItemMeta()
    ItemMeta can be null
    Code:
    if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK));
    
    The semicolon closes the if statement, making it check every action.
    Use "==" for Enums (you did with Material.Paper)


    Run meta.toString() to make it into a string.
     
  7. Offline

    sebafudi

    @ipodtouch0218

    Code:
        public void dowod(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
           
            player.sendMessage(event.getAction().toString());
           
            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (event.getItem().getType() == Material.PAPER) {
                    if (event.getItem().getItemMeta().getLore() != null) {
                        player.sendMessage(event.getItem().getItemMeta().getLore().toString());
                    }
                }
            }
        }
    I've changed it, now it check if it is Paper and if it has lore, but still error. I've got this same error when i click on any block with hand, paper and any other item, that i can't place.
     
  8. Offline

    timtower Administrator Administrator Moderator

    @sebafudi Item can be null, itemMeta can be null, lore can be null.
    They can all throw an error while you run this line:
    event.getItem().getItemMeta().getLore() != null
     
  9. Offline

    sebafudi

    @timtower @ipodtouch0218

    Code:
        public void dowod(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
           
            player.sendMessage(event.getAction().toString());
    
            if (event.getItem() != null && event.getItem().getType() != null && event.getItem().getItemMeta() != null) {
                if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                    if (event.getItem().getType() == Material.PAPER) {
                        if (event.getItem().getItemMeta().getLore() != null) {
                            player.sendMessage(event.getItem().getItemMeta().getLore().toString());
                            event.setCancelled(true);
                        }
                    }
                }
            }
        }
    I added that if and it all work now. Thank you all.
     
Thread Status:
Not open for further replies.

Share This Page