Skip to content

Commit 27e6309

Browse files
GamerFilebot174
andauthored
ScoreboardM Project (#163)
* ScoreboardM Project * Update [fixed err] * Update index.js * fix * verbose * no capital letters --------- Co-authored-by: bot174 <[email protected]>
1 parent 6c08d5a commit 27e6309

5 files changed

Lines changed: 238 additions & 6 deletions

File tree

.github/workflows/cl.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ jobs:
2727
- name: Build tsc
2828
run: tsc --build --verbose
2929

30-
- run: git pull
31-
3230
- name: Commit Changes
3331
if: ${{ github.event_name == 'schedule' }}
3432
uses: EndBug/add-and-commit@v9
3533
with:
3634
add: ./scripts
3735
message: 'Upload transpiled JS files.'
3836
default_author: github_actions
37+
pull: '-v'
3938

4039
format-check:
4140
name: 'Format Check'
@@ -53,8 +52,8 @@ jobs:
5352

5453
- name: Format Check
5554
run: node ./tools/index.js
56-
57-
- run: git pull
55+
env:
56+
event_name: ${{ github.event_name }}
5857

5958
- name: Commit Changes
6059
if: ${{ github.event_name == 'schedule' }}
@@ -63,3 +62,4 @@ jobs:
6362
add: ./scripts
6463
message: 'Upload generated README files.'
6564
default_author: github_actions
65+
pull: '-v'

scripts/scoreboard-m/index.js

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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+
}

tools/headerChecks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function checkScripts () {
4444
jsEmittedFileLines.splice(0, 0, ...header);
4545

4646
fs.writeFileSync(fullpath, jsEmittedFileLines.join('\n'));
47+
hasError = false;
4748
};
4849
};
4950
hasError ? packagesHaveError++ : null;
@@ -65,7 +66,8 @@ export function execute () {
6566
const results = checkScripts();
6667
const statusCode = results.packagesHaveError > 0 ? 1 : 0;
6768

68-
if (statusCode > 0) printMessages(results.errorMessages, results.packagesHaveError);
69+
printMessages(results.errorMessages, results.packagesHaveError);
70+
console.log('Exit task with code', statusCode);
6971

7072
return statusCode;
7173
};

tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function runTasks (tasks: Task[]): number {
2222
taskStatus.push(statusCode);
2323
};
2424

25+
console.log(taskStatus.map((status, index) => `Task: ${tasks[index].message}, Status: ${status}`));
26+
2527
if (taskStatus.length <= 0) return 1;
2628
else if (taskStatus.filter(status => status !== 0).length > 0) return 1;
2729
else return 0;

tools/packageReadme.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export function execute (): 0 | 1 {
7272
}
7373

7474
// attempt to commit
75-
if (scriptsChanged.length > 0) console.warn(`Add README to ${scriptsChanged.length} packages, please check artifacts for generated files.`);
75+
if (scriptsChanged.length > 0 && process.env.event_name === 'schedule') console.warn(`Add README to ${scriptsChanged.length} packages.`);
76+
else if (scriptsChanged.length > 0) console.warn(`There are ${scriptsChanged.length} packages don't have README. Add a README to your package so that users know how to get started.`);
7677
else console.log("All script packages have a README file.");
7778

7879
return 0;

0 commit comments

Comments
 (0)