Interface TestServerConnection

All Known Subinterfaces:
TestDedicatedServerConnection

@NonExtendable public interface TestServerConnection
Context for a client gametest containing various helpful functions while a connection to a server is open.

Unless otherwise specified, methods in this class can only be called on the client gametest thread.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default timeout in ticks to wait for chunks to load/render (1 minute).
  • Method Summary

    Modifier and Type
    Method
    Description
    net.minecraft.client.multiplayer.ClientLevel
    Gets the client level.
    net.minecraft.client.player.LocalPlayer
    Gets the client player.
    net.minecraft.server.level.ServerLevel
    Gets the server level of the same dimension as the client level.
    net.minecraft.server.level.ServerPlayer
    Gets the server player corresponding to the connected client.
    default int
    Waits for all chunks that will be downloaded from the server to be downloaded.
    int
    waitForChunksDownload(int timeout)
    Waits for all chunks that will be downloaded from the server to be downloaded.
    default int
    Waits for all chunks to be downloaded and rendered.
    default int
    waitForChunksRender(boolean waitForDownload)
    Waits for all chunks to be rendered, optionally waiting for chunks to be downloaded first.
    int
    waitForChunksRender(boolean waitForDownload, int timeout)
    Waits for all chunks to be rendered, optionally waiting for chunks to be downloaded first.
    default int
    waitForChunksRender(int timeout)
    Waits for all chunks to be downloaded and rendered.
    void
    waitForClientboundEntityUpdates(net.minecraft.world.entity.EntityType<?> entityType, net.minecraft.world.entity.EntityType<?>... moreEntityTypes)
    Waits for updates to entities of the specified types on the server to be sent, and received and processed by the client.
    void
    Waits for all packets that have already been sent on the server to be received and processed by the client.
    void
    Waits for all packets that have already been sent on the client to be received and processed by the server.
  • Field Details

    • DEFAULT_CHUNK_LOAD_TIMEOUT

      static final int DEFAULT_CHUNK_LOAD_TIMEOUT
      The default timeout in ticks to wait for chunks to load/render (1 minute).
      See Also:
  • Method Details

    • waitForChunksDownload

      default int waitForChunksDownload()
      Waits for all chunks that will be downloaded from the server to be downloaded. Fails if the chunks haven't been downloaded after DEFAULT_CHUNK_LOAD_TIMEOUT ticks. See waitForChunksDownload(int) for details.
      Returns:
      The number of ticks waited
    • waitForChunksDownload

      int waitForChunksDownload(int timeout)
      Waits for all chunks that will be downloaded from the server to be downloaded. After this, methods such as Level.getChunk(int, int) and Level.getBlockState(BlockPos) will return the expected value. However, the chunks may not yet be rendered and may not appear in screenshots, if you need this, use waitForChunksRender(int) instead. Fails if the chunks haven't been downloaded after timeout ticks.
      Parameters:
      timeout - The number of ticks before timing out
      Returns:
      The number of ticks waited
    • waitForChunksRender

      default int waitForChunksRender()
      Waits for all chunks to be downloaded and rendered. After this, all chunks that will ever be visible are visible in screenshots. Fails if the chunks haven't been downloaded and rendered after DEFAULT_CHUNK_LOAD_TIMEOUT ticks.
      Returns:
      The number of ticks waited
    • waitForChunksRender

      default int waitForChunksRender(int timeout)
      Waits for all chunks to be downloaded and rendered. After this, all chunks that will ever be visible are visible in screenshots. Fails if the chunks haven't been downloaded and rendered after timeout ticks.
      Parameters:
      timeout - The number of ticks before timing out
      Returns:
      The number of ticks waited
    • waitForChunksRender

      default int waitForChunksRender(boolean waitForDownload)
      Waits for all chunks to be rendered, optionally waiting for chunks to be downloaded first. After this, all chunks that are present in the client level will be visible in screenshots. Fails if the chunks haven't been rendered (and optionally downloaded) after DEFAULT_CHUNK_LOAD_TIMEOUT ticks.
      Parameters:
      waitForDownload - Whether to wait for chunks to be downloaded
      Returns:
      The number of ticks waited
    • waitForChunksRender

      int waitForChunksRender(boolean waitForDownload, int timeout)
      Waits for all chunks to be rendered, optionally waiting for chunks to be downloaded first. After this, all chunks that are present in the client level will be visible in screenshots. Fails if the chunks haven't been rendered (and optionally downloaded) after timeout ticks.
      Parameters:
      waitForDownload - Whether to wait for chunks to be downloaded
      timeout - The number of ticks before timing out
      Returns:
      The number of ticks waited
    • waitForClientboundPackets

      void waitForClientboundPackets()
      Waits for all packets that have already been sent on the server to be received and processed by the client.

      Note that the server batches some updates, sending them later in the tick, so in some cases a wait may need to be added before calling this method to ensure the packets are sent. Notable examples include:

      It may be tempting to call ClientGameTestContext.waitTick() instead of this method. This often appears to work, especially in singleplayer, since packets can often take less than a tick to arrive. However it is not 100% reliable and will produce flaky tests. For a similar reason, forgetting to call ClientGameTestContext.waitTick() before this method for a block change often works anyway, since the packets sent by the server batching block updates can still arrive before control is returned to the client gametest thread, however this is not guaranteed.

    • waitForServerboundPackets

      void waitForServerboundPackets()
      Waits for all packets that have already been sent on the client to be received and processed by the server.
    • waitForClientboundEntityUpdates

      void waitForClientboundEntityUpdates(net.minecraft.world.entity.EntityType<?> entityType, net.minecraft.world.entity.EntityType<?>... moreEntityTypes)
      Waits for updates to entities of the specified types on the server to be sent, and received and processed by the client. This waits the maximum of all the update intervals of the specified entity types, then waits for the packets to be received.

      Some entities interpolate on the client when they are moved, rather than snapping immediately to the right position. If you encounter issues with this, you may need to wait for the interpolation to finish after calling this method. Living entities interpolate for LinearInterpolationHandler.DEFAULT_INTERPOLATION_STEPS ticks.

      Parameters:
      entityType - The entity type to wait for
      moreEntityTypes - Additional entity types to wait for
    • getClientPlayer

      net.minecraft.client.player.LocalPlayer getClientPlayer()
      Gets the client player.

      This method can only be called from the render (client) thread.

      Returns:
      The client player
    • getServerPlayer

      net.minecraft.server.level.ServerPlayer getServerPlayer()
      Gets the server player corresponding to the connected client.

      This method can only be called from the server thread.

      Returns:
      The server player
    • getClientLevel

      net.minecraft.client.multiplayer.ClientLevel getClientLevel()
      Gets the client level.

      This method can only be called from the render (client) thread.

      Returns:
      The client level
    • getServerLevel

      net.minecraft.server.level.ServerLevel getServerLevel()
      Gets the server level of the same dimension as the client level.

      This method can only be called from the server thread.

      Returns:
      The server level