diff --git a/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java b/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java index 2dfb3da..e17cbd9 100644 --- a/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java +++ b/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java @@ -69,7 +69,7 @@ public class DatabaseManager { private Connection connection; private final Map groups = new HashMap<>(); - private final Set warriors = new HashSet<>(); + private final Map warriors = new HashMap<>(); private final List winners = new ArrayList<>(); private enum CountType { @@ -315,24 +315,17 @@ public GroupData getGroupData(@NotNull String id) { @NotNull public Warrior getWarrior(@NotNull OfflinePlayer player) { - UUID uuid = player.getUniqueId(); - for (Warrior warrior : warriors) { - if (warrior.toPlayer().getUniqueId().equals(uuid)) { - if (player instanceof Player) { - warrior.setOnlinePlayer((Player) player); - } - return warrior; - } - } - - Warrior warrior = new Warrior(player, plugin::getGroupManager); - warriors.add(warrior); - return warrior; + return getWarrior(player.getUniqueId()); } @NotNull public Warrior getWarrior(@NotNull UUID uuid) { - return getWarrior(Bukkit.getOfflinePlayer(uuid)); + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid); + Warrior warrior = warriors.computeIfAbsent(uuid, (id) -> new Warrior(offlinePlayer, plugin::getGroupManager)); + if (offlinePlayer instanceof Player) { + warrior.setOnlinePlayer((Player) offlinePlayer); + } + return warrior; } private void loopThroughGroups() { @@ -390,7 +383,7 @@ private void loopThroughWarriors() { Warrior warrior = new Warrior(player, plugin::getGroupManager, playerData.get(CountType.KILLS), playerData.get(CountType.DEATHS), playerData.get(CountType.VICTORIES)); - warriors.add(warrior); + warriors.put(warrior.getUniqueId(), warrior); } @@ -562,7 +555,7 @@ public Winners getWinners(Date date) { } public Set getWarriors() { - return Collections.unmodifiableSet(warriors); + return new HashSet<>(warriors.values()); } public Map getGroups() {