Category / Java

Columnized text in Minecraft Chat February 4, 2015 at 8:11 pm

Minecraft uses variable width fonts in the chat output during gameplay. For plugins that benefit from displaying tables of data this will cause issues. It isn’t possible to just use spaces to align columns of data. Any text that contains characters outside of the ‘normal width’ will cause columns to be pushed to one side or the other.

Most characters are equivalent to six pixels in width. The ones that cause problems are not the same width. The biggest troublemaker is ‘space’. A space is rendered equivalent to four pixels wide. You could almost ‘fix’ the problem filtering out troublesome characters, but the output would unpleasant.

While searching for a solution I ran into an implementation that came very close to fixing the issue, but still had minor misaligned text which was highly visible and distracting to me. Not being happy with the improperly aligned text I started doing some tinkering and testing various unicode characters for width. I was able to stumble onto an unobtrusive character that seems to be on most platforms AND (more importantly) is the equivalent to one pixel wide.

In addition to fixing the problem with misaligned text I was able to add the ability to ‘properly’ align numbers to the right (for those of us using left to right rendering fonts).

Tables of output for the SimbeyMod teleport feature:
tp1

The implementation allows for left, right, and center alignment for columns. In addition the behavior keeps the table only as wide as required for the widest display area needed and will collapse ‘less important’ information as required.

Dynamic sizing of the ‘Name’ Column:
tp4

Dynamic removal of ‘less important’ Column:
tp5

I ended up with following solution:

Usage:

Ernie