Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add connect method which use a default position #102

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand Down
26 changes: 18 additions & 8 deletions src/test/java/net/minestom/server/instance/WeatherTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.minestom.server.instance;

import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.ChangeGameStatePacket;
import net.minestom.testing.Env;
import net.minestom.testing.extension.MicrotusExtension;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -14,26 +16,34 @@

@ExtendWith(MicrotusExtension.class)
class WeatherTest {
@Test
void weatherTest(Env env) {
var instance = env.createFlatInstance();

// Defaults
@Test
void testWeatherDefaults(@NotNull Env env) {
Instance instance = env.createFlatInstance();
Weather weather = instance.getWeather();
assertFalse(weather.isRaining());
assertEquals(0, weather.rainLevel());
assertEquals(0, weather.thunderLevel());

env.destroyInstance(instance);
}

@Test
void testWeatherSendOnJoin(@NotNull Env env) {
Instance instance = env.createFlatInstance();

instance.setWeather(new Weather(1, 0.5f), 1);
instance.tick(0);

// Weather sent on instance join
var connection = env.createConnection();
var tracker = connection.trackIncoming(ChangeGameStatePacket.class);
connection.connect(instance, new Pos(0, 0, 0)).join();
var tracker = connection.trackIncoming(ChangeGameStatePacket.class)
;
Player player = connection.connect(instance).join();
assertEquals(Pos.ZERO, player.getPosition());
tracker.assertCount(4);
List<ChangeGameStatePacket> packets = tracker.collect();
var state = packets.get(0);
var state = packets.getFirst();
assertEquals(ChangeGameStatePacket.Reason.BEGIN_RAINING, state.reason());

state = packets.get(1);
Expand All @@ -48,7 +58,7 @@ void weatherTest(Env env) {
var tracker2 = connection.trackIncoming(ChangeGameStatePacket.class);
instance.setWeather(new Weather(0, 0), 2);
instance.tick(0);
state = tracker2.collect().get(0);
state = tracker2.collect().getFirst();
assertEquals(ChangeGameStatePacket.Reason.RAIN_LEVEL_CHANGE, state.reason());
assertEquals(0.5f, state.value());
}
Expand Down
14 changes: 14 additions & 0 deletions testing/src/main/java/net/minestom/testing/TestConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

/**
* Represents a connection to a test server.
*
* @version 1.1.0
* @since 1.0.0
*/
public interface TestConnection {

Expand All @@ -30,6 +33,7 @@ public interface TestConnection {
* }
* }
* }</pre>
*
* @param provider the custom player provider
*/
void setCustomPlayerProvider(@NotNull PlayerProvider provider);
Expand All @@ -43,6 +47,16 @@ public interface TestConnection {
*/
@NotNull CompletableFuture<@NotNull Player> connect(@NotNull Instance instance, @NotNull Pos pos);

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

/**
* Tracks incoming packets of a specific type.
*
Expand Down
Loading