Interface ClientGameTestContext
@NonExtendable
public interface ClientGameTestContext
Context for a client gametest containing various helpful functions and functions to access the game.
Functions in this class can only be called on the client gametest thread.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default timeout in ticks for wait tasks (10 seconds).static final int
Used to specify that a wait task should have no timeout. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clickScreenButton
(String translationKey) Presses the button in the current screen whose label is the given translation key.<T,
E extends Throwable>
TcomputeOnClient
(org.apache.commons.lang3.function.FailableFunction<MinecraftClient, T, E> function) Runs the given function on the render thread (client thread), and returns the result.getInput()
Gets the input handler used to simulate inputs to the client.void
Restores all game options inMinecraftClient.options
to their default values for client gametests.<E extends Throwable>
voidrunOnClient
(org.apache.commons.lang3.function.FailableConsumer<MinecraftClient, E> action) Runs the given action on the render thread (client thread), and waits for it to complete.void
Opens aScreen
on the client.takeScreenshot
(String name) Takes a screenshot and saves it in the screenshots directory.takeScreenshot
(TestScreenshotOptions options) Takes a screenshot with the given options.boolean
tryClickScreenButton
(String translationKey) Presses the button in the current screen whose label is the given translation key, if the button exists.int
waitFor
(Predicate<MinecraftClient> predicate) Waits for a predicate to be true.int
waitFor
(Predicate<MinecraftClient> predicate, int timeout) Waits for a predicate to be true.int
waitForScreen
(@Nullable Class<? extends Screen> screenClass) Waits for the given screen class to be shown.void
waitTick()
Runs a single tick and waits for it to complete.void
waitTicks
(int ticks) Runsticks
ticks and waits for them to complete.Creates a world builder for creating singleplayer worlds and dedicated servers.
-
Field Details
-
NO_TIMEOUT
static final int NO_TIMEOUTUsed to specify that a wait task should have no timeout.- See Also:
-
DEFAULT_TIMEOUT
static final int DEFAULT_TIMEOUTThe default timeout in ticks for wait tasks (10 seconds).- See Also:
-
-
Method Details
-
waitTick
void waitTick()Runs a single tick and waits for it to complete. -
waitTicks
void waitTicks(int ticks) Runsticks
ticks and waits for them to complete.- Parameters:
ticks
- The amount of ticks to run
-
waitFor
Waits for a predicate to be true. Fails if the predicate is not satisfied afterDEFAULT_TIMEOUT
ticks.- Parameters:
predicate
- The predicate to check- Returns:
- The number of ticks waited
-
waitFor
Waits for a predicate to be true. Fails if the predicate is not satisfied aftertimeout
ticks. Iftimeout
isNO_TIMEOUT
, there is no timeout.- Parameters:
predicate
- The predicate to checktimeout
- The number of ticks before timing out- Returns:
- The number of ticks waited
-
waitForScreen
Waits for the given screen class to be shown. IfscreenClass
isnull
, waits for the current screen to benull
. Fails if the screen does not open afterDEFAULT_TIMEOUT
ticks.- Parameters:
screenClass
- The screen class to wait to open- Returns:
- The number of ticks waited
-
setScreen
Opens aScreen
on the client.- Parameters:
screen
- The screen to open- See Also:
-
clickScreenButton
Presses the button in the current screen whose label is the given translation key. Fails if the button couldn't be found.- Parameters:
translationKey
- The translation key of the label of the button to press
-
tryClickScreenButton
Presses the button in the current screen whose label is the given translation key, if the button exists. Returns whether the button was found.- Parameters:
translationKey
- The translation key of the label of the button to press- Returns:
- Whether the button was found
-
takeScreenshot
Takes a screenshot and saves it in the screenshots directory.- Parameters:
name
- The name of the screenshot- Returns:
- The
Path
to the screenshot
-
takeScreenshot
Takes a screenshot with the given options.- Parameters:
options
- TheTestScreenshotOptions
to take the screenshot with- Returns:
- The
Path
to the screenshot
-
getInput
TestInput getInput()Gets the input handler used to simulate inputs to the client.- Returns:
- The client gametest input handler
-
worldBuilder
TestWorldBuilder worldBuilder()Creates a world builder for creating singleplayer worlds and dedicated servers.- Returns:
- A new world builder
-
restoreDefaultGameOptions
void restoreDefaultGameOptions()Restores all game options inMinecraftClient.options
to their default values for client gametests. This is called automatically before each gametest is run, so you only need to call this explicitly if you want to do it in the middle of the test. -
runOnClient
<E extends Throwable> void runOnClient(org.apache.commons.lang3.function.FailableConsumer<MinecraftClient, E> action) throws ERuns the given action on the render thread (client thread), and waits for it to complete.- Type Parameters:
E
- The type of checked exception that the action throws- Parameters:
action
- The action to run on the render thread- Throws:
E
- When the action throws an exception
-
computeOnClient
<T,E extends Throwable> T computeOnClient(org.apache.commons.lang3.function.FailableFunction<MinecraftClient, T, throws EE> function) Runs the given function on the render thread (client thread), and returns the result.- Type Parameters:
T
- The type of the value to returnE
- The type of the checked exception that the function throws- Parameters:
function
- The function to run on the render thread- Returns:
- The result of the function
- Throws:
E
- When the function throws an exception
-