Skip to content

Commit 400ac96

Browse files
authored
Merge pull request #281 from LostPizzaMan/feature/death-event-guide
feat: add death event guide
2 parents 45864b6 + 5676c07 commit 400ac96

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: "Player Death Event"
3+
description: "Learn how to use the OnDeathSystem."
4+
authors:
5+
- name: "Bird"
6+
link: "https://discord.com/users/495709580068651009"
7+
---
8+
9+
# Overview
10+
11+
In this guide, you'll learn how to use the OnDeathSystem to detect when a player dies.
12+
13+
## Code Example
14+
15+
```java
16+
package org.example.plugin.Systems;
17+
18+
import com.hypixel.hytale.component.CommandBuffer;
19+
import com.hypixel.hytale.component.Ref;
20+
import com.hypixel.hytale.component.Store;
21+
import com.hypixel.hytale.component.query.Query;
22+
import com.hypixel.hytale.server.core.Message;
23+
import com.hypixel.hytale.server.core.entity.entities.Player;
24+
import com.hypixel.hytale.server.core.modules.entity.damage.Damage;
25+
import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent;
26+
import com.hypixel.hytale.server.core.modules.entity.damage.DeathSystems;
27+
import com.hypixel.hytale.server.core.universe.Universe;
28+
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
29+
30+
import javax.annotation.Nonnull;
31+
32+
public class TestDeathSystem extends DeathSystems.OnDeathSystem {
33+
34+
@Nonnull
35+
@Override
36+
public Query<EntityStore> getQuery() {
37+
return Query.and(Player.getComponentType());
38+
}
39+
40+
@Override
41+
public void onComponentAdded(@Nonnull Ref ref, @Nonnull DeathComponent component, @Nonnull Store store, @Nonnull CommandBuffer commandBuffer) {
42+
Player playerComponent = (Player) store.getComponent(ref, Player.getComponentType());
43+
44+
assert playerComponent != null;
45+
46+
Universe.get().sendMessage(Message.raw("Death player: " + playerComponent.getDisplayName()));
47+
Damage deathInfo = component.getDeathInfo();
48+
if (deathInfo != null) {
49+
Universe.get().sendMessage(Message.raw("Death info amount: " + deathInfo.getAmount()));
50+
}
51+
}
52+
}
53+
```
54+
55+
## Steps
56+
57+
### 1. Extend the Death System
58+
59+
Start by creating a new class that extends `DeathSystems.OnDeathSystem`. This allows your system to automatically react whenever an entity dies.
60+
61+
```java
62+
public class TestDeathSystem extends DeathSystems.OnDeathSystem {
63+
```
64+
65+
---
66+
67+
### 2. Define Which Entities This System Applies To
68+
69+
Override the `getQuery()` method to filter for the entities you want. In this case, we only want **players**:
70+
71+
```java
72+
@Nonnull
73+
@Override
74+
public Query<EntityStore> getQuery() {
75+
return Query.and(Player.getComponentType());
76+
}
77+
```
78+
79+
<Callout>
80+
`Query.and(...)` allows you to combine multiple filters if needed.
81+
</Callout>
82+
83+
---
84+
85+
### 3. Listen for the Death Event
86+
87+
The `onComponentAdded` method is called automatically whenever a `DeathComponent` is added to an entity matching your query.
88+
89+
```java
90+
@Override
91+
public void onComponentAdded(
92+
@Nonnull Ref ref,
93+
@Nonnull DeathComponent component,
94+
@Nonnull Store store,
95+
@Nonnull CommandBuffer commandBuffer
96+
)
97+
```
98+
99+
---
100+
101+
### 4. Get the Player Component
102+
103+
Use the `Store` to retrieve the `Player` component from the entity that just died:
104+
105+
```java
106+
Player playerComponent = (Player) store.getComponent(ref, Player.getComponentType());
107+
```
108+
109+
This lets you access information such as the player's display name.
110+
111+
---
112+
113+
### 5. Get Death Damage Information
114+
115+
The `DeathComponent` contains information about the damage that killed the player:
116+
117+
```java
118+
Damage deathInfo = component.getDeathInfo();
119+
```
120+
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

Comments
 (0)