Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
entity

Heal Player

Heal a player to full health and stamina using EntityStatsModule. Based on working AdminUI code.

Example Code

java
// Heal player to full health and stamina (from AdminUI PlayerGui)
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatsModule;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatValue;
import com.hypixel.hytale.server.core.modules.entitystats.asset.EntityStatType;

public void heal(Store<EntityStore> store, Ref<EntityStore> ref) {
    // Get the entity stat map component
    var entityStatMap = store.getComponent(ref,
        EntityStatsModule.get().getEntityStatMapComponentType());

    if (entityStatMap == null) return;

    // Get stat indices
    int healthIndex = EntityStatType.getAssetMap().getIndex("Health");
    int staminaIndex = EntityStatType.getAssetMap().getIndex("Stamina");

    // Heal to max
    EntityStatValue health = entityStatMap.get(healthIndex);
    if (health != null) {
        entityStatMap.setStatValue(healthIndex, health.getMax());
    }

    // Restore stamina
    EntityStatValue stamina = entityStatMap.get(staminaIndex);
    if (stamina != null) {
        entityStatMap.setStatValue(staminaIndex, stamina.getMax());
    }
}

Thread Safety

Run on world thread.

Common Mistakes

Forgetting to get the stat map component type from EntityStatsModule.