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
FieldsModifier 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 TypeMethodDescriptiondefault org.joml.Vector2i
assertScreenshotContains
(String templateImage) Takes a screenshot, searches for the template image in that screenshot, and throws if it wasn't found.org.joml.Vector2i
Takes a screenshot, searches for a template image in that screenshot, and throws if it wasn't found.default void
assertScreenshotEquals
(String templateImage) Takes a screenshot, matches it against the template image, and throws if it doesn't match.void
Takes a screenshot, matches it against a template image, and throws if it doesn't match.void
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.default Path
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
-
assertScreenshotEquals
Takes a screenshot, matches it against the template image, and throws if it doesn't match. This method does a fuzzy match, seeTestScreenshotComparisonOptions
for details.- Parameters:
templateImage
- The path to the template image. The template image should be in thetemplates
directory in the resources directory of the mod which registers the gametest
-
assertScreenshotEquals
Takes a screenshot, matches it against a template image, and throws if it doesn't match. The exact details of these steps are specified in the given options, see the documentation for that class for details.- Parameters:
options
- The options for the screenshot comparison
-
assertScreenshotContains
Takes a screenshot, searches for the template image in that screenshot, and throws if it wasn't found. This method searches with a fuzzy match, seeTestScreenshotComparisonOptions
for details.- Parameters:
templateImage
- The path to the template image. The template image should be in thetemplates
directory in the resources directory of the mod which registers the gametest- Returns:
- The coordinates of the template image that was found. If there are multiple matches, returns one arbitrarily
-
assertScreenshotContains
Takes a screenshot, searches for a template image in that screenshot, and throws if it wasn't found. The exact details of these steps are specified in the given options, see the documentation for that class for details.- Parameters:
options
- The options for screenshot comparison- Returns:
- The coordinates of the template image that was found. If there are multiple matches, returns one arbitrarily
-
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
-