diff --git a/NookureStaff-API/src/main/java/com/nookure/staff/api/manager/PlayerWrapperManager.java b/NookureStaff-API/src/main/java/com/nookure/staff/api/manager/PlayerWrapperManager.java index 807b7818..daf27ee0 100644 --- a/NookureStaff-API/src/main/java/com/nookure/staff/api/manager/PlayerWrapperManager.java +++ b/NookureStaff-API/src/main/java/com/nookure/staff/api/manager/PlayerWrapperManager.java @@ -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; @@ -28,6 +30,8 @@ */ @Singleton public final class PlayerWrapperManager { + @Inject + private Logger logger; private final BiMap playerWrappersByPlayerClass = HashBiMap.create(); private final LinkedHashMap playerWrappersByUUID = new LinkedHashMap<>(); private final ArrayList staffPlayers = new ArrayList<>(); @@ -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()); + } + } } /** @@ -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()); + } + } } /**