-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetScore.js
31 lines (29 loc) · 1.14 KB
/
getScore.js
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
import { Entity } from "@minecraft/server";
const cache = {}
/**
* @param {Entity | String} target The target to get the score of ( can be a string, or an entity instance )
* @param {String | String[]} objective The objective to get the score of
*
* @returns {Number | Object} The score of the objective, or an object containing all scores for all objectives
*
* @example getScore(player, "kills") // returns the value of the kills objective for the player ( 0 if not set )
*
* @example getScore(player, ["kills", "deaths"]) // returns an object containing the values of the kills and deaths objectives for the player
*/
const getScore = (target, objective) => {
if (typeof objective === "string") try {
return (cache[objective] ??= world.scoreboard.getObjective(objective))?.getScore(target) || 0
} catch {
return 0;
}
// get all scores for all objectives
return objective.reduce((acc, cur) => {
try {
var obj = (cache[cur] ??= world.scoreboard.getObjective(cur))
acc[cur] = obj.getScore(target);
} catch {
acc[cur] = 0;
}
return acc;
}, {})
}