Package net.darkhax.bookshelf.api.util
Class TextHelper
java.lang.Object
net.darkhax.bookshelf.api.util.TextHelper
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final net.minecraft.resources.ResourceLocationA constant reference to the ID of the alt font.static final net.minecraft.resources.ResourceLocationA constant reference to the ID of the default Minecraft font.static final net.minecraft.resources.ResourceLocationA constant reference to the ID of the illager font.static final net.minecraft.resources.ResourceLocationA constant reference to the ID of the uniform font. -
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 custom font to a text component and all of it's children components.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.MutableComponentgetFormattedTime(int ticks) Creates a component that displays a duration of ticks in HH:MM:SS format.static net.minecraft.network.chat.MutableComponentgetFormattedTime(int ticks, boolean includeHover) static net.minecraft.network.chat.MutableComponentgetFormattedTime(int ticks, boolean includeHover, float tickRate) Creates a component that displays a duration of ticks in HH:MM:SS format.static net.minecraft.network.chat.MutableComponentgetFormattedTime(int ticks, boolean includeHover, net.minecraft.world.level.Level level) 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>Gathers a set of font IDs from the game.static net.minecraft.network.chat.MutableComponentjoin(net.minecraft.network.chat.Component separator, Collection<net.minecraft.network.chat.Component> toJoin) Joins multiple components together using a separator component.static net.minecraft.network.chat.MutableComponentjoin(net.minecraft.network.chat.Component separator, Iterator<net.minecraft.network.chat.Component> toJoin) Joins multiple components together using a separator component.static net.minecraft.network.chat.MutableComponentjoin(net.minecraft.network.chat.Component separator, net.minecraft.network.chat.Component... toJoin) Joins multiple components together using a separator component.static net.minecraft.network.chat.MutableComponentlookupTranslation(String key, Object... args) Attempts to localize the text.static net.minecraft.network.chat.MutableComponentlookupTranslation(String key, BiFunction<String, Object[], net.minecraft.network.chat.MutableComponent> fallback, Object... args) Attempts to localize text, if the text can not be localized the fallback will be used.static net.minecraft.network.chat.MutableComponentlookupTranslation(String key, net.minecraft.network.chat.MutableComponent fallback, Object... args) Attempts to localize the text.static net.minecraft.network.chat.MutableComponentlookupTranslationWithAlias(String[] keys, Object... params) Attempts to localize several translation keys, only returning the first key that can be localized.static net.minecraft.network.chat.MutableComponentlookupTranslationWithAlias(net.minecraft.resources.ResourceLocation id, String... keys) Attempts to localize several translation keys.static net.minecraft.network.chat.MutableComponentmutable(net.minecraft.network.chat.Component component) Gets a mutable instance of a component.static net.minecraft.network.chat.MutableComponentsetCopyText(net.minecraft.network.chat.MutableComponent component, String copy) Adds a click event to a component that will copy the provided text to the players clipboard when they click it.static <AV,AT extends net.minecraft.network.chat.HoverEvent.Action<AV>>
net.minecraft.network.chat.MutableComponentsetHover(net.minecraft.network.chat.Component component, AT action, AV value) Attaches a hover event to a component.static net.minecraft.network.chat.MutableComponentsetHover(net.minecraft.network.chat.Component component, net.minecraft.network.chat.Component hoverInfo) Attaches a component to another as a hover component.static net.minecraft.network.chat.MutableComponentsetHover(net.minecraft.network.chat.Component component, net.minecraft.network.chat.HoverEvent hoverInfo) Attaches a hover event to a component.static net.minecraft.network.chat.MutableComponentsetHover(net.minecraft.network.chat.Component component, net.minecraft.world.entity.Entity hoverInfo) Attaches a hover event that displays information about an entity.static net.minecraft.network.chat.MutableComponentsetHover(net.minecraft.network.chat.Component component, net.minecraft.world.item.Item hoverInfo) Attaches a hover event that displays information about an item.static net.minecraft.network.chat.MutableComponentsetHover(net.minecraft.network.chat.Component component, net.minecraft.world.item.ItemStack hoverInfo) Attaches a hover event that displays information about an ItemStack.static net.minecraft.network.chat.MutableComponenttextWithCopy(String text) Creates a component that will copy the value to the players clipboard when they click it.
-
Field Details
-
FONT_DEFAULT
public static final net.minecraft.resources.ResourceLocation FONT_DEFAULTA constant reference to the ID of the default Minecraft font. This font is assumed when no font is specified. -
FONT_ALT
public static final net.minecraft.resources.ResourceLocation FONT_ALTA constant reference to the ID of the alt font. This font displays the Standard Galactic Alphabet and is used by the enchanting table and enchantment system. -
FONT_ILLAGER
public static final net.minecraft.resources.ResourceLocation FONT_ILLAGERA constant reference to the ID of the illager font. This font is associated with the illagers but is not used in the vanilla game. This font originally appeared in the spin-off game Minecraft Dungeons. -
FONT_UNIFORM
public static final net.minecraft.resources.ResourceLocation FONT_UNIFORMA constant reference to the ID of the uniform font. This is a more traditional and unstylized font.
-
-
Constructor Details
-
TextHelper
public TextHelper()
-
-
Method Details
-
getFormattedTime
public static net.minecraft.network.chat.MutableComponent getFormattedTime(int ticks) Creates a component that displays a duration of ticks in HH:MM:SS format. Hovering over the component will display a tooltip with the amount of ticks.- Parameters:
ticks- The duration of ticks to format.- Returns:
- A component that displays the duration of ticks.
-
getFormattedTime
public static net.minecraft.network.chat.MutableComponent getFormattedTime(int ticks, boolean includeHover) -
getFormattedTime
public static net.minecraft.network.chat.MutableComponent getFormattedTime(int ticks, boolean includeHover, net.minecraft.world.level.Level level) -
getFormattedTime
public static net.minecraft.network.chat.MutableComponent getFormattedTime(int ticks, boolean includeHover, float tickRate) Creates a component that displays a duration of ticks in HH:MM:SS format.- Parameters:
ticks- The duration of ticks to format.includeHover- Should the component show a tooltip with the raw tick time when the mouse hovers over it?- Returns:
- A component that displays the duration of ticks.
-
setHover
public static net.minecraft.network.chat.MutableComponent setHover(net.minecraft.network.chat.Component component, net.minecraft.network.chat.Component hoverInfo) Attaches a component to another as a hover component. The hover component will be displayed in a tooltip that appears when hovering over the component.- Parameters:
component- The base component to attach a hover component to.hoverInfo- The component to display when the base component is hovered over.- Returns:
- A mutable instance of the component with the hover info attached.
-
setHover
public static net.minecraft.network.chat.MutableComponent setHover(net.minecraft.network.chat.Component component, net.minecraft.world.item.Item hoverInfo) Attaches a hover event that displays information about an item. This will use the default stack instance.- Parameters:
component- The component to attach the hover event to.hoverInfo- The ItemStack to derive hover info from.- Returns:
- A mutable instance of the component with the hover event attached.
-
setHover
public static net.minecraft.network.chat.MutableComponent setHover(net.minecraft.network.chat.Component component, net.minecraft.world.item.ItemStack hoverInfo) Attaches a hover event that displays information about an ItemStack.- Parameters:
component- The component to attach the hover event to.hoverInfo- The ItemStack to derive hover info from.- Returns:
- A mutable instance of the component with the hover event attached.
-
setHover
public static net.minecraft.network.chat.MutableComponent setHover(net.minecraft.network.chat.Component component, net.minecraft.world.entity.Entity hoverInfo) Attaches a hover event that displays information about an entity.- Parameters:
component- The component to attach the hover event to.hoverInfo- The entity to derive hover info from.- Returns:
- A mutable instance of the component with the hover event attached.
-
setHover
public static <AV,AT extends net.minecraft.network.chat.HoverEvent.Action<AV>> net.minecraft.network.chat.MutableComponent setHover(net.minecraft.network.chat.Component component, AT action, AV value) Attaches a hover event to a component.- Type Parameters:
AV- The type of the actions value.AT- The type of the action.- Parameters:
component- The component to attach the hover event to.action- The type of action to perform on hover.value- The value for the action.- Returns:
- A mutable instance of the component with the hover event attached.
-
setHover
public static net.minecraft.network.chat.MutableComponent setHover(net.minecraft.network.chat.Component component, net.minecraft.network.chat.HoverEvent hoverInfo) Attaches a hover event to a component.- Parameters:
component- The component to attach the hover event ot.hoverInfo- The hover event to append.- Returns:
- A mutable instance of the component with the hover event attached.
-
mutable
public static net.minecraft.network.chat.MutableComponent mutable(net.minecraft.network.chat.Component component) Gets a mutable instance of a component. If the component is not mutable a mutable copy will be created. Components that are already mutable will just be passed through.- Parameters:
component- The component to get a mutable instance of.- Returns:
- A mutable instance of the component.
-
applyFont
public static net.minecraft.network.chat.Component applyFont(net.minecraft.network.chat.Component text, net.minecraft.resources.ResourceLocation font) Recursively applies a custom font to a text component and all of it's children components.- Parameters:
text- The text to apply the font to.font- The ID of the font to apply.- Returns:
- A modified text component that has had the font applied.
-
getRegisteredFonts
Gathers a set of font IDs from the game. Fonts are only registered on the client so the set will be empty or crash if accessed on the server.- Returns:
- A set of available font IDs.
-
lookupTranslationWithAlias
@Nullable public static net.minecraft.network.chat.MutableComponent lookupTranslationWithAlias(net.minecraft.resources.ResourceLocation id, String... keys) Attempts to localize several translation keys. If the first key does not have a matching value it will go on to try the next.- Parameters:
id- An ID to include within each key using basic string formatting. The first parameter is the namespace and the second is the value. For example if a key was "tooltip.{0}.{1}.info", passing in minecraft:stick will produce tooltip.minecraft.stick.info as the key.keys- An array of potential keys to use.- Returns:
- If the key has a valid localization it will be returned. When no matches are found the value will be null.
-
lookupTranslationWithAlias
@Nullable public static net.minecraft.network.chat.MutableComponent lookupTranslationWithAlias(String[] keys, Object... params) Attempts to localize several translation keys, only returning the first key that can be localized. If no keys can be localized the result will be null.- Parameters:
keys- An array of keys to attempt to localize.params- An array of arguments to include when formatting the translated text.- Returns:
- If any of the keys can be localized a mutable component will be returned. Otherwise, the result will be null.
-
lookupTranslation
@Nullable public static net.minecraft.network.chat.MutableComponent lookupTranslation(String key, Object... args) Attempts to localize the text. If the text can not be localized the result will be null.- Parameters:
key- The key to localize.args- An array of arguments to include when formatting the translated text.- Returns:
- If the key can be resolved a mutable component will be returned. Otherwise, the value will be null.
-
lookupTranslation
@Nullable public static net.minecraft.network.chat.MutableComponent lookupTranslation(String key, net.minecraft.network.chat.MutableComponent fallback, Object... args) Attempts to localize the text. If the text can not be localized the fallback will be used.- Parameters:
key- The key to localize.fallback- A fallback component to use when the key can not be localized.args- An array of arguments to include when formatting the translated text.- Returns:
- If the key can be resolved a mutable component will be returned. Otherwise, the fallback value will be used.
-
lookupTranslation
@Nullable public static net.minecraft.network.chat.MutableComponent lookupTranslation(String key, @Nullable BiFunction<String, Object[], net.minecraft.network.chat.MutableComponent> fallback, Object... args) Attempts to localize text, if the text can not be localized the fallback will be used.- Parameters:
key- The key to localize.fallback- A fallback function that will take the localization key and arguments and produce a new component. If this function is not provided the fallback result will just be null.args- An array of arguments to include when formatting the translated text.- Returns:
- If the translation can be resolved a mutable component will be returned, otherwise the value will be null.
-
textWithCopy
Creates a component that will copy the value to the players clipboard when they click it.- Parameters:
text- The text to display and copy.- Returns:
- A mutable component that will copy the text when you click it.
-
setCopyText
public static net.minecraft.network.chat.MutableComponent setCopyText(net.minecraft.network.chat.MutableComponent component, String copy) Adds a click event to a component that will copy the provided text to the players clipboard when they click it.- Parameters:
component- The component to attach a click event to.copy- The text to copy to the clipboard.- Returns:
- A mutable component that will copy the text when you click it.
-
join
public static net.minecraft.network.chat.MutableComponent join(net.minecraft.network.chat.Component separator, net.minecraft.network.chat.Component... toJoin) Joins multiple components together using a separator component.- Parameters:
separator- The separator component to insert between other components.toJoin- An array of components to join.- Returns:
- A mutable 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 multiple components together using a separator component.- Parameters:
separator- The separator component to insert between other components.toJoin- A collection of components to join.- Returns:
- A mutable 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 multiple components together using a separator component.- Parameters:
separator- The separator component to insert between other components.toJoin- An iterable group of components to join.- Returns:
- A mutable 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.
-