@@ -73,7 +73,7 @@ public class CommandFramework implements CommandExecutor, TabCompleter {
73
73
* Map of registered command cooldowns by framework.
74
74
*/
75
75
@ NotNull
76
- private final Map <CommandSender , Long > cooldowns = new HashMap <>();
76
+ private final Map <CommandSender , Map < Command , Long > > cooldowns = new HashMap <>();
77
77
/**
78
78
* Consumer to accept if there is no matched commands related framework.
79
79
*/
@@ -133,7 +133,7 @@ public void registerCommands(@NotNull Object instance) {
133
133
}
134
134
135
135
registerCommand (command , method , instance );
136
- }else if (method .getAnnotation (Completer .class ) != null ) {
136
+ } else if (method .getAnnotation (Completer .class ) != null ) {
137
137
if (!List .class .isAssignableFrom (method .getReturnType ())) {
138
138
plugin .getLogger ().log (Level .WARNING , "Skipped registration of {0} because it is not returning java.util.List type." , method .getName ());
139
139
continue ;
@@ -143,7 +143,7 @@ public void registerCommands(@NotNull Object instance) {
143
143
144
144
if (completer .name ().contains ("." )) {
145
145
subCommandCompletions .put (completer , me .despical .commons .util .Collections .mapEntry (method , instance ));
146
- }else {
146
+ } else {
147
147
commandCompletions .put (completer , me .despical .commons .util .Collections .mapEntry (method , instance ));
148
148
}
149
149
}
@@ -160,7 +160,7 @@ public void registerCommands(@NotNull Object instance) {
160
160
private void registerCommand (Command command , Method method , Object instance ) {
161
161
if (command .name ().contains ("." )) {
162
162
subCommands .put (command , me .despical .commons .util .Collections .mapEntry (method , instance ));
163
- }else {
163
+ } else {
164
164
commands .put (command , me .despical .commons .util .Collections .mapEntry (method , instance ));
165
165
}
166
166
@@ -224,14 +224,40 @@ private Map.Entry<Command, Map.Entry<Method, Object>> getAssociatedCommand(@NotN
224
224
return null ;
225
225
}
226
226
227
+ private boolean hasCooldown (final CommandSender sender , final Command command ) {
228
+ if (command .cooldown () < 1 ) return false ;
229
+
230
+ final Map <Command , Long > cooldownMap = cooldowns .get (sender );
231
+
232
+ if (cooldownMap == null ) {
233
+ cooldowns .put (sender , me .despical .commons .util .Collections .mapOf (command , System .currentTimeMillis ()));
234
+ return false ;
235
+ } else if (!cooldownMap .containsKey (command )) {
236
+ cooldownMap .put (command , System .currentTimeMillis ());
237
+
238
+ cooldowns .replace (sender , cooldownMap );
239
+ return false ;
240
+ }
241
+
242
+ final int remainingTime = (int ) ((System .currentTimeMillis () - cooldownMap .get (command )) / 1000 ) % 60 ;
243
+
244
+ if (remainingTime <= command .cooldown ()) {
245
+ sender .sendMessage (String .format (WAIT_BEFORE_USING_AGAIN , command .cooldown () - remainingTime ));
246
+ return true ;
247
+ } else {
248
+ cooldownMap .put (command , System .currentTimeMillis ());
249
+
250
+ cooldowns .replace (sender , cooldownMap );
251
+ return false ;
252
+ }
253
+ }
254
+
227
255
@ Override
228
256
public boolean onCommand (@ NotNull CommandSender sender , @ NotNull org .bukkit .command .Command cmd , @ NotNull String label , String [] args ) {
229
257
final Map .Entry <Command , Map .Entry <Method , Object >> entry = this .getAssociatedCommand (cmd .getName (), args );
230
258
231
259
if (entry == null ) {
232
- if (anyMatchConsumer != null ) {
233
- anyMatchConsumer .accept (new CommandArguments (sender , cmd , label , args ));
234
- }
260
+ if (anyMatchConsumer != null ) anyMatchConsumer .accept (new CommandArguments (sender , cmd , label , args ));
235
261
236
262
return true ;
237
263
}
@@ -254,18 +280,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
254
280
return true ;
255
281
}
256
282
257
- if (cooldowns .containsKey (sender )) {
258
- final int remainingTime = (int ) ((System .currentTimeMillis () - cooldowns .get (sender )) / 1000 ) % 60 ;
259
-
260
- if (command .cooldown () > 0 && remainingTime <= command .cooldown ()) {
261
- sender .sendMessage (String .format (WAIT_BEFORE_USING_AGAIN , command .cooldown () - remainingTime ));
262
- return true ;
263
- }else {
264
- cooldowns .put (sender , System .currentTimeMillis ());
265
- }
266
- }else {
267
- cooldowns .put (sender , System .currentTimeMillis ());
268
- }
283
+ if (this .hasCooldown (sender , command )) return true ;
269
284
270
285
final String [] splitted = command .name ().split ("\\ ." ), newArgs = Arrays .copyOfRange (args , splitted .length - 1 , args .length );
271
286
@@ -277,7 +292,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
277
292
e .printStackTrace ();
278
293
return true ;
279
294
}
280
- }else {
295
+ } else {
281
296
sender .sendMessage (SHORT_OR_LONG_ARG_SIZE );
282
297
return true ;
283
298
}
0 commit comments