forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
50 lines (43 loc) · 1.97 KB
/
index.ts
File metadata and controls
50 lines (43 loc) · 1.97 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
// Script example for ScriptAPI
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { world, Entity, ScoreboardIdentity } from "@minecraft/server";
/**
* Gets the score recorded for a participant on objective
* @param participant
* @param objectiveId
*/
export function getScore(participant: string | Entity | ScoreboardIdentity, objectiveId: string) {
const objective = world.scoreboard.getObjective(objectiveId);
if (!objective) throw new Error(`Objective ${objectiveId} not found`);
return objective.getScore(participant);
}
/**
* Sets the score recorded for a participant on objective
* @param participant
* @param objectiveId
* @param score
* @returns {ScoreboardIdentity} participant that was changed in objective
*/
export function setScore(participant: string | Entity | ScoreboardIdentity, objectiveId: string, score: number): ScoreboardIdentity {
const objective = world.scoreboard.getObjective(objectiveId);
if (!objective) throw new Error(`Objective ${objectiveId} not found`);
objective.setScore(participant, score);
if (participant instanceof Entity) return participant.scoreboardIdentity!;
else if (participant instanceof ScoreboardIdentity) return participant;
else return objective.getParticipants().find(p => p.displayName === participant)!;
};
/**
* Add the score recorded for entity on objective
* @param participant
* @param objectiveId
* @param score
*/
export function addScore(participant: string | Entity | ScoreboardIdentity, objectiveId: string, score: number): ScoreboardIdentity {
const objective = world.scoreboard.getObjective(objectiveId);
if (!objective) throw new Error(`Objective ${objectiveId} not found`);
objective.addScore(participant, score);
if (participant instanceof Entity) return participant.scoreboardIdentity!;
else if (participant instanceof ScoreboardIdentity) return participant;
else return objective.getParticipants().find(p => p.displayName === participant)!;
}