Solved Get Shooter and Damaged player when hit with an arrow.

Discussion in 'Plugin Development' started by Mycrowut, Aug 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Mycrowut

    Hello all, I found code from someone else on the forums that says how to get the Person who shot an arrow. Everything works perfectly fine while shooting players but then once a non-player entity is shot, it throws a huge error.

    The main error is:
    PHP:
    Caused byjava.lang.ClassCastExceptionorg.bukkit.craftbukkit.v1_6_R2.entity.CraftZombie cannot be cast to org.bukkit.entity.Player
    Here is my current code (I put it inside of its own thread just to make the error smaller? xD)
    And also, If I am checking the instances and it is giving me the above error, how can I stop / check if they are actually a player?

    Code:java
    1. else if(event.getDamager() instanceof Arrow){
    2. final Arrow arrow = (Arrow) event.getDamager();
    3.  
    4. if(arrow.getShooter() instanceof Player){
    5. new Thread(new Runnable(){
    6.  
    7. @Override
    8. public void run(){
    9.  
    10. Player player = (Player) event.getEntity();
    11.  
    12. Player damager = (Player) arrow.getShooter();
    13. Bukkit.broadcastMessage("" + player.getName() + " shot by " + damager.getName());
    14.  
    15.  
    16. }
    17. }).start();
    18. }
    19. }


    But like I said everything works great but an error gets thrown when shooting a sheep or pig ... ...

    Here is the error that it is giving me whilst shooting a sheep/pig/other non-player entity:
    PHP:
    2013-08-02 01:06:45 [SEVERECould not pass event EntityDamageByEntityEvent to BlockieUtility v2.8
    org
    .bukkit.event.EventException
        at org
    .bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        
    at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callEvent(CraftEventFactory.java:95)
        
    at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:383)
        
    at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:408)
        
    at net.minecraft.server.v1_6_R2.EntityLiving.damageEntity(EntityLiving.java:614)
        
    at net.minecraft.server.v1_6_R2.EntityMonster.damageEntity(EntityMonster.java:39)
        
    at net.minecraft.server.v1_6_R2.EntityZombie.damageEntity(EntityZombie.java:127)
        
    at net.minecraft.server.v1_6_R2.EntityArrow.l_(EntityArrow.java:229)
        
    at net.minecraft.server.v1_6_R2.World.entityJoinedWorld(World.java:1354)
        
    at net.minecraft.server.v1_6_R2.World.playerJoinedWorld(World.java:1335)
        
    at net.minecraft.server.v1_6_R2.World.tickEntities(World.java:1223)
        
    at net.minecraft.server.v1_6_R2.WorldServer.tickEntities(WorldServer.java:480)
        
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:572)
        
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused byjava.lang.ClassCastExceptionorg.bukkit.craftbukkit.v1_6_R2.entity.CraftZombie cannot be cast to org.bukkit.entity.Player
        at net
    .blockie.blockieutility.BlockieUtility.onEntityDamageByEntityEvent(BlockieUtility.java:1150)
        
    at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
        
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        
    at java.lang.reflect.Method.invoke(Unknown Source)
        
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 
    19 more
    Thank you
    -Mycro
     
  2. Hopefully won't get ninjad' but anyhoo this line is you problem.
    Code:
    Player player = (Player) event.getEntity();
    You are trying to make an entity into player without ever checking if event.getEntity() is a player.
    So just put an if-statements in there making sure that is player and you should be good. =)
     
    Mycrowut likes this.
  3. Offline

    ampayne2

    Mycrowut You check that the damager is an arrow, and that its shooter is a player, but you never check if the entity being shot is a player :p

    Edit:
    Ninja :p
     
    Mycrowut likes this.
  4. Offline

    Mycrowut

    ampayne2 likes this.
Thread Status:
Not open for further replies.

Share This Page