Minecraft Modding: Text Formatting

Introduction


There are several places in mods where you may want to display text and you probably want to control the formatting (like the color, font, etc.). For example, you may create a GUI for a custom lore book, or you might want to display some response to a custom command.

Example #1: Making Rainbow-Colored Text


Here is a method I use to create rainbow colored text. Hopefully it is self explanatory. Basically I count through the letters in the input string and cycle through the colors of the rainbow.


public static String stringToRainbow(String parString, boolean parReturnToBlack)
{
   int stringLength = parString.length();
   if (stringLength < 1)
   {
      return "";
   }
   String outputString = "";
   TextFormatting[] colorChar = 
      {
         TextFormatting.RED,
         TextFormatting.GOLD,
         TextFormatting.YELLOW,
         TextFormatting.GREEN,
         TextFormatting.AQUA,
         TextFormatting.BLUE,
         TextFormatting.LIGHT_PURPLE,
         TextFormatting.DARK_PURPLE
      };
   for (int i = 0; i < stringLength; i++)
   {
      outputString = outputString+colorChar[i%8]+parString.substring(i, i+1);
   }
   // return color to a common one after (most chat is white, but for other GUI might want black)
   if (parReturnToBlack)
   {
      return outputString+TextFormatting.BLACK;
   }
   return outputString+TextFormatting.WHITE;
}


Example #2: Dynamic Formatting (Gold Shimmer Effect)


It is possible to have the formatting change over time to create an "animation" effect. In this example, the text will be a gold color that will have a shimmer move through the text occasionally.


public static String stringToGolden(String parString, int parShineLocation, boolean parReturnToBlack)
{
   int stringLength = parString.length();
   if (stringLength < 1)
   {
      return "";
   }
   String outputString = "";
   for (int i = 0; i < stringLength; i++)
   {
      if ((i+parShineLocation+Minecraft.getSystemTime()/20)%88==0)
      {
         outputString = outputString+TextFormatting.WHITE+parString.substring(i, i+1);    
      }
      else if ((i+parShineLocation+Minecraft.getSystemTime()/20)%88==1)
      {
          outputString = outputString+TextFormatting.YELLOW+parString.substring(i, i+1);    
      }
      else if ((i+parShineLocation+Minecraft.getSystemTime()/20)%88==87)
      {
         outputString = outputString+TextFormatting.YELLOW+parString.substring(i, i+1);    
      }
      else
      {
         outputString = outputString+TextFormatting.GOLD+parString.substring(i, i+1);        
      }
   }
   // return color to a common one after (most chat is white, but for other GUI might want black)
   if (parReturnToBlack)
   {
      return outputString+TextFormatting.BLACK;
   }
   return outputString+TextFormatting.WHITE;
}


Create Web-Linked Clickable Text


Thanks to Choonster for this tip.

You can either use ITextComponent.getStyle().setClickEvent on the text component to make the whole text clickable with whatever action you want or you can use ForgeHooks.newChatWithLinks, which will parse a String for URLs and make them clickable.

Using A Custom Unicode Font


Thanks to Matryoshika for this tip.

To use a custom font, you need to extend FontRenderer and override the FontRenderer#renderUnicodeChar() method.

The actual font should be added to the game as a PNG file; The ResourceLocation of said file is used when instantiating your own FontRenderer.

You should instantiate it as final inside your proxy, and call the FontRenderer from your proxy whenever you need to use it.

To use this different font, you merely use the stored FontRenderer instance instead of calling Minecraft#fontRenderer like usual (or the FontRenderer given to you if that's the case), and draw the text with FontRenderer#drawString() or any of the other drawing options.

For an example, look at AbyssalCraft's source, as they use the Aklo font to obfuscate some text.

No comments:

Post a Comment