Solved Entity Death

Discussion in 'Plugin Development' started by kangkyuchang, Jan 13, 2020.

  1. Offline

    KarimAKL

  2. Offline

    kangkyuchang

    @KarimAKL Oh, Thank you. You are kind. Is my code correct?
     
  3. Offline

    KarimAKL

    @kangkyuchang No, read my post again.
    1. You're still using lists insteads of sets.
    2. You still have "PU".
    3. You went back to using Map#keySet().add() instead of using Map#put()
     
  4. Offline

    Strahan

    I noticed in your code you have List<UUID> set. You do realize when he tells you to use list instead of set, he doesn't mean the variable name right? What he's talking about is a totally different type of collection. Also you shouldn't really name your variables the same as classes, though granted you did at least use lowercase. It's best to name a variable related to what it is for. So in your case, that list is a list of people who hit a skeleton horse so maybe like "skelHorseAttackers" or the like.
     
  5. Offline

    kangkyuchang

    Code:
    public Map<UUID,Set<UUID>> damageplayer = new HashMap<UUID,Set<UUID>>();
        Set<UUID> Attackers = Set();
        private Set<UUID> Set() {
            return Attackers;
        }
           
        @EventHandler
        public void Damage(EntityDamageByEntityEvent e) {
            if(!e.getEntityType().equals(EntityType.SKELETON_HORSE)) return;
            Attackers.add(e.getEntity().getUniqueId());
            if (damageplayer.containsKey(e.getEntity().getUniqueId())) {
                damageplayer.get(e.getEntity().getUniqueId());
                damageplayer.put(e.getEntity().getUniqueId(), Attackers);
            }
            damageplayer.put(e.getEntity().getUniqueId(), Attackers);
        }
    I did it my own way but I still don't know if it is correct.
     
  6. Offline

    Strahan

    Well... you're kinda close, but at the same time waaaaay off. Like what is all that about in the beginning before the event handler?? Do not make methods named from existing classes. Use a name that is descriptive of its function. Secondly, you are setting Attackers equal to the return of the Set() function. All Set() does is return Attackers. See the problem there? It makes no sense at all. There is no need to create a set outside the scope of the event anyway.

    Now in your event, you are your logic goes pretty decent except for a few problems. (collection).get returns a value, yet you are doing absolutely nothing with it. You'd want to set Attackers equal to the return so you keep the people who are already in there. Then you add the person who just attacked. So your logic should be like:

    Code:
    event {
      if not horse return
      create set named attackers
      if (damageplayer contains the entity) attackers = get the set from damageplayer
      add attacker to attackers
      put attackers back into the set
    }
    That example also moves the set from being a member variable to being a local variable, because there is no need for attackers to be in the class scope
     
  7. Offline

    kangkyuchang

    Don't do this,
    there be a red line here.
     
  8. Online

    timtower Moderator Moderator

  9. Offline

    kangkyuchang

  10. Online

    timtower Moderator Moderator

    Then hover your mouse over it.
    It will tell you why there is a red line.
     
  11. Offline

    kangkyuchang

    new Set(); -> It's called Cannot instantiate the type Set.
    new Set<UUID>(); ->It's called Cannot instantiate the type Set<UUID>.
     
  12. Online

    timtower Moderator Moderator

  13. Offline

    kangkyuchang

    Code:
    public Map<UUID,Set<UUID>> damageplayer = new HashMap<UUID,Set<UUID>>();
           
        @EventHandler
        public void Damage(EntityDamageByEntityEvent e) {
            Set<UUID> Attackers = new HashSet<UUID>();
            if(!e.getEntityType().equals(EntityType.SKELETON_HORSE)) return;
            Attackers.add(e.getDamager().getUniqueId());
            if (damageplayer.containsKey(e.getEntity().getUniqueId())) {
                damageplayer.get(e.getEntity().getUniqueId());
                Attackers.add(e.getDamager().getUniqueId());
                damageplayer.put(e.getEntity().getUniqueId(), Attackers);
            }
            damageplayer.put(e.getEntity().getUniqueId(), Attackers);
        }
    Is it perfect?
     
  14. Online

    timtower Moderator Moderator

  15. Offline

    Sw_aG

    @kangkyuchang Why not just use the EntityDeathEvent and check if their last hitter is a player ?

    Edit: the get killer variable actually returns you a Human Entity, just use
    Code:
    e.getEntity().getKiller();
    Then cast it to player if you want..
    And then give him the item (If needed, check what the entity type was, if its like a mob arena of somethin')
     
    Last edited: Jan 26, 2020
  16. Offline

    KarimAKL

    Did you read the first post in this thread?

    Also:
    You don't need to cast a Player object to a Player.
     
  17. Offline

    Sw_aG

    It returns a Human Entity, not a Player.
     
  18. Offline

    KarimAKL

  19. Offline

    kangkyuchang

    Code:
    @EventHandler
        public void Death(EntityDeathEvent e)
        {
            if(e.getEntityType() == EntityType.SKELETON_HORSE)
            {
                if(damageplayer.containsKey(e.getEntity().getUniqueId()))
                {
                    e.getEntity().getKiller().sendMessage("TEST");
                }
              
            }
        }
    @KarimAKL
    I tried this, but the item only give to the player who killed the entity last.
    I would appreciate it if you could tell me how.
     
  20. Offline

    Strahan

    Nope. You call damageplayer.get after checking if the damageplayer contains the entity ID, but you do nothing with the return value so Attackers remains with just the current attacker, it doesn't include any others. So you'd be wiping out prior attackers every time someone else hits the horse.

    Also you do not need to do damageplayer.put twice. In the logic statement, set the Attackers from the map then add the current then leave the put outside the logic because either way, you need to put.

    That will fix it so you have valid tracking on who all attacked the horse. Bear in mind, this does nothing to track the last attacker when the entity dies. Though as I said prior, I'd track who contributed the most rather than just who put in the last hit.

    Well, yea, that's what the code is set to do. It will only send the message to the person who killed the entity, assuming said entity is being tracked in the map. You have to use the map's value to pull everyone who had attacked, not just rely on getKiller.
     
  21. Offline

    kangkyuchang

    Code:
    public HashMap<UUID,Set<UUID>> damageplayer = new HashMap<UUID,Set<UUID>>();
           
        @EventHandler
        public void Damage(EntityDamageByEntityEvent e) {
            Set<UUID> Attackers = new HashSet<UUID>();
            if(!e.getEntityType().equals(EntityType.SKELETON_HORSE)) return;
            Attackers.add(e.getDamager().getUniqueId());
            if (damageplayer.containsKey(e.getEntity().getUniqueId())) {
                damageplayer.get(e.getEntity().getUniqueId());
                if(!Attackers.contains(e.getDamager().getUniqueId()))
                {
                    Attackers.add(e.getDamager().getUniqueId());
                }
            }
            damageplayer.put(e.getEntity().getUniqueId(), Attackers);
        }
        @EventHandler
        public void Death(EntityDeathEvent e)
        {
            if(e.getEntityType() == EntityType.SKELETON_HORSE)
            {
                if(damageplayer.containsKey(e.getEntity().getUniqueId()))
                {
                }
               
            }
        }
    I do not know how to erase the prior attacker. And I don't know how to give an item with HashMap when Skeletal Horse dies.
     
    Last edited: Feb 12, 2020

Share This Page