Skip to content

Commit

Permalink
Update EssentialsPlayerListener.java -> handlePlayerCommandPreprocess()
Browse files Browse the repository at this point in the history
no need to recreate a HashMap instance to prevent concurrency since user.getCommandCooldowns() already makes a new HashMap  instance

the second if condition verifying entry.getValue() > System.currentTimeMillis() is redundant as it is already implied by the first if condition

Don't break in case there are other command cooldowns left to clear
  • Loading branch information
Zarkreyy authored Nov 30, 2024
1 parent b560bbd commit 9dd1e25
Showing 1 changed file with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -705,21 +705,17 @@ public void handlePlayerCommandPreprocess(final PlayerCommandPreprocessEvent eve
// If so, no need to check for (and write) new ones.
boolean cooldownFound = false;

// Iterate over a copy of getCommandCooldowns in case of concurrent modifications
for (final Entry<Pattern, Long> entry : new HashMap<>(user.getCommandCooldowns()).entrySet()) {
for (final Entry<Pattern, Long> entry : user.getCommandCooldowns().entrySet()) {
// Remove any expired cooldowns
if (entry.getValue() <= System.currentTimeMillis()) {
user.clearCommandCooldown(entry.getKey());
// Don't break in case there are other command cooldowns left to clear.
} else if (entry.getKey().matcher(fullCommand).matches()) {
// User's current cooldown hasn't expired, inform and terminate cooldown code.
if (entry.getValue() > System.currentTimeMillis()) {
final String commandCooldownTime = DateUtil.formatDateDiff(entry.getValue());
user.sendTl("commandCooldown", commandCooldownTime);
cooldownFound = true;
event.setCancelled(true);
break;
}
final String commandCooldownTime = DateUtil.formatDateDiff(entry.getValue());
user.sendTl("commandCooldown", commandCooldownTime);
cooldownFound = true;
event.setCancelled(true);
}
}

Expand Down

0 comments on commit 9dd1e25

Please sign in to comment.