|
| 1 | +// Script example for ScriptAPI |
| 2 | +// Author: GamerFile <https://github.com/GamerFile> |
| 3 | +// Project: https://github.com/JaylyDev/ScriptAPI |
| 4 | + |
| 5 | +import * as mc from '@minecraft/server' |
| 6 | + |
| 7 | +//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) { |
| 16 | + if (this.getObj(id)) { |
| 17 | + this.delObj(id) |
| 18 | + this.setObj(id, v) |
| 19 | + } else { |
| 20 | + mc.world.scoreboard.addObjective(id, v) |
| 21 | + } |
| 22 | + return this; |
| 23 | + }, |
| 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) { |
| 30 | + if (this.getObj(id)) { |
| 31 | + return true |
| 32 | + |
| 33 | + } else { |
| 34 | + return false |
| 35 | + } |
| 36 | + }, |
| 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) { |
| 43 | + if (mc.world.scoreboard.getObjective(id)) { |
| 44 | + return mc.world.scoreboard.getObjective(id) |
| 45 | + } |
| 46 | + }, |
| 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) { |
| 54 | + if (this.getObj(id)) { |
| 55 | + throw new Error("Objective Already exist. Error At newObj()") |
| 56 | + return |
| 57 | + } |
| 58 | + return this.setObj(id, v) |
| 59 | + }, |
| 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) { |
| 69 | + if (this.getObj(Objective)) { |
| 70 | + mc.world.getDimension("overworld").runCommandAsync(`scoreboard players set ${target.name} "${Objective}" ${Number(score)}`) |
| 71 | + return this |
| 72 | + } else { |
| 73 | + throw new Error("Objective Doesnt exist . At setScore()") |
| 74 | + } |
| 75 | + }, |
| 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) { |
| 84 | + if (this.getObj(Objective)) { |
| 85 | + var obj = mc.world.scoreboard.getObjective(Objective) |
| 86 | + var players = obj.getParticipants() |
| 87 | + for (var i = 0; i < players.length; i++) { |
| 88 | + var plr = players[i] |
| 89 | + var dname = plr.displayName |
| 90 | + if (dname == target.name) { |
| 91 | + return obj.getScore(plr) |
| 92 | + } |
| 93 | + } |
| 94 | + return 0 |
| 95 | + } else { |
| 96 | + throw new Error("Objective Doesn't exist. at getScore()") |
| 97 | + } |
| 98 | + }, |
| 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) { |
| 108 | + if (this.getObj(Objective)) { |
| 109 | + mc.world.getDimension("overworld").runCommandAsync(`scoreboard players add ${target.name} "${Objective}" ${score}`) |
| 110 | + return this |
| 111 | + } else { |
| 112 | + throw new Error("Objective Doesn't exist. at addScore()") |
| 113 | + } |
| 114 | + }, |
| 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) { |
| 121 | + if (this.hasObj(id)) { |
| 122 | + mc.world.scoreboard.removeObjective(id) |
| 123 | + return this; |
| 124 | + } |
| 125 | + }, |
| 126 | + /** |
| 127 | + * This Method Is To Get An Array Of Names Having Given Score. Could Work On Fakeplayers , Players And Entities |
| 128 | + * @param {number} score - Score To Get Names |
| 129 | + * @param {string} Objective - Id Of Objective To Get Names |
| 130 | + * @returns {String[] | undefined} - Contains All Names and also undefined it there isnt any of that score |
| 131 | + */ |
| 132 | + getNameByScore : function(score, Objective) { |
| 133 | + if (this.hasObj(Objective)) { |
| 134 | + |
| 135 | + let obj = this.getObj(Objective) |
| 136 | + |
| 137 | + let list = []; |
| 138 | + |
| 139 | + for (let plr of obj.getParticipants()) { |
| 140 | + |
| 141 | + let score1 = obj.getScore(plr); |
| 142 | + |
| 143 | + if (score == score1) { |
| 144 | + |
| 145 | + list.push(plr.displayName); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + return list.length == 0 ? undefined : list |
| 152 | + |
| 153 | + } else { |
| 154 | + |
| 155 | + return undefined; |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + }, |
| 160 | + /** |
| 161 | + * This Method Is To Get An Array Of Names And Scores Having. Could Work On Fakeplayers |
| 162 | + * @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 |
| 164 | + */ |
| 165 | + getList : function(obj) { |
| 166 | + |
| 167 | + if (this.hasObj(obj)) { |
| 168 | + |
| 169 | + let Obj = this.getObj(obj) |
| 170 | + |
| 171 | + let list = []; |
| 172 | + |
| 173 | + for (let plr of Obj.getParticipants()) { |
| 174 | + |
| 175 | + let score = Obj.getScore(plr); |
| 176 | + |
| 177 | + let name = plr.displayName; |
| 178 | + |
| 179 | + list.push({ |
| 180 | + name: name, |
| 181 | + score: score |
| 182 | + }) |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + return list.length == 0 ? undefined : list; |
| 187 | + |
| 188 | + } else return undefined; |
| 189 | + |
| 190 | + }, |
| 191 | + /** |
| 192 | + * 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 |
| 195 | + * @example |
| 196 | + * ScoreboardM.getDisplaySlot("sidebar") |
| 197 | + */ |
| 198 | + getDisplaySlot: function(id) { |
| 199 | + |
| 200 | + return mc.world.scoreboard.getObjectiveAtDisplaySlot(id); |
| 201 | + }, |
| 202 | + /** |
| 203 | + * This Clears The Displaying Objective At Given Slot |
| 204 | + * @param {string} id - The Display Slot Id To Clear |
| 205 | + * @returns {ScoreboardM} - The Main ScoreboardM Object |
| 206 | + * @example |
| 207 | + * ScoreboardM.clearDisplaySlot("sidebar") |
| 208 | + */ |
| 209 | + clearDisplaySlot: function(id) { |
| 210 | + |
| 211 | + mc.world.scoreboard.clearObjectiveAtDisplaySlot(id); |
| 212 | + return this; |
| 213 | + }, |
| 214 | + /** |
| 215 | + * 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 |
| 218 | + * @returns {ScoreboardM} - The Main ScoreboardM Object |
| 219 | + * @example |
| 220 | + * ScoreboardM.setDisplaySlot("sidebar",{objective: ScoreboardM.getObj("money"),sortOrder: 0}) |
| 221 | + */ |
| 222 | + setDisplaySlot: function(id,opt) { |
| 223 | + mc.world.scoreboard.setObjectiveAtDisplaySlot(id,opt); |
| 224 | + return this; |
| 225 | + |
| 226 | + } |
| 227 | +} |
0 commit comments