Error using BreakBlockEvent

Discussion in 'Spigot Plugin Development' started by Xuho, May 1, 2017.

  1. I get this error when a player break a block

    Code (Text):
    [22:21:19 ERROR]: Could not pass event BlockBreakEvent to HowhitSurvival v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.breakBlock(PlayerI
    nteractManager.java:286) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.a(PlayerInteractMa
    nager.java:215) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :614) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:40) [s
    pigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:10) [s
    pigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
    ot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    18) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    67) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    57) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :560) [spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at com.gmail.icomboderp.Picos.PicarSpawners(Picos.java:22) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_131]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot-1.8.7.jar:git-Spigot-f928e7a-994b2aa]
            ... 17 more
    Error code:
    Code (Text):
            if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getLore().contains("asd")) {
                    p.updateInventory();
            }
    Thanks in advance.
     
  2. You need to check to see if itemInHand is null before you check to see if it has meta. Then if it also has meta you can get the lore.

    Items with no lore or a null item will cause this error


    Sent from my iPhone using Tapatalk
     
  3. #getItemInHand returned null, or it doesn't have a lore.
     
  4. thanks, I'll try it.
     
  5. I still get the error.
    I do this:
    Code (Text):
    if (!e.getPlayer().getInventory().getItemInHand().equals(null) || e.getPlayer().getInventory().getItemInHand().getItemMeta().getLore().equals(null)) {
        if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getLore().contains("asd")) {
            //things
        }
    } else if (e.getPlayer().getInventory().getItemInHand().equals(null) || e.getPlayer().getInventory().getItemInHand().getItemMeta().getLore().equals(null)) {
        return;
    }
     
  6. Don't check with .equals if something is null. Use the compare operator (==).
    Example:
    Code (Text):
    if (e.getPlayer().getInventory().getItemInHand() != null && e.getPlayer().getInventory().getItemInHand().getItemMeta().getLore() == null) {
    /*do something*/
    }
    Edit:
    Is it correct that you do a null check for the lore and calling the contains method if the check is sucessful?
     
  7. You should do if itemstack == null && ItemStack.hasMeta()

    Before checking the lore / display name
    Sent from my iPhone using Tapatalk
     
  8. ty, It's working but I have a doubt Why does not it work using .equals? In theory it does the same thing as the ==
     
  9. Equals is for string comparisons when you're using enum or objects use ==


    Sent from my iPhone using Tapatalk
     
  10. thanks for the explanation
    Ups, I did not realize this, when i break with the empty iteminhand it still gives error

    Code:
    Code (Text):
            if (e.getPlayer().getInventory().getItemInHand() != null && e.getPlayer().getInventory().getItemInHand().getItemMeta().getLore() != null) {
            }
     
  11. BananaPuncher714

    Supporter

    You don't check if it's null. You have to check if the type is equal to Material.AIR. Then you can check the lore.
    Code (Text):
    if ( item.getType() != Material.AIR && item.hasItemMeta() && item.getItemMeta().hasLore() ) {
        // Do stuff
    }
     
  12. Haaaa, no.

    Any retrieval from an inventory will return null for an empty itemstack. InventoryClickEvent#getCursor returns AIR.
     
    • Agree Agree x 1
  13. Make sure to check if the item meta data exist ( != null) and if the lore exists ( != null)
     
  14. I Tried but still no working