3030using Essentials . Api . Module ;
3131using Essentials . Common ;
3232
33- namespace Essentials . LogCommands
33+ namespace Essentials . Modules . LogCommands
3434{
3535 [ ModuleInfo (
3636 Name = "LogCommands" ,
3737 Author = "Leonardosc" ,
3838 Version = "1.0.0" ,
3939 Flags = ModuleFlags . AUTO_REGISTER_EVENTS
4040 ) ]
41- public class TestModule : EssModule
41+ public class LogCommands : EssModule
4242 {
4343 public static Dictionary < ulong , List < string > > Cache { get ; } = new Dictionary < ulong , List < string > > ( ) ;
44- public static TestModule Instance { get ; private set ; }
44+ public static LogCommands Instance { get ; private set ; }
4545 private int _checks ;
46- private char DirSep = Path . DirectorySeparatorChar ;
46+
4747 public string LogFolder
4848 {
4949 get
5050 {
51- var path = $ " { Folder } { DirSep } logs{ DirSep } " ;
51+ var path = Path . Combine ( Folder , " logs" ) ;
5252 if ( ! Directory . Exists ( path ) )
53- {
5453 Directory . CreateDirectory ( path ) ;
55- }
5654 return path ;
5755 }
5856 }
5957
60- public override void OnLoad ( ) => Instance = this ;
61- public override void OnUnload ( ) { }
58+ public override void OnLoad ( )
59+ {
60+ Logger . LogInfo ( "Enabled!" ) ;
61+ Instance = this ;
62+ }
63+
64+ public override void OnUnload ( ) {
65+ Logger . LogInfo ( "Disabled!" ) ;
66+ }
6267
6368 [ SubscribeEvent ( EventType . ESSENTIALS_COMMAND_POS_EXECUTED ) ]
6469 private void OnCommandExecuted ( CommandPosExecuteEvent e )
6570 {
6671 if ( e . Source . IsConsole ) return ;
72+
73+ // TODO: Improve saving...
6774 if ( Cache . Count >= 15 || ( _checks > 20 && CheckValues ( ) ) )
6875 {
6976 SaveCache ( ) ;
@@ -73,9 +80,10 @@ private void OnCommandExecuted( CommandPosExecuteEvent e )
7380 {
7481 _checks ++ ;
7582 }
83+
7684 var playerId = ulong . Parse ( e . Source . Id ) ;
7785 var sb = new StringBuilder ( ) ;
78-
86+
7987 sb . Append ( $ "[{ DateTime . Now } ] " )
8088 . Append ( $ "[{ e . Result . Type } " )
8189 . Append ( string . IsNullOrEmpty ( e . Result . Message ) ? "] " : $ "({ e . Result . Message } )] " )
@@ -84,14 +92,11 @@ private void OnCommandExecuted( CommandPosExecuteEvent e )
8492 . Append ( $ "\" /{ e . Command . Name } " )
8593 . Append ( e . Arguments . IsEmpty ? "\" " : $ " { e . Arguments . Join ( 0 ) } \" " ) ;
8694 var text = sb . ToString ( ) ;
95+
8796 if ( Cache . ContainsKey ( playerId ) )
88- {
8997 Cache [ playerId ] . Add ( text ) ;
90- }
9198 else
92- {
9399 Cache . Add ( playerId , new List < string > { text } ) ;
94- }
95100 }
96101
97102 private void SaveCache ( )
@@ -102,17 +107,13 @@ private void SaveCache()
102107 new Thread ( ( ) => {
103108 var text = new StringBuilder ( ) ;
104109 contents . ForEach ( ( k ) => {
105- var playerFolder = $ " { LogFolder } { k . Key } { DirSep } " ;
110+ var playerFolder = Path . Combine ( LogFolder , k . Key . ToString ( ) ) ;
106111 var commandsFile = $ "{ playerFolder } commands.txt";
107112
108113 if ( ! Directory . Exists ( playerFolder ) )
109- {
110114 Directory . CreateDirectory ( playerFolder ) ;
111- }
112115 if ( ! File . Exists ( commandsFile ) )
113- {
114116 File . Create ( commandsFile ) . Close ( ) ;
115- }
116117
117118 k . Value . ForEach ( t => text . Append ( t ) . Append ( Environment . NewLine ) ) ;
118119 File . AppendAllText ( commandsFile , text . ToString ( ) ) ;
0 commit comments