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 = / s e t ( .+ ) ( .+ ) / ;
32+ var removeRegex = / r e m o v e ( .+ ) / ;
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 = / .* r a n k - .+ / ;
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" ) ;
0 commit comments