From b79591faac5f076780733d696d2679e2f3cae6b4 Mon Sep 17 00:00:00 2001 From: Phillipp Glanz <6745190+TheMeinerLP@users.noreply.github.com> Date: Tue, 15 Oct 2024 20:33:20 +0200 Subject: [PATCH] Fix custom player provider usage for junit (#81) * Add possibility for custom player provider * Improve documentation for custom player provider method at test connection --- .../net/minestom/testing/TestConnection.java | 44 +++++++++++++++++++ .../minestom/testing/TestConnectionImpl.java | 12 +++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/testing/src/main/java/net/minestom/testing/TestConnection.java b/testing/src/main/java/net/minestom/testing/TestConnection.java index c25b74be0e6..a2c37dea7fa 100644 --- a/testing/src/main/java/net/minestom/testing/TestConnection.java +++ b/testing/src/main/java/net/minestom/testing/TestConnection.java @@ -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. + *
+ * For a successful test you need to override the sendChunk method in the player implementation. + * Example: + *
{@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());
+     *    }
+     * }
+     * }
+ * @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 the packet type + * @return a collector for the tracked packets + */ @NotNull Collector trackIncoming(@NotNull Class type); + /** + * Tracks all incoming packets. + * + * @return a collector for all incoming packets + */ default @NotNull Collector trackIncoming() { return trackIncoming(ServerPacket.class); } diff --git a/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java b/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java index f6d963728d5..588dc50145d 100644 --- a/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java +++ b/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java @@ -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; @@ -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 = Optional.of(TestPlayerImpl::new); private final List> 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 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");