Skip to content

Commit 6a1023a

Browse files
GamerFilebot174
andauthored
Update index.js (JaylyDev#165)
* Update index.js • Updated .setObj method. • Improved Js Docs * Update index.js Minor fixes --------- Co-authored-by: bot174 <[email protected]>
1 parent b79a501 commit 6a1023a

File tree

1 file changed

+121
-79
lines changed

1 file changed

+121
-79
lines changed

scripts/scoreboard-m/index.js

Lines changed: 121 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,114 @@
11
// Script example for ScriptAPI
22
// Author: GamerFile <https://github.com/GamerFile>
33
// Project: https://github.com/JaylyDev/ScriptAPI
4-
54
import * as mc from '@minecraft/server'
65

76
//The Main ScoreboardM Object For Managing scoreboard
8-
export const ScoreboardM = {
9-
/**
10-
* This Is A Method For Setting An Objective Different displayName
11-
* @param {string} id - The Id Of Objective
12-
* @param {string} v - The Name To Be Displayed
13-
* @returns {ScoreboardM} - Returns The Main Object
14-
*/
15-
setObj : function(id, v) {
7+
let objOptions = [],list;
8+
9+
export const ScoreboardM = {
10+
/**
11+
* This Is A Method For Setting An Objective Different displayName
12+
* @param {string} id - The Id Of Objective
13+
* @param {string} v - The Name To Be Displayed
14+
* @returns {ScoreboardM} - Returns The Main Object
15+
*/
16+
setObj: function(id, v) {
1617
if (this.getObj(id)) {
17-
this.delObj(id)
18+
objOptions[0] = this.getDisplaySlot("sidebar") ? this.getDisplaySlot("sidebar") : undefined;
19+
20+
objOptions[1] = this.getDisplaySlot("list") ? this.getDisplaySlot("list") : undefined;
21+
22+
objOptions[2] = this.getDisplaySlot("belowName") ? this.getDisplaySlot("belowName") : undefined;
23+
24+
list = this.getList(id)
25+
this.delObj(id);
1826
this.setObj(id, v)
27+
1928
} else {
20-
mc.world.scoreboard.addObjective(id, v)
29+
mc.world.scoreboard.addObjective(id, v);
30+
for (let ind in objOptions) {
31+
let opts = objOptions[ind]
32+
if (opts?.objective.id == id) {
33+
list?.forEach(obj => {
34+
this.setScore(obj, id, obj.score)
35+
})
36+
switch (ind) {
37+
case "0":
38+
this.setDisplaySlot("sidebar", opts);
39+
break;
40+
case "1":
41+
this.setDisplaySlot("list", opts);
42+
break;
43+
case "2":
44+
this.setDisplaySlot("belowName", opts);
45+
break;
46+
}
47+
}
48+
}
2149
}
2250
return this;
2351
},
24-
/**
25-
* This method tells whether a objective exist or not
26-
* @param {string} id - The Id Of Objective
27-
* @returns {boolean} - Returns True Or False , If Objective Exists then
28-
*/
29-
hasObj : function(id) {
52+
/**
53+
* This method tells whether a objective exist or not
54+
* @param {string} id - The Id Of Objective
55+
* @returns {boolean} - Returns True Or False , If Objective Exists then
56+
*/
57+
hasObj: function(id) {
3058
if (this.getObj(id)) {
3159
return true
3260

3361
} else {
3462
return false
3563
}
3664
},
37-
/**
38-
* This Method For Getting Objective Object
39-
* @param {string} id - The Id Of Objective
40-
* @returns {Object} - Returns The Objective Object
41-
*/
42-
getObj : function(id) {
65+
/**
66+
* This Method For Getting Objective Object
67+
* @param {string} id - The Id Of Objective
68+
* @returns {mc.ScoreboardObjective} - Returns The Objective Object
69+
*/
70+
getObj: function(id) {
4371
if (mc.world.scoreboard.getObjective(id)) {
4472
return mc.world.scoreboard.getObjective(id)
4573
}
4674
},
47-
/**
48-
* This Is A Method For Creating An Objective With The Display Name
49-
* @param {string} id - The Id Of Objective
50-
* @param {string} v - The Name To Be Displayed
51-
* @returns {ScoreboardM} - Returns The Main Object
52-
*/
53-
newObj : function(id, v) {
75+
/**
76+
* This Is A Method For Creating An Objective With The Display Name
77+
* @param {string} id - The Id Of Objective
78+
* @param {string} v - The Name To Be Displayed
79+
* @returns {ScoreboardM} - Returns The Main Object
80+
*/
81+
newObj: function(id, v) {
5482
if (this.getObj(id)) {
5583
throw new Error("Objective Already exist. Error At newObj()")
5684
return
5785
}
5886
return this.setObj(id, v)
5987
},
60-
/**
61-
* This Is A Method For Setting Target's Score
62-
* @param {Object} target - The Target's Object To Set Score
63-
* @param {string} Objective - The Id Of Objective
64-
* @param {number} score - Score To Be Setted To Target
65-
* @returns {ScoreboardM} - Returns The Main Object
66-
* @throws {Error} - Throws Error When Objective Doesnt Exist
67-
*/
68-
setScore : function(target, Objective, score) {
88+
/**
89+
* This Is A Method For Setting Target's Score
90+
* @param {{name: string}} target - The Target's Object To Set Score
91+
* @param {string} Objective - The Id Of Objective
92+
* @param {number} score - Score To Be Setted To Target
93+
* @returns {ScoreboardM} - Returns The Main Object
94+
* @throws {Error} - Throws Error When Objective Doesnt Exist
95+
*/
96+
setScore: function(target, Objective, score) {
6997
if (this.getObj(Objective)) {
7098
mc.world.getDimension("overworld").runCommandAsync(`scoreboard players set ${target.name} "${Objective}" ${Number(score)}`)
7199
return this
72100
} else {
73101
throw new Error("Objective Doesnt exist . At setScore()")
74102
}
75103
},
76-
/**
77-
* This Is A Method For Getting Target's Score
78-
* @param {Object} target - The Target's Object To Get Score
79-
* @param {string} Objective - The Id Of Objective
80-
* @returns {number} - Returns The Target's Score
81-
* @throws {Error} - Throws Error When Objective Doesnt Exist
82-
*/
83-
getScore : function(target, Objective) {
104+
/**
105+
* This Is A Method For Getting Target's Score
106+
* @param {{name: string}} target - The Target's Object To Get Score
107+
* @param {string} Objective - The Id Of Objective
108+
* @returns {number} - Returns The Target's Score
109+
* @throws {Error} - Throws Error When Objective Doesnt Exist
110+
*/
111+
getScore: function(target, Objective) {
84112
if (this.getObj(Objective)) {
85113
var obj = mc.world.scoreboard.getObjective(Objective)
86114
var players = obj.getParticipants()
@@ -96,28 +124,42 @@ import * as mc from '@minecraft/server'
96124
throw new Error("Objective Doesn't exist. at getScore()")
97125
}
98126
},
99-
/**
100-
* This Is A Method For Adding Score To Target
101-
* @param {Object} target - The Target's Object To Add Score
102-
* @param {string} Objective - The Id Of Objective
103-
* @param {number} score - The Number To Add
104-
* @returns {ScoreboardM} - Returns The Main ScoreboardM Object
105-
* @throws {Error} - Throws Error When Objective Doesnt Exist
106-
*/
107-
addScore : function(target, Objective, score) {
127+
/**
128+
* This Is A Method For Adding Score To Target
129+
* @param {{name: string}} target - The Target's Object To Add Score
130+
* @param {string} Objective - The Id Of Objective
131+
* @returns {ScoreboardM} - Returns The Main ScoreboardM Object
132+
* @throws {Error} - Throws Error When Objective Doesnt Exist
133+
*/
134+
addScore: function(target, Objective, score) {
108135
if (this.getObj(Objective)) {
109136
mc.world.getDimension("overworld").runCommandAsync(`scoreboard players add ${target.name} "${Objective}" ${score}`)
110137
return this
111138
} else {
112139
throw new Error("Objective Doesn't exist. at addScore()")
113140
}
114141
},
115-
/**
116-
* This Method For Deleting Objective Object
117-
* @param {string} id - The Id Of Objective
118-
* @returns {Object} - Returns The Main ScoreboardM Object
119-
*/
120-
delObj : function(id) {
142+
/**
143+
* This Method Is For Removing a entity,fakeplayer or player name from given objective
144+
* @param {{name: string}} target - The Object Of The Player Or Fakeplayer
145+
* #param {string} Objective - Id Of The Objective
146+
* @returns {ScoreboardM} - Returns The Main ScoreboardM Object
147+
* @throws {Error} - Throws Error When Objective Doesnt Exist
148+
*/
149+
removeName: function(target,Objective) {
150+
if (this.hasObj(Objective)) {
151+
mc.world.getDimension("overworld").runCommandAsync(`scoreboard players reset ${target.name} "${Objective}"`)
152+
return this;
153+
} else {
154+
throw new Error("Objective Doesn't exist. at removeName()")
155+
}
156+
},
157+
/**
158+
* This Method For Deleting Objective Object
159+
* @param {string} id - The Id Of Objective
160+
* @returns {ScoreboardM} - Returns The Main ScoreboardM Object
161+
*/
162+
delObj: function(id) {
121163
if (this.hasObj(id)) {
122164
mc.world.scoreboard.removeObjective(id)
123165
return this;
@@ -129,7 +171,7 @@ import * as mc from '@minecraft/server'
129171
* @param {string} Objective - Id Of Objective To Get Names
130172
* @returns {String[] | undefined} - Contains All Names and also undefined it there isnt any of that score
131173
*/
132-
getNameByScore : function(score, Objective) {
174+
getNameByScore: function(score, Objective) {
133175
if (this.hasObj(Objective)) {
134176

135177
let obj = this.getObj(Objective)
@@ -157,12 +199,12 @@ import * as mc from '@minecraft/server'
157199
}
158200

159201
},
160-
/**
202+
/**
161203
* This Method Is To Get An Array Of Names And Scores Having. Could Work On Fakeplayers
162204
* @param {string} obj - Id Of Objective To Get Names And Scores
163-
* @returns {Object[] | undefined} - Contains All Names And Scores and also undefined it there isnt any of that score
205+
* @returns {{name: any,score: any}[] | undefined} - Contains All Names And Scores and also undefined it there isnt any of that score
164206
*/
165-
getList : function(obj) {
207+
getList: function(obj) {
166208

167209
if (this.hasObj(obj)) {
168210

@@ -190,38 +232,38 @@ import * as mc from '@minecraft/server'
190232
},
191233
/**
192234
* This Method Is To Getting Display Slot Options That Is Currently Displaying
193-
* @param {string} id - Id Of Slot To Get Options
194-
* @returns {Object} - This Returns Display Options
235+
* @param {'sidebar' | 'list' | 'belowName'} id - Id Of Slot To Get Options
236+
* @returns {mc.ScoreboardObjectiveDisplayOptions} - This Returns Display Options
195237
* @example
196238
* ScoreboardM.getDisplaySlot("sidebar")
197239
*/
198240
getDisplaySlot: function(id) {
199-
200-
return mc.world.scoreboard.getObjectiveAtDisplaySlot(id);
241+
242+
return mc.world.scoreboard.getObjectiveAtDisplaySlot(id);
201243
},
202244
/**
203245
* This Clears The Displaying Objective At Given Slot
204-
* @param {string} id - The Display Slot Id To Clear
246+
* @param {'sidebar'| 'list' | 'belowName'} id - The Display Slot Id To Clear
205247
* @returns {ScoreboardM} - The Main ScoreboardM Object
206248
* @example
207249
* ScoreboardM.clearDisplaySlot("sidebar")
208250
*/
209251
clearDisplaySlot: function(id) {
210-
211-
mc.world.scoreboard.clearObjectiveAtDisplaySlot(id);
212-
return this;
252+
253+
mc.world.scoreboard.clearObjectiveAtDisplaySlot(id);
254+
return this;
213255
},
214256
/**
215257
* This Sets/Displays Objective At Given Slots
216-
* @param {string} id - The Slot Id To Display Objective
217-
* @param {Object} opt - The scoreboard Options to display the Objective
258+
* @param {'sidebar'|'list'| 'belowName'} id - The Slot Id To Display Objective
259+
* @param {mc.ScoreboardObjectiveDisplayOptions} opt - The scoreboard Options to display the Objective
218260
* @returns {ScoreboardM} - The Main ScoreboardM Object
219261
* @example
220262
* ScoreboardM.setDisplaySlot("sidebar",{objective: ScoreboardM.getObj("money"),sortOrder: 0})
221263
*/
222-
setDisplaySlot: function(id,opt) {
223-
mc.world.scoreboard.setObjectiveAtDisplaySlot(id,opt);
224-
return this;
264+
setDisplaySlot: function(id, opt) {
265+
mc.world.scoreboard.setObjectiveAtDisplaySlot(id, opt);
266+
return this;
225267

226268
}
227269
}

0 commit comments

Comments
 (0)