Jump to content

How can I get the original Max Health of the entity type (mob)?


IPECTER

Recommended Posts

How can I get the original Max Health of the entity type (mob)?

For example, like this.

@SubscribeEvent
    public void onMobSpawn(EntityJoinWorldEvent e) {
        if (e.getEntity() instanceof Mob) {
            Mob mob = (Mob) e.getEntity();
            if (mob.getOriginalMaxHealth() == mob.getMaxHealth()){
                //do something (modify mob max health)
            }
        }
    }

" mob.getOriginalMaxHealth() "

 

Edited by IPECTER
Link to comment
Share on other sites

EntityJoinWorldEvent occurs every time a player visits the world.
 

So,

In order not to continue to increase the mob's max health, if the mob's max health is more than 100 times the mob's original max health, it will not change the mob's max health.

Edited by IPECTER
Link to comment
Share on other sites

19 hours ago, diesieben07 said:

If you use AttributeModifier like I told you in your other thread multiple times already it will achieve this automatically without any extra work on your part.

private static final AttributeModifier x100MaxHealth = new AttributeModifier(Attributes.MAX_HEALTH, "x100 Max Health", 100, AttributeModifier.Operation.MULTIPLY_BASE);
    

error...

Cannot resolve constructor 'AttributeModifier(net.minecraft.world.entity.ai.attributes.Attribute, java.lang.String, int, net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation)

Link to comment
Share on other sites

 

16 minutes ago, diesieben07 said:

For the future: Please refrain from creating multiple topics for the same thing. In your other thread you mentioned success, is this solved?

sorry.....

I only succeeded in raising the mob's hp above 1024.

Now I have to find a way to control Mob's physical strength using AttributeModifier.

Link to comment
Share on other sites

On 2/18/2022 at 8:33 PM, diesieben07 said:

Look at the AttributeModifier constructor. You are not passing the correct parameters.

success. thx.
but....

private static final AttributeModifier x100MaxHealth = new AttributeModifier("x100 Max Health", 99, AttributeModifier.Operation.MULTIPLY_BASE); @SubscribeEvent public static void onMobSpawn(EntityJoinWorldEvent e) { if (e.getEntity() instanceof Mob) { Mob mob = (Mob) e.getEntity(); if (mob.getHealth() > mob.getAttribute(Attributes.MAX_HEALTH).getBaseValue()) { mob.getAttribute(Attributes.MAX_HEALTH).addTransientModifier(x100MaxHealth); mob.setHealth(mob.getHealth() * 100); } } }

    private static final AttributeModifier x100MaxHealth = new AttributeModifier("x100 Max Health", 99, AttributeModifier.Operation.MULTIPLY_BASE);
    @SubscribeEvent
    public static void onMobSpawn(EntityJoinWorldEvent e) {

        if (e.getEntity() instanceof Mob) {
            Mob mob = (Mob) e.getEntity();
            if (mob.getHealth() >= mob.getAttribute(Attributes.MAX_HEALTH).getBaseValue()) {
                mob.getAttribute(Attributes.MAX_HEALTH).addTransientModifier(x100MaxHealth);
                mob.setHealth(mob.getHealth() * 100);
            }
        }

    }

Every time a player join the world, even if the mob's hp is lower than the maximum hp, all hp is recovered. What should I do?

Edited by IPECTER
Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Do not do this check. Simply add the modifier.

Do not set the health.

private static final AttributeModifier x100MaxHealth = new AttributeModifier("x100 Max Health", 99, AttributeModifier.Operation.MULTIPLY_BASE);
    @SubscribeEvent
    public static void onMobSpawn(EntityJoinWorldEvent e) {

        if (e.getEntity() instanceof Mob) {
            Mob mob = (Mob) e.getEntity();
            mob.getAttribute(Attributes.MAX_HEALTH).addTransientModifier(x100MaxHealth);
        }

    }

like this?

When mobs spawn for the first time, Mobs have to have full hp.

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

You have to put a flag in the entity's data (you can either use a capability or getPersistentData, in the latter case make sure to prefix it with your ModID). If the flag is not yet present, increase mob health.

@Mod.EventBusSubscriber(modid = "x100health", bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class EntityJoinWorld {
    private static final AttributeModifier x100MaxHealth = new AttributeModifier("x100 Max Health", 99, AttributeModifier.Operation.MULTIPLY_BASE);
    @SubscribeEvent
    public static void onMobSpawn(EntityJoinWorldEvent e) {

        if (e.getEntity() instanceof Mob) {
            Mob mob = (Mob) e.getEntity();
            mob.getAttribute(Attributes.MAX_HEALTH).addTransientModifier(x100MaxHealth);
            if (!mob.getPersistentData().getBoolean("x100health")){
                mob.getPersistentData().put("x100health", tag..?);
                mob.setHealth(mob.getHealth() * 100);
            }
        }

    }
}

mob.getPersistentData().put("x100health", tag..?);

tag..???? What I should do?

Edited by IPECTER
Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

Maybe don't use put then, there are other methods.

@Mod.EventBusSubscriber(modid = "x100health", bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class EntityJoinWorld {
    private static final AttributeModifier x100MaxHealth = new AttributeModifier("x100 Max Health", 99, AttributeModifier.Operation.MULTIPLY_BASE);
    @SubscribeEvent
    public static void onMobSpawn(EntityJoinWorldEvent e) {

        if (e.getEntity() instanceof Mob) {
            Mob mob = (Mob) e.getEntity();
            mob.getAttribute(Attributes.MAX_HEALTH).addTransientModifier(x100MaxHealth);
            if (!mob.getPersistentData().contains("x100health")){
                mob.getPersistentData().putBoolean("x100health", true);
                mob.setHealth(mob.getHealth() * 100);
            }
        }

    }
}

like this! right?

Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Looks OK.

@Mod.EventBusSubscriber(modid = "x100health", bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class EntityJoinWorld {
    private static final AttributeModifier x100MaxHealth = new AttributeModifier("x100 Max Health", 99, AttributeModifier.Operation.MULTIPLY_BASE);
    @SubscribeEvent
    public static void onMobSpawn(EntityJoinWorldEvent e) {

        if (e.getEntity() instanceof Mob) {
            Mob mob = (Mob) e.getEntity();
            mob.getAttribute(Attributes.MAX_HEALTH).addTransientModifier(x100MaxHealth);
            if (!mob.getPersistentData().contains("x100health")){
                mob.getPersistentData().putBoolean("x100health", true);
                mob.setHealth(mob.getHealth() * 100);
            }
        }

    }
}

When the player joins the world, the mob's current hp is initialized.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.