Lightning strike get killer

Discussion in 'Plugin Development' started by TerroDoor, Dec 15, 2019.

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

    TerroDoor

    I have a cool kit called Thor and it lets you strike lightning with your axe upon right click! I’ve canceled the fire igniting blocks but now I’m having trouble with adding the kill to my config if the player dies to lightning

    Maybe a death event and checking the death cause? Thanks for any advice


    Sent from my iPhone using Tapatalk
     
  2. Offline

    DerDonut

    Both, World#strikeLightning() and World#strikeLightningEffect() return a LighningStrike object which you could save in a hashMap with the killer as a key. LightningStrike is also an entity, I would rename it to the name or uuid of the killer and check e.g. in EntityDamageByEntityEvent for that
     
    KarimAKL and TerroDoor like this.
  3. Offline

    TerroDoor

    I still can’t get this to work, I don’t want to put the player in a map I just want to listen for a entitydeathevent and if the cause of death is lightning, add the kill to the killers config


    Sent from my iPhone using Tapatalk
     
  4. Offline

    KarimAKL

    @TerroDoor You can check if the cause of death was lightning like this:
    Code:Java
    1. @EventHandler
    2. private void onDeath(PlayerDeathEvent event) {
    3. Player player = event.getEntity();
    4. DamageCause cause = player.getLastDamageCause().getCause();
    5. if (cause == DamageCause.LIGHTNING) {
    6. // The cause of death was lightning
    7. }
    8. }
     
  5. Offline

    TerroDoor

    I’ve been testing this but I must not be doing it right, I’ll post my code


    Sent from my iPhone using Tapatalk

    @KarimAKL

    Code:
    
     @EventHandler
     public void onKills(EntityDeathEvent e) {
    
     LivingEntity p = (LivingEntity)e.getEntity();
     Player k = (Player)e.getEntity().getKiller();
     
     int kills = pl.getConfig().getInt(k.getName() + ".kills");
     int streak = pl.getConfig().getInt(k.getName() + ".streak");
     int coins = pl.getConfig().getInt(k.getName() + ".coins");
    
     pl.getConfig().set(k.getName() + ".kills", kills + 1);
     pl.getConfig().set(k.getName() + ".streak", streak + 1);
     pl.getConfig().set(k.getName() + ".coins", coins + 5);
     pl.saveConfig();
    
     scoreboard.updateScoreboard(k);
     checkStreak(k); 
    
    //THE CODE ABOVE IS WORKING
    
     if (p.getLastDamageCause().getCause() == DamageCause.LIGHTNING) {
    
    //I need to get the shooter somehow? 
    //nothing updates
    
     pl.getConfig().set(k.getName() + ".kills", kills + 1);
     pl.getConfig().set(k.getName() + ".streak", streak + 1);
     pl.getConfig().set(k.getName() + ".coins", coins + 5);
     pl.saveConfig();
     scoreboard.updateScoreboard(k);
     checkStreak(k); 
     }
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 16, 2019
  6. Offline

    KarimAKL

    @TerroDoor Lightning doesn't count as a projectile so you can't get the "shooter", instead you'll have to save it yourself. I'd do something like this:
    Code:Java
    1. private Map<UUID, List<LightningStrike>> lightning = new HashMap<>();
    2.  
    3. @EventHandler
    4. private void onInteract(PlayerInteractEvent event) {
    5. // I'm guessing you're using this event to summon the lightning
    6. Player player = event.getPlayer();
    7. UUID uuid = player.getUniqueId();
    8. World world = ...;
    9. Location location = ...;
    10. List<LightningStrike> list = new ArrayList<>();
    11. List<LightningStrike> lightning = this.lightning.get(uuid);
    12. if (lightning != null) list.addAll(lightning);
    13. list.add(world.strikeLightning(location));
    14. this.lightning.put(uuid, list);
    15. }
    16.  
    17. @EventHandler
    18. private void onDeath(PlayerDeathEvent event) {
    19. Player player = event.getEntity();
    20. Player killer = player.getKiller();
    21. if (killer == null) {
    22. EntityDamageEvent dmgEvent = player.getLastDamageCause();
    23. if (dmgEvent.getCause() == DamageCause.LIGHTNING) {
    24. EntityDamageByEntityEvent dmgByEntityEvent = (EntityDamageByEntityEvent) dmgEvent;
    25. LightningStrike lightning = (LightningStrike) dmgByEntityEvent.getDamager();
    26. for (Entry<UUID, List<LightningStrike>> entry : new HashMap<>(this.lightning).entrySet()) {
    27. List<LightningStrike> strikes = new ArrayList<>(entry.getValue());
    28. for (LightningStrike strike : strikes) if (strike.getUniqueId().equals(lightning.getUniqueId())) {
    29. killer = Bukkit.getPlayer(entry.getKey());
    30. strikes.remove(strike);
    31. if (strikes.isEmpty()) this.lightning.remove(entry.getKey());
    32. else this.lightning.put(entry.getKey(), strikes);
    33. break;
    34. }
    35. if (killer != null) break;
    36. }
    37. }
    38. }
    39. // Use the killer here
    40. }
    41.  
    42. // This is just to remove lightning strikes that missed their target from the map
    43. @EventHandler
    44. private void onStrike(LightningStrikeEvent event) {
    45. for (Entry<UUID, List<LightningStrike>> entry : new HashMap<>(lightning).entrySet()) {
    46. List<LightningStrike> strikes = new ArrayList<>(entry.getValue());
    47. for (LightningStrike strike : strikes) if (strike.getUniqueId().equals(event.getLightning().getUniqueId())) {
    48. new BukkitRunnable() {
    49. @Override
    50. public void run() {
    51. strikes.remove(strike);
    52. if (strikes.isEmpty()) lightning.remove(entry.getKey());
    53. else lightning.put(entry.getKey(), strikes);
    54. }
    55. }.runTaskLater(plugin, 1);
    56. break;
    57. }
    58. }
    59. }

    Do note that i just wrote this up on the spot, i haven't tested it, neither have i thought about the performance.
     
  7. Offline

    DerDonut

    Whats wrong with that?
     
  8. Offline

    TerroDoor

    Thanks for the responses guys but I’m having big trouble with this

    So I need to put the player that gets struck into a list and if they die from lightning and Contain the list I award the killer the points? How do I get the killer of that specific plaher


    Sent from my iPhone using Tapatalk
     
  9. Offline

    KarimAKL

  10. Offline

    TerroDoor

    Maybe instead of a list I could use the lightningStrikEffect, and damage the player, although this wouldn’t make the killer the damager, how would I do that to ensure that the killer is the damager?


    Sent from my iPhone using Tapatalk

    Solved, thanks for point me In the right direction with this @KarimAKL @DerDonut

    On another thread I found you can attach the killer to the e.getEntity().damage() method!


    Sent from my iPhone using Tapatalk

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 16, 2019
  11. Offline

    KarimAKL

  12. Offline

    DerDonut

    Directly passing the killer to the damage function is probably the best thing to do
     
  13. Offline

    TerroDoor

    YeAh it’s working great, the damage from the lightningeffect is attached to the killer I believe


    Sent from my iPhone using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page