1
1
// Script example for ScriptAPI
2
2
// Author: GamerFile <https://github.com/GamerFile>
3
3
// Project: https://github.com/JaylyDev/ScriptAPI
4
-
5
4
import * as mc from '@minecraft/server'
6
5
7
6
//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 ) {
16
17
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 ) ;
18
26
this . setObj ( id , v )
27
+
19
28
} 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
+ }
21
49
}
22
50
return this ;
23
51
} ,
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 ) {
30
58
if ( this . getObj ( id ) ) {
31
59
return true
32
60
33
61
} else {
34
62
return false
35
63
}
36
64
} ,
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 ) {
43
71
if ( mc . world . scoreboard . getObjective ( id ) ) {
44
72
return mc . world . scoreboard . getObjective ( id )
45
73
}
46
74
} ,
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 ) {
54
82
if ( this . getObj ( id ) ) {
55
83
throw new Error ( "Objective Already exist. Error At newObj()" )
56
84
return
57
85
}
58
86
return this . setObj ( id , v )
59
87
} ,
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 ) {
69
97
if ( this . getObj ( Objective ) ) {
70
98
mc . world . getDimension ( "overworld" ) . runCommandAsync ( `scoreboard players set ${ target . name } "${ Objective } " ${ Number ( score ) } ` )
71
99
return this
72
100
} else {
73
101
throw new Error ( "Objective Doesnt exist . At setScore()" )
74
102
}
75
103
} ,
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 ) {
84
112
if ( this . getObj ( Objective ) ) {
85
113
var obj = mc . world . scoreboard . getObjective ( Objective )
86
114
var players = obj . getParticipants ( )
@@ -96,28 +124,42 @@ import * as mc from '@minecraft/server'
96
124
throw new Error ( "Objective Doesn't exist. at getScore()" )
97
125
}
98
126
} ,
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 ) {
108
135
if ( this . getObj ( Objective ) ) {
109
136
mc . world . getDimension ( "overworld" ) . runCommandAsync ( `scoreboard players add ${ target . name } "${ Objective } " ${ score } ` )
110
137
return this
111
138
} else {
112
139
throw new Error ( "Objective Doesn't exist. at addScore()" )
113
140
}
114
141
} ,
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 ) {
121
163
if ( this . hasObj ( id ) ) {
122
164
mc . world . scoreboard . removeObjective ( id )
123
165
return this ;
@@ -129,7 +171,7 @@ import * as mc from '@minecraft/server'
129
171
* @param {string } Objective - Id Of Objective To Get Names
130
172
* @returns {String[] | undefined } - Contains All Names and also undefined it there isnt any of that score
131
173
*/
132
- getNameByScore : function ( score , Objective ) {
174
+ getNameByScore : function ( score , Objective ) {
133
175
if ( this . hasObj ( Objective ) ) {
134
176
135
177
let obj = this . getObj ( Objective )
@@ -157,12 +199,12 @@ import * as mc from '@minecraft/server'
157
199
}
158
200
159
201
} ,
160
- /**
202
+ /**
161
203
* This Method Is To Get An Array Of Names And Scores Having. Could Work On Fakeplayers
162
204
* @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
164
206
*/
165
- getList : function ( obj ) {
207
+ getList : function ( obj ) {
166
208
167
209
if ( this . hasObj ( obj ) ) {
168
210
@@ -190,38 +232,38 @@ import * as mc from '@minecraft/server'
190
232
} ,
191
233
/**
192
234
* 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
195
237
* @example
196
238
* ScoreboardM.getDisplaySlot("sidebar")
197
239
*/
198
240
getDisplaySlot : function ( id ) {
199
-
200
- return mc . world . scoreboard . getObjectiveAtDisplaySlot ( id ) ;
241
+
242
+ return mc . world . scoreboard . getObjectiveAtDisplaySlot ( id ) ;
201
243
} ,
202
244
/**
203
245
* 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
205
247
* @returns {ScoreboardM } - The Main ScoreboardM Object
206
248
* @example
207
249
* ScoreboardM.clearDisplaySlot("sidebar")
208
250
*/
209
251
clearDisplaySlot : function ( id ) {
210
-
211
- mc . world . scoreboard . clearObjectiveAtDisplaySlot ( id ) ;
212
- return this ;
252
+
253
+ mc . world . scoreboard . clearObjectiveAtDisplaySlot ( id ) ;
254
+ return this ;
213
255
} ,
214
256
/**
215
257
* 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
218
260
* @returns {ScoreboardM } - The Main ScoreboardM Object
219
261
* @example
220
262
* ScoreboardM.setDisplaySlot("sidebar",{objective: ScoreboardM.getObj("money"),sortOrder: 0})
221
263
*/
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 ;
225
267
226
268
}
227
269
}
0 commit comments