|
1 | 1 | package me.darki.simplecommandcooldown; |
2 | 2 |
|
3 | | -import javafx.util.Pair; |
4 | 3 | import org.bukkit.ChatColor; |
5 | 4 | import org.bukkit.entity.Player; |
6 | 5 | import org.bukkit.event.EventHandler; |
|
9 | 8 | import org.bukkit.event.player.PlayerCommandPreprocessEvent; |
10 | 9 | import org.bukkit.event.player.PlayerQuitEvent; |
11 | 10 |
|
12 | | -import java.util.Map; |
| 11 | +import java.util.UUID; |
13 | 12 | import java.util.concurrent.ConcurrentHashMap; |
14 | | -import java.util.concurrent.locks.ReentrantLock; |
15 | 13 |
|
16 | 14 | public class CommandListener implements Listener { |
17 | | - final private ConcurrentHashMap<Pair<Player, String>, Long> cooldownMap; |
18 | | - final private ReentrantLock mutex = new ReentrantLock(); |
| 15 | + |
| 16 | + final private ConcurrentHashMap<Data, Long> cooldownMap; |
19 | 17 |
|
20 | 18 | public CommandListener() { |
21 | 19 | cooldownMap = new ConcurrentHashMap<>(); |
22 | 20 | } |
23 | 21 |
|
24 | | - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) |
| 22 | + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) |
25 | 23 | public void onCommand(PlayerCommandPreprocessEvent event) { |
| 24 | + |
26 | 25 | Player player = event.getPlayer(); |
27 | 26 | String commandName = event.getMessage().split(" ")[0]; |
| 27 | + Data data = new Data(player.getUniqueId(), commandName); |
28 | 28 |
|
29 | | - Pair<Player, String> pair = new Pair<>(player, commandName); |
30 | | - |
31 | | - try { |
32 | | - this.mutex.lock(); |
33 | | - |
34 | | - if (this.cooldownMap.containsKey(pair)) { |
35 | | - if (System.currentTimeMillis() - this.cooldownMap.get(pair) > SimpleCommandCooldown.cooldown) { |
36 | | - this.cooldownMap.remove(pair); |
37 | | - } else { |
38 | | - player.sendMessage(ChatColor.RED + "Please wait a bit before using this command again!"); |
39 | | - event.setCancelled(true); |
40 | | - } |
41 | | - } else { |
42 | | - this.cooldownMap.put(pair, System.currentTimeMillis()); |
43 | | - } |
44 | | - } finally { |
45 | | - this.mutex.unlock(); |
| 29 | + if (!cooldownMap.containsKey(data) || System.currentTimeMillis() - cooldownMap.get(data) > SimpleCommandCooldown.cooldown) { |
| 30 | + // off cooldown |
| 31 | + cooldownMap.put(data, System.currentTimeMillis()); |
| 32 | + } else { |
| 33 | + // on cooldown |
| 34 | + event.setCancelled(true); |
| 35 | + player.sendMessage(ChatColor.RED + SimpleCommandCooldown.message); |
46 | 36 | } |
| 37 | + |
47 | 38 | } |
48 | 39 |
|
49 | 40 | @EventHandler(ignoreCancelled = true) |
50 | 41 | public void onPlayerQuit(PlayerQuitEvent event) { |
51 | | - Player player = event.getPlayer(); |
| 42 | + cooldownMap.forEach((data, timeout) -> { |
| 43 | + if (data.getUuid().equals(event.getPlayer().getUniqueId())) { |
| 44 | + cooldownMap.remove(data); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + public static class Data { |
52 | 50 |
|
53 | | - for (Map.Entry<Pair<Player, String>, Long> entry : this.cooldownMap.entrySet()) { |
54 | | - if (entry.getKey().getKey() == player) |
55 | | - this.cooldownMap.remove(entry.getKey()); |
| 51 | + private final UUID uuid; |
| 52 | + private final String command; |
| 53 | + |
| 54 | + public Data(UUID uuid, String command) { |
| 55 | + this.uuid = uuid; |
| 56 | + this.command = command; |
| 57 | + } |
| 58 | + |
| 59 | + public UUID getUuid() { |
| 60 | + return uuid; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean equals(Object o) { |
| 65 | + if (this == o) return true; |
| 66 | + if (o == null || getClass() != o.getClass()) return false; |
| 67 | + Data data = (Data) o; |
| 68 | + if (!uuid.equals(data.uuid)) return false; |
| 69 | + return command.equals(data.command); |
56 | 70 | } |
| 71 | + |
| 72 | + @Override |
| 73 | + public int hashCode() { |
| 74 | + int result = uuid.hashCode(); |
| 75 | + result = 31 * result + command.hashCode(); |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
57 | 79 | } |
| 80 | + |
58 | 81 | } |
0 commit comments