Solved Get The Last Damager on PlayerDeathEvent

Discussion in 'Plugin Development' started by jonacroco, Nov 30, 2015.

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

    Scimiguy

    Are you still storing players?

    Pretty sure you want .getDamager()
     
  2. Offline

    jonacroco

    I have this, don't worry :3
    Code:
    public HashMap<UUID, Player> lastdamager = new HashMap<UUID, Player>();
    In the PlayerDeathEvent ther is not getDamager() function :/ There is also getkiller() but when the killer is null, this function doesn't return to the last damager :/

    Thanks
     
  3. Offline

    Scimiguy

    Youre still storing players
    Stop storing them

    And the get damager is in entitydamagebyentityevent
     
  4. Offline

    Mrs. bwfctower

    It's fine to store players if they are removed properly. Or if WeakReferences are used.
     
  5. Offline

    Scimiguy

    @Mrs. bwfctower
    I generally assume on here that they're not removed properly.
    So instead of going through all that, it's easier to tell them to just not store them unless they absolutely have to, and use names/uuids
     
  6. Offline

    teej107

    Not that hard to tell them to use a WeakReference or a WeakHashMap (for Player keys) either.

    EDIT: or "Hey! Remove those players from the Map/Collection in the PlayerQuitEvent!"
     
    Mrs. bwfctower likes this.
  7. Offline

    jonacroco

    @Scimiguy
    Oh ok ! SO i have to take UUID and what in the HashMap ?

    @teej107
    HashMaps are so hard :eek:

    Thanks
     
  8. Offline

    Scimiguy

    Probably the UUID of the player, and the EntityLiving that last attacked the player
     
  9. Offline

    jonacroco

    @Scimiguy
    Okey so i have the HashMap with UUID of the payer who was hitted and the EntityLiving which is the last Entity to attack the Player.
    Then, in my EntityDamagedByEntityEvent, I get that :
    Code:
    Player damaged = (Player) e.getEntity();
    
            Player damager = (Player) e.getDamager();
           
            lastdamager.put(damager.getUniqueId(), damaged);
    
    And in the PlayerDeathEvent i have this :
    Code:
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
           
            e.setDeathMessage("");
    
            Player player = e.getEntity();
    
           
            if(player.getKiller() == null) {
               
                Bukkit.broadcastMessage("killer == null");
               
                Player killer = Bukkit.getServer().getPlayer(lastdamager.get(player.getUniqueId()));
                killer = player.getKiller();
               
                if(BumbRush.RedTeam.contains(killer)) {
                    Bukkit.broadcastMessage("4");
                    int i = 0;
                    ScoreBoardLobby.bluekills.add(i);
                }
    
                if(BumbRush.BlueTeam.contains(killer)) {
                    Bukkit.broadcastMessage("5");
                    int i = 0;
                    ScoreBoardLobby.redkills.add(i++);
                }
               
                player.sendMessage("§aVous §6avec été tué par §c" + killer.getName());
                killer.sendMessage("§aVous §6avez tué §c" + player.getName());
               
            } else if(player.getKiller() != null && player.getKiller() instanceof Player) {
               
                Bukkit.broadcastMessage("killer != null");
               
                Player killer = player.getKiller();
                Bukkit.broadcastMessage("killer == " + killer.getName());
               
                if(BumbRush.RedTeam.contains(killer)) {
                    Bukkit.broadcastMessage("4");
                    int i = 0;
                    ScoreBoardLobby.bluekills.add(i);
                }
    
                if(BumbRush.BlueTeam.contains(killer)) {
                    Bukkit.broadcastMessage("5");
                    int i = 0;
                    ScoreBoardLobby.redkills.add(i++);
                }
               
                player.sendMessage("§aVous §6avec été tué par §c" + killer.getName());
                killer.sendMessage("§aVous §6avez tué §c" + player.getName());
               
            }
    
    But the line where I define who is the killer returns me an error again.. :/
     
  10. Offline

    Zombie_Striker

    @jonacroco
    I will tell you two things: 1. If you do not know how to do something, google it. That will be a great help. 2. Really think this one through...
    By getting the damager, you can get the player who has died. But wait, that is the exact reverse of what you want. You want to by giving the damaged player, get the the last damager.
     
  11. Offline

    jonacroco

    First, thanks all for your help ! We have finally found the solution ! :happy:
    I have this code, if others are intrested !
    Code:
    public static HashMap<UUID, String> lastdamager = new HashMap<UUID, String>();
    
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
           
            e.setDeathMessage("");
    
            Player player = (Player) e.getEntity().getLastDamageCause().getEntity();
    
            if(player.getKiller() == null && !lastdamager.isEmpty()) {
               
                Player killer = Bukkit.getServer().getPlayer(lastdamager.get(player.getUniqueId()));
                lastdamager.remove(player.getUniqueId(), killer.getName());           
               
                if(BumbRush.RedTeam.contains(killer)) {
                    int i = 0;
                    ScoreBoardLobby.redkills.add(i++);
                }
    
                if(BumbRush.BlueTeam.contains(killer)) {
                    int i = 0;
                    ScoreBoardLobby.bluekills.add(i++);
                }
               
                player.sendMessage("§aVous §6avez été tué par §c" + killer.getName());
                killer.sendMessage("§aVous §6avez tué §c" + player.getName());
               
            } else if(player.getKiller() != null && player.getKiller() instanceof Player) {
    
                Player killer = player.getKiller();
                lastdamager.remove(player.getUniqueId(), killer.getName());
    
                if(BumbRush.RedTeam.contains(killer)) {
                    int i = 0;
                    ScoreBoardLobby.redkills.add(i++);
                }
    
                if(BumbRush.BlueTeam.contains(killer)) {
                    int i = 0;
                    ScoreBoardLobby.bluekills.add(i++);
                }
    
                player.sendMessage("§aVous §6avez été tué par §c" + killer.getName());
                killer.sendMessage("§aVous §6avez tué §c" + player.getName());
               
            } else {
                return;
            }
           
    
            ScoreBoardLobby.scoreBoardLoad();
            BonusPoints.CheckBonus();
        }
    
    }
    
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
                   
                Player damaged = (Player) e.getEntity();
    
                Player damager = (Player) e.getDamager();
                   
                PlayerDeath.lastdamager.put(damaged.getUniqueId(), damager.getName());
    }
    
    
    Thanks all :)
    Solved !!
     
Thread Status:
Not open for further replies.

Share This Page