Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix custom player provider usage for junit #81

Merged
merged 2 commits into from
Oct 15, 2024
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
44 changes: 44 additions & 0 deletions testing/src/main/java/net/minestom/testing/TestConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,60 @@
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.network.PlayerProvider;
import net.minestom.server.network.packet.server.ServerPacket;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;

/**
* Represents a connection to a test server.
*/
public interface TestConnection {

/**
* Sets the custom player provider for this test connection.
* <br>
* For a successful test you need to override the sendChunk method in the player implementation.
* Example:
* <pre>{@code
* public class CustomGamePlayerImpl extends Player {
* public CustomGamePlayerImpl(UUID uuid, String username, PlayerConnection playerConnection) {
* super(uuid, username, playerConnection);
* }
* @Override
* public void sendChunk(Chunk chunk) {
* sendPacket(chunk.getFullDataPacket());
* }
* }
* }</pre>
* @param provider the custom player provider
*/
void setCustomPlayerProvider(@NotNull PlayerProvider provider);

/**
* Connects a player to the server.
*
* @param instance the instance to spawn the player in
* @param pos the position to spawn the player at
* @return a future that completes when the player is connected
*/
@NotNull CompletableFuture<@NotNull Player> connect(@NotNull Instance instance, @NotNull Pos pos);

/**
* Tracks incoming packets of a specific type.
*
* @param type the packet type to track
* @param <T> the packet type
* @return a collector for the tracked packets
*/
<T extends ServerPacket> @NotNull Collector<T> trackIncoming(@NotNull Class<T> type);

/**
* Tracks all incoming packets.
*
* @return a collector for all incoming packets
*/
default @NotNull Collector<ServerPacket> trackIncoming() {
return trackIncoming(ServerPacket.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
import net.minestom.server.instance.Instance;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.PlayerProvider;
import net.minestom.server.network.packet.server.SendablePacket;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.configuration.SelectKnownPacksPacket;
Expand All @@ -18,26 +19,31 @@
import java.net.SocketAddress;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;

final class TestConnectionImpl implements TestConnection {
private final Env env;
private final ServerProcess process;
private final PlayerConnectionImpl playerConnection = new PlayerConnectionImpl();
private volatile Optional<PlayerProvider> playerProvider = Optional.of(TestPlayerImpl::new);

private final List<IncomingCollector<ServerPacket>> incomingTrackers = new CopyOnWriteArrayList<>();

TestConnectionImpl(Env env) {
this.env = env;
this.process = env.process();
}

@Override
public void setCustomPlayerProvider(@NotNull PlayerProvider provider) {
this.playerProvider = Optional.ofNullable(provider);
}

@Override
public @NotNull CompletableFuture<Player> connect(@NotNull Instance instance, @NotNull Pos pos) {
// Use player provider to disable queued chunk sending
process.connection().setPlayerProvider(TestPlayerImpl::new);
process.connection().setPlayerProvider(playerProvider.orElse(TestPlayerImpl::new));

playerConnection.setConnectionState(ConnectionState.LOGIN);
var player = process.connection().createPlayer(playerConnection, UUID.randomUUID(), "RandName");
Expand Down
Loading