Skip to content

Commit b2181b2

Browse files
committed
Now cooldowns work based on sub commands
1 parent 99686fc commit b2181b2

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

.github/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ To add this project as a dependency to your project, add the following to your p
3939
<dependency>
4040
<groupId>com.github.Despical</groupId>
4141
<artifactId>CommandFramework</artifactId>
42-
<version>1.1.6</version>
42+
<version>1.1.7</version>
4343
<scope>compile</scope>
4444
</dependency>
4545
```
@@ -52,7 +52,7 @@ repositories {
5252
```
5353
```
5454
dependencies {
55-
compileOnly group: "com.github.Despical", name: "CommandFramework", version: "1.1.6";
55+
compileOnly group: "com.github.Despical", name: "CommandFramework", version: "1.1.7";
5656
}
5757
```
5858

@@ -132,7 +132,7 @@ I accept Pull Requests via GitHub. There are some guidelines which will make app
132132
You can learn more about contributing via GitHub in [contribution guidelines](../CONTRIBUTING.md).
133133

134134
## Known issues
135-
* Cooldowns are not working command based.
135+
* ~~Cooldowns are not working command based.~~
136136
* ~~Framework can't detect the sub commands when a sub command registered with the name of main command.~~
137137
* ~~Sub-commands aren't compatible with tab completions.~~
138138

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>me.despical</groupId>
55
<artifactId>command-framework</artifactId>
6-
<version>1.1.7</version>
6+
<version>1.1.8</version>
77

88
<properties>
99
<java.version>8</java.version>
@@ -48,7 +48,7 @@
4848
<dependency>
4949
<groupId>com.github.Despical</groupId>
5050
<artifactId>Commons</artifactId>
51-
<version>1.5.5</version>
51+
<version>1.6.0</version>
5252
</dependency>
5353
</dependencies>
5454

src/main/java/me/despical/commandframework/CommandFramework.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class CommandFramework implements CommandExecutor, TabCompleter {
7373
* Map of registered command cooldowns by framework.
7474
*/
7575
@NotNull
76-
private final Map<CommandSender, Long> cooldowns = new HashMap<>();
76+
private final Map<CommandSender, Map<Command, Long>> cooldowns = new HashMap<>();
7777
/**
7878
* Consumer to accept if there is no matched commands related framework.
7979
*/
@@ -133,7 +133,7 @@ public void registerCommands(@NotNull Object instance) {
133133
}
134134

135135
registerCommand(command, method, instance);
136-
}else if (method.getAnnotation(Completer.class) != null) {
136+
} else if (method.getAnnotation(Completer.class) != null) {
137137
if (!List.class.isAssignableFrom(method.getReturnType())) {
138138
plugin.getLogger().log(Level.WARNING, "Skipped registration of {0} because it is not returning java.util.List type.", method.getName());
139139
continue;
@@ -143,7 +143,7 @@ public void registerCommands(@NotNull Object instance) {
143143

144144
if (completer.name().contains(".")) {
145145
subCommandCompletions.put(completer, me.despical.commons.util.Collections.mapEntry(method, instance));
146-
}else {
146+
} else {
147147
commandCompletions.put(completer, me.despical.commons.util.Collections.mapEntry(method, instance));
148148
}
149149
}
@@ -160,7 +160,7 @@ public void registerCommands(@NotNull Object instance) {
160160
private void registerCommand(Command command, Method method, Object instance) {
161161
if (command.name().contains(".")) {
162162
subCommands.put(command, me.despical.commons.util.Collections.mapEntry(method, instance));
163-
}else {
163+
} else {
164164
commands.put(command, me.despical.commons.util.Collections.mapEntry(method, instance));
165165
}
166166

@@ -224,14 +224,40 @@ private Map.Entry<Command, Map.Entry<Method, Object>> getAssociatedCommand(@NotN
224224
return null;
225225
}
226226

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+
227255
@Override
228256
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command cmd, @NotNull String label, String[] args) {
229257
final Map.Entry<Command, Map.Entry<Method, Object>> entry = this.getAssociatedCommand(cmd.getName(), args);
230258

231259
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));
235261

236262
return true;
237263
}
@@ -254,18 +280,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
254280
return true;
255281
}
256282

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;
269284

270285
final String[] splitted = command.name().split("\\."), newArgs = Arrays.copyOfRange(args, splitted.length - 1, args.length);
271286

@@ -277,7 +292,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
277292
e.printStackTrace();
278293
return true;
279294
}
280-
}else {
295+
} else {
281296
sender.sendMessage(SHORT_OR_LONG_ARG_SIZE);
282297
return true;
283298
}

0 commit comments

Comments
 (0)