Skip to content

Commit c2e448e

Browse files
committed
e
1 parent 7d2b67d commit c2e448e

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/PotatoEssentials.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ private void registerCommands() {
134134
//noinspection ConstantValue
135135
if (genDocs) {
136136
File file = new File(PotatoEssentials.INSTANCE.getDataFolder(), "commands-docs.md");
137-
if (file.exists()) file.delete();
137+
if (file.exists()) //noinspection ResultOfMethodCallIgnored
138+
file.delete();
138139
List<PotatoCommand> commands = new ArrayList<>(registrar.getRegisteredCommands());
139140
commands.addAll(registrar.getUnregisteredCommands());
140141
try (FileWriter fileWriter = new FileWriter(file); BufferedWriter writer = new BufferedWriter(fileWriter)) {

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/api/event/ChatCooldownEvent.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ public class ChatCooldownEvent extends PlayerEvent {
2121
private final String channel;
2222
@Getter
2323
private final double remainingSeconds;
24+
private boolean shouldSendMessage = true;
2425

25-
public ChatCooldownEvent(@NotNull Player player, Component message, double remainingSeconds, String channel) {
26-
super(player);
26+
public ChatCooldownEvent(@NotNull Player player, Component message, double remainingSeconds, String channel, boolean async) {
27+
super(player, async);
2728
this.message = message;
2829
this.channel = channel;
2930
this.remainingSeconds = remainingSeconds;
3031
}
32+
public boolean shouldSendMessage() {
33+
return shouldSendMessage;
34+
}
35+
public void shouldSendMessage(boolean shouldSendMessage) {
36+
this.shouldSendMessage = shouldSendMessage;
37+
}
3138
@Override
3239
public @NotNull HandlerList getHandlers() {
3340
return handlers;

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/listeners/ChatListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public void onPlayerChat(AsyncChatEvent e) {
4747
double cooldownRemainder = cooldownRemainderMillis / 1000D;
4848
var msg = Config.replaceFormat(Config.getString("chat.cooldownMessage"),
4949
new Replacer("cooldown", df.format(cooldownRemainder)));
50-
var event = new ChatCooldownEvent(player, msg, cooldownRemainder,"global");
50+
var event = new ChatCooldownEvent(player, msg, cooldownRemainder,"global", e.isAsynchronous());
5151
Bukkit.getPluginManager().callEvent(event);
52-
player.sendMessage(msg);
52+
if (event.shouldSendMessage()) player.sendMessage(msg);
5353
return;
5454
}
5555
}
@@ -66,7 +66,7 @@ public void onPlayerChat(AsyncChatEvent e) {
6666
new Replacer("message", message, false));
6767
Map<Player, CustomChat> playerChatMap = CustomChat.getPlayerChat();
6868
if (Config.customChatsEnabled() && playerChatMap.containsKey(player)) {
69-
playerChatMap.get(player).sendMessage(player, message);
69+
playerChatMap.get(player).sendMessage(player, message, e.isAsynchronous());
7070
return;
7171
}
7272
Bukkit.broadcast(finalMessage);

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/objects/CustomChat.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class CustomChat {
3737
private final @Nullable String command;
3838
private final @Nullable Integer cooldown;
3939
@Getter
40-
private final HashMap<CommandSender, Long> lastChatTimes = new HashMap<>();
40+
private final HashMap<Player, Long> lastChatTimes = new HashMap<>();
4141

4242
public CustomChat(String key, String name, @Nullable String permission, @Nullable String command, @Nullable Integer cooldown) {
4343
this.key = key;
@@ -80,11 +80,11 @@ public CustomChat(String key, String name, @Nullable String permission, @Nullabl
8080
if (permission!=null) cmd.withPermission(permission);
8181
cmd.register("potatoessentialschannel");
8282
}
83-
public void sendMessage(CommandSender sender, String message) {
84-
if (this.getCooldown()!=null) {
85-
boolean canBypassCooldown = sender.hasPermission(PotatoEssentials.NAMESPACE+".chat.bypass-cooldown");
86-
if (this.getLastChatTimes().containsKey(sender) && !canBypassCooldown) {
87-
long lastTime = this.getLastChatTimes().get(sender);
83+
public void sendMessage(CommandSender sender, String message, boolean async) {
84+
if (sender instanceof Player p && this.getCooldown()!=null) {
85+
boolean canBypassCooldown = p.hasPermission(PotatoEssentials.NAMESPACE+".chat.bypass-cooldown");
86+
if (this.getLastChatTimes().containsKey(p) && !canBypassCooldown) {
87+
long lastTime = this.getLastChatTimes().get(p);
8888
long currentTime = System.currentTimeMillis();
8989
long cooldownMillis = this.getCooldown() * 1000L;
9090
long timeElapsed = currentTime - lastTime;
@@ -95,13 +95,13 @@ public void sendMessage(CommandSender sender, String message) {
9595
double cooldownRemainder = cooldownRemainderMillis / 1000D;
9696
var msg = Config.replaceFormat(Config.getString("chat.cooldownMessage"),
9797
new Replacer("cooldown", df.format(cooldownRemainder)));
98-
var event = new ChatCooldownEvent((Player) sender, msg, cooldownRemainder, this.getKey());
98+
var event = new ChatCooldownEvent(p, msg, cooldownRemainder, this.getKey(), async);
9999
Bukkit.getPluginManager().callEvent(event);
100-
sender.sendMessage(msg);
100+
if (event.shouldSendMessage()) p.sendMessage(msg);
101101
return;
102102
}
103103
}
104-
this.getLastChatTimes().put(sender, System.currentTimeMillis());
104+
this.getLastChatTimes().put(p, System.currentTimeMillis());
105105
}
106106
boolean miniMsg = sender.hasPermission(PotatoEssentials.NAMESPACE+".chat.minimessage");
107107
if (!miniMsg) message = Utils.escapeTags(message);
@@ -122,6 +122,9 @@ public void sendMessage(CommandSender sender, String message) {
122122
recipients.forEach(p -> p.sendMessage(msg));
123123
Bukkit.getConsoleSender().sendMessage(msg);
124124
}
125+
public void sendMessage(CommandSender sender, String message) {
126+
sendMessage(sender, message, false);
127+
}
125128
public boolean checkPerm(CommandSender sender) {
126129
return this.getPermission() == null || sender.hasPermission(this.getPermission());
127130
}

0 commit comments

Comments
 (0)