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
63 lines (63 loc) · 2.37 KB
/
index.js
File metadata and controls
63 lines (63 loc) · 2.37 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
// Script example for ScriptAPI
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { world, DisplaySlotId } from "@minecraft/server";
/**
* Action to modify entity scoreboard
*/
var ScoreboardAction;
(function (ScoreboardAction) {
ScoreboardAction["add"] = "add";
ScoreboardAction["remove"] = "remove";
ScoreboardAction["set"] = "set";
})(ScoreboardAction || (ScoreboardAction = {}));
;
/**
* fetch scoreboard objective display
* @deprecated Scoreboards Setter APIs Client Sync issue is fixed in 1.20.10
*/
const updateDisplay = (objective) => {
/**
* @type {(keyof typeof DisplaySlotId)[]}
*/
const displaySlots = [DisplaySlotId.BelowName, DisplaySlotId.List, DisplaySlotId.Sidebar];
for (const displaySlotId of displaySlots) {
const displaySlot = world.scoreboard.getObjectiveAtDisplaySlot(DisplaySlotId[displaySlotId]);
if (displaySlot?.objective === objective) {
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar);
world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, displaySlot);
}
}
;
};
/**
* Set entity score and fetch scoreboard objective display
* @param {Entity} entity Entity's scoreboard to change
* @param {string} objectiveId Objective to apply the score to.
* @param {number} score Score value
* @param {ScoreboardAction} action Decides whether to add, remove, or set score to entity (default = set)
*/
function setScore(entity, objectiveId, score, action) {
// Check if scoreboard object exist first
const objective = world.scoreboard.getObjective(objectiveId);
if (!objective)
throw new ReferenceError('Scoreboard objective does not exist in world.');
const previousScore = !!entity.scoreboardIdentity ? objective.getScore(entity.scoreboardIdentity) : 0;
switch (action) {
case ScoreboardAction.add:
score += previousScore;
break;
case ScoreboardAction.remove:
score -= previousScore;
break;
default:
break;
}
// If entity doesnt have scoreboard property, run command
if (!entity.scoreboardIdentity)
entity.runCommand('scoreboard players set @s ' + objective + ' ' + score);
else
objective.setScore(entity.scoreboardIdentity, score);
}
;
export { setScore, ScoreboardAction };