Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to get/check player hp


 Share

Recommended Posts

Hey, i havent made mods in minecraft before and im currently starting out with trying to make some small simple mods.

 

Im currently trying to make a mod that checks when youre below a certain hp value and then gives you slowness or another potion effect.

I tried searching on google how to get the value from the player health as a int/var to use in a if sentence.

 

If anyone knows how to do this please respond! :)

 

https://imgur.com/a/a6tH8Zq

Tried this but it says errer: cannot find symbol

Link to comment
Share on other sites

FMLClientSetupEvent is only called once, and is only called on the client (you'll want to give potion effects on the server), so the event you should use is TickEvent.PlayerTickEvent, then event.player.getHealth() to get the health, and lastly to add a potion effect you can also call .addEffect on the player instance provided by the event

Link to comment
Share on other sites

Alright. the FMLClientSetupEvent is not the corret event to listen to (at all).

You will need to listen for the PlayerTickEvent event.

public class PlayerTickHandler {

    @SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent event) {
        PlayerEntity player = event.player;
        if (player.getHealth() < 7) {
            player.addPotionEffect(new EffectInstance(Effects.SLOWNESS, 60));
        }
    }
}

And register it for Forge event bus inside your mod's construcor like this: 

MinecraftForge.EVENT_BUS.register(new PlayerTickHandler());

Note the @SubscribeEvent, this essentially tells forge that the methods is listening for an event.

Edited by than00ber1
Link to comment
Share on other sites

11 minutes ago, than00ber1 said:

You will need to listen for the PlayerTickEvent event.

that's a bad idea, add the Effect for example when the Player joins the World or when he mine a Block but not each Tick

11 minutes ago, than00ber1 said:

And register it for Forge event bus inside your mod's construcor like this:

use the @EventBusSubscriber annotation you need to pass as parameter the Mod Id

what did you try to achieve?

Edited by Luis_ST
Link to comment
Share on other sites

44 minutes ago, than00ber1 said:

Alright. the FMLClientSetupEvent is not the corret event to listen to (at all).

You will need to listen for the PlayerTickEvent event.

public class PlayerTickHandler {

    @SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent event) {
        PlayerEntity player = event.player;
        if (player.getHealth() < 7) {
            player.addPotionEffect(new EffectInstance(Effects.SLOWNESS, 60));
        }
    }
}

And register it for Forge event bus inside your mod's construcor like this: 

MinecraftForge.EVENT_BUS.register(new PlayerTickHandler());

Note the @SubscribeEvent, this essentially tells forge that the methods is listening for an event.

This gave me this error

https://imgur.com/a/o2TcJjr

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.