Skip to content
This repository was archived by the owner on May 1, 2021. It is now read-only.

Commit 1ff5d02

Browse files
committed
[ranks] v1 - More features + bug fixes
Only people with staff tag can use the commands. Commands are easily changeable, regex is used to filter them.
1 parent 794ef5f commit 1ff5d02

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

scripts/management/ranks.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Rank system, with chat command. Commands editable below, in section VARIABLES.
33
44
Regex is used for any subCommands.
55
6-
v0.1 - alpha 2
6+
Check ranksConfig.js for configuration details.
7+
8+
v1
79
*/
810

911
import config from './ranksConfig.js';
@@ -42,6 +44,7 @@ onChat((cmdObject) => {
4244
if (!cmdObject.content.startsWith(baseCommand)) {
4345
return;
4446
}
47+
4548
//if exactly baseCommand, show info
4649
if (cmdObject.content === baseCommand) {
4750
system.executeCommand(`tellraw @a[name="${cmdObject.sender}"] {"rawtext":[{"text":"${infoMessage}"}]}`, () => {})
@@ -59,13 +62,13 @@ onChat((cmdObject) => {
5962
let playerName = args[1];
6063
let rank = args[2];
6164
//run function
62-
setRank(playerName, rank);
65+
setRank(cmdObject.sender, playerName, rank);
6366
}
6467
//Remove sub-command
6568
if (removeRegex.test(subCommand)) {
6669
let playerName = subCommand.match(removeRegex)[1];
6770
//run function remove rank
68-
removeRank(playerName);
71+
removeRank(cmdObject.sender, playerName);
6972
}
7073

7174
} catch(err) {
@@ -78,32 +81,32 @@ onChat((cmdObject) => {
7881
//===============
7982

8083
//get rank
81-
function rank(callback) {
82-
system.executeCommand(`tag "${player.name}" list`, (result) => {
84+
function rank(playerName, callback) {
85+
system.executeCommand(`tag "${playerName}" list`, (result) => {
8386
let message = result.data.statusMessage;
8487
let rankRegex= /.*rank-.+/;
8588
let tags = message.substring(message.indexOf(":")+1,message.length).trim().split(", ");
86-
let rank = tags.find(function (value) {if (rankRegex.test(value)) {return value}});
89+
let uncleaned = tags.find(function (value) {if (rankRegex.test(value)) {return value}});
90+
let rank = uncleaned.match(/\u00A7a(.+)\u00A7r/)[1];
8791
callback(rank)
8892
});
8993
}
9094

9195
//SET RANK
92-
export function setRank(playerName, rank) {
96+
export function setRank(sender, playerName, rank) {
9397
try {
9498
console.log(`Ranks: Try to set ${playerName}'s rank to ${rank}.`)
9599

96100
let customName = eval(`ranksConfig.${rank}.prefix`);
97-
console.log("ree")
98101

99102
//set prefix and level tag
100-
system.executeCommand(`execute @a[name="${playerName}"] ~ ~ ~ custom-name set prefix @s "${customName}"`, () => {});
101-
system.executeCommand(`execute @a[name="${playerName}"] ~ ~ ~ tag @s add "rank-${rank}"`, () => {});
103+
system.executeCommand(`execute @a[name="${sender}",tag=staff] ~ ~ ~ execute @a[name="${playerName}"] ~ ~ ~ custom-name set prefix @s "${customName}"`, () => {});
104+
system.executeCommand(`execute @a[name="${sender}",tag=staff] ~ ~ ~ execute @a[name="${playerName}"] ~ ~ ~ tag @s add "rank-${rank}"`, () => {});
102105

103106
//particles
104107
for (let i = 0; i < eval(`ranksConfig.${rank}.particles`).length; i++) {
105108
let p = eval(`ranksConfig.${rank}.particles[i]`);
106-
system.executeCommand(`execute @a[name="${playerName}"] ~ ~ ~ tag @s add "p${p}"`, () => {});
109+
system.executeCommand(`execute @a[name="${sender}",tag=staff] ~ ~ ~ execute @a[name="${playerName}"] ~ ~ ~ tag @s add "p${p}"`, () => {});
107110
}
108111

109112
console.log("Ranks: Done!")
@@ -113,25 +116,27 @@ export function setRank(playerName, rank) {
113116
}
114117

115118
//REMOVE RANK
116-
export function removeRank(playerName) {
119+
export function removeRank(sender, playerName) {
117120
try {
118121
console.log(`Ranks: Try to remove rank from ${playerName}.`)
119122

120-
//find current rank
121-
let currentRank = rank(function(result) {
122-
return result;
123-
});
124-
125123
//remove custom name
126-
system.executeCommand(`execute @a[name="${playerName}"] ~ ~ ~ custom-name clear @s""`, () => {});
127-
128-
//remove current rank
129-
system.executeCommand(`execute @a[name="${playerName}"] ~ ~ ~ tag @s remove "${currentRank}"`, () => {});
130-
131-
//remove particles from current rank
132-
system.executeCommand(`execute @a[name="${playerName}"] ~ ~ ~ tag @s remove "${currentRank}"`, () => {});
133-
134-
console.log("Ranks: Done!")
124+
system.executeCommand(`execute @a[name="${sender}",tag=staff] ~ ~ ~ execute @a[name="${playerName}"] ~ ~ ~ custom-name clear @s`, (blah) => {console.log(JSON.stringify(blah))});
125+
126+
rank(playerName, function(rank) {
127+
//remove current rank
128+
system.executeCommand(`execute @a[name="${sender}",tag=staff] ~ ~ ~ execute @a[name="${playerName}"] ~ ~ ~ tag @s remove "${rank}"`, () => {});
129+
//remove particles from current rank
130+
let pureRankName = rank.match(/rank-(.+)/)[1];
131+
let particleTagsArray = eval(`ranksConfig.${pureRankName}.particles`);
132+
for (let i = 0; i < particleTagsArray.length; i++) {
133+
let p = eval(`ranksConfig.${pureRankName}.particles`)[i];
134+
let q = `p${p}`;
135+
system.executeCommand(`execute @a[name="${sender}",tag=staff] ~ ~ ~ execute @a[name="${playerName}"] ~ ~ ~ tag @s remove "${q}"`, () => {});
136+
}
137+
138+
console.log("Ranks: Done!")
139+
});
135140
} catch(err) {
136141
console.error(err);
137142
}

0 commit comments

Comments
 (0)