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):
        public void EbowShoot(EntityShootBowEvent e) {
            Player p = e.getEntity().getKiller();
            Projectile proj = (Projectile) e.getProjectile();

            if (proj == null) {
            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
        at$1.execute( ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callEntityShootBowEvent( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.ItemBow.a( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.ItemStack.a( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.EntityLiving.clearActiveItem( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a( [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$ [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_111]
        at 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( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C( [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
        at Source) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
        at me.KohakuSaintCrown.BowTrailsSuper.Core.Animations.EbowShoot( ~[?:?]
        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$1.execute( ~[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):
        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;););)