Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/main/java/net/theevilreaper/xerus/api/ItemShiftOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import net.minestom.server.entity.Player;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Collection;
import java.util.Locale;

/**
* Contains some methods that help to set the player items.
*
* @author theEvilReaper
* @version 1.1.0
* @version 1.3.0
* @since 1.2.0
**/
@FunctionalInterface
Expand All @@ -23,10 +24,10 @@ public interface ItemShiftOption {
* @param locale the locale to determine the right items
* @param shiftedSlots specifies whether the items should be added in a different order
*/
void setEquipment(Player player, Locale locale, int... shiftedSlots);
void setEquipment(Player player, @Nullable Locale locale, int @Nullable ... shiftedSlots);

/**
* Sets an equipment to an specific player.
* Sets equipment to a specific player.
*
* @param player the player who receives the equipment
* @param shiftedSlots array containing shifted slots for the items
Expand All @@ -42,11 +43,9 @@ default void setEquipment(Player player, int... shiftedSlots) {
* @param players the players who receive the equipment
* @param shiftedSlots array containing shifted slots for the items
*/
default void setEquipment(List<Player> players, int... shiftedSlots) {
default void setEquipment(Collection<Player> players, int... shiftedSlots) {
if (players.isEmpty()) return;

for (int i = 0; i < players.size(); i++) {
this.setEquipment(players.get(i), shiftedSlots);
}
players.forEach(player -> setEquipment(player, shiftedSlots));
}
}
50 changes: 25 additions & 25 deletions src/main/java/net/theevilreaper/xerus/api/Joinable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,76 @@
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.util.Set;
import java.util.Collection;
import java.util.function.Consumer;

/**
* The interface provides some method to add a single player object or a set off players.
* The interface provides some method to add a single player object or a collection of players.
* Each developer can implement the interface into his class but must write his own logic for the methods.
*
* @author theEvilReaper
* @version 1.1.0
* @version 1.2.0
* @since 1.0.0
*/
public interface Joinable {

/**
* Add a single player
* @param paramPlayer The player to add
* @param player The player to add
*/
default void addPlayer(Player paramPlayer) {
this.addPlayer(paramPlayer, null);
default void addPlayer(Player player) {
this.addPlayer(player, null);
}

/**
* Add a single {@link Player} entry to a structure.
* @param paramPlayer the player to add
* @param player the player to add
* @param consumer a consumer which is called to execute some logic
*/
void addPlayer(Player paramPlayer, @Nullable Consumer<Player> consumer);
void addPlayer(Player player, @Nullable Consumer<Player> consumer);

/**
* Add a set off players
* @param players The set which contains the players to add
* Add a collection of players
* @param players The collection which contains the players to add
*/
default void addPlayers(Set<Player> players) {
default void addPlayers(Collection<Player> players) {
this.addPlayers(players, null);
}

/**
* Add a set off players
* @param players The set which contains the players to add
* Add a collection of players
* @param players The collection which contains the players to add
* @param consumer a consumer which is called to execute some logic
*/
void addPlayers(Set<Player> players, @Nullable Consumer<Player> consumer);
void addPlayers(Collection<Player> players, @Nullable Consumer<Player> consumer);

/**
* Remove a single player
* @param paramPlayer The player to remove
* @param player The player to remove
*/
default void removePlayer(Player paramPlayer) {
this.removePlayer(paramPlayer, null);
default void removePlayer(Player player) {
this.removePlayer(player, null);
}

/**
* Remove a single {@link Player} entry from a structure.
* @param paramPlayer the player to remove
* @param player the player to remove
* @param consumer a consumer which is called to execute some logic
*/
void removePlayer(Player paramPlayer, @Nullable Consumer<Player> consumer);
void removePlayer(Player player, @Nullable Consumer<Player> consumer);

/**
* Remove a set off players
* @param players The set which contains the players to remove
* Remove a collection of players
* @param players The collection which contains the players to remove
*/
default void removePlayers(Set<Player> players) {
default void removePlayers(Collection<Player> players) {
this.removePlayers(players, null);
}

/**
* Remove a set off players
* @param players The set which contains the players to remove
* Remove a collection of players
* @param players The collection which contains the players to remove
* @param consumer a consumer which is called to execute some logic
*/
void removePlayers(Set<Player> players, @Nullable Consumer<Player> consumer);
void removePlayers(Collection<Player> players, @Nullable Consumer<Player> consumer);
}
5 changes: 3 additions & 2 deletions src/main/java/net/theevilreaper/xerus/api/team/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.theevilreaper.xerus.api.team.event.TeamAction;
import org.jetbrains.annotations.*;

import java.util.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -75,7 +76,7 @@ default void addPlayer(@NotNull Player player, @Nullable Consumer<Player> consum
* @param consumer a consumer which is called to execute some logic
*/
@Override
default void addPlayers(@NotNull Set<Player> players, @Nullable Consumer<Player> consumer) {
default void addPlayers(@NotNull Collection<Player> players, @Nullable Consumer<Player> consumer) {
if (players.isEmpty()) return;
players.removeIf(player -> !getPlayers().add(player));
Runnable successCallback = consumer == null ? EMPTY : () -> getPlayers().forEach(consumer);
Expand Down Expand Up @@ -103,7 +104,7 @@ default void removePlayer(@NotNull Player paramPlayer, @Nullable Consumer<Player
* @param consumer a consumer which is called to execute some logic
*/
@Override
default void removePlayers(@NotNull Set<Player> players, @Nullable Consumer<Player> consumer) {
default void removePlayers(@NotNull Collection<Player> players, @Nullable Consumer<Player> consumer) {
if (players.isEmpty()) return;
players.removeIf(player -> getPlayers().contains(player));
Runnable successCallback = consumer == null ? EMPTY : () -> getPlayers().forEach(consumer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
import net.minestom.server.event.trait.CancellableEvent;
import org.jetbrains.annotations.NotNull;

import java.util.Set;
import java.util.Collection;

/**
* The event will be fired if an action is applied to a team where more than one player is involved.
* There is a separate event for it, so that the event is called with only one player less
*
* @author theEvilReaper
* @version 1.1.0
* @version 1.2.0
* @since 1.0.11
**/
public class MultiPlayerTeamEvent implements Event, CancellableEvent {

private final Team team;
private final Set<Player> players;
private final Collection<Player> players;
private final TeamAction action;
private boolean cancelled;

/**
* Event is called when multiple players interact with a team.
*
* @param team the team which is involved in the event
* @param players the set with the players who are involved in the event
* @param players the collection with the players who are involved in the event
* @param teamAction the action for the event
*/
public MultiPlayerTeamEvent(@NotNull Team team, @NotNull Set<Player> players, @NotNull TeamAction teamAction) {
public MultiPlayerTeamEvent(@NotNull Team team, @NotNull Collection<Player> players, @NotNull TeamAction teamAction) {
this.team = team;
this.players = players;
this.action = teamAction;
Expand Down Expand Up @@ -59,10 +59,10 @@ public boolean isCancelled() {
/**
* Returns the players who are involved in this event.
*
* @return the set with the involved players
* @return the collection with the involved players
*/
@NotNull
public Set<Player> getPlayers() {
public Collection<Player> getPlayers() {
return players;
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/net/theevilreaper/xerus/api/JoinableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,20 @@ void testMultipleOperation(Env env) {
this.joinable.removePlayers(players);
assertTrue(this.joinable.getPlayers().isEmpty());
}

@Test
void testMultipleOperationWithList(Env env) {
final Instance instance = env.createFlatInstance();
final var playersList = java.util.List.of(
env.createPlayer(instance, Pos.ZERO),
env.createPlayer(instance, Pos.ZERO)
);

assertTrue(this.joinable.getPlayers().isEmpty());
this.joinable.addPlayers(playersList);
assertEquals(2, this.joinable.getPlayers().size());

this.joinable.removePlayers(playersList);
assertTrue(this.joinable.getPlayers().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
Expand All @@ -18,47 +19,55 @@ public TestJoinableImpl() {
}

@Override
public void addPlayer(@NotNull Player paramPlayer) {
this.players.add(paramPlayer);
public void addPlayer(@NotNull Player player) {
this.players.add(player);
}

@Override
public void addPlayer(@NotNull Player paramPlayer, @Nullable Consumer<Player> consumer) {
this.players.add(paramPlayer);
public void addPlayer(@NotNull Player player, @Nullable Consumer<Player> consumer) {
this.players.add(player);

if (consumer != null) {
consumer.accept(paramPlayer);
consumer.accept(player);
}
}

@Override
public void addPlayers(@NotNull Set<Player> players) {
public void addPlayers(@NotNull Collection<Player> players) {
this.players.addAll(players);
}

@Override
public void addPlayers(@NotNull Set<Player> players, @Nullable Consumer<Player> consumer) {

public void addPlayers(@NotNull Collection<Player> players, @Nullable Consumer<Player> consumer) {
this.players.addAll(players);
if (consumer != null) {
players.forEach(consumer);
}
}

@Override
public void removePlayer(@NotNull Player paramPlayer) {
this.players.remove(paramPlayer);
public void removePlayer(@NotNull Player player) {
this.players.remove(player);
}

@Override
public void removePlayer(@NotNull Player paramPlayer, @Nullable Consumer<Player> consumer) {

public void removePlayer(@NotNull Player player, @Nullable Consumer<Player> consumer) {
if (this.players.remove(player) && consumer != null) {
consumer.accept(player);
}
}

@Override
public void removePlayers(@NotNull Set<Player> players) {
public void removePlayers(@NotNull Collection<Player> players) {
this.players.removeAll(players);
}

@Override
public void removePlayers(@NotNull Set<Player> players, @Nullable Consumer<Player> consumer) {

public void removePlayers(@NotNull Collection<Player> players, @Nullable Consumer<Player> consumer) {
this.players.removeAll(players);
if (consumer != null) {
players.forEach(consumer);
}
}

public Set<Player> getPlayers() {
Expand Down