Solved Mob.setHealth() not setting the health of a mob correctly

Discussion in 'Spigot Plugin Development' started by TrexCrusher, Jul 31, 2020 at 9:24 PM.

  1. So I have a plugin where I want a mob of a certain class to have 200HP.

    Code (Java):
        public static void giveKits(Bounty bounty,PigZombie ent)
        {
         
            EntityEquipment ee = ent.getEquipment();
         
            if(bounty.getType().equals("drug"))
            {
         
             
                ee.setItemInMainHand(new ItemStack(Material.POTION));
             
                ent.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(200.0D);

             
                AttributeInstance speedAttribute = ent.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
                speedAttribute.setBaseValue(0.5);
             
                AttributeInstance damageAttribute = ent.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
                damageAttribute.setBaseValue(4);
             
    ent.setHealth(200);
             
            }
         
         
         
         
        }
    The code works because the mob gains the appropriate speed and items in hand. However the mob dies within like 5 punches of a players Hand leading me to believe that something is wrong. When I put a System.println for the health whenever the mob gets hit, in the console it shows that the health is decreasing from 200 to 199 to 198 and etc however the mob dies way before it even gets past like 195
     
    #1 TrexCrusher, Jul 31, 2020 at 9:24 PM
    Last edited: Jul 31, 2020 at 9:31 PM
  2. I cannot test it right now, but maybe you have to remove the: ent.sethealth(200);
    Because you are already set the Health of the Mob in the Attribute Methods.
     
  3. But how would that change anything. The health should be 200 either way
     
  4. Does it have to do with it being Static?
     
  5. No
     
  6. do you know why it is not working>
     
  7. Weirdly enough, i tried to isolate the code you wrote and it was working fine.
    [​IMG]

    And @Felixlol without .setHealth(), the mob had 20 HP so it seems important.

    Code (Java):

        @EventHandler
        public void onInteract(PlayerInteractEvent event) {

            if(event.getHand() == EquipmentSlot.OFF_HAND) return;

            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack item = event.getItem();
            if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
                if (item != null && item.getType() == Material.BOOK) {
                    PigZombie yeet = (PigZombie) player.getWorld().spawnEntity(player.getLocation(),EntityType.ZOMBIFIED_PIGLIN);
                    yeet.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(200.0D);
                    yeet.setHealth(yeet.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue());
                    yeet.setCustomName("TESTING");
                }

        }

        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e){
            if(!(e.getDamager() instanceof Player)) return;
            if(!(e.getEntity() instanceof PigZombie) && e.getEntity().getCustomName() != "TESTING") return;
            Player ply = (Player)e.getDamager();
            PigZombie pz = (PigZombie)e.getEntity();
            ply.sendMessage("" + pz.getHealth());
        }

    Btw, i do apologize for not reading your code before answering the first time. Just assumed from title.
     
  8. How many hits did it take you to kill it? It only takes me like 5 hits with the main hand
     
  9. Well as you can see, i've hit it 11 times with a diamond sword and it still had 124hp
     
  10. that's so weird, your code is almost the same as mine except its dying in like 5 hits. Like it counts down from like 200 to 199 to 198 and then it dies at like 190

    https://imgur.com/zgha8JS


    [​IMG]
     
  11. Have you tried your plugin in a "plugin-free" environment ? Maybe it is something else that causes it to die even though i doubt it.

    EDIT: The issue doesn't seems to be the 200hp so maybe check the events that proc when you attack the mob ?

    EDIT 2:

    I was in creative mode when i did the tests.

    Would it be possible to take a look at the events related to this issue ? (Entity damage and more)
     
    #13 NullExceptionArg, Aug 2, 2020 at 3:37 AM
    Last edited: Aug 2, 2020 at 3:44 AM
  12. I isolated the plugin and it did nothing lemme post the events


    Code (Java):
    @EventHandler
        public void Druglord(EntityDamageByEntityEvent event)
        {
       
           
            if(event.getEntityType() == EntityType.ZOMBIFIED_PIGLIN)
            {
               
               
                for(Bounty b: BountyManager.getBountyList())
                {
                   
                    if(event.getEntity().getEntityId() == b.getBountyId())
                    {
                       
                        LivingEntity ent = (LivingEntity) event.getEntity();
                        if(b.getType().equals("drug"))
                        {
                           
                            if(event.getDamager() instanceof Player)
                            {
                                Player p = (Player)event.getDamager();
                                p.sendMessage(""+ent.getHealth());
                               
                               
                            }
                       
                           
                            if(rollOdds(45))
                            {
                               
                               
                                for (double i = 0; i < 2 * Math.PI; i += 0.1) {
                                    double x = Math.cos(i) * 5;
                                    double y = Math.sin(i) * 5;

                                    ent.getWorld().spawnParticle(
                                            Particle.REDSTONE, ent.getLocation().getX() + x,
                                            ent.getLocation().getY(), ent.getLocation().getZ() + y, 1,
                                            new Particle.DustOptions(Color.GREEN,
                                                    5));

                                }
                               
                            for (Entity e : ent.getNearbyEntities(14, 10, 14))
                            {

                                if (e instanceof Player)
                                {
                                   

                                    Player hit = (Player)e;

                                                                           

                                   
                                        hit.sendMessage(ChatColor.DARK_GREEN
                                                + "You Have Been Poisoned!");
                                       
                                        hit.addPotionEffect(new PotionEffect(PotionEffectType.POISON,120,1));
                                        hit.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,120,1));

                                       
                               
                                }
                            }
                               
                               
                               
                               
                            }
                           
                            else if(rollOdds(30))
                            {
                               
                               
                               
                                for (double i = 0; i < 2 * Math.PI; i += 0.1) {
                                    double x = Math.cos(i) * 5;
                                    double y = Math.sin(i) * 5;

                                    ent.getWorld().spawnParticle(
                                            Particle.REDSTONE, ent.getLocation().getX() + x,
                                            ent.getLocation().getY(), ent.getLocation().getZ() + y, 1,
                                            new Particle.DustOptions(Color.YELLOW,
                                                    5));

                                }

                                        LivingEntity entity = (LivingEntity)ent;
                                        entity.addPotionEffect(new PotionEffect(PotionEffectType.HEAL,1,20));
                                        entity.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,120,1));

                            }
                           
                            else if(rollOdds(40))
                            {
                               
                               
                                for (double i = 0; i < 2 * Math.PI; i += 0.1) {
                                    double x = Math.cos(i) * 5;
                                    double y = Math.sin(i) * 5;

                                    ent.getWorld().spawnParticle(
                                            Particle.REDSTONE, ent.getLocation().getX() + x,
                                            ent.getLocation().getY(), ent.getLocation().getZ() + y, 1,
                                            new Particle.DustOptions(Color.GRAY,
                                                    5));

                                }
                               
                            for (Entity e : ent.getNearbyEntities(14, 10, 14))
                            {

                                if (e instanceof Player)
                                {
                                   

                                    Player hit = (Player)e;

                                                                           

                                   
                                        hit.sendMessage(ChatColor.DARK_GRAY
                                                + "You Have Been Stunned!");
                                       
                                        hit.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,120,5));
                                        hit.addPotionEffect(new PotionEffect(PotionEffectType.HARM,1,2));

                                       
                               
                                }
                            }
                               
                               
                               
                               
                               
                            }
                           
                           
                           
                           
                           
                           
                           
                           
                        }
                       
                       
                       
                       
                    }

                   
                }
               
               
               
               
               
               
               
               
            }
     

    This is the only Entityonentity damage event, the powers run fine so I know its running
     
  13. Code (Java):
        @EventHandler
        public void onKillBounty(EntityDeathEvent event)
        {
           
            for(Bounty b: BountyManager.getBountyList())
            {
               
                if(b.getBountyId() == event.getEntity().getEntityId())
                {
                   
                    ItemStack item = new ItemStack(Material.EMERALD);
                    item.setAmount((int)((Math.random()*15)+5));
                   
                    event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(),item);
                   
                   
                    if(b.getType().equals("drug"))
                    {
                       
                       
                       
                        ItemStack drug = new ItemStack(Material.BLAZE_ROD);
                        drug.setAmount((int)((Math.random()*5)+2));
                       
                        ItemMeta drugim = drug.getItemMeta();
                       
                        drugim.setDisplayName(ChatColor.ITALIC+"Death Sticks");
                        drug.setItemMeta(drugim);
                       
                       
                        event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(),drug);
                       
                       
                       
                       
                    }
                   
                   
                   
                   
                   
                   
                   
                   
                   
                   
                   
                   
                    BountyManager.getBountyList().remove(b);
                   
                    spawnNewBounty();
                   
                    if(event.getEntity().getKiller() instanceof Player)
                    {
                        Player p = event.getEntity().getKiller();
                        Levels.addexp(p, 30);
                        ChatUtilities.broadcastall(b.getName() + " Was Hunted by "+p.getName());
                       
                        ChatUtilities.broadcast("You Gained 30xp from Completing a Bounty!", p);
                    }
                   
                    break;
                   
                   
                   
                }
               
               
               
            }
           
           
           
           
           
           
        }

    Here is the entity death event
     
  14. I personally prefer to directly use the base value instead of a hard-coded value in invoking setHealth.
    Ensures there are no discrepancies between the values.
    This works for me. Try for yourself?
    Code (Java):
    mob.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(valueGoesHere);
    mob.setHealth(mob.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue());
     
  15. didn't work, died in like 4 hits counted down like from my imgur image above except died after it said 195



    Code (Java):
                ee.setItemInMainHand(new ItemStack(Material.POTION));
               
               

                target.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(200.0D);
                target.setHealth(target.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue());
               
                AttributeInstance speedAttribute = target.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
                speedAttribute.setBaseValue(0.5);
               
                AttributeInstance damageAttribute = target.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
                damageAttribute.setBaseValue(4);
     
  16. Do you listen to EntityDamageEvent as well? That event is still called with the cause being ENTITY_ATTACK.
     
  17. Actually I do call it here:




    Code (Java):
        @EventHandler
        public void preventFall(EntityDamageEvent event)
        {
           
            for(Bounty b:BountyManager.getBountyList())
            {
               
                if(b.getBountyId() == event.getEntity().getEntityId())
                {
                   
                    if(event.getCause() == DamageCause.FALL)
                    {
                        event.setCancelled(true);
                    }

                   
                }
               
               
            }
           
           
        }
       
     
  18. I'm trying real hard, but i'm not seeing anything wrong...

    The problem is not the maxhealth, because getHealth() prints 200.
    The problem is probably not the EntityDamageByEntityEvent because nothing seems to modify the health of the mob.
    The entity dies after 10HP from what your screenshot seems to show. (?) It cannot be the wrong entity because of the verification you do. And anyway, Piglins have a default 20HP.

    The DeathEvent gets called properly. And you get xp so it means you did kill it and it did not simply disappear right ?