Solved Collecting amount of hit from specific mobs.

Discussion in 'Plugin Development' started by Jetsinsu, Dec 19, 2018.

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

    Jetsinsu

    Hey guys! I haven't been on here for a while :p. I need some help figuring out how I would separately get each hit from a specific mob (Different Wither Skeletons). I tried using UUID on them but it doesn't really seem to work. Every time I get hit by the first skeleton twice and get hit by the others only once, the first skeleton keeps repeating the msg 2..2..2 and never moves on to three.

    Code:
    HashMap<UUID, Integer> hit = new HashMap<UUID, Integer>();
      
        @EventHandler
        public void onSkeletonAttack(EntityDamageByEntityEvent e) {
            if (e.getEntity() instanceof Player) {
                if (e.getDamager() instanceof Skeleton) {
                    Skeleton s = (Skeleton) e.getDamager();
                    if (s.getType() == EntityType.WITHER_SKELETON) {
                        if(s.getName().equalsIgnoreCase("Wither Skeleton V2")) {
                            if (!hit.containsKey(s.getUniqueId())) {
                                hit.put(s.getUniqueId(), 1);
                                Bukkit.getServer().broadcastMessage("1");
                                return;
                            }
                            if (hit.containsKey(s.getUniqueId()) && hit.containsValue(1)) {
                                hit.put(s.getUniqueId(), 2);
                                Bukkit.getServer().broadcastMessage("2");
                                return;
                            }
                            if (hit.containsKey(s.getUniqueId()) && hit.containsValue(2)) {
                                hit.put(s.getUniqueId(), 3);
                                Bukkit.getServer().broadcastMessage("3");
                                return;
                            }
                            if (hit.containsKey(s.getUniqueId()) && hit.containsValue(3)) {
                                if (s.getEquipment().getItemInMainHand().getType() == Material.STONE_SWORD) {
                                    //do something
                                    hit.remove(s.getUniqueId(), 3);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
    Code is a lil sloopy, ehh. :p
     
  2. First of all, no need to constantly check if the Map contains the Skeleton as a key, one check would suffice. The individual check of each number of hits is also unnecessary.

    Checking if the entity or damager is of a certain type by checking if it's an instance of something is rather uncommon. I'd say it's best to compare EntityTypes.

    I've quickly created a small snipped of code that might fix things. I'm using the default instance of it the Entity, instead of instantly declaring it as a Skeleton, in an attempt to see if that would fix things. Tell me if it works :)
    Code:
    @EventHandler
    public void onSkeletonAttack(EntityDamageByEntityEvent e) {
       if (e.getEntity().getType() != EntityType.PLAYER) {return;}
       if (e.getDamager().getType() != EntityType.WITHER_SKELETON) {return;}
    
       Entity damager = e.getDamager();
    
       if(damager.getName().equalsIgnoreCase("Wither Skeleton V2")) {
         if(!hit.containsKey(damager.getUniqueId())) {
           hit.put(damager.getUniqueId(), 1);
           Bukkit.getServer().broadcastMessage("1");
           return;
         }
         else if (hit.get(damager.getUniqueId()) == 3) {
           if (((Skeleton)damager).getEquipment().getItemInMainHand().getType() == Material.STONE_SWORD) {
             // Do something
             hit.remove(damager.getUniqueId());
             return;
           }
         } else {
           hit.put(damager.getUniqueId(), hit.get(damager.getUniqueId())+1);
           Bukkit.getServer().broadcastMessage(String.valueOf(hit.get(damager.getUniqueId())));
           return;
         }
       }
    }
    
    EDIT: Corrected a few things in the code, sorry about that.

    EDIT 2: I've spotted what you were doing wrong: you were checking if the Map contained the value of a certain Integer in general. What you should be doing is getting the Integer directly linked with the UUID instance in the HashMap, with
    Code:
    hit.get(damager.getUniqueID())
    This will return the amount of hits that were made by that specific Wither Skeleton.
     
    Last edited: Dec 19, 2018
  3. Offline

    Jetsinsu

    LMAO, right before I refreshed and saw your edited post, I was wonder if I used the HashMap wrongly, and I did! I haven't used hasmaps in a while so I got confused. It works now.

    I also have a question. Does the declaring the entity as a skeleton affect the way that the UUID was used or no and how? I actually never tried UUID for entities in a hashmap before so.... :p
     
  4. I'm not sure if it affects the UUID. Most likely not, since it's a special identifier for that specific Entity, regardless of it's type, but I just used Entity instead of Skeleton to rule out the possibility of that being the cause.

    Happy everything's working now! :D
     
  5. Offline

    Jetsinsu

    Ahhh, ok. That makes sense. Thanks for the help and useful information about HashMap! :)
     
    Kevinzuman22 likes this.
Thread Status:
Not open for further replies.

Share This Page