Solved Could not pass event

Discussion in 'Spigot Plugin Help' started by DuckKing, Mar 14, 2020.

Thread Status:
Not open for further replies.
  1. Hello, I have this annoying error:

    Could not pass event ProjectileHitEvent to Gadgets v1.0-SNAPSHOT
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callProjectileHitEvent(CraftEventFactory.java:745) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.EntityArrow.s_(EntityArrow.java:225) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.World.entityJoinedWorld(World.java:1604) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.World.g(World.java:1579) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.World.tickEntities(World.java:1422) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.WorldServer.tickEntities(WorldServer.java:576) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:772) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.entity.CraftArrow cannot be cast to org.bukkit.entity.Player
    at me.duck.gadgets.Events.onBowShoot(Events.java:17) ~[?:?] // Here I believe is the error but I can't get it to working.
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
    ... 14 more
    Hello, I have been stuck at an annoying thing for quite a while, the error says it could not pass ProjectileHitEvent. What I am basically trying to do is making the player shoot with bow with custom name and wherever the arrow hits, it teleports the player to the arrow. Here is the error:

    It seems to be saying at line 17 there is an error.

    Line 17 is Player player = (Player) e.getEntity();

    Yes I am new to java if you are wondering. Here is my code:

    Code (Java):
    package me.duck.gadgets;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;

    public class Events implements Listener {

        @EventHandler
        public void onBowShoot(ProjectileHitEvent e) {
            if(e.getEntity() instanceof Arrow) {
                Player shooter = (Player) e.getEntity().getShooter();
                Player player = (Player) e.getEntity();

                if(!player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Teleportation " + ChatColor.AQUA + "Bow") || (player.getInventory() == null)) {
                    return;
                }else {
                    Location location = e.getEntity().getLocation();

                    shooter.teleport(location);
                    shooter.sendMessage(ChatColor.GREEN + "Woosh!");
                }

            }

        }
    }

    Hope someone can help me with this, thanks.
     
  2. Skeletons can also shoot arrows, so make sure e.getEntity.getShooter() is actually a player.
     
  3. Ok so you have two problems:
    1. You first have to check if the shooter is instanceof Player before casting it
    2. You have checked if e.getEntity() is instanceof Arrow and then you casted it to Player. You don’t need that variable anyway
     
  4. Okay, i'll add a check.

    Edit: Okay so I added the check, and yet still the same error. Here is my code:

    Code (Java):
    package me.duck.gadgets;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;

    public class Events implements Listener {

        @EventHandler
        public void onBowShoot(ProjectileHitEvent e) {
            if(e.getEntity() instanceof Arrow) {
                Player shooter = (Player) e.getEntity().getShooter();
                Player player = (Player) e.getEntity();

                if(shooter instanceof Player) {
                    if(!player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Teleportation " + ChatColor.AQUA + "Bow") || (player.getInventory() == null)) {
                        return;
                    }else {
                        Location location = e.getEntity().getLocation();

                        shooter.teleport(location);
                        shooter.sendMessage(ChatColor.GREEN + "Woosh!");
                    }
                }

            }

        }
    }

    The error is I believe the same, but if you are wondering:

    Code (Text):
    Could not pass event ProjectileHitEvent to Gadgets v1.0-SNAPSHOT
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callProjectileHitEvent(CraftEventFactory.java:745) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.EntityArrow.s_(EntityArrow.java:225) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.World.entityJoinedWorld(World.java:1604) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.World.g(World.java:1579) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.World.tickEntities(World.java:1422) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.WorldServer.tickEntities(WorldServer.java:576) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:772) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.entity.CraftArrow cannot be cast to org.bukkit.entity.Player
            at me.duck.gadgets.Events.onBowShoot(Events.java:17) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-c3c767f-33d5de3]
            ... 14 more
     
  5. You have to add the check before casting it (Player shooter = (Player) e.getEntity().getShooter())

    Also remove the Player player = (Player) e.getEntity() because e.getEntity() is an arrow not a player and you want to do the actions for the shooter
     
  6. Ah okay

    Sorry if I am wrong but it still sent the error, here is my code:

    Code (Java):
    package me.duck.gadgets;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;

    public class Events implements Listener {

        @EventHandler
        public void onBowShoot(ProjectileHitEvent e) {
            Player shooter = (Player) e.getEntity().getShooter();
            if(shooter instanceof Player) {

                if(!shooter.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Teleportation " + ChatColor.AQUA + "Bow") || (shooter.getInventory() == null)) {
                    return;
                }else {
                    Location location = e.getEntity().getLocation();

                    shooter.teleport(location);
                    shooter.sendMessage(ChatColor.GREEN + "Woosh!");
                }
            }


        }
    }
     
    #6 DuckKing, Mar 14, 2020
    Last edited: Mar 14, 2020
  7. You can't cast the entity to player before checking the entity is an instance of a player, think logically about what is happening and you'll be able to see why the error is thrown.

    Check if e.getEntity() is an instanceof Player before you cast any type of Player.
     
  8. Put the Player shooter thing after the check (one line below). And don’t worry, we’ve all been there. You’ll be able to figure out these kind of problems by yourself soon!
     
  9. Okay, so I don't get any errors but now it does not teleport me.

    Edit: Thanks for the quote by the way! :D (Makes me feel a lot better)
     
    • Friendly Friendly x 1
  10. Strahan

    Benefactor

    The logic you are using is like step 1. open umbrella step 2. check if it's raining lol

    Post your current code.
     
    • Funny Funny x 1
    • Friendly Friendly x 1
  11. Lol, here is my code:

    Code (Java):
    package me.duck.gadgets;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;

    public class Events implements Listener {

        @EventHandler
        public void onBowShoot(ProjectileHitEvent e) {
            if(e.getEntity() instanceof Player) {
                Player shooter = (Player) e.getEntity().getShooter();

                    if (!shooter.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Teleportation " + ChatColor.AQUA + "Bow") || (shooter.getInventory() == null)) {
                        return;
                    } else {
                        if(!shooter.getInventory().getItemInMainHand().getItemMeta().getLore().equals(ChatColor.AQUA + "Teleports you where the bow has been shot!")) {
                            return;
                        }else {
                            Location location = e.getEntity().getLocation();

                            shooter.teleport(location);
                            shooter.sendMessage(ChatColor.GREEN + "Woosh!");
                        }
                    }

            }
        }
    }
     
  12. Does it tell you the “woosh”?
    If not, the bow’s name might be wron
    You don’t need the lore check because players can’t get colored items by themselves anyways. Also, add a debug message after the check to see if the code knows the shooter is holding the bow (System.out.println(something))
     
  13. Hm, it does not send "Woosh" and the name seems to be right, I added to check the lore because it was not working, so I added it to see if it works or not.
     
  14. Remove the lore check. It does not say woosh because the name isn’t right probably. Just add the debug message and see if it is
     
  15. Okay i'll see.
     
  16. It seems to be this part: "if(e.getEntity() instanceof Player)" is wrong, (Yes it has { and })
     
  17. Because it’s the e.getEntity().getShooter() that’s the player. e.getEntity() is an arrow
     
  18. Oh okay, so do I change it to e.getEntity().getShooter() ?
     
  19. Once more, e.getEntity() returns an arrow entity, it will indeed not be player instance x), use e.getEntity().getShooter() instead
     
  20. Okay so I did if(e.getEntity().getShooter() instanceof Player) {

    }

    And it sends this error:

    upload_2020-3-15_2-52-37.png
     
Thread Status:
Not open for further replies.