I got tired of using Minecraft's Text objects like huge builders (e.g Text.literal("Hello " + Formatting.RED + " world!")). And if you need to concatenate multiple Text objects, it becomes even worse. That's why I've created this library to help people write these kinds of literals easily and without any boilerplate code. Also, it doesn't support things like ClickEvent or HoverEvent, and I'm not planning on adding them in the future. But if you want, you can make a pull request!
There are a few placeholders that you need to know about:
{}- Resets the current color and style to the previous one.{var}- Sets the text color to the given variable color (e.g{red},{cyan}, check Minecraft Color Codes).{#hex}- Sets the text color to the given hex value (e.g{#fff},{#ff00ff}).+obiusmodifiers - Sets the modifier of the text. You can also combine modifiers, e.g{+bi}- bold + italic.- o - obfuscated text
- b - bold text
- i - italic text
- u - underline text
- s -
strikethrough text
You can install this library using Jitpack.io
Add jitpack repository to your build.gradle:
maven { url "https://jitpack.io" }And then add CatFormat as a dependency:
implementation("com.github.cattyngmd.catformat:catformat-core:2.1.0")Or if you use fabric
modImplementation("com.github.cattyngmd.catformat:catformat-fabric:2.1.0")Simple usage with Minecraft's built-in MutableText:
importdev.cattyn.catformat.fabric.FabricCatFormat;
importjava.awt.Color;
importjava.util.Random;
// you can create only one static instance of the Formatter and use it everywhere!FabricCatFormatformatter = newFabricCatFormat();
voidmain() {
formatter.format("{red} Hello world!"); // red colored 'Hello world!'formatter.format("{#f0f} Hello world!"); // magenta colored 'Hello world!'formatter.format("{#0000ff} Hello world!"); // blue colored 'Hello world!'formatter.format("{red+b} Hello {} world!"); // red colored 'Hello' with bold style and ' world!' without any style// It also supports Java's default formatting tool - String.format()formatter.format("{red} Hello %s!", "world"); // red colored 'Hello world!'// You can create your own color namespaces too!formatter.add("light_green", 0x99FF99);
formatter.add("pink", Color.PINK.hashCode());
formatter.add("random_color", () -> newRandom().nextInt(0xFFFFFF));
formatter.format("{light_green} Hello {random_color} world {pink}!");
}Also, you can use classes with colors and use them in your CatFormat string:
importdev.cattyn.catformat.stylist.annotations.*;
importdev.cattyn.catformat.stylist.color.*;
importjava.util.Random;
importjava.awt.*;
classStyles {
// Mark your color with final keyword if you are not// going to change it, because it parses a bit faster@Stylefinalintblack = Color.BLACK.hashCode();
// You can also use `value` property in @Style// to set custom name to the color@Style("custom_name")
finalColorred = Color.RED;
@Style("dynamic_color")
intdynamic = Color.CYAN.hashCode();
// And of course functions :p@Style("random")
intgetRandomColor() { // Beware, you cant pass any paramsreturnnewRandom().nextInt(0xFFFFFF);
}
}
// Custom color objects are supported with `ARGBProvider` interfacepublicrecordColorHolder(intrgb) implementsColorProvider { @OverridepublicintgetRGB() {
returnrgb;
}
}
// If the object is mutable you should use `ARGBProvider.Mutable`publicclassColorHolderMutableimplementsColorProvider.Mutable {
privateintrgb;
publicvoidsetColor(intcolor) {
this.color = color;
}
@OverridepublicintgetRGB() {
returnrgb;
}
}To use it in a Minecraft plugin or any other project you can make your own TextWrapper. For example
importdev.cattyn.catformat.CatWrapper;
importdev.cattyn.catformat.text.TextWrapper;
classStringWrapperimplementsTextWrapper<String> { // idk why but you can do thatStringcolored(Stringtext, intcolor) {
returntext; // NOP
}
Stringconcat(Stringtext, Stringtext2) {
returntext + text2;
}
Stringmodify(Stringtext, Modifiersmodifiers) {
returntext;
}
StringnewText(Stringtext) {
returntext;
}
}
voidfunc() {
CatFormatformat = newCatFormat(newStringWrapper());
}