Skip to content

Commit 3d25280

Browse files
committed
Fixed cooldown commands can run twice after first execution and now message shows remaining time to execute command again
1 parent 5a5d64b commit 3d25280

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
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.3</version>
6+
<version>1.1.4</version>
77

88
<properties>
99
<java.version>8</java.version>

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private Map.Entry<Command, Map.Entry<Method, Object>> getAssociatedCommand(@NotN
212212
public static String ONLY_BY_CONSOLE = ChatColor.RED + "This command is only executable by console!";
213213
public static String NO_PERMISSION = ChatColor.RED + "You don't have enough permission to execute this command!";
214214
public static String SHORT_OR_LONG_ARG_SIZE = ChatColor.RED + "Required argument length is less or greater than needed!";
215-
public static String WAIT_BEFORE_USING_AGAIN = ChatColor.RED + "You have to wait before using this command again!";
215+
public static String WAIT_BEFORE_USING_AGAIN = ChatColor.RED + "You have to wait %ds before using this command again!";
216216

217217
@Override
218218
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command cmd, @NotNull String label, String[] args) {
@@ -244,11 +244,13 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
244244
}
245245

246246
if (cooldowns.containsKey(sender)) {
247-
if (command.cooldown() > 0 && ((System.currentTimeMillis() - cooldowns.get(sender)) / 1000) % 60 <= command.cooldown()) {
248-
sender.sendMessage(WAIT_BEFORE_USING_AGAIN);
247+
final int remainingTime = (int) ((System.currentTimeMillis() - cooldowns.get(sender)) / 1000) % 60;
248+
249+
if (command.cooldown() > 0 && remainingTime <= command.cooldown()) {
250+
sender.sendMessage(String.format(WAIT_BEFORE_USING_AGAIN, command.cooldown() - remainingTime));
249251
return true;
250252
} else {
251-
cooldowns.remove(sender);
253+
cooldowns.put(sender, System.currentTimeMillis());
252254
}
253255
} else {
254256
cooldowns.put(sender, System.currentTimeMillis());
@@ -297,7 +299,7 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull org.bu
297299
@NotNull
298300
public List<Command> getCommands() {
299301
List<Command> commands = new ArrayList<>(this.commands.keySet());
300-
commands.addAll(subCommands.keySet());
302+
commands.addAll(this.subCommands.keySet());
301303

302304
return commands;
303305
}

0 commit comments

Comments
 (0)