11---
2- title : " Death Event"
2+ title : " Player Death Event"
33description : " Learn how to use the OnDeathSystem."
44authors :
55 - name : " Bird"
@@ -56,6 +56,8 @@ public class TestDeathSystem extends DeathSystems.OnDeathSystem {
5656
5757### 1. Extend the Death System
5858
59+ Start by creating a new class that extends ` DeathSystems.OnDeathSystem ` . This allows your system to automatically react whenever an entity dies.
60+
5961``` java
6062public class TestDeathSystem extends DeathSystems .OnDeathSystem {
6163```
@@ -64,6 +66,8 @@ public class TestDeathSystem extends DeathSystems.OnDeathSystem {
6466
6567### 2. Define Which Entities This System Applies To
6668
69+ Override the `getQuery()` method to filter for the entities you want. In this case , we only want ** players** :
70+
6771```java
6872@Nonnull
6973@Override
@@ -72,42 +76,54 @@ public Query<EntityStore> getQuery() {
7276}
7377```
7478
75- * Filter entities to ** players only**
79+ < Callout >
80+ `Query . and(... )` allows you to combine multiple filters if needed.
81+ < / Callout >
7682
7783-- -
7884
7985### 3. Listen for the Death Event
8086
87+ The `onComponentAdded` method is called automatically whenever a `DeathComponent ` is added to an entity matching your query.
88+
8189```java
8290@Override
8391public void onComponentAdded(
8492 @Nonnull Ref ref,
8593 @Nonnull DeathComponent component,
8694 @Nonnull Store store,
8795 @Nonnull CommandBuffer commandBuffer
88- ) {
96+ )
8997```
9098
91- This method is automatically called when:
92-
93- * A `DeathComponent ` is added to an entity matching your query (players)
94-
9599-- -
96100
97101### 4. Get the Player Component
98102
103+ Use the `Store ` to retrieve the `Player ` component from the entity that just died:
104+
99105```java
100106Player playerComponent = (Player ) store. getComponent(ref, Player . getComponentType());
101107```
102108
103- * Fetches the ` Player ` component from the dead entity
109+ This lets you access information such as the player ' s display name.
104110
105111---
106112
107113### 5. Get Death Damage Information
108114
115+ The `DeathComponent` contains information about the damage that killed the player:
116+
109117```java
110118Damage deathInfo = component.getDeathInfo();
111119```
112120
113- * Lets you access death info such as amount and source
121+ You can then check:
122+
123+ ```java
124+ if (deathInfo != null) {
125+ Universe.get().sendMessage(Message.raw("Damage amount: " + deathInfo.getAmount()));
126+ }
127+ ```
128+
129+ This provides the **amount of damage** and the **source of the death**.
0 commit comments