Skip to content

Commit

Permalink
Fix some synchronization problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelillo15 committed Jun 16, 2024
1 parent 64597a5 commit 5c2cd39
Showing 1 changed file with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.nookure.staff.api.Logger;
import com.nookure.staff.api.PlayerWrapper;
import com.nookure.staff.api.StaffPlayerWrapper;
import org.jetbrains.annotations.NotNull;
Expand All @@ -28,6 +30,8 @@
*/
@Singleton
public final class PlayerWrapperManager<T> {
@Inject
private Logger logger;
private final BiMap<T, PlayerWrapper> playerWrappersByPlayerClass = HashBiMap.create();
private final LinkedHashMap<UUID, PlayerWrapper> playerWrappersByUUID = new LinkedHashMap<>();
private final ArrayList<UUID> staffPlayers = new ArrayList<>();
Expand Down Expand Up @@ -101,10 +105,29 @@ public void addPlayerWrapper(@NotNull T player, @NotNull PlayerWrapper playerWra
Objects.requireNonNull(player, "Player cannot be null");
Objects.requireNonNull(playerWrapper, "PlayerWrapper cannot be null");

playerWrappersByPlayerClass.put(player, playerWrapper);
playerWrappersByUUID.put(playerWrapper.getUniqueId(), playerWrapper);
synchronized (playerWrappersByPlayerClass) {
if (playerWrappersByPlayerClass.containsKey(player)) {
logger.warning("PlayerWrapper already exists for player: %s", player);
return;
}

playerWrappersByPlayerClass.put(player, playerWrapper);
}

synchronized (playerWrappersByUUID) {
if (playerWrappersByUUID.containsKey(playerWrapper.getUniqueId())) {
logger.warning("PlayerWrapper already exists for UUID: %s", playerWrapper.getUniqueId());
return;
}

playerWrappersByUUID.put(playerWrapper.getUniqueId(), playerWrapper);
}

if (isStaff) staffPlayers.add(playerWrapper.getUniqueId());
if (isStaff) {
synchronized (staffPlayers) {
staffPlayers.add(playerWrapper.getUniqueId());
}
}
}

/**
Expand All @@ -124,11 +147,18 @@ public void addPlayerWrapper(@NotNull T player, @NotNull PlayerWrapper playerWra
*/
public void removePlayerWrapper(@NotNull T player) {
Objects.requireNonNull(player, "Player cannot be null");
synchronized (playerWrappersByPlayerClass) {
PlayerWrapper playerWrapper = playerWrappersByPlayerClass.remove(player);
if (playerWrapper == null) {
logger.warning("PlayerWrapper not found for player: %s", player);
return;
}
if (playerWrapper instanceof StaffPlayerWrapper) staffPlayers.remove(playerWrapper.getUniqueId());

PlayerWrapper playerWrapper = playerWrappersByPlayerClass.remove(player);
playerWrappersByUUID.remove(playerWrapper.getUniqueId());

if (playerWrapper instanceof StaffPlayerWrapper) staffPlayers.remove(playerWrapper.getUniqueId());
synchronized (playerWrappersByUUID) {
playerWrappersByUUID.remove(playerWrapper.getUniqueId());
}
}
}

/**
Expand Down

0 comments on commit 5c2cd39

Please sign in to comment.