Skip to content

Commit

Permalink
perf: changed warriors collection from hashset to hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
RoinujNosde committed May 20, 2024
1 parent 77ea34c commit c74c8e4
Showing 1 changed file with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class DatabaseManager {
private Connection connection;

private final Map<String, GroupData> groups = new HashMap<>();
private final Set<Warrior> warriors = new HashSet<>();
private final Map<UUID, Warrior> warriors = new HashMap<>();
private final List<Winners> winners = new ArrayList<>();

private enum CountType {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -562,7 +555,7 @@ public Winners getWinners(Date date) {
}

public Set<Warrior> getWarriors() {
return Collections.unmodifiableSet(warriors);
return new HashSet<>(warriors.values());
}

public Map<String, GroupData> getGroups() {
Expand Down

0 comments on commit c74c8e4

Please sign in to comment.