Solved ProjectileSource instanceof Player

Discussion in 'Plugin Development' started by JeremyPark, May 30, 2014.

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

    JeremyPark

    Hi, I am trying to get a player from a projectile source, and it isn't working. I have the if statement "if(shooter instanceof Player)" and it isn't getting run when the player shoots something, and damages an entity. Can someone tell me what I am doing wrong?

    Here's my code.

    Code:java
    1. Player player = null;
    2.  
    3. if (event.getDamager() instanceof Arrow){
    4. Arrow arrow = (Arrow) event.getDamager();
    5. ProjectileSource shooter = arrow.getShooter();
    6. if(shooter instanceof Player){
    7. player = (Player) shooter;
    8. }
     
  2. Offline

    JBoss925

    getShooter returns a living entity I believe so try:
    Code:java
    1. @EventHandler
    2. public void projectileThing(EntityDamageByEntityEvent event){
    3. if (event.getDamager() instanceof Arrow){
    4. Arrow arrow = (Arrow) event.getDamager();
    5. LivingEntity shooter = arrow.getShooter();
    6. if(shooter instanceof Player){
    7. Player player = (Player) shooter;
    8. }
    9. }
    10. }
     
  3. Offline

    JeremyPark

    The LivingEntity getShooter is deprecated, I am using the new getShooter, which returns ProjectileSource
     
  4. Offline

    renaud444

    Try this:
    Code:java
    1. @EventHandler
    2. public void projectileThing(EntityDamageByEntityEvent event){
    3. if (event.getDamager() instanceof Arrow){
    4. Arrow arrow = (Arrow) event.getDamager();
    5. if(event.getShooter() instanceof Player){
    6. (Player)ProjectileSource shooter = arrow.getShooter();
    7. }
    8. }
    9. }
     
  5. Offline

    Garris0n

    Do you have CraftBukkit or Bukkit in your build path?
     
  6. Offline

    JeremyPark

    I have Bukkit in the build path because I was having issues with entity.getHealth(), and a few other things. This seems to be the downside to using Bukkit
     
  7. Offline

    CoderRyan

    the getShooter method does not work in the Bukkit build path, You need to use craftbukkit
     
  8. Offline

    Aengo

    JeremyPark
    Remove the bukkit, instead of using bukkit, to get the health I always use
    Code:java
    1. ((Damageable) entity).getHealth();
     
  9. Offline

    Garris0n

    Stop spreading misinformation.

    Anyway, @OP, how far exactly is the code getting? Have you put in some debug messages to see where it fails, etc?
     
    AoH_Ruthless likes this.
  10. Offline

    JeremyPark


    Thanks Aengo for the idea of casting the entity to damageable to use getHealth(). It's a great solution.
    Garris0n I ended up removing bukkit from the build path, as it wasn't helping on getShooter(). Yes, I did put debug messages in to see where it failed. It wouldn't trigger the if (shooter instanceof Player) even if the arrow was fired from a player. The getShooter() that returns an entity worked fine for that if statement though.
     
  11. Offline

    Garris0n

    why? Unless you're using NMS, you should not have CraftBukkit. Are you using NMS?

    And, apparently, the shooter wasn't an instance of Player. Don't ask me why, I have no clue what the situational details were, but it wasn't a player.
     
  12. Offline

    JeremyPark

    I am using reflection in one of my classes in order to modify the movement speed of zombies.

    Well, if it wasn't a player, there was something quite wrong. The arrow was shot by myself at a zombie, so it was definitely a player that shot it, but it may read as perhaps a living entity, or something else. Either way, it wasn't reading that it was a Player
     
  13. Offline

    Garris0n

    Perhaps there's a bug in CraftBukkit. Either way, it should have nothing to do with whether you're using Bukkit or CraftBukkit in your build path. I generally either use Bukkit or both.
     
Thread Status:
Not open for further replies.

Share This Page