Solved Help with custom health double value, cant figure it out

Discussion in 'Spigot Help' started by ZachThePwn, Jul 1, 2018.

  1. Basically, in the onDamagingHit method, I am trying to make it so on each damaging hit it will take away the health equal to the damage value. Right now it sort of works, but it spams it all at once instead of on each hit. I believe its because of the do while loop, but i dont know how else to make it so it doesnt reset the health value back to the initial on each damaging hit. I am not trying to use the normal minecraft health, im trying to make it so when the double health hits 0 or lower that the enemy will die. Right now it takes the players health stat from their armor just for testing purposes but I am going to make it so it checks for the entities armor once I figure this problem out. Im really lost on where to go from here so if anyone has any advice I would really appreciate it.

    In the picture it does the values right, but does them all at once.


    The damaging hit method
    Code (Text):
        public void onDamagingHit(EntityDamageByEntityEvent e) {  
            if (e.getDamager() instanceof Player && e.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
                Player p = (Player)e.getDamager();
                double phealth = 0;
                double health = getHealth(p, phealth);
                if (p.getInventory().getItemInMainHand().getType() != Material.WOOD_SWORD
                        && p.getInventory().getItemInMainHand().getType() != Material.STONE_SWORD
                        ) return;
                ItemStack woodensword = p.getInventory().getItemInMainHand();
                if (!woodensword.hasItemMeta()) return;
                if (!woodensword.getItemMeta().hasDisplayName()) return;
                if (woodensword.getItemMeta().getLore().get(0).contains("Damage : ")) {
                    do {
                        String lore = woodensword.getItemMeta().getLore().get(0).replace(ChatColor.stripColor("Damage : "), "");
                           double damage = Double.valueOf(ChatColor.stripColor(lore));
                           p.sendMessage(ChatColor.GREEN + "Damage : " + damage);
                           p.sendMessage(ChatColor.RED + "Remaining Health" + (health - damage));
                           health = setHealthDamage(damage, health);
                    } while (health > 0);
    my method to check for the health value
    Code (Text):
    public double getHealth(Player p, double phealth) {
            double maxhealth = 100;
            ItemStack helmet = p.getInventory().getHelmet();
            ItemStack chestplate = p.getInventory().getChestplate();
            ItemStack leggings = p.getInventory().getLeggings();
            ItemStack boots = p.getInventory().getBoots();
            if (!(p.getInventory().getHelmet() == null)) {
                if (p.getInventory().getHelmet().getItemMeta().getLore().get(0).contains("Health : ")) {
                    String lore = helmet.getItemMeta().getLore().get(0).replace(ChatColor.stripColor("Health : "), "");
                    double health = Double.valueOf(ChatColor.stripColor(lore));
                    maxhealth = health + maxhealth;  
            if (!(p.getInventory().getChestplate() == null)) {
                if (p.getInventory().getChestplate().getItemMeta().getLore().get(0).contains("Health : ")) {
                    String lore2 = chestplate.getItemMeta().getLore().get(0).replace(ChatColor.stripColor("Health : "), "");
                    double health2 = Double.valueOf(ChatColor.stripColor(lore2));
                    maxhealth = health2 + maxhealth;
            if (!(p.getInventory().getLeggings() == null)) {
                if (p.getInventory().getLeggings().getItemMeta().getLore().get(0).contains("Health : ")) {
                    String lore3 = leggings.getItemMeta().getLore().get(0).replace(ChatColor.stripColor("Health : "), "");
                    double health3 = Double.valueOf(ChatColor.stripColor(lore3));
                    maxhealth = health3 + maxhealth;
            if (!(p.getInventory().getBoots() == null)) {
                if (p.getInventory().getBoots().getItemMeta().getLore().get(0).contains("Health : ")) {
                    String lore4 = boots.getItemMeta().getLore().get(0).replace(ChatColor.stripColor("Health : "), "");
                    double health4 = Double.valueOf(ChatColor.stripColor(lore4));
                    maxhealth = health4 + maxhealth;
            //p.sendMessage(ChatColor.RED + "Health : " + maxhealth);
            phealth = maxhealth;
            return phealth;
    And finally the setHealthDamage method seen at the end of the damaging hit method.
    Code (Text):
    public double setHealthDamage(double damage, double health) {
            health = health - damage;
            return health;
  2. Also if I remove the do while loop it doesnt spam it but then it doesnt keep the health variable
  3. I was thinking maybe i could save the entities health in the config while they are alive and having it remove itself when its finished but i dont know how to do that or if it would work for multiplayer
  4. Another way I was thinking of was making it so the entity loses health on a percentage based on the health variable i made
  5. Hello!
    First try not to double post or in this case triple post xD Just edit it with the info if you don't mind :D

    Second, you are having it send the message while the health is greater than 0. You should just have it send the message when the entity is damaged if you are trying to prevent the spam. It will still send the message but it won't send it over and over. If that is what you are trying to get at?
  6. sorry about that i just was putting out ideas but ill try not to in the future. and i actually figured it out with the help of a friend. forgot to set as solved. @FlameKnight
  7. Ah ok. Well, glad you got it figured out! Thanks for letting me know
    • Winner Winner x 1
  8. Yea the plugin is actually going very well, i am about to post a question about mob spawning since its being odd for some reason but other than that its made so much progress. im very excited!