Solved Getting player from EntityShootBowEvent error

Discussion in 'Spigot Plugin Development' started by KohakuSaintCrown, Jun 2, 2017.

  1. Im trying send sound or anything to player when him shoot a bow, but is getting errors.

    Code (Text):
     @EventHandler
        public void EbowShoot(EntityShootBowEvent e) {
            Player p = e.getEntity().getKiller();
            Projectile proj = (Projectile) e.getProjectile();

            if (proj == null) {
                return;
            }
            p.getWorld().playSound(p.getLocation(), Sound.ENTITY_SHULKER_BULLET_HIT, 1, 1); //Error line
        }
    Is getting: Caused by: java.lang.NullPointerException
     
    • Naming Conventions
    • Show us the full error
     
  2. Code (Text):
    22:37:09] [Server thread/ERROR]: Could not pass event EntityShootBowEvent to BowTrailsSuper v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callEntityShootBowEvent(CraftEventFactory.java:252) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.ItemBow.a(ItemBow.java:88) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.ItemStack.a(ItemStack.java:541) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.EntityLiving.clearActiveItem(EntityLiving.java:2372) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:819) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:40) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:10) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
        at me.KohakuSaintCrown.BowTrailsSuper.Core.Animations.EbowShoot(Animations.java:33) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        ... 19 more
     
  3. I believe you should first check if the shooter is a player, and then cast it to a player when declaring your player variable.

    Other than that, I believe the error could come from a null player variable, since you say the error line is the playSound.
     
  4. How do you know the player died?

    From what I see your event is well EntityShootBowEvent meaning...

    For this code not to have an error the entity would somehow have a killer before their bow was shot?
    Then it would get the killer of the entity(player) that will in the future shoot an arrow and play a sound to the killer?

    What do you want to have happen here?

    Also do what @Cervinakuy said and check before casting. ;)
     
    #5 Lightspeed360, Jun 2, 2017
    Last edited: Jun 2, 2017
    • Like Like x 1
  5. @Lightspeed360 is right, that's what I was thinking too. It's a null player variable, and it makes sense, the player isn't always dead :p
     
  6. Give this a try. This will play a sound to the shooter.
    Code (Text):
        @EventHandler
        public void EbowShoot(EntityShootBowEvent e) {
            if(e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
                Entity proj = e.getProjectile();
                if(proj == null) return;
               
                p.getWorld().playSound(p.getLocation(), Sound.ENTITY_SHULKER_BULLET_HIT, 1, 1);
            }
        }
     
    • Winner Winner x 1
  7. Yes!!! is working finally !!! thank you a lot guys !! :D:D:D;););)