forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (88 loc) · 2.47 KB
/
index.js
File metadata and controls
98 lines (88 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Script example for ScriptAPI
// Author: Jayly#1397 <Jayly Discord>
// Project: https://github.com/JaylyDev/ScriptAPI
import { Player, world } from "@minecraft/server";
/**
* @type {((arg: PlayerExistEvent) => void)[]}
*/
const callbacks = [];
export class PlayerExistEvent {
/**
* If true, this is the initial spawn of a player after joining
* the game.
* @type {boolean}
* @readonly
*/
initialSpawn;
/**
* Object that represents the player that joined the game.
* @type {Player}
* @readonly
*/
player;
/**
* Opaque string identifier of the player that joined the game.
* @type {string}
* @readonly
*/
playerId;
/**
* Name of the player that has joined.
* @type {string}
* @readonly
*/
playerName;
/**
* @param {boolean} initialSpawn
* @param {Player} player
* @param {string} playerId
* @param {string} playerName
*/
constructor (initialSpawn, player, playerId, playerName) {
this.initialSpawn = initialSpawn;
this.player = player;
this.playerId = playerId;
this.playerName = playerName;
};
};
// backend
world.afterEvents.playerJoin.subscribe((event) => {
const { playerId, playerName } = event;
const onPlayerSpawn = world.afterEvents.playerSpawn.subscribe((event) => {
const { player, initialSpawn } = event;
if (player.name === playerName && player.id === playerId && initialSpawn === true) {
world.afterEvents.playerSpawn.unsubscribe(onPlayerSpawn);
for (const callback of callbacks) {
callback(new PlayerExistEvent(initialSpawn, player, playerId, playerName));
};
};
});
});
/**
* Manages callbacks that are connected to when an entity dies.
*/
class PlayerExistEventSignal {
/**
* @remarks
* Adds a callback that will be called when an entity dies.
* @param {(arg: PlayerExistEvent) => void} callback
* @param {import("@minecraft/server").EntityEventOptions} options
* @returns {(arg: PlayerExistEvent) => void}
*/
subscribe(callback, options = {}) {
callbacks.push(callback);
return callback;
};
/**
* @remarks
* Removes a callback from being called when an entity dies.
* @param {(arg: PlayerExistEvent) => void} callback
* @throws This function can throw errors.
*/
unsubscribe(callback) {
const index = callbacks.findIndex((value) => value === callback);
callbacks.splice(index);
};
}
const playerExist = new PlayerExistEventSignal();
export { playerExist, PlayerExistEventSignal };