Skip to content

Commit

Permalink
Add connect method which use a default position (#102)
Browse files Browse the repository at this point in the history
* Add connect to method which use Pos.ZERO as default position

* Cleanup imports

* Refactor weather test and use connect without a position parameter

* Revert some changes to fix test execution

* Fix indent at a semicolon
  • Loading branch information
theEvilReaper authored Nov 18, 2024
1 parent b63f386 commit b2eea60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
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
23 changes: 16 additions & 7 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,33 @@

@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();
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 +57,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

0 comments on commit b2eea60

Please sign in to comment.