Class TextHelper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic net.minecraft.network.chat.ComponentapplyFont(net.minecraft.network.chat.Component text, net.minecraft.resources.ResourceLocation font) Recursively applies a font to text and all of its subcomponents.static net.minecraft.network.chat.MutableComponentCreates a text component that will copy the value to the players clipboard when they click it.static <T> StringformatCollection(Collection<T> collection) Formats a collection of values to a string usingObject.toString().static <T> StringformatCollection(Collection<T> collection, Function<T, String> formatter, String delimiter) Formats a collection of values to a string.static net.minecraft.network.chat.MutableComponentformatDuration(int ticks) Formats a duration of time in ticks into its real world time counterpart.static net.minecraft.network.chat.MutableComponentformatDuration(int ticks, boolean showTicksOnHover, float tickRate) Formats a duration of time in ticks into its real world time counterpart.static net.minecraft.network.chat.MutableComponentformatDuration(int ticks, boolean includeHover, net.minecraft.world.level.Level level) Formats a duration of time in ticks into its real world time counterpart.static net.minecraft.network.chat.MutableComponentformatDuration(int ticks, net.minecraft.world.level.Level level) Formats a duration of time in ticks into its real world time counterpart.static net.minecraft.network.chat.MutableComponentfromResourceLocation(@Nullable String prefix, @Nullable String suffix, net.minecraft.resources.ResourceLocation location, Object... args) Creates translated text from a resource location.getPossibleMatches(String input, Iterable<String> candidates) Finds a set of possible matches within an iterable group of strings.getPossibleMatches(String input, Iterable<String> candidates, int threshold) Finds a set of possible matches within an iterable group of strings.static Set<net.minecraft.resources.ResourceLocation> static StringgetTagName(net.minecraft.tags.TagKey<?> tag) Creates a translation key that should map to a display name for the tag.static net.minecraft.network.chat.HoverEventhoverEvent(net.minecraft.world.entity.Entity entity) Creates a new hover event for an entity.static net.minecraft.network.chat.MutableComponentjoin(net.minecraft.network.chat.Component separator, Collection<net.minecraft.network.chat.Component> toJoin) Joins several components together using a separator.static net.minecraft.network.chat.MutableComponentjoin(net.minecraft.network.chat.Component separator, Iterator<net.minecraft.network.chat.Component> toJoin) Joins several components together using a separator.static net.minecraft.network.chat.MutableComponentjoin(net.minecraft.network.chat.Component separator, net.minecraft.network.chat.Component... toJoin) Joins several components together using a separator.static @Nullable net.minecraft.network.chat.MutableComponentlookupTranslation(String key, Object... args) Attempts to localize text.static @Nullable net.minecraft.network.chat.MutableComponentlookupTranslation(String key, @Nullable BiFunction<String, Object[], net.minecraft.network.chat.MutableComponent> fallback, Object... args) Attempts to localize text.static @Nullable net.minecraft.network.chat.MutableComponentlookupTranslation(String key, net.minecraft.network.chat.MutableComponent fallback, Object... args) Attempts to localize text.static @Nullable net.minecraft.network.chat.MutableComponentlookupTranslationWithAlias(String[] keys, Object... params) Attempts to localize several different translation keys and will return the first one that is available on the client.static @Nullable net.minecraft.network.chat.MutableComponentlookupTranslationWithAlias(net.minecraft.resources.ResourceLocation id, String... keys) Attempts to localize several different translation keys and will return the first one that is available on the client.static net.minecraft.network.chat.MutableComponentmutable(net.minecraft.network.chat.Component component) Provides mutable access to a component.static net.minecraft.network.chat.MutableComponentsetCopyText(net.minecraft.network.chat.MutableComponent component, String copy) Adds a click event to a text component that will copy text to the players clipboard when they click on it.static net.minecraft.network.chat.MutableComponentwithHover(net.minecraft.network.chat.Component base, net.minecraft.network.chat.Component hover) Applies hover text to a text component.static net.minecraft.network.chat.MutableComponentwithHover(net.minecraft.network.chat.Component base, net.minecraft.network.chat.HoverEvent hover) Applies a hover event to a text component.static net.minecraft.network.chat.MutableComponentwithHover(net.minecraft.network.chat.Component base, net.minecraft.world.entity.Entity hover) Applies hover text based on an entity to a text component.static net.minecraft.network.chat.MutableComponentwithHover(net.minecraft.network.chat.Component base, net.minecraft.world.item.ItemStack hover) Applies hover text based on an item to a text component.
-
Constructor Details
-
TextHelper
public TextHelper()
-
-
Method Details
-
fromResourceLocation
public static net.minecraft.network.chat.MutableComponent fromResourceLocation(@Nullable @Nullable String prefix, @Nullable @Nullable String suffix, net.minecraft.resources.ResourceLocation location, Object... args) Creates translated text from a resource location.- Parameters:
prefix- The prefix to add at the start of the key.suffix- The suffix to add at the end of the key.location- The resource location to use in the key.args- An optional array of arguments to format into the translated text.- Returns:
- A translated component based on the resource location.
-
formatDuration
public static net.minecraft.network.chat.MutableComponent formatDuration(int ticks) Formats a duration of time in ticks into its real world time counterpart. This method should ONLY be used if a world context is not available or if the duration of time is not affected by custom world tick rates.- Parameters:
ticks- The duration of ticks.- Returns:
- The formatted time duration.
-
formatDuration
public static net.minecraft.network.chat.MutableComponent formatDuration(int ticks, net.minecraft.world.level.Level level) Formats a duration of time in ticks into its real world time counterpart.- Parameters:
ticks- The duration of ticks.level- The world level that the duration is taking place. This is used to account for custom tick rates set using the game rule.- Returns:
- The formatted time duration.
-
formatDuration
public static net.minecraft.network.chat.MutableComponent formatDuration(int ticks, boolean includeHover, net.minecraft.world.level.Level level) Formats a duration of time in ticks into its real world time counterpart.- Parameters:
ticks- The duration of ticks.includeHover- Should the raw tick amount be shown when hovering the text?level- The world level that the duration is taking place. This is used to account for custom tick rates set using the game rule.- Returns:
- The formatted time duration.
-
formatDuration
public static net.minecraft.network.chat.MutableComponent formatDuration(int ticks, boolean showTicksOnHover, float tickRate) Formats a duration of time in ticks into its real world time counterpart.- Parameters:
ticks- The duration of ticks.showTicksOnHover- Should the raw tick amount be shown when hovering the text?tickRate- The tick rate of the current world.- Returns:
- The formatted time duration.
-
withHover
public static net.minecraft.network.chat.MutableComponent withHover(net.minecraft.network.chat.Component base, net.minecraft.network.chat.Component hover) Applies hover text to a text component.- Parameters:
base- The base text component to append.hover- The text to display while hovering.- Returns:
- A component instance with the hover event applied.
-
withHover
public static net.minecraft.network.chat.MutableComponent withHover(net.minecraft.network.chat.Component base, net.minecraft.world.entity.Entity hover) Applies hover text based on an entity to a text component.- Parameters:
base- The base text component to append.hover- The Entity to display in the hover text.- Returns:
- A component instance with the hover event applied.
-
withHover
public static net.minecraft.network.chat.MutableComponent withHover(net.minecraft.network.chat.Component base, net.minecraft.world.item.ItemStack hover) Applies hover text based on an item to a text component.- Parameters:
base- The base text component to append.hover- The ItemStack to display in the hover text.- Returns:
- A component instance with the hover event applied.
-
withHover
public static net.minecraft.network.chat.MutableComponent withHover(net.minecraft.network.chat.Component base, net.minecraft.network.chat.HoverEvent hover) Applies a hover event to a text component.- Parameters:
base- The base text component to append.hover- The hover event to apply.- Returns:
- A component instance with the hover event applied.
-
hoverEvent
public static net.minecraft.network.chat.HoverEvent hoverEvent(net.minecraft.world.entity.Entity entity) Creates a new hover event for an entity.- Parameters:
entity- The entity to create a HoverEvent for.- Returns:
- When Mixins are available the entity will create its own HoverEvent, otherwise a fallback based on the default implementation will be used.
-
mutable
public static net.minecraft.network.chat.MutableComponent mutable(net.minecraft.network.chat.Component component) Provides mutable access to a component.- Parameters:
component- The component to access.- Returns:
- If the component is already mutable the same component instance will be returned. Otherwise, a mutable copy of the component will be created.
-
applyFont
public static net.minecraft.network.chat.Component applyFont(net.minecraft.network.chat.Component text, net.minecraft.resources.ResourceLocation font) Recursively applies a font to text and all of its subcomponents.- Parameters:
text- The text to apply the font to.font- The ID of the font to apply.- Returns:
- The input text with the font applied to its style and the style of its subcomponents.
-
lookupTranslationWithAlias
@Nullable public static @Nullable net.minecraft.network.chat.MutableComponent lookupTranslationWithAlias(net.minecraft.resources.ResourceLocation id, String... keys) Attempts to localize several different translation keys and will return the first one that is available on the client. If no keys are mapped the result will be null.- Parameters:
id- An ID the format within each key using basic string formatting. The first parameter is the namespace and the second is the path. For example if a key was "tooltip.{0}.{1}.info" the ID "minecraft:stick" will produce a final key of "tooltip.minecraft.stick.info".keys- An array of translation keys to attempt localizing.- Returns:
- A component for the first translation key that is mapped, or null if none of the keys are mapped.
-
lookupTranslationWithAlias
@Nullable public static @Nullable net.minecraft.network.chat.MutableComponent lookupTranslationWithAlias(String[] keys, Object... params) Attempts to localize several different translation keys and will return the first one that is available on the client. If no keys are mapped the result will be null.- Parameters:
keys- An array of translation keys to attempt localizing.params- Arguments that are passed into the translated text.- Returns:
- A component for the first translation key that is mapped, or null if none of the keys are mapped.
-
lookupTranslation
@Nullable public static @Nullable net.minecraft.network.chat.MutableComponent lookupTranslation(String key, Object... args) Attempts to localize text. If the translation key is not mapped on the client the component will be null.- Parameters:
key- The translation key to localize.args- Arguments that are passed into the translated text.- Returns:
- If the key can be translated a component will be returned, otherwise null.
-
lookupTranslation
@Nullable public static @Nullable net.minecraft.network.chat.MutableComponent lookupTranslation(String key, net.minecraft.network.chat.MutableComponent fallback, Object... args) Attempts to localize text. If the translation key is not mapped on the client the fallback will be used.- Parameters:
key- The translation key to localize.fallback- The fallback text to use when the key is unavailable.args- Arguments that are passed into the translated text.- Returns:
- If the key can be translated a component will be returned, otherwise the fallback will be used.
-
lookupTranslation
@Nullable public static @Nullable net.minecraft.network.chat.MutableComponent lookupTranslation(String key, @Nullable @Nullable BiFunction<String, Object[], net.minecraft.network.chat.MutableComponent> fallback, Object... args) Attempts to localize text. If the translation key is not mapped on the client it will try to use the fallback.- Parameters:
key- The translation key to localize.fallback- A function that provides fallback text based on the original translation key and arguments. Both the function and the result of this function may be null.args- Arguments that are passed into the translated text.- Returns:
- If the key can be translated a component will be returned, otherwise the fallback will be used.
-
copyText
Creates a text component that will copy the value to the players clipboard when they click it.- Parameters:
text- The text to display and copy to the clipboard.- Returns:
- A component that displays text and copies that text to the clipboard when the player clicks on it.
-
setCopyText
public static net.minecraft.network.chat.MutableComponent setCopyText(net.minecraft.network.chat.MutableComponent component, String copy) Adds a click event to a text component that will copy text to the players clipboard when they click on it.- Parameters:
component- The component to attack the click event to.copy- The text to be copied to the clipboard.- Returns:
- A text component that will copy the text when the player clicks on it.
-
join
public static net.minecraft.network.chat.MutableComponent join(net.minecraft.network.chat.Component separator, net.minecraft.network.chat.Component... toJoin) Joins several components together using a separator.- Parameters:
separator- The separator to insert between other components.toJoin- The components to join together.- Returns:
- A component containing the joint components.
-
join
public static net.minecraft.network.chat.MutableComponent join(net.minecraft.network.chat.Component separator, Collection<net.minecraft.network.chat.Component> toJoin) Joins several components together using a separator.- Parameters:
separator- The separator to insert between other components.toJoin- The components to join together.- Returns:
- A component containing the joint components.
-
join
public static net.minecraft.network.chat.MutableComponent join(net.minecraft.network.chat.Component separator, Iterator<net.minecraft.network.chat.Component> toJoin) Joins several components together using a separator.- Parameters:
separator- The separator to insert between other components.toJoin- The components to join together.- Returns:
- A component containing the joint components.
-
getPossibleMatches
Finds a set of possible matches within an iterable group of strings. This can be used to take invalid user input and attempt to find a plausible match using known good values.Possible matches are determined using the Levenshtein distance between the input value and the potential candidates. The Levenshtein distance represents the number of characters that need to be changed in order for the strings to match. For example "abc" to "def" has a difference of three, while "123" to "1234" has a distance of 1.
- Parameters:
input- The input string.candidates- An iterable group of possible candidates.- Returns:
- A set of possible matches for the input. This set will include all candidates that have the lowest possible distance. For example if there were 100 candidates and five had a distance of one all five of the lowest distance values will be returned.
-
getPossibleMatches
public static Set<String> getPossibleMatches(String input, Iterable<String> candidates, int threshold) Finds a set of possible matches within an iterable group of strings. This can be used to take invalid user input and attempt to find a plausible match using known good values.Possible matches are determined using the Levenshtein distance between the input value and the potential candidates. The Levenshtein distance represents the number of characters that need to be changed in order for the strings to match. For example "abc" to "def" has a difference of three, while "123" to "1234" has a distance of 1.
- Parameters:
input- The input string.candidates- An iterable group of possible candidates.threshold- The maximum distance allowed for a value to be considered. For example if the threshold is two, only entries with a distance of two or less will be considered.- Returns:
- A set of possible matches for the input. This set will include all candidates that have the lowest possible distance. For example if there were 100 candidates and five had a distance of one all five of the lowest distance values will be returned.
-
formatCollection
Formats a collection of values to a string usingObject.toString(). If the collection has more than one value each entry will be separated by commas. Each value will also be quoted.- Type Parameters:
T- The type of value being formatted.- Parameters:
collection- The collection of values to format.- Returns:
- The formatted string.
-
formatCollection
public static <T> String formatCollection(Collection<T> collection, Function<T, String> formatter, String delimiter) Formats a collection of values to a string. If the collection has more than one value each entry will be separated using the delimiter.- Type Parameters:
T- The type of value being formatted.- Parameters:
collection- The collection of values to format.formatter- A function used to format the value to a string.delimiter- A delimiter used to separate values in a list.- Returns:
- The formatted string.
-
getRegisteredFonts
-
getTagName
Creates a translation key that should map to a display name for the tag.Tags for vanilla registries use the format tag.reg_path.namespace.path and tags for modded registries use the format tag.reg_namespace.reg_path.namespace.path.
This is a new standard being pushed by the Fabric API and recipe viewers. While it has not been universally adopted yet, it should be considered best practice to do so moving forward.
- Parameters:
tag- The tag to provide a name key for.- Returns:
- A translation key that should map to a display name.
-