Skip to content

Death Event

The death event triggers when an entity dies. It's a component-based event - death is detected when DeathComponent is added to an entity.

API Reference

See full API: DeathComponent | DeathSystems

Concepts

Before reading this, familiarize yourself with the Event System.

Overview

PropertyValue
ComponentDeathComponent
Base HandlerDeathSystems.OnDeathSystem
TriggerDeathComponent added to entity

The DeathComponent

Location: com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent

When an entity's health reaches zero, DeathComponent is added automatically. This component contains:[^1]

MethodDescription
getDeathCause()The DamageCause that killed the entity
getDeathInfo()The Damage object that caused death
getDeathMessage()Message shown on death screen
setDeathMessage(Message)Set custom death message
isShowDeathMenu()Whether to show respawn screen
setShowDeathMenu(boolean)Control respawn screen visibility
getItemsLostOnDeath()Items that were dropped
setItemsLossMode(ItemsLossMode)How items are lost (ALL, CONFIGURED, NONE)
setItemsAmountLossPercentage(double)Percentage of items to lose

[^1]: See DeathComponent API for full method list

Creating a Handler[^2]

Extend DeathSystems.OnDeathSystem:

[^2]: See DeathSystems API for base class methods

java
package com.example.myplugin;

import com.hypixel.hytale.component.*;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.entity.damage.*;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import javax.annotation.Nonnull;

public class MyDeathHandler extends DeathSystems.OnDeathSystem {

    @Nonnull
    @Override
    public Query getQuery() {
        return Player.getComponentType();  // Only handle player deaths
    }

    @Override
    public void onComponentAdded(@Nonnull Ref<EntityStore> ref,
                                  @Nonnull DeathComponent component,
                                  @Nonnull Store store,
                                  @Nonnull CommandBuffer commandBuffer) {
        // Entity just died

        Player player = (Player) store.getComponent(ref, Player.getComponentType());

        // Get death info
        Damage deathInfo = component.getDeathInfo();
        DamageCause cause = component.getDeathCause();

        System.out.println("Player died from: " + cause.getId());

        // Check who killed them
        if (deathInfo != null && deathInfo.getSource() instanceof Damage.EntitySource source) {
            Ref<EntityStore> killerRef = source.getRef();
            if (killerRef.isValid()) {
                // Do something with the killer
                Player killer = (Player) store.getComponent(killerRef, Player.getComponentType());
                if (killer != null) {
                    killer.getPlayerRef().sendMessage(Message.raw("You killed someone!"));
                }
            }
        }

        // Modify death behavior
        component.setShowDeathMenu(true);
        component.setItemsLossMode(DeathConfig.ItemsLossMode.NONE);  // Keep items
    }
}

Common Use Cases

Track Kill Statistics

java
@Override
public void onComponentAdded(@Nonnull Ref<EntityStore> ref,
                              @Nonnull DeathComponent component,
                              @Nonnull Store store,
                              @Nonnull CommandBuffer commandBuffer) {

    Damage deathInfo = component.getDeathInfo();
    if (deathInfo == null) return;

    if (deathInfo.getSource() instanceof Damage.EntitySource source) {
        Ref<EntityStore> killerRef = source.getRef();
        if (killerRef.isValid()) {
            Player killer = (Player) store.getComponent(killerRef, Player.getComponentType());
            if (killer != null) {
                // Increment kill counter (your custom logic)
                incrementKills(killer);
            }
        }
    }
}

Prevent Item Loss

java
@Override
public void onComponentAdded(@Nonnull Ref<EntityStore> ref,
                              @Nonnull DeathComponent component,
                              @Nonnull Store store,
                              @Nonnull CommandBuffer commandBuffer) {

    // Keep all items on death
    component.setItemsLossMode(DeathConfig.ItemsLossMode.NONE);

    // Or configure partial loss
    component.setItemsLossMode(DeathConfig.ItemsLossMode.CONFIGURED);
    component.setItemsAmountLossPercentage(25.0);  // Lose 25% of items
}

Custom Death Message

java
@Override
public void onComponentAdded(@Nonnull Ref<EntityStore> ref,
                              @Nonnull DeathComponent component,
                              @Nonnull Store store,
                              @Nonnull CommandBuffer commandBuffer) {

    component.setDeathMessage(Message.raw("You have fallen!"));
}

Built-in Death Handlers

The system includes these handlers:

HandlerPurpose
ClearHealthSets health to 0
ClearInteractionsCancels current interactions
ClearEntityEffectsRemoves all effects
PlayerKilledPlayerSends "you killed X" message
DropPlayerDeathItemsDrops inventory items
PlayerDropItemsConfigConfigures item loss
RunDeathInteractionsRuns death interaction chain
KillFeedBroadcasts kill feed
PlayerDeathScreenShows respawn UI
DeathAnimationPlays death animation
CorpseRemovalRemoves corpse after delay

Registration

java
@Override
protected void setup() {
    ComponentRegistryProxy<EntityStore> registry = this.getEntityStoreRegistry();
    registry.registerSystem(new MyDeathHandler());
}

See Also

Unofficial documentation · Any questions? Found a mistake? Have something you want documented? Join the Discord server at the top and let us know in #hytale!