Overview
Simple wrapper for making and sending toasts
Last updated
Simple wrapper for making and sending toasts
Last updated
The Toasts module is a simple way of showing Toasts to the player.
Toasts created by this module are composed of a background format, a title, an optional message, and optional icon made of an image file or ItemStack, and an optional progress bar along the bottom.
Minecraft has 5 slots available to any given toast, and a given toast can be up to 5 slots in height. Any toasts not able to immediately be placed on the screen will be queued until there is space.
The simplest way to get a message across is to use Toasts.INSTANCE.sendFromMod()
:
This automatically grabs the icon and title from the given mod ID, adds the given message, and adds a timer and matching progress bar. You can see it in action in the above GIF.
For anything more interesting, you can use the builder from ToastBuilder.builder()
. This allows you to use custom images, progress bar mechanics and colours.
There are 4 built-in formats for toasts, recreating the 4 types used in the vanilla game. They are available under the ToastFormat
class.
It is possible to create your own formats with according text and progress bar colours; simply create your own instance of the ToastFormat record. However, for multi-version mods the format varies depending on the version you are targeting:
For 1.20.2 and above, create a GUI sprite as normal, as seen here.
For 1.20.1 and below, you will need to supply the sprite and UV information in the code, as seen here.
Progress bars are shown at the bottom of the toast. Their values range between 0f and 1f, and can be used in 3 different ways:
As an indicator for how long the toast has left, as seen in the above two examples;E
Pushing progress values from outside, by using CustomToast.setProgress()
on the built toast object;
Pulling progress values from the toast every frame, by using ToastBuilder.progressPuller()
.
Toasts can optionally have images, supplied via ToastBuilder.withIcon()
. There are built-in methods to create icons that display textures, items or lists of items in the ToastIcon
class.
It is also possible to implement the ToastIcon
interface yourself if you so wish.
By default, toasts expire after a given number of milliseconds (modified by the accessibility multiplier). However, if you want it to disappear based on other criteria you can use expiresWhenProgressComplete()
. This will cause the given toast to expire when progress is set to 1.
Make sure you remember to complete the progress bar if this is the case, either through CustomToast.setProgress
or ToastBuilder.progressPuller
. Otherwise, they may never disappear off the user's screen.