Skip to content

Commit

Permalink
Fix custom player provider usage for junit (#81)
Browse files Browse the repository at this point in the history
* Add possibility for custom player provider

* Improve documentation for custom player provider method at test connection
  • Loading branch information
TheMeinerLP committed Oct 31, 2024
1 parent 3b86227 commit b79591f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
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

0 comments on commit b79591f

Please # to comment.