Cannot cast an arrow's shooter to a player?

Discussion in 'Plugin Development' started by AnOrangeGamer, Mar 3, 2014.

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

    AnOrangeGamer

    Hey all, I'm trying to make a plugin for where a player's health reaches to 0 by a bow, an event will occur. I've come across trying to check if the damager's instance was an arrow (this is under the EntityDamageByEntityEvent btw.) Then after that, I would cast the damager to an arrow and get the shooter from there, then casting the shooter to a player, yet it would not work, and it would give me an error.

    Here is my code:
    Code:java
    1. if(event.getDamager() instanceof Arrow) {
    2. Arrow arrow = (Arrow) event.getDamager();
    3. Player damager = (Player) arrow.getShooter();
    4. //do stuff to the shooter
    5. }


    Error:
    http://pastebin.com/B7DrCFNQ

    Line 207 is this:
    Code:java
    1. Player damager = (Player) arrow.getShooter();


    Thanks for the help
     
  2. Offline

    HungerCraftNL

    Try to use the ProjectileHitEvent and get the shooter with this:
    PHP:
    e.getEntity().getShooter();
     
  3. Offline

    calebbfmv

  4. Offline

    AnOrangeGamer

    I don't get what you're saying about "event.getDamager()"
    I'm trying to check if the damager in EntityDamageByEntityEvent is an arrow, if it is, get the arrow's shooter and do something to him (send a message, teleport him, etc)
    If I don't get the shooter of the arrow, and get the damager, then it'll try to send a message to the arrow because it's the damager, which then would throw an error.
     
  5. Offline

    Wolfey

    Code:java
    1.  
    2. Arrow arrow = (Arrow) e.getDamager();
    3. Player shooter = (Player) arrow.getShooter();
    4.  

    Be sure to check if the instance of getDamager() is an arrow and the instance of getShooter() is a player or you will get errors.
     
    calebbfmv likes this.
  6. Offline

    AnOrangeGamer

    I had that code to begin with and it threw an error, I also put that code in the thread as well.
     
  7. Offline

    d3v1n302418

    AnOrangeGamer
    Code:java
    1. if(arrow.getShooter() instanceof Player){
    2. Player shooter = (Player) arrow.getShooter();
    3. }
     
  8. Offline

    Wolfey

     
  9. Offline

    AnOrangeGamer

    Wolfey
    I did that and it threw the exact same error on the line where I checked if the shooter's instance was a player.

    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    nuno1212sss

    Use his method it should work AnOrangeGamer
     
  11. Offline

    AnOrangeGamer

    I was thinking about that at first, but I need to get the player that is getting hit by the arrow, then check if the player's health reaches 0, it does an event.
     
  12. Offline

    nuno1212sss

    Code:java
    1. @EventHandler
    2. public void onHit(EntityDamageByEntityEvent e) {
    3. if (!(e.getDamager() instanceof Arrow))
    4. return;
    5. if(!(e.getEntity() instanceof Player)) return;
    6. Arrow r = (Arrow) e.getDamager();
    7. Player p = (Player) e.getEntity();
    8. if(!(r.getShooter() instanceof Player)) return;
    9. Player shooter = (Player) r.getShooter();
    10. }
    AnOrangeGamer That doesn't work?
     
  13. Offline

    AnOrangeGamer

    I did something similar to that, but that I'm pretty sure would not work. The error I have is on the line where I cast the shooter to a player. If you need the error check the pastebin link I put in the thread.
     
  14. Offline

    nuno1212sss

  15. Offline

    AnOrangeGamer

  16. Offline

    AnOrangeGamer

    Still no solution... bump?
     
  17. Offline

    diage

    You said you did something similar to that example above? What exactly did you do? Just the code you provided?
     
  18. Offline

    AnOrangeGamer

    Yes. I initially had more to the code, like I would add a score to the shooter.
     
  19. Offline

    diage

    I mean, in the above code he did a couple of extra checks, you replied saying you did something basically similar. Often times I find similar does not imply correct. So I was just curious as to what you meant y similar.
     
  20. Offline

    AnOrangeGamer

    Yes, I did what he did. All he did was check the instances. I did the same thing then returned if it wasn't true. I did the same thing.
     
  21. Offline

    diage

    Ya, but checking the instances is extremely important.
     
  22. Offline

    unon1100

    There is a different thing that is used to get the shooter.
    Code:java
    1. ProjectileSource ps = ((ProjectileSource) e.getEntity().getShooter());

    Eclipse still says it is deprecated because if it returns a player it is deprecated, but if you set it equal to a projectilesource and cast it, it should work... haven't tested though. You can then check if(ps instanceof Player) and cast the ps to Player to get the player who shot it. Try that :)
     
  23. Offline

    AnOrangeGamer

    Oops, sorry, worded it wrong. Yes I did check the instances as well.
    unon1100
    Thank you, I will try that.
     
Thread Status:
Not open for further replies.

Share This Page