Rendering methods are responsible, as the name suggest, for rendering the health bar. These are located inside the HealthBar class.
Accessing the methods
The HealthBar class provides an instance that is used to access the methods. To access the methods, then you need to use the HEALTH_INSTANCE provided by the same class, otherwise the methods won't appear, like this:
// use this --> HealthBar.HEALTH_INSTANCEHealthBar.HEALTH_INSTANCE.method();
When should my health bar be overridden? shouldRenderHealthBar(...)
The first method to understand is shouldRenderHealthBar(...). It controls whether the rendering event is actually rendering the health bar or not: if you were to use the RenderGuiLayerEvent.Pre event exactly as it is, you are taking action on everything that is rendered in a player's screen. This method allows you to access and modify only that particular instance that is responsible for rendering hearts.
It is provided with a runnable component, allowing all other methods to be put inside the lambda expression.
HealthBar.HEALTH_INSTANCE.shouldRenderHealthBar(event, livingEntity -> {//Activating only if the event is rendering the hearts//This is you workspace!});//Not this /!\//Any methods called outside the "control" method will activated//no metter if the event is rendering the health bar or not!
As you can see this method provides a livingEntity instance that can be used to check all sort of thing about a player (does he has any effects? is he taking damage?...) and also allows to access the level using the same entity variable (livingEntity.level)
How do I render my custom health bar? renderHealthBar(...)
The renderHealthBar(...) method is responsible for the overriding and rendering of the health bar. It presents in two different ways, the first requiring only 1 parameter (being a RenderGuiLayerEvent.Pre, so the "event"), and the other one requiring 2 parameter being the RenderGuiLayerEvent.Pre, and another lambda expression.
The lambda expression is necessary to override certain elements through the manipulation methods.
Heads up!
To know more about manipulation methods check out the dedicated page.
HealthBar.HEALTH_INSTANCE.renderHealthBar(event);
This method operates in multiple steps:
- Aborting: the method cancels the event, interrupting the rendering of the health bar (int this phase, the health bar is no longer rendered);
- Building resources: variables are registered, textures are initialized and player and world data are stored.
- Rendering: hearts are rendered following a particular order: first the absorption hearts are rendered, followed by the containers (the empty hearts), then the blinking hearts (the ones that appear when taking damage) and, at the end, the normal hearts (half and full red hearts).
- Fix and reset: finally the y start point of the armor bar is realigned to its normal value, and some variables get reset.
This list can come in handy when trying to understand the manipulation methods.
Heads up!
There must be only 1 renderHealthBar(...) method running at a time! Otherwise multiple health bar will be shown!
What are these effects? PreSets.PRESETS_INSTANCE
Inside the HealthBar class, there's a subclass called PreSets. In here you can find some methods (accessible with the PRESETS_INSTANCE), responsible for the activation of the heart effects. These methods will not do anything alone, but need to be pared with the renderHealthBar(...) method:
// access -> HealthBar.PreSets.PRESETS_ISTANCE.method();HealthBar.PreSets.PRESETS_ISTANCE.activateLightBlueEffect();
Once an effect is activated, it will change the texture of the hearts once applied. Having multiple effects applied at the same time will result in only one being shown, and that is the last event activated in you code:
HealthBar.PreSets.PRESETS_ISTANCE.activateLightBlueEffect();HealthBar.PreSets.PRESETS_ISTANCE.activateOrangeEffect();HealthBar.PreSets.PRESETS_ISTANCE.activateYellowEffect();//Last effect will have priority in case of multiple effects present at the same time//Green hearts will have the priorityHealthBar.PreSets.PRESETS_ISTANCE.activateGreenEffect();
Heads up!
These activation methods simply set a variable responsible for the resource building (phase 2 of the renderHealthBar(...) method) which will then be used to set the textures of the hearts. It doesn't render anything, it just sets variables then will be used from the render method.
-> That's the end of rendering methods. Let's go over the manipulation methods!