Solved BlockBreakEvent error with tryBreakandDrop() method.

Discussion in 'Spigot Plugin Development' started by daveyxbl, Sep 16, 2016.

  1. Code (Text):
        @EventHandler(ignoreCancelled=true, priority=EventPriority.HIGH)
          public void onMine(BlockBreakEvent event)
          {
            if ((event.getPlayer().getInventory().getItemInMainHand() != null) && (!event.isCancelled()) && (!this.blacklistedWorlds.contains(event.getPlayer().getWorld().getName())))
            {
              if (event.getPlayer().getInventory().getItemInMainHand().getEnchantments().containsKey(this.ENCHANTMENTS.get("POWERMINER"))) {
    BlockFace face = (BlockFace)this.lastClicked.get(event.getPlayer().getName());
                  event.getBlock().getWorld().playSound(event.getPlayer().getLocation(), Sound.BLOCK_ANVIL_PLACE, 1.0F, 1.0F);
                 for (int i = 1; i <= 5; i++) {            
                  for (int x = -1; x <= 1; x++) {
                    for (int y = -1; y <= 1; y++) {
                      for (int z = -1; z <= 1; z++) {
    ####################################################################
                        tryBreakAndDrop(event.getPlayer(), event.getBlock().getRelative(x, y, z));
    #####################################################################
                      }
                    }
                  }
                 }
                  damageHeldItem(event.getPlayer());
              }
              }
          }
        private void damageHeldItem(Player p)
          {
            p.getItemInHand().setDurability((short)(p.getItemInHand().getDurability() + 1));
            if (p.getItemInHand().getDurability() == p.getItemInHand().getType().getMaxDurability()) {
              p.setItemInHand(null);
            }
          }
       
          private boolean tryBreak(Player player, Block block)
          {
            if ((block != null) && (block.getType() != Material.BEDROCK) && (this.wgp.canBuild(player, block)))
            {
              block.setType(Material.AIR);
              return true;
            }
            return false;
          }
       
          private boolean tryBreakAndDrop(Player player, Block block)
          {
    #################################################################
            if ((block != null) && (block.getType() != Material.BEDROCK) && (this.wgp.canBuild(player, block)))
    #################################################################
            {
              block.breakNaturally();
              return true;
            }
            return false;
          }

    Errors will be encased with '#'s

    Error log:

    Code (Text):
    [23:10:48] [Server thread/ERROR]: Could not pass event BlockBreakEvent to FrozenPicks v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerInteractManager.breakBlock(PlayerInteractManager.java:292) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerInteractManager.a(PlayerInteractManager.java:218) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:846) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInBlockDig.a(SourceFile:40) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInBlockDig.a(SourceFile:10) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
        at org.db.fp.m.Main.tryBreakAndDrop(Main.java:176) ~[?:?]
        at org.db.fp.m.Main.onMine(Main.java:148) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.9.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-e6f93f4-935f18b]
        ... 17 more
     
  2. What's the actual error?
     
  3. I've added the log.
     
  4. Not sure if this is the fix, but I'm assuming the NPE is coming from the blocks around the location that was mined, try checking if all of the blocks you're trying to break with the tryBreakAndDrop(event.getPlayer(), event.getBlock().getRelative(x, y, z)); aren't null first
     
  5. There is already a null check within the method itself.
     
  6. Are you sure this isn't null? Did you initialize the variable?
     
  7. "WorldGuardPlugin wgp;" unless i need more than that i'm pretty sure i did.
     
  8. Of course your variable is going to be null, you're not setting it to anything...
     
    • Agree Agree x 1