Solved Death Cause problems (2 problems)

Discussion in 'Plugin Development' started by Unica, Oct 1, 2014.

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

    Unica

    Hi,

    First problem
    I want to check if a player got killed by a Projectile, and since I fire a snowball, and use custom damage with that, I assume the player didn't directly got killed by a snowball.
    Code:java
    1. public static void doDamage(Projectile p, Player shot, Player shooter) {
    2. Random r = new Random(); //create new random
    3. if (p instanceof Snowball) {
    4. Snowball b = (Snowball) p;
    5. shot.damage(2); //Custom damage


    I need the shooter of the projectile too btw.
    I assumed you have to check for the last damage cause, but when I try
    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e){
    3. //Assume I already casted it (I did)
    4. if(player.getLastDamageCause().equals(DamageCause.PROJECTILE)){
    5. System.out.println("Debug");
    6. }
    7. }

    It doesn't work. Any ideas?

    Second problem
    I made grenades, which do work. However, when the damaged player(s) took enough damage to die, I need to get the thrower of the grenade.

    Code:java
    1. private void onExplosion(Item item, double damage, Player thrower,
    2. Sound sound, int radius, ParticleEffect... effect) {
    3. for (ParticleEffect eff : effect) {
    4. eff.display(item.getLocation(), 2, 1, 2, 0, 300);
    5. }
    6. for (Entity e : item.getNearbyEntities(radius, radius, radius)) {
    7. if (e instanceof Player) {
    8. Player hit = (Player) e;
    9. hit.playSound(hit.getLocation(), sound, 30, 1);
    10. if (hit.equals(thrower) || m.teams.sameTeam(thrower, hit))
    11. continue;
    12. hit.damage(damage);
    13. }
    14. }
    15. }


    ----------------------------------

    Note:
    I tried using
    Code:java
    1. public void checkDeath(Player killer, Player killed){
    2. if(killed.getHealth() == 0 || killed.getHealth() < 0){


    But that bugs quite alot (multiple times fires when a single player dies)
     
  2. Unica Why not just use setDamage() inside the EntityDamageByEntityEvent (which fires when you're hit by a snowball)?
     
  3. Offline

    Unica

    AdamQpzm
    Will that automatically know that the shooter = the killer in the PlayerDeathEvent ?
     
  4. Unica I don't have the resources to check at the moment, so you should probably test that. Although I would say that it would - based on the fact that, by default, being shot to death gives the player as the killer in vanilla death messages.
     
  5. Offline

    Unica

    AdamQpzm

    Thank you ;) It does actually work

    How would achieve a thing like this with a explosion? Since I am checking for nearby entities, and they did not get hit by something.
     
  6. Unica I don't understand the issue?
     
  7. Offline

    Unica

    AdamQpzm
    Let me enlighten you,

    The 'grenade' which I made is a dropped item which I change velocity of.
    Therefor no person get's damaged from the dropped item. I set custom damage for that,
    so in the entityDamageEvent I cannot use
    Code:java
    1. if(e.getDamager() instanceof myGrenade)


    Basically, I need to get the thrower + person who died by my explosion (could be more than one) in my
    PlayerDeathEvent, so I can broadcast a message etc.
     
  8. Unica is myGrenade an Entity?
     
  9. Offline

    fireblast709

    Unica When you invoke onExplosion, set set a field somewhere to your entity and set it to null afterwards. Then, if a player died and that field isn't null, they died because of the grenade (because the damage and death events should be triggered before the damage method returns).
     
  10. Offline

    Unica

  11. Offline

    fireblast709

    Unica a field in your listener class, for example.
     
  12. Offline

    Unica

    fireblast709

    Code:java
    1. private boolean grenade = false;
    2.  
    3. private void onExplosion(Item item, double damage, Player thrower,
    4. Sound sound, int radius, ParticleEffect... effect) {
    5. for (ParticleEffect eff :
    6. eff.display(item.getLocation(), 2, 1, 2, 0, 300);
    7. }
    8. grenade = true;
    9. for (Entity e : item.getNearbyEntities(radius, radius, radius)) {
    10. if (e instanceof Player) {
    11. Player hit = (Player) e;
    12. hit.playSound(hit.getLocation(), sound, 30, 1);
    13. if (hit.equals(thrower) || m.teams.sameTeam(thrower, hit))
    14. continue;
    15. hit.damage(damage);
    16. }
    17. }
    18. grenade = false;
    19. }


    Something like that?
    If yes, what if there were multiple grenades thrown at the same time (multiple players), how would I achieve such a thing then?
     
  13. Offline

    fireblast709

    Unica that's why I suggested an Entity (or a Player field since that would be the information you want) field instead of a boolean :p.
     
  14. Offline

    Unica

    fireblast709
    mm, That is indeed more logic.
    But if I would 'private Player inRadius = playerFromLoop; that would turn out in one single player inside that field,
    isn't it then smarter to use a Set?
     
  15. Offline

    fireblast709

    Unica no lol. Set the 'private Player thrower' to the thrower, since that's the Player that actually kills them. Then, when a PlayerDeathEvent is fired, check if 'private Player thrower' is null. If it's not, then it's almost certain that the player died by a grenade from that Player.

    Afterwards, set 'thrower' to null so the death events that follow (which aren't caused by the damage dealt in the explode method) won't be seen as grenade kills.
     
    Unica likes this.
  16. Offline

    Unica

    fireblast709

    I owe you alot ;)
    Thanks!

    For the ppl that are wondering how fireblast709 did this,

    Code:java
    1. private Player thrower = null;
    2.  
    3. public Player getThrower(){ //Method
    4. return this.thrower;
    5. }
    6.  
    7. public void explode(Player thrower){ //Earlier method when a grenade explodes
    8. this.thrower = thrower;
    9. for(Player nearby : bombThatGotThrown.getNearbyEntites(x, x, x)){
    10. nearby.damage(double);
    11. }
    12. thrower = null;
    13. }
    14.  
    15. //OnDeath
    16.  
    17. @EventHandler
    18. public void onDeath(PlayerDeathEvent e){
    19. if(getThrower != null){
    20. //Entity got killed by grenade
    21. }
    22. }
    23.  
     
Thread Status:
Not open for further replies.

Share This Page