Skip to content

Commit

Permalink
Fix sincronization erros, share primitives and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelillo15 committed Aug 26, 2024
1 parent 05e5c74 commit e8df898
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.nookure.core.PlayerWrapperBase;
import com.nookure.core.command.Command;
import com.nookure.core.command.CommandManager;
import com.nookure.core.command.config.CommandConfig;
Expand All @@ -15,7 +16,7 @@
import java.util.List;

@Singleton
public class PaperCommandManager extends CommandManager {
public class PaperCommandManager<P extends PlayerWrapperBase> extends CommandManager {
private static final CommandMap commandMap;

static {
Expand All @@ -33,7 +34,7 @@ public class PaperCommandManager extends CommandManager {

@Override
public void registerCommand(@NotNull Command command) {
TemplateCommand templateCommand = new TemplateCommand(command);
TemplateCommand<P> templateCommand = new TemplateCommand(command);
injector.injectMembers(templateCommand);
CommandPartial commandPartial;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import java.util.List;
import java.util.Optional;

public class TemplateCommand extends Command {
public class TemplateCommand<P extends PlayerWrapperBase> extends Command {
private final com.nookure.core.command.Command command;
@Inject
private PlayerWrapperManager<Player, ?> playerWrapperManager;
private PlayerWrapperManager<Player, PlayerWrapperBase> playerWrapperManager;
@Inject
private ConsoleCommandSender consoleCommandSender;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

/**
Expand All @@ -27,7 +28,7 @@
@Singleton
public class PlayerWrapperManager<T, P extends PlayerWrapperBase> {
private final BiMap<T, P> playerWrappersByPlayerClass = HashBiMap.create();
private final LinkedHashMap<UUID, P> playerWrappersByUUID = new LinkedHashMap<>();
private final Map<UUID, P> playerWrappersByUUID = new ConcurrentHashMap<>();

/**
* Gets a player wrapper by its player class.
Expand All @@ -36,9 +37,11 @@ public class PlayerWrapperManager<T, P extends PlayerWrapperBase> {
* @return an optional containing the player wrapper if it exists
*/
@NotNull
public Optional<PlayerWrapperBase> getPlayerWrapper(@NotNull T player) {
public Optional<P> getPlayerWrapper(@NotNull T player) {
Objects.requireNonNull(player, "Player cannot be null");
return Optional.ofNullable(playerWrappersByPlayerClass.get(player));
synchronized (playerWrappersByPlayerClass) {
return Optional.ofNullable(playerWrappersByPlayerClass.get(player));
}
}

/**
Expand All @@ -48,7 +51,7 @@ public Optional<PlayerWrapperBase> getPlayerWrapper(@NotNull T player) {
* @return an optional containing the player wrapper if it exists
*/
@NotNull
public Optional<PlayerWrapperBase> getPlayerWrapper(@NotNull UUID uuid) {
public Optional<P> getPlayerWrapper(@NotNull UUID uuid) {
Objects.requireNonNull(uuid, "UUID cannot be null");
return Optional.ofNullable(playerWrappersByUUID.get(uuid));
}
Expand All @@ -62,7 +65,9 @@ public Optional<PlayerWrapperBase> getPlayerWrapper(@NotNull UUID uuid) {
@NotNull
public Optional<T> getPlayer(@NotNull P playerWrapperBase) {
Objects.requireNonNull(playerWrapperBase, "PlayerWrapper cannot be null");
return Optional.ofNullable(playerWrappersByPlayerClass.inverse().get(playerWrapperBase));
synchronized (playerWrappersByPlayerClass) {
return Optional.ofNullable(playerWrappersByPlayerClass.inverse().get(playerWrapperBase));
}
}

/**
Expand All @@ -74,8 +79,9 @@ public Optional<T> getPlayer(@NotNull P playerWrapperBase) {
public void addPlayerWrapper(@NotNull T player, @NotNull P playerWrapperBase) {
Objects.requireNonNull(player, "Player cannot be null");
Objects.requireNonNull(playerWrapperBase, "PlayerWrapper cannot be null");

playerWrappersByPlayerClass.put(player, playerWrapperBase);
synchronized (playerWrappersByPlayerClass) {
playerWrappersByPlayerClass.put(player, playerWrapperBase);
}
playerWrappersByUUID.put(playerWrapperBase.getUniqueId(), playerWrapperBase);
}

Expand All @@ -86,9 +92,12 @@ public void addPlayerWrapper(@NotNull T player, @NotNull P playerWrapperBase) {
*/
public void removePlayerWrapper(@NotNull T player) {
Objects.requireNonNull(player, "Player cannot be null");

PlayerWrapperBase playerWrapperBase = playerWrappersByPlayerClass.remove(player);
playerWrappersByUUID.remove(playerWrapperBase.getUniqueId());
synchronized (playerWrappersByPlayerClass) {
P playerWrapperBase = playerWrappersByPlayerClass.remove(player);
if (playerWrapperBase != null) {
playerWrappersByUUID.remove(playerWrapperBase.getUniqueId());
}
}
}

/**
Expand All @@ -104,7 +113,9 @@ public Stream<P> stream() {
* Clears all the mappings from the manager.
*/
public void clear() {
playerWrappersByPlayerClass.clear();
synchronized (playerWrappersByPlayerClass) {
playerWrappersByPlayerClass.clear();
}
playerWrappersByUUID.clear();
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id("maven-publish")
}

val versionCode = "1.0.0"
val versionCode = "1.1.0"

version = "${versionCode}-${grgit.head().abbreviatedId}"
group = "com.nookure.core"
Expand Down

0 comments on commit e8df898

Please sign in to comment.