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

Commit 794ef5f

Browse files
committed
[ranks] Initial commit
Basic functionality only.
1 parent 46519bf commit 794ef5f

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

scripts/management/ranks.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
Rank system, with chat command. Commands editable below, in section VARIABLES.
3+
4+
Regex is used for any subCommands.
5+
6+
v0.1 - alpha 2
7+
*/
8+
9+
import config from './ranksConfig.js';
10+
11+
import {
12+
getPlayerByNAME,
13+
onPlayerInitialized
14+
} from "ez:player";
15+
16+
import {
17+
onChat
18+
} from "ez:chat";
19+
20+
const system = server.registerSystem(0, 0);
21+
22+
//ummm.. this works, idk why. but it doesn't without.
23+
//leave it alone
24+
const ranksConfig = config;
25+
26+
//===============
27+
//== VARIABLES ==
28+
//===============
29+
30+
var baseCommand = ".rank";
31+
var setRegex = /set (.+) (.+)/;
32+
var removeRegex = /remove (.+)/;
33+
var infoMessage = "Rank system.\nMade by Weissnix4711\n\nDo .rank set <player name> <rank name>\n\nEdit the config to include your own ranks.";
34+
35+
//===================
36+
//== CHAT COMMANDS ==
37+
//===================
38+
39+
onChat((cmdObject) => {
40+
try {
41+
//if does not begin with baseCommand, return
42+
if (!cmdObject.content.startsWith(baseCommand)) {
43+
return;
44+
}
45+
//if exactly baseCommand, show info
46+
if (cmdObject.content === baseCommand) {
47+
system.executeCommand(`tellraw @a[name="${cmdObject.sender}"] {"rawtext":[{"text":"${infoMessage}"}]}`, () => {})
48+
return;
49+
}
50+
51+
let subRegex = new RegExp(`${baseCommand} (.+)`);
52+
let match = cmdObject.content.match(subRegex);
53+
let subCommand = match[1];
54+
55+
//Set sub-command
56+
if (setRegex.test(subCommand)) {
57+
//set arguments
58+
let args = subCommand.match(setRegex);
59+
let playerName = args[1];
60+
let rank = args[2];
61+
//run function
62+
setRank(playerName, rank);
63+
}
64+
//Remove sub-command
65+
if (removeRegex.test(subCommand)) {
66+
let playerName = subCommand.match(removeRegex)[1];
67+
//run function remove rank
68+
removeRank(playerName);
69+
}
70+
71+
} catch(err) {
72+
console.error(err)
73+
}
74+
});
75+
76+
//===============
77+
//== FUNCTIONS ==
78+
//===============
79+
80+
//get rank
81+
function rank(callback) {
82+
system.executeCommand(`tag "${player.name}" list`, (result) => {
83+
let message = result.data.statusMessage;
84+
let rankRegex= /.*rank-.+/;
85+
let tags = message.substring(message.indexOf(":")+1,message.length).trim().split(", ");
86+
let rank = tags.find(function (value) {if (rankRegex.test(value)) {return value}});
87+
callback(rank)
88+
});
89+
}
90+
91+
//SET RANK
92+
export function setRank(playerName, rank) {
93+
try {
94+
console.log(`Ranks: Try to set ${playerName}'s rank to ${rank}.`)
95+
96+
let customName = eval(`ranksConfig.${rank}.prefix`);
97+
console.log("ree")
98+
99+
//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}"`, () => {});
102+
103+
//particles
104+
for (let i = 0; i < eval(`ranksConfig.${rank}.particles`).length; i++) {
105+
let p = eval(`ranksConfig.${rank}.particles[i]`);
106+
system.executeCommand(`execute @a[name="${playerName}"] ~ ~ ~ tag @s add "p${p}"`, () => {});
107+
}
108+
109+
console.log("Ranks: Done!")
110+
} catch(err) {
111+
console.error(err);
112+
}
113+
}
114+
115+
//REMOVE RANK
116+
export function removeRank(playerName) {
117+
try {
118+
console.log(`Ranks: Try to remove rank from ${playerName}.`)
119+
120+
//find current rank
121+
let currentRank = rank(function(result) {
122+
return result;
123+
});
124+
125+
//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!")
135+
} catch(err) {
136+
console.error(err);
137+
}
138+
}
139+
140+
console.log("ranks.js loaded");

scripts/management/ranksConfig.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
This is the configuration file for ranks.
3+
4+
### PARTICLES
5+
Note: The ranks script does NOT give the player any particle effects.
6+
The "particles" array in here is only a list of tags given to a player when they recieve that rank.
7+
This is because I use another addon for the particle effects themselves.
8+
9+
If you do not wish to use particles, leave the array empty.
10+
11+
### PREFIX
12+
Prefix is meant for use with ElementZero's essentials mod. You must enable custom-name under essentials' config.
13+
14+
### DO NOT IMPORT THIS FILE!
15+
It must only be imported once by the ranks script.
16+
It is not necessary to import this config file from index.js
17+
*/
18+
19+
export default {
20+
"rankOne" : {
21+
"prefix": "[One] ",
22+
"particles": [
23+
"52"
24+
]
25+
},
26+
"rankTwo" : {
27+
"prefix": "[Two]",
28+
"particles": [
29+
"76"
30+
]
31+
},
32+
"rankThree" : {
33+
"prefix": "[Three] ",
34+
"particles": [
35+
"76"
36+
]
37+
},
38+
"vip" : {
39+
"prefix": "[VIP] ",
40+
"particles": [
41+
"40",
42+
"82",
43+
"65"
44+
]
45+
}
46+
};

0 commit comments

Comments
 (0)