Skip to content

Utility Classes

Bogdan T edited this page Jan 28, 2024 · 5 revisions

The Most Useful

Here you will find the description of the most commonly used utility classes, that might greatly help you

String Utils

StringUtils can help you to format the text, and replace() faster than using a default java method.

Major Methods:

 * Format a string with color codes.
 *
 * @param text The text to format.
 * @return The formatted string.
 *
 String format(@NotNull String text)   // This method is overloaded. You can use List as the parameter as well

 * Format a string with color codes and placeholders.
 *
 * @param atumMod The mod.
 * @param text The text to format.
 * @param context The placeholder context.
 * @return The formatted string.
 *
 String formatWithPlaceholders(@NotNull AtumMod atumMod,
                               @NotNull String text,
                               @NotNull PlaceholderContext context) // This method is overloaded. You can use List as the parameter as well


 * Fast implementation of {@link String#replace(CharSequence, CharSequence)}
 *
 * @param input       The input string.
 * @param target      The target string.
 * @param replacement The replacement string.
 * @return The replaced string.
 *
 String replaceFast(@NotNull final String input,
                    @NotNull final String target,
                    @NotNull final String replacement)


 * Remove color codes from a string.
 *
 * @param input The input string.
 * @return The string without color codes.
 *
 String removeColorCodes(String input)

Math Utils

MathUtils Pretty useful, if you use trigonometry, especially in animations

Major Methods:

 * Get sin from lookup table
 * it is significantly faster that Math#sin()
 *
 * @param radians the radians
 * @return result
 *
 double fastSin(final double radians)


 * Get cos from lookup table
 * it is significantly faster that Math#cos()
 *
 * @param radians the radians
 * @return result
 *
 double fastCos(final double radians)

Number Utils

NumberUtils Helps to format the numbers, work with numerals and check the number type.

Major Methods:

 * Format double to string with 2 decimal places.
 *
 * @param toFormat The number to format.
 * @return Formatted.
 *
 String format(final double toFormat)


 * Get Roman Numeral from number.
 *
 * @param number The number to convert.
 * @return The number, converted to a roman numeral.
 *
 String toNumeral(final int number)

 * Get number from roman numeral.
 *
 * @param numeral The numeral to convert.
 * @return The number, converted from a roman numeral.
 *
 int fromNumeral(@NotNull final String numeral)

Time Utils

TimeUtils helps to format the time to a string. Example: 2d 12h 2m

You can change the time symbols if you want to, rn it is made a little bit weird and might not work well with multithreading, I'll work on that later. If you use multithreading, you have to change it manually after receiving the result

Methods:

 * Parse time to string
 * <p>Example: 1h 30m 20s</p>
 * <p>You can change the symbols used: </p>
 * <p>{@link #SYMBOL_SECOND} {@link #SYMBOL_MINUTE} {@link #SYMBOL_HOUR}</p>
 *
 * @param time the time
 * @return the result
 *
 String parseTimeToString(long time)


 * Get time left parsed to string
 * <p>see also {@link #parseTimeToString(long)}</p>
 *
 * @param startTime the start time
 * @param endTime the end time
 * @return the result
 *
 String getTimeLeftString(long startTime, long endTime)


 * Parse string time to long
 * <p>see also {@link #parseTimeToString(long)}</p>
 *
 * @param text the text to parse
 * @return time
 *
 long parseStringToTime(String text)

Render Utils

RenderUtils is a powerful class, allowing you to work with display feature more efficiently.

The most importantly, it can ignore the minecraft scaleFactor and texture max resolution restriction.

You might find some of its methods weird, they require optimization and improvements.

Major methods

 * Draw a filled rectangle.
 * <p>min and max are the corners of the rectangle.</p>
 *
 *
 void fill(float minX, float minY,
           float maxX, float maxY, 
           int color,
           float opacity)


 * Draw a filled circle
 *
 * @param centerX The center X coordinate
 * @param centerY The center Y coordinate
 * @param radius The radius of the circle
 * @param polygons The number of polygons to use to draw the circle.
 *                 In most cases 40 is optimal
 * @param color The color of the circle
 *
 void drawCircle(float centerX, float centerY, 
                 float radius,
                 int polygons,
                 AtumColor color)

 *  Bind a texture to the OpenGL context.
 *  
 * @param texture The texture to bind.
 *
 void bindTexture(ResourceLocation texture)


 * Draw a complete image.
 *
 * @param posX The X position.
 * @param posY The Y position.
 * @param width The width.
 * @param height The height.
 *
public static void drawCompleteImage(int posX, int posY, 
                                     int width, int height)

 * Draw the specified part of a texture
 *
 * @param posX The X position.
 * @param posY The Y position.
 * @param width The width.
 * @param height The height.
 * @param textureX The X position of the image.
 * @param textureY The Y position of the image.
 * @param texturePartWidth The width of the image part.
 * @param texturePartHeight The height of the image part.
 *
 void drawPartialImage(int posX, int posY,
                       int width, int height,
                       int textureX, int textureY,
                       int texturePartWidth, 
                       int texturePartHeight)


 * Draw an outline.
 * 
 * @param x The X position.
 * @param y The Y position.
 * @param width The width.
 * @param height The height.
 * @param outlineSize The outline size.
 * @param color The color.
 *
 void drawOutline(int x, int y, 
                  int width, int height, 
                  int outlineSize, 
                  AtumColor color)


 * Fix the coordinates to ignore the mc scale factor.
 *
 * <p>Attention! This method uses cached scale factor, so, you should use
 * {@link #getScaleFactor()} before calling this method.</p>
 * @param x The X coordinate.
 * @param y The Y coordinate.
 * @param width The width.
 * @param height The height.
 * @param fixRatio If true, the size will keep the ratio.
 * @return The fixed coordinates.
 *
 int[] fixCoordinates(int x, int y, 
                      int width, int height,
                      boolean fixRatio)


 * Get the minecraft scale factor.
 * <p>If the guiScale and window size wasn't changed</p>
 * <p>the method will return the cached value</p>
 *
 * @return The scale factor
 *
 int getScaleFactor()

Extra

These are used in specific cases and more rarely.

Objects

The replacement of Java 8 same class. It has methods from newer Java versions.

If you are annoyed for not having methods like requireNonNullElse and requireNonNullElseGet then this class is for you

Player Utils

Currently, it has only the methods to work with the skin texture, so, u can bind the player's skin texture or save.

File Utils

Helps to work with zip files. The most useful method there in my opinion is getAllPathsInResourceFolder() which can help to get all files in a resource folder

Clone this wiki locally