fix: allow custom player provider in env tests (fixes #2438)

This commit is contained in:
mworzala 2024-10-23 07:49:01 -04:00 committed by Matt Worzala
parent 102db50d05
commit 5e17487155
3 changed files with 39 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -19,6 +19,10 @@ final class EnvImpl implements Env {
public EnvImpl(ServerProcess process) { public EnvImpl(ServerProcess process) {
this.process = 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 @Override

View File

@ -36,8 +36,6 @@ final class TestConnectionImpl implements TestConnection {
TestConnectionImpl(Env env) { TestConnectionImpl(Env env) {
this.env = env; this.env = env;
this.process = env.process(); this.process = env.process();
// Use player provider to disable queued chunk sending
env.process().connection().setPlayerProvider(TestPlayerImpl::new);
} }
@Override @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) { public TestPlayerImpl(@NotNull PlayerConnection playerConnection, @NotNull GameProfile gameProfile) {
super(playerConnection, gameProfile); super(playerConnection, gameProfile);
} }