From 5e17487155379174af7b4c7f045cb5608fe0e3c4 Mon Sep 17 00:00:00 2001 From: mworzala Date: Wed, 23 Oct 2024 07:49:01 -0400 Subject: [PATCH] fix: allow custom player provider in env tests (fixes #2438) --- .../test/EnvTestPlayerProviderTest.java | 34 +++++++++++++++++++ .../java/net/minestom/testing/EnvImpl.java | 4 +++ .../minestom/testing/TestConnectionImpl.java | 4 +-- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/minestom/server/test/EnvTestPlayerProviderTest.java diff --git a/src/test/java/net/minestom/server/test/EnvTestPlayerProviderTest.java b/src/test/java/net/minestom/server/test/EnvTestPlayerProviderTest.java new file mode 100644 index 000000000..afc575747 --- /dev/null +++ b/src/test/java/net/minestom/server/test/EnvTestPlayerProviderTest.java @@ -0,0 +1,34 @@ +package net.minestom.server.test; + +import net.minestom.server.coordinate.Pos; +import net.minestom.server.entity.Player; +import net.minestom.server.network.player.GameProfile; +import net.minestom.server.network.player.PlayerConnection; +import net.minestom.testing.Env; +import net.minestom.testing.EnvTest; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +@EnvTest +public class EnvTestPlayerProviderTest { + + public static class CustomPlayer extends Player { + public CustomPlayer(@NotNull PlayerConnection playerConnection, @NotNull GameProfile gameProfile) { + super(playerConnection, gameProfile); + } + } + + @Test + void testPlayerProviderUsedInEnvTest(Env env) { + // Note: By default the test environment will use a player provider of its own to bypass the queued chunk system + // overriding in a particular test will mean that chunk packets are not received consistently (they require the + // chunk queue interaction). However, this is not a problem for many tests, so we do support it. + + env.process().connection().setPlayerProvider(CustomPlayer::new); + var instance = env.createFlatInstance(); + var player = env.createPlayer(instance, new Pos(0, 42, 0)); + assertInstanceOf(CustomPlayer.class, player); + } +} diff --git a/testing/src/main/java/net/minestom/testing/EnvImpl.java b/testing/src/main/java/net/minestom/testing/EnvImpl.java index 89f1c9c7a..ec02253fb 100644 --- a/testing/src/main/java/net/minestom/testing/EnvImpl.java +++ b/testing/src/main/java/net/minestom/testing/EnvImpl.java @@ -19,6 +19,10 @@ final class EnvImpl implements Env { public EnvImpl(ServerProcess process) { this.process = process; + + // Use player provider to disable queued chunk sending. + // Set here to allow an individual test to override if they want. + process.connection().setPlayerProvider(TestConnectionImpl.TestPlayerImpl::new); } @Override diff --git a/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java b/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java index 114adee4d..ae9877111 100644 --- a/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java +++ b/testing/src/main/java/net/minestom/testing/TestConnectionImpl.java @@ -36,8 +36,6 @@ final class TestConnectionImpl implements TestConnection { TestConnectionImpl(Env env) { this.env = env; this.process = env.process(); - // Use player provider to disable queued chunk sending - env.process().connection().setPlayerProvider(TestPlayerImpl::new); } @Override @@ -133,7 +131,7 @@ final class TestConnectionImpl implements TestConnection { } } - final class TestPlayerImpl extends Player { + static final class TestPlayerImpl extends Player { public TestPlayerImpl(@NotNull PlayerConnection playerConnection, @NotNull GameProfile gameProfile) { super(playerConnection, gameProfile); }