Adding your own Text Formatting tags in Modopedia is incredibly simple.
Step 1.) Create a TextFormatter class
First, create a class for your formatting tag. It should implement net.favouriteless.modopedia.api.text.TextFormatter
. Your class will need to provide a method determining if a given tag matches yours (excluding the parentheses), and one used to modify the StyleStack
if it does. Feel free to throw an exception if you get an invalid input, but ideally you should check in matches
.
public class CommandFormatter implements TextFormatter {@Overridepublic boolean matches(String tag) {return tag.startsWith("cmd:");}@Overridepublic void apply(StyleStack styleStack, String tag) {String command = tag.substring(4);if(command.length() < 2 || !command.startsWith("/")) {styleStack.pop();throw new IllegalArgumentException(command + " is not a valid command.");}styleStack.modify(style -> style.withClickEvent(new ClickEvent(Action.RUN_COMMAND, command)));}}
If you decide your formatting tag shouldn't do anything, call StyleStack#pop
and exit the method.
Step 2.) Registering your TextFormatter
All you need to do now is register your TextFormatter by calling TextFormatterRegistry#register
in your mod's Client Entrypoint.
TextFormatterRegistry.get().register(new CommandFormatter());