Skip to content

Commit a3e0aa0

Browse files
committed
EntityDeathEvent -> PlayerDeathEvent
1 parent da70290 commit a3e0aa0

3 files changed

Lines changed: 77 additions & 65 deletions

File tree

scripts/EntityDeathEvent.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

scripts/PlayerDeathEvent.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license MIT
3+
* @author JaylyMC
4+
*/
5+
import { EntityHealthComponent, world, Player } from "mojang-minecraft";
6+
7+
/**
8+
* Contains information related to an player death.
9+
*/
10+
export class PlayerDeathEvent {
11+
/**
12+
* @param {Player} player
13+
*/
14+
constructor (player) {
15+
this.player = player;
16+
};
17+
};
18+
19+
/**
20+
* Manages callbacks that are connected to when an player dies.
21+
*/
22+
export class PlayerDeathEventSignal {
23+
/**
24+
* Subscribe
25+
* @param {(arg: PlayerDeathEvent) => void} arg
26+
* @return {(arg: PlayerDeathEvent) => void}
27+
*/
28+
subscribe (arg) {
29+
arg["playerDeath"] = true;
30+
/**
31+
* @type {Player[]}
32+
*/
33+
const deadPlayers = [];
34+
let callback = world.events.tick.subscribe(() => {
35+
for (let player of world.getPlayers()) {
36+
if (!player.hasComponent("health")) return;
37+
/**
38+
* @type {EntityHealthComponent}
39+
*/
40+
// @ts-ignore
41+
let health = player.getComponent("health");
42+
if (health.current === 0 && arg["playerDeath"] === true) {
43+
const playerIndex = deadPlayers.findIndex(pl => pl.name === player.name);
44+
if (playerIndex < 0) {
45+
arg(new PlayerDeathEvent(player));
46+
deadPlayers.push(player);
47+
let playerDeathCallback = world.events.tick.subscribe(() => {
48+
if (health.current > 0) {
49+
deadPlayers.splice(playerIndex, 1);
50+
world.events.tick.unsubscribe(playerDeathCallback);
51+
};
52+
});
53+
}
54+
} else if (arg["playerDeath"] === false) {
55+
world.events.tick.unsubscribe(callback);
56+
};
57+
}
58+
});
59+
60+
return arg;
61+
};
62+
63+
/**
64+
* Unsubscribe
65+
* @param {(arg: PlayerDeathEvent) => void} arg
66+
* @return {void}
67+
*/
68+
unsubscribe (arg) {
69+
arg["playerDeath"] = false;
70+
};
71+
};

scripts/README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@
2828

2929
### [Scoreboard level up](./scoreboard/levelup.js)
3030

31-
### [Entity death event signal](./EntityDeathEvent.js)
31+
### [Player death event signal](./PlayerDeathEvent.js)
3232

33-
Example Usage:
3433
```js
35-
import { EntityDeathEventSignal } from "./EntityDeathEvent.js";
34+
import { PlayerDeathEventSignal } from "./PlayerDeathEvent.js";
3635

37-
let deathEvent = new EntityDeathEventSignal();
36+
let playerDeath = new PlayerDeathEventSignal();
3837

39-
deathEvent.subscribe(function (evd) {
40-
evd.deadEntity.runCommand("say i'm dead");
38+
let callback = playerDeath.subscribe(({player}) => {
39+
// callback function
40+
playerDeath.unsubscribe(callback); // unsubscribes
4141
});
42-
43-
deathEvent.unsubscribe();
4442
```

0 commit comments

Comments
 (0)