1+ using System ;
2+ using System . Linq ;
3+ using AltV . Net . Elements . Entities ;
4+ using AltV . Net . Native ;
5+
6+ namespace AltV . Net . Chat
7+ {
8+ public class Chat : Resource
9+ {
10+ private static Action < string , Action < IPlayer , string , string [ ] > > registerCmd ;
11+
12+ public override void OnStart ( )
13+ {
14+ Alt . On < IPlayer , string > ( "chatmessage" , OnChatMessage , OnChatMessageParser ) ;
15+
16+ registerCmd = delegate ( string s , Action < IPlayer , string , string [ ] > action )
17+ {
18+ CommandHandlers . Add ( s , action ) ;
19+ } ;
20+
21+ Alt . Export < string , Action < IPlayer , string , string [ ] > > ( "registerCmd" , CommandHandlers . Add ) ;
22+ Alt . Export ( "broadcast" , delegate ( string message ) { ChatUtils . SendBroadcastChatMessage ( message ) ; } ) ;
23+ Alt . Export ( "send" , delegate ( IPlayer player , string message ) { player . SendChatMessage ( message ) ; } ) ;
24+ }
25+
26+ public override void OnStop ( )
27+ {
28+ }
29+
30+ private void OnChatMessage ( IPlayer player , string message )
31+ {
32+ if ( string . IsNullOrEmpty ( message ) ) return ;
33+ if ( message . StartsWith ( "/" ) )
34+ {
35+ message = message . Trim ( ) . Remove ( 0 , 1 ) ;
36+ if ( message . Length > 0 )
37+ {
38+ Alt . Log ( "[chat:cmd] " + player . Name + ": /" + message ) ;
39+ var args = message . Split ( ' ' ) ;
40+ var cmd = args [ 0 ] ;
41+ CommandHandlers . InvokeCmd ( player , cmd , args . Skip ( 1 ) . ToArray ( ) ) ;
42+ }
43+ }
44+ else
45+ {
46+ message = message . Trim ( ) ;
47+ if ( message . Length > 0 )
48+ {
49+ Alt . Log ( "[chat:msg] " + player . Name + ": " + message ) ;
50+
51+ player . Emit ( "chatmessage" , player . Name , message ) ;
52+ }
53+ }
54+ }
55+
56+ private void OnChatMessageParser ( IPlayer player , ref MValueArray mValueArray ,
57+ Action < IPlayer , string > func )
58+ {
59+ if ( mValueArray . Size != 1 ) return ;
60+ var reader = mValueArray . Reader ( ) ;
61+ if ( ! reader . GetNext ( out string message ) ) return ;
62+ /*if (!reader.GetNext(out MValueArray mValueArgs)) return;
63+ var argsCount = (int) mValueArgs.Size;
64+ var args = new string[argsCount];
65+ var argsReader = mValueArgs.Reader();
66+ for (var i = 0; i < argsCount; i++)
67+ {
68+ if (!argsReader.GetNext(out string currArg)) return;
69+ args[i] = currArg;
70+ }*/
71+
72+ func ( player , message ) ;
73+ }
74+
75+ ~ Chat ( )
76+ {
77+ Console . WriteLine ( "chat deinit" ) ;
78+ }
79+ }
80+ }
0 commit comments