Skip to content

Commit

Permalink
Replace iterator removal with for each removeIf removal (#141)
Browse files Browse the repository at this point in the history
Explanation: AuthenticatingAccountBucket#getAccountIdEntries returns new collection of the player ids, which was "removed", but wouldn't affect original bucket
  • Loading branch information
bivashy authored Jan 12, 2024
1 parent e76544d commit 00102b8
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.mastercapexd.auth.task;

import java.util.Iterator;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

import com.bivashy.auth.api.AuthPlugin;
Expand All @@ -20,13 +20,11 @@ public AuthenticationTimeoutTask(AuthPlugin plugin) {
long now = System.currentTimeMillis();
long authTimeoutMillis = plugin.getConfig().getAuthTime();

// Iterator for preventing ConcurrentModificationException, because we are removing element on specific condition
Iterator<String> accountIdIterator = plugin.getAuthenticatingAccountBucket().getAccountIdEntries().iterator();
while (accountIdIterator.hasNext()) {
String accountPlayerId = accountIdIterator.next();
Account account = plugin.getAuthenticatingAccountBucket().getAuthenticatingAccountNullable(PlayerIdSupplier.of(accountPlayerId));
int accountEnterElapsedMillis = (int) (now -
plugin.getAuthenticatingAccountBucket().getEnterTimestampOrZero(PlayerIdSupplier.of(accountPlayerId)));
Collection<String> playerIds = plugin.getAuthenticatingAccountBucket().getAccountIdEntries();
for (String playerId : playerIds) {
PlayerIdSupplier playerIdSupplier = PlayerIdSupplier.of(playerId);
Account account = plugin.getAuthenticatingAccountBucket().getAuthenticatingAccountNullable(playerIdSupplier);
int accountEnterElapsedMillis = (int) (now - plugin.getAuthenticatingAccountBucket().getEnterTimestampOrZero(playerIdSupplier));

for (LinkEntryUser entryUser : plugin.getLinkEntryBucket().find(user -> user.getAccount().getPlayerId().equals(account.getPlayerId())))
if (entryUser != null)
Expand All @@ -35,12 +33,14 @@ public AuthenticationTimeoutTask(AuthPlugin plugin) {
} catch(UnsupportedOperationException ignored) { // If link type has no settings support
}

if(!account.getPlayer().isPresent())
plugin.getAuthenticatingAccountBucket().modifiable().removeIf(state -> state.getPlayerId().equals(playerId));

if (accountEnterElapsedMillis < authTimeoutMillis)
continue;
account.getPlayer()
.ifPresent(
player -> player.disconnect(plugin.getConfig().getServerMessages().getMessage("time-left", new ServerMessageContext(account))));
accountIdIterator.remove();
}
}, 0, 1, TimeUnit.SECONDS);
}
Expand Down

0 comments on commit 00102b8

Please sign in to comment.