Solved Error BlockBreakEvent

Discussion in 'Spigot Plugin Development' started by titivermeesch, Dec 4, 2017.

  1. Hi there,

    I have an error on this line while trying to execute a piece of code, but only if it has a specific item name.
    Code (Text):
    if(!(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§8*** §7Pickaxe 3x3 §8***") || p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§8*** §7Pickaxe 5x5 §8***") || p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§8*** §7Pickaxe 11x11 §8***") || p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§8*** §7Pickaxe 15x15 §8***"))) {
    And this is my error

    Code (Text):
    [22:10:34] [Server thread/ERROR]: Could not pass event BlockBreakEvent to QCore v1.0
        at$1.execute( ~[spigot_server.jar:git-Spigot-db6de12-07c3001]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot_server.jar:git-Spigot-db6de12-07c3001]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.breakBlock( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.a( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:40) [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:10) [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_144]
        at Source) [?:1.8.0_144]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at [spigot_server.jar:git-Spigot-db6de12-07c3001]
        at Source) [?:1.8.0_144]
    Caused by: java.lang.NullPointerException
        at ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_144]
        at$1.execute( ~[spigot_server.jar:git-Spigot-db6de12-07c3001]
        ... 17 more
    Can someone help me?
  2. If(p.getItemInHand() != null && (the rest of your if statement in parentheses like this)){
  3. If you've got essentials protect, get rid of it
  4. This has nothing to do with essentials protect..... This is a Null Pointer Exception, you should read up on what a NPE is.
  5. Since you're checking if it has a displayname which is apart of the ItemMeta you have to check if the item has ItemMeta and if it has a displayname or it'll throw that exception.
  6. Try to check this :
    Item in player's hand if its null or not. But if its null.., return;
    Block that breaked by player, if its null or block#getType() == Material#AIR, return;
    Displayname on the item that holded by the player, if it didn't has an DisplayName, return; ( you can use item#getItemMeta#hasDisplayName )

    I think that should fix your issue. Sorry 4 my bad English. Btw, I'm not going to give you the code ( I'm trying to not spoon feed here )
  7. Use your Brain: Caused by: java.lang.NullPointerException
    • Funny Funny x 1
  8. Btw you don't need to check if the block is air because BlockBreakEvent won't be called if it's air.
  9. Oh xD. My bad
  10. Nah it's all good! :) I'm trying to clean up the community from all the guys that are spreading improper code, don't worry you're not the first :p
  11. if (p.getItemInHand() != null) :V
  12. Please stop posting random shit, you can see it's already been done and this is like a 3 day old thread that hasn't gotten any posts in 3 days. Also checking if it's not null is not the issue please learn what you're saying before you say it.
  13. Hay una cosa que se llama prefijo que es para poner si esta solucionado o no.
  14. ?? lol?, please talk in English
  15. There is a thing that we call a prefix that is to put if the thread is solved or not.
    • Like Like x 1
  16. I tried your ideas but don't seems to help, I just need to continue my event when my pickaxe has that name, but I can't figure out why this error happens
  17. Add me on Discord: JavaEnum#9408
  18. Code (Text):
    public void event(BlockBreakEvent e) {
        Player p = e.getPlayer();
        if(!p.getInventory().getItemInMainHand().hasDisplayName()) return; //Default pickaxe
        String itemName = p.getInventory().getItemInMainHand().getDisplayName();
        if(!(itemName.equalsIgnoreCase("name1") || itemName.equalsIgnoreCase("name2"))) return; // If its name isn't one of the two names we want, don't continue
        // Continue event here
  19. That's starts helping me, but when the hand isn't holding anything, it gives me the error again, so I wrote this :

    Code (Text):
    String itemName = p.getInventory().getItemInHand().getItemMeta().getDisplayName();
            if(!p.getInventory().getItemInHand().getItemMeta().hasDisplayName() || p.getItemInHand() == null) return;
    For some reasons it still give me the error when I break a block by hand without any item.
  20. Before the first if check in my example, you need to check if the ItemInHand is null or ItemInHand.getType() is Material.AIR. Think about the logic behind it, too. If you have nothing or air in your hand, it can't be checked to see if it has a name.

Share This Page