Skip to content
Merged
32 changes: 13 additions & 19 deletions src/main/java/net/theevilreaper/xerus/api/team/DefaultTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
import net.minestom.server.entity.Player;
import net.minestom.server.utils.validate.Check;
import net.theevilreaper.xerus.api.component.ObjectComponent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* The class represents a default implementation of the {@link Team} interface.
* It provides a generic team which should work for most use cases.
* If a case doesn't fit into this, it is recommended to create a custom implementation or inherit from this class.
*
* @author theEvilReaper
* @version 2.0.0
* @version 2.1.0
* @since 1.0.0
*/
public class DefaultTeam implements Team {
Expand All @@ -39,46 +37,42 @@ public class DefaultTeam implements Team {
* @param key the key for the team name
* @param initialCapacity the initial capacity for the team
*/
protected DefaultTeam(@NotNull Key key, int initialCapacity) {
protected DefaultTeam(Key key, int initialCapacity) {
this.key = key;
this.capacity = initialCapacity;
if (initialCapacity == DEFAULT_CAPACITY) {
this.players = new HashSet<>();
} else {
this.players = HashSet.newHashSet(initialCapacity);
}
this.components = new HashMap<>();
this.players = ConcurrentHashMap.newKeySet();
this.components = new ConcurrentHashMap<>();
}

/**
* {@inheritDoc}}
*/
@Override
public <T extends ObjectComponent> void add(@NotNull Class<T> componentClass, @NotNull T component) {
public <T extends ObjectComponent> void add(Class<T> componentClass, T component) {
this.components.computeIfAbsent(componentClass, k -> component);
}

/**
* {@inheritDoc}
*/
@Override
public <T extends ObjectComponent> boolean has(@NotNull Class<T> componentClass) {
public <T extends ObjectComponent> boolean has(Class<T> componentClass) {
return this.components.containsKey(componentClass);
}

/**
* {@inheritDoc}
*/
@Override
public <T extends ObjectComponent> @Nullable T get(@NotNull Class<T> componentClass) {
public <T extends ObjectComponent> @Nullable T get(Class<T> componentClass) {
return componentClass.cast(this.components.get(componentClass));
}

/**
* {@inheritDoc}
*/
@Override
public <T extends ObjectComponent> @Nullable T remove(@NotNull Class<T> componentClass) {
public <T extends ObjectComponent> @Nullable T remove(Class<T> componentClass) {
return componentClass.cast(this.components.remove(componentClass));
}

Expand All @@ -87,7 +81,7 @@ public <T extends ObjectComponent> boolean has(@NotNull Class<T> componentClass)
*/
@Override
public void setCapacity(int capacity) {
Check.argCondition(capacity < 0, "The capacity of the can't be negative");
Check.argCondition(capacity < 0 && capacity != DEFAULT_CAPACITY, "The capacity of the team can't be negative");
this.capacity = capacity;
}

Expand All @@ -100,7 +94,7 @@ public boolean canJoin() {
}

@Override
public int compare(@NotNull Team o1, @NotNull Team o2) {
public int compare(Team o1, Team o2) {
return o1.key().compareTo(o2.key());
}

Expand All @@ -119,7 +113,7 @@ public int hashCode() {
* {@inheritDoc}
*/
@Override
public @NotNull Key key() {
public Key key() {
return this.key;
}

Expand All @@ -143,7 +137,7 @@ public int getCurrentSize() {
* {@inheritDoc}
*/
@Override
public @NotNull Set<Player> getPlayers() {
public Set<Player> getPlayers() {
return players;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,56 @@

import net.kyori.adventure.key.Key;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
* The default implementation of the {@link TeamService} interface.
*
* @author theEvilReaper
* @version 1.1.0
* @version 1.3.0
* @since 1.0.1
*/
public final class StandardTeamService implements TeamService {

private final List<Team> teams;
private final Map<Key, Team> teams;

/**
* Creates a new instance from the {@link StandardTeamService}.
*/
StandardTeamService() {
this.teams = new ArrayList<>();
this.teams = new ConcurrentHashMap<>();
}

/**
* {@inheritDoc}
*/
@Override
public void add(@NotNull Team team) {
this.teams.add(team);
public void add(Team team) {
this.teams.put(team.key(), team);
}

/**
* {@inheritDoc}
*/
@Override
public void remove(@NotNull Team team) {
this.teams.remove(team);
public void remove(Team team) {
this.teams.remove(team.key());
}

/**
* {@inheritDoc}
*/
@Override
public void remove(@NotNull Key identifier) {
this.teams.removeIf(team -> team.key().equals(identifier));
public void remove(Key identifier) {
this.teams.remove(identifier);
}

/**
Expand All @@ -58,8 +61,8 @@ public void remove(@NotNull Key identifier) {
public void clear() {
if (this.teams.isEmpty()) return;

for (int i = 0; i < this.teams.size(); i++) {
this.teams.get(i).clearPlayers();
for (Team team : this.teams.values()) {
team.clearPlayers();
}

this.teams.clear();
Expand All @@ -69,76 +72,46 @@ public void clear() {
* {@inheritDoc}
*/
@Override
public @NotNull Optional<@Nullable Team> getTeam(@NotNull Key identifier) {
int i = 0;

while (i < teams.size() && !teams.get(i).key().equals(identifier)) {
i++;
}

return Optional.ofNullable(teams.get(i));
public Optional<@Nullable Team> getTeam(Key identifier) {
return Optional.ofNullable(this.teams.get(identifier));
}

/**
* {@inheritDoc}
*/
@Override
public boolean exists(@NotNull Key identifier) {
if (teams.isEmpty()) {
return false;
}

int i = 0;

while (i < teams.size() && !teams.get(i).key().equals(identifier)) {
i++;
}

return i != teams.size();
public boolean exists(Key identifier) {
return this.teams.containsKey(identifier);
}

/**
* {@inheritDoc}
*/
@Override
public @NotNull Optional<@Nullable Team> getTeam(@NotNull Player player) {
Team team = null;

for (int i = 0; i < getTeams().size() && team == null; i++) {
if (getTeams().get(i).hasPlayer(player)) {
team = getTeams().get(i);
public Optional<@Nullable Team> getTeam(Player player) {
for (Team team : this.teams.values()) {
if (team.hasPlayer(player)) {
return Optional.of(team);
}
}

return Optional.ofNullable(team);
return Optional.empty();
}

/**
* {@inheritDoc}
*/
@Override
public Optional<@Nullable Team> getSmallestTeam() {
if (!teams.isEmpty()) {
int i = 1;
var team = teams.getFirst();

while (i < teams.size() && teams.get(i).getCurrentSize() < team.getCurrentSize()) {
team = teams.get(i);
i++;
}

return Optional.ofNullable(team);
}

return Optional.empty();
return this.teams.values().stream().min(Comparator.comparingInt(Team::getCurrentSize));
}

/**
* {@inheritDoc}
*/
@Contract(pure = true)
@Override
public @NotNull @UnmodifiableView List<Team> getTeams() {
return Collections.unmodifiableList(teams);
public @UnmodifiableView List<Team> getTeams() {
return List.copyOf(this.teams.values());
}
}
Loading
Loading