Skip to content
This repository was archived by the owner on Jul 22, 2021. It is now read-only.

Commit e0f39bb

Browse files
committed
remove mutex, warn message configurable
1 parent 1edc0e5 commit e0f39bb

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/main/java/me/darki/simplecommandcooldown/CommandListener.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010

1111
import java.util.UUID;
1212
import java.util.concurrent.ConcurrentHashMap;
13-
import java.util.concurrent.locks.ReentrantLock;
1413

1514
public class CommandListener implements Listener {
1615

1716
final private ConcurrentHashMap<Data, Long> cooldownMap;
18-
final private ReentrantLock mutex;
1917

2018
public CommandListener() {
2119
cooldownMap = new ConcurrentHashMap<>();
22-
mutex = new ReentrantLock();
2320
}
2421

2522
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -29,19 +26,15 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
2926
String commandName = event.getMessage().split(" ")[0];
3027
Data data = new Data(player.getUniqueId(), commandName);
3128

32-
mutex.lock();
33-
3429
if (!cooldownMap.containsKey(data) || System.currentTimeMillis() - cooldownMap.get(data) > SimpleCommandCooldown.cooldown) {
35-
// command off cooldown
30+
// off cooldown
3631
cooldownMap.put(data, System.currentTimeMillis());
3732
} else {
38-
// command on fresh cooldown
33+
// on cooldown
3934
event.setCancelled(true);
40-
player.sendMessage(ChatColor.RED + "Please wait a bit before using this command again!");
35+
player.sendMessage(ChatColor.RED + SimpleCommandCooldown.message);
4136
}
4237

43-
mutex.unlock();
44-
4538
}
4639

4740
@EventHandler(ignoreCancelled = true)

src/main/java/me/darki/simplecommandcooldown/SimpleCommandCooldown.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
public final class SimpleCommandCooldown extends JavaPlugin {
77

88
public static long cooldown = 0L;
9+
public static String message = "";
910

1011
public void onEnable() {
12+
initConfig();
13+
Bukkit.getServer().getPluginManager().registerEvents(new CommandListener(), this);
14+
}
1115

16+
private void initConfig() {
1217
getConfig().addDefault("cooldown", 3000);
18+
getConfig().addDefault("message", "Please wait a bit before using this command again!");
1319
getConfig().options().copyDefaults(true);
1420
saveConfig();
15-
16-
Bukkit.getServer().getPluginManager().registerEvents(new CommandListener(), this);
1721
cooldown = getConfig().getLong("cooldown");
18-
22+
message = getConfig().getString("message");
1923
}
2024

2125
}

0 commit comments

Comments
 (0)