@@ -57,11 +57,80 @@ public bool RequiresPermissions(CCSPlayerController player, Permission permissio
5757 return true ;
5858 }
5959 }
60- public bool OnCooldown ( CCSPlayerController player , Commands cmd )
60+
61+ /// <summary>
62+ /// Checks if the command is on cooldown
63+ /// </summary>
64+ /// <param name="player"></param>
65+ /// <param name="cmd"></param>
66+ /// <returns></returns>
67+ public bool IsCommandOnCooldown ( CCSPlayerController player , Commands cmd )
6168 {
62-
63-
69+ // Check global cooldown
70+ if ( IsCommandOnCooldownWithCondition ( x => x . IsGlobal == true && x . CommandID == cmd . ID , player , cmd ) )
71+ return true ;
72+
73+ // Check player cooldown
74+ if ( IsCommandOnCooldownWithCondition ( x => x . PlayerID == player . UserId && x . CommandID == cmd . ID , player , cmd ) )
75+ return true ;
76+
77+ return false ;
78+ }
79+
80+ private bool IsCommandOnCooldownWithCondition ( Func < CooldownTimer , bool > predicate , CCSPlayerController player , Commands cmd )
81+ {
82+ int index = PluginGlobals . CooldownTimer . FindIndex ( x => predicate ( x ) && x . CooldownTime > DateTime . Now ) ;
83+
84+ if ( index != - 1 )
85+ {
86+ string timeleft = PluginGlobals . CooldownTimer [ index ] . CooldownTime . Subtract ( DateTime . Now ) . Seconds . ToString ( ) ;
87+ player . PrintToChat ( $ "{ PluginGlobals . Config . Prefix } { cmd . Cooldown . CooldownMessage . Replace ( "{TIME}" , timeleft )
88+ ?? $ "This command is for { timeleft } seconds on cooldown"} " ) ;
89+
90+ return true ;
91+ }
92+
93+ return false ;
6494 }
95+
96+ /// <summary>
97+ /// Adds the command to the cooldown list
98+ /// </summary>
99+ /// <param name="isGlobal"></param>
100+ /// <param name="playerID"></param>
101+ /// <param name="commandID"></param>
102+ /// <param name="cooldownTime"></param>
103+ public void AddToCooldownList ( bool isGlobal , int playerID , Guid commandID , int cooldownTime )
104+ {
105+ var timer = new CooldownTimer ( ) {
106+ IsGlobal = isGlobal ,
107+ CommandID = commandID ,
108+ CooldownTime = DateTime . Now . AddSeconds ( cooldownTime )
109+ } ;
110+
111+ if ( isGlobal )
112+ {
113+ int index = PluginGlobals . CooldownTimer . FindIndex ( x =>
114+ x . IsGlobal == true
115+ && x . CommandID == commandID ) ;
116+ if ( index != - 1 )
117+ PluginGlobals . CooldownTimer [ index ] . CooldownTime = timer . CooldownTime ;
118+ else
119+ PluginGlobals . CooldownTimer . Add ( timer ) ;
120+ }
121+ else
122+ {
123+ timer . PlayerID = playerID ;
124+ int index = PluginGlobals . CooldownTimer . FindIndex ( x =>
125+ x . PlayerID == playerID
126+ && x . CommandID == commandID ) ;
127+ if ( index != - 1 )
128+ PluginGlobals . CooldownTimer [ index ] . CooldownTime = timer . CooldownTime ;
129+ else
130+ PluginGlobals . CooldownTimer . Add ( timer ) ;
131+ }
132+ }
133+
65134 /// <summary>
66135 /// Sets the cooldown for the command
67136 /// </summary>
@@ -75,40 +144,16 @@ public void SetCooldown(CCSPlayerController player, Commands cmd)
75144 {
76145 case JsonValueKind . Number :
77146 int cooldown = ( int ) cmd . Cooldown ;
78-
79147 if ( cooldown == 0 )
80148 break ;
81149
82- var timer = new CooldownTimer ( ) {
83- IsGlobal = false ,
84- PlayerID = player . UserId ?? 0 ,
85- CommandID = cmd . ID ,
86- CooldownTime = DateTime . Now . AddSeconds ( cooldown )
87- } ;
88- PluginGlobals . CooldownTimer . Add ( timer ) ;
89-
150+ AddToCooldownList ( false , player . UserId ?? 0 , cmd . ID , cooldown ) ;
90151 break ;
152+
91153 case JsonValueKind . Object :
92154 Cooldown cooldownObject = ( Cooldown ) cmd . Cooldown ;
93155
94- if ( cooldownObject . IsGlobal )
95- {
96- var timerObj = new CooldownTimer ( ) {
97- IsGlobal = true ,
98- CommandID = cmd . ID ,
99- CooldownTime = DateTime . Now . AddSeconds ( cooldownObject . CooldownTime )
100- } ;
101- PluginGlobals . CooldownTimer . Add ( timerObj ) ;
102- } else {
103- var timerObj = new CooldownTimer ( ) {
104- IsGlobal = false ,
105- PlayerID = player . UserId ?? 0 ,
106- CommandID = cmd . ID ,
107- CooldownTime = DateTime . Now . AddSeconds ( cooldownObject . CooldownTime )
108- } ;
109- PluginGlobals . CooldownTimer . Add ( timerObj ) ;
110- }
111-
156+ AddToCooldownList ( cooldownObject . IsGlobal , player . UserId ?? 0 , cmd . ID , cooldownObject . CooldownTime ) ;
112157 break ;
113158
114159 default :
0 commit comments