Solved Attempting to change attribute of an entity when they spawn

Discussion in 'Spigot Plugin Development' started by RespenXI, Jul 11, 2021.

  1. Code (Text):
    @EventHandler
        public void onHorseSpawn(EntitySpawnEvent e){

            Entity entity = e.getEntity();

            if(entity instanceof Horse) {


                double baseHealth = ((Attributable) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();

                double fiftyPercent = baseHealth / 50;

                ((Attributable) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(baseHealth + fiftyPercent);
             
                ((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 1, false, false));
            }
        }
    }
    Trying to make it so that when a horse spawns it increases their max health by 50% (as opposed to a flat amount) and gives them Speed 1, but so far it isn't working. Any idea as to why/how I can make it work.
     
    #1 RespenXI, Jul 11, 2021
    Last edited: Jul 11, 2021
  2. - Is your plugin is correctly loaded and enabled?
    - Is your listener class registered as an event listener?
    - Is the method being called? Try debug messages
     
  3. It started to work when I removed the entire fiftyPercent double. Not sure why, but yeah. Just gonna look for another way to add a % value
     
  4. Oh, weird haha, try doing baseValue * 1.5
     
  5. Ended up casting it as an int which fixed it
     
    • Like Like x 1