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
64 lines (56 loc) · 2.24 KB
/
index.ts
File metadata and controls
64 lines (56 loc) · 2.24 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
// Script example for ScriptAPI
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { Entity, world, DisplaySlotId, ScoreboardObjective } from "@minecraft/server";
/**
* Action to modify entity scoreboard
*/
enum ScoreboardAction {
add = "add",
remove = "remove",
set = "set"
};
/**
* fetch scoreboard objective display
* @deprecated Scoreboards Setter APIs Client Sync issue is fixed in 1.20.10
*/
const updateDisplay = (objective: ScoreboardObjective) => {
/**
* @type {(keyof typeof DisplaySlotId)[]}
*/
const displaySlots: (keyof typeof DisplaySlotId)[] = [ 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: Entity, objectiveId: string, score: number, action?: ScoreboardAction) {
// 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 };