When Player Get Hit By Snowball Error On EntityDamageByEntityEvent

Discussion in 'Spigot Plugin Development' started by toxiccoke, May 19, 2015.

  1. Hi
    I am getting a error on my entitydamagebyentityevent i have tried many things but i cannot get it to work
    This is my code
    Code (Text):
           @EventHandler
            public void onEntityDamage(EntityDamageByEntityEvent e) {
                if (e.getDamager() instanceof Snowball) {
                    Snowball s = (Snowball) e.getDamager();
                    if (s.getShooter() instanceof Player) {
                        Player shooter = (Player) s.getShooter();
                        if (shooter.getItemInHand().getType()== Material.WOOD_HOE) {
                            if (shooter.getItemInHand().getItemMeta().getDisplayName().equals("§6Pistol")){
                                e.setDamage(7.0);
                            }
                        }
                    }
                }
           }
    }
    This is my stacktrace

    Code (Text):
    [22:34:35 ERROR]: Could not pass event EntityDamageByEntityEvent to Paintball v1
    .0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302) ~[spigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:514) [spigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:499) [spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callEvent(Craf
    tEventFactory.java:108) [spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callEntityDama
    geEvent(CraftEventFactory.java:554) [spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.handleEntityDa
    mageEvent(CraftEventFactory.java:467) [spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.handleLivingEn
    tityDamageEvent(CraftEventFactory.java:586) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityLiving.d(EntityLiving.java:1040) [
    spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityHuman.d(EntityHuman.java:824) [spi
    got.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityLiving.damageEntity(EntityLiving.j
    ava:684) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityHuman.damageEntity(EntityHuman.jav
    a:765) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityPlayer.damageEntity(EntityPlayer.j
    ava:465) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntitySnowball.a(SourceFile:30) [spigot.
    jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityProjectile.h(EntityProjectile.java
    :155) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.World.entityJoinedWorld(World.java:1620)
    [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.World.playerJoinedWorld(World.java:1595)
    [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.World.tickEntities(World.java:1460) [spi
    got.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.WorldServer.tickEntities(WorldServer.jav
    a:516) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    06) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1649]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.entity.C
    raftSnowball cannot be cast to org.bukkit.entity.Player
            at me.toxiccoke.paintball.Events.EntityDamageByEntity.playerHitPlayerEve
    nt(EntityDamageByEntity.java:16) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _76]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _76]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_76]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_76]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:298) ~[spigot.jar:git-Spigot-1649]
            ... 23 more
    I cannot work out what is wrong
    Thanks toxiccoke
     
  2. Code (Text):
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.entity.CraftSnowball cannot be cast to org.bukkit.entity.Player
    Code (Text):
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.entity.CraftSnowball cannot be cast to org.bukkit.entity.Player
    at me.toxiccoke.paintball.Events.EntityDamageByEntity.playerHitPlayerEvent(EntityDamageByEntity.java:16) ~[?:?]
    It really helps to know how to read stacktraces.
     
    • Useful Useful x 1
  3. Error
    Code (Text):
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.entity.C
    raftSnowball cannot be cast to org.bukkit.entity.Player
            at me.toxiccoke.paintball.Events.EntityDamageByEntity.playerHitPlayerEve
    nt(EntityDamageByEntity.java:16) ~[?:?]
    Look class: EntityDamageByEntity.java line 16
    Look this post for read errors http://bukkit.org/threads/how-to-re...ubleshoot-your-own-plugins-by-yourself.32457/
     
    • Optimistic Optimistic x 1
  4. @ToPoEdiTs i have tired fixing the error on line 16 but im having no luck do you mind helping me out
     
  5. Can you post all of your class please? There is something odd going on, because it says you are trying to cast a Snowball Entity to a Player Entity. Which you can't do. From the snippet you gave us, it's hard to figure out where it is coming to the conclusion.

    What is odd, is have almost the exact instance checking code and have no problems.
     
  6. In lay-mans terms its saying that you cant cast it as a (SnowBall). Try to cast it as a (Projectile). When I get home ill try to help more
     

  7. Thats not what it is saying. It is saying that a snowball can not be cast to a Player. At some point he is trying to cast a snowball to a player

    (Player) iAmASnowball <--- Snowball

    Now in the snippet given... the only place he casts to a Player is the '(Player) shooter = s.getShooter;' line. However he does an instance check before that, so it shouldn't even get that far. Again, I feel like there is something out of context we are missing.

    Since he is using a custom item to 'launch' the snowball, I am wondering if the projectile source (shooter) is even a player or not. We wouldn't know unless we saw that portion.
     
  8. @ismooch This is the only other class that involves that event

    Code (Text):
    package me.toxiccoke.paintball.Guns;

    import java.util.ArrayList;

    import me.toxiccoke.paintball.Main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;

    public class Pistol implements Listener{
       
         static ArrayList<String> cooldown = new ArrayList<String>();
       
        static Main plugin;
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e){
            final Player p = e.getPlayer();
            ItemMeta im = p.getItemInHand().getItemMeta();
            if(im.getDisplayName().equals("§6Pistol")){
                    p.launchProjectile(Snowball.class);
            }
        }
        }
     
  9. Add some debug messages, check what the shooter is before trying to cast it (not just with a conditional.. print it out so you can see)

    When using the launchProjectile() method, does it set the shooter to the player?
     
  10. Im sorry this may sound stupid but how would i print again i havent debuged like that since i stoped coding a year ago
     

  11. just send it to the console:

    System.out.println("message string");
     
  12. wait am i spose to change the message string if so what
     
  13. Change it to whatever is recognizable to you when you read the console. I'd suggest using getLogger().info("We have gotten to <insert method name here> method"); . It lets you know at approximately what point in your code something went wrong. Paste a few of them in before, during, and after the parts with the error and see exactly what line your program reaches before you get the error.
    For example:
    Code (Text):
    public class Pistol implements Listener{
     
         static ArrayList<String> cooldown = new ArrayList<String>();
     
        static Main plugin;
     
        @EventHandler
        public void onInteract(PlayerInteractEvent e){
            getLogger().info("onInteract Action listener has been called");
            final Player p = e.getPlayer();
            ItemMeta im = p.getItemInHand().getItemMeta();
            if(im.getDisplayName().equals("§6Pistol")){
            getLogger().info("We are launching a projectile");
                    p.launchProjectile(Snowball.class);
            }
        }
        }
    Make sure to remove these once you have solved your problem so that it doesn't spam on the console when players use your plugin.