1- import { world } from ' @minecraft/server' ;
1+ import { world , system } from " @minecraft/server" ;
22
33class CommandSystem {
4- static Prefix = "!" ;
5- static Commands = { } ;
4+ static Prefix = "!" ;
5+ static Commands = { } ;
66
7- static RegisterCommand ( name , callback , paramTypes , opOnly = false ) {
8- this . Commands [ name ] = {
9- callback : callback ,
10- paramTypes : paramTypes ,
11- opOnly : opOnly
12- } ;
13- }
14-
15- static executeCommand ( input , source , event ) {
16- if ( input . startsWith ( CommandSystem . Prefix ) ) {
17- event . cancel = true ;
18- const text = input . replace ( this . Prefix , "" ) ;
19- const parts = text . split ( " " ) ;
20- const commandName = parts [ 0 ] ;
21- const args = parts . slice ( 1 ) ;
7+ static RegisterCommand ( name , callback , paramTypes , opOnly = false ) {
8+ this . Commands [ name ] = {
9+ callback : callback ,
10+ paramTypes : paramTypes ,
11+ opOnly : opOnly ,
12+ } ;
13+ }
2214
23- if ( this . Commands [ commandName ] ) {
24- const command = this . Commands [ commandName ] ;
25- if ( command . opOnly && ! source . isOp ( ) ) {
26- source . sendMessage ( "§cThis command can only be used by operators!" ) ;
27- return ;
28- }
29- const typedArgs = { } ;
15+ static executeCommand ( input , source , event ) {
16+ if ( input . startsWith ( CommandSystem . Prefix ) ) {
17+ event . cancel = true ;
18+ system . run ( ( ) => {
19+ const text = input . replace ( this . Prefix , "" ) ;
20+ const parts = text . split ( " " ) ;
21+ const commandName = parts [ 0 ] ;
22+ const args = parts . slice ( 1 ) ;
3023
31- typedArgs [ "source" ] = source ;
24+ if ( this . Commands [ commandName ] ) {
25+ const command = this . Commands [ commandName ] ;
26+ if ( command . opOnly && ! source . isOp ( ) ) {
27+ source . sendMessage ( "§cThis command can only be used by operators!" ) ;
28+ return ;
29+ }
30+ const typedArgs = { } ;
3231
33- let i = 0 ;
34- for ( const [ paramName , type ] of Object . entries ( command . paramTypes ) ) {
35- let value = args [ i ] ;
32+ typedArgs [ "source" ] = source ;
3633
37- switch ( type ) {
38- case "string" :
39- if ( value == undefined ) {
40- source . sendMessage ( `§cInvalid Paramater Input: §e[${ paramName } ]§r. Received §e[${ value } ]§r, Expected §e[${ type } ]§r` ) ;
41- return ;
42- }
43- break ;
44- case "integer" : value = parseInt ( value ) ;
45- if ( isNaN ( value ) ) {
46- source . sendMessage ( `§cInvalid Paramater Input: §e[${ paramName } ]§r. Received §e[${ value } ]§r, Expected §e[${ type } ]§r` ) ;
47- return ;
48- }
49- break ;
50- case "float" : value = parseFloat ( value ) ;
51- if ( isNaN ( value ) ) {
52- source . sendMessage ( `§cInvalid Paramater Input: §e[${ paramName } ]§r. Received §e[${ value } ]§r, Expected §e[${ type } ]§r` ) ;
53- return ;
54- }
55- break ;
56- default : source . sendMessage ( `§cInvalid parameter type: ${ type } for param ${ paramName } ` ) ;
57- return ;
58- }
34+ let i = 0 ;
35+ for ( const [ paramName , type ] of Object . entries ( command . paramTypes ) ) {
36+ let value = args [ i ] ;
5937
60- typedArgs [ paramName ] = value ;
61- i ++ ;
38+ switch ( type ) {
39+ case "string" :
40+ if ( value == undefined ) {
41+ source . sendMessage (
42+ `§cInvalid Paramater Input: §e[${ paramName } ]§r. Received §e[${ value } ]§r, Expected §e[${ type } ]§r`
43+ ) ;
44+ return ;
6245 }
63- command . callback . call ( null , typedArgs ) ;
64- } else {
65- source . sendMessage ( `§cUnkown command: ${ commandName } ` ) ;
46+ break ;
47+ case "integer" :
48+ value = parseInt ( value ) ;
49+ if ( isNaN ( value ) ) {
50+ source . sendMessage (
51+ `§cInvalid Paramater Input: §e[${ paramName } ]§r. Received §e[${ value } ]§r, Expected §e[${ type } ]§r`
52+ ) ;
53+ return ;
54+ }
55+ break ;
56+ case "float" :
57+ value = parseFloat ( value ) ;
58+ if ( isNaN ( value ) ) {
59+ source . sendMessage (
60+ `§cInvalid Paramater Input: §e[${ paramName } ]§r. Received §e[${ value } ]§r, Expected §e[${ type } ]§r`
61+ ) ;
62+ return ;
63+ }
64+ break ;
65+ default :
66+ source . sendMessage (
67+ `§cInvalid parameter type: ${ type } for param ${ paramName } `
68+ ) ;
69+ return ;
6670 }
71+
72+ typedArgs [ paramName ] = value ;
73+ i ++ ;
74+ }
75+ command . callback . call ( null , typedArgs ) ;
76+ } else {
77+ source . sendMessage ( `§cUnkown command: ${ commandName } ` ) ;
6778 }
79+ } ) ;
6880 }
81+ }
6982}
7083
71- world . events . beforeChat . subscribe ( ev => {
72- CommandSystem . executeCommand ( ev . message , ev . sender , ev ) ;
73- } )
84+ world . beforeEvents . chatSend . subscribe ( ( ev ) => {
85+ CommandSystem . executeCommand ( ev . message , ev . sender , ev ) ;
86+ } ) ;
7487
75- export { CommandSystem }
88+ export { CommandSystem } ;
0 commit comments