Util Table generator

Discussion in 'Resources' started by FisheyLP, Aug 8, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    FisheyLP

    As simple as it sounds to generate a table and display it to a player, there's actually much more to it.

    Minecraft's font is not monospace. That means that some characters are wider (@ for example) and some are shorter (k for example).
    But don't worry, I implemented both fonts for either client or console :)

    How to use it:

    1. Initialize a new table generator:
    Code:
    TableGenerator tg = new TableGenerator(Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT);
    This will be the alignment for the columns. It also defines the amount of columns (3 in this example).

    There are 3 available alignments:
    • left
    • right
    • center

    2. Add rows to it:
    Code:
    tg.addRow("example", "§3§nLongest string");
    tg.addRow("§6left", "§ecenter", "§cright");
    tg.addRow("§aanother string", ":)", "the end.");
    It also has support for empty rows:
    Code:
    tg.addRow();
    As you can see in the first row, I only put 2 texts inside, but that doesn't matter, the third column will be empty.
    But providing more texts than available columns will cause an IllegalArgumentException to be thrown.

    3. Generate the table:
    Code:
    tg.generate(Receiver receiver, boolean ignoreColors, boolean coloredDistances)
    • The receiver CLIENT is for the chat window (supports minecraft's custom font width).
    • The receiver CONSOLE is for the console (monospace font).
    The boolean ignoreColors will not count color codes as the text's length, when set to true.
    The boolean coloredDistances will make all distance chars appear in dark gray.
    Example of "distance chars" (open)

    [​IMG]
    These chars are almost not seeable with the gray chat window behind it, if noone told you that they're there before.

    It will return a List<String> of the rows.

    3.1 Displaying it to a player:
    Code:
    for (String line : tg.generate(Receiver.CLIENT, true, true)) {
        player.sendMessage(line);
    }
    3.2 Displaying it to the console:
    Code:
    for (String line : tg.generate(Receiver.CONSOLE, true, false)) {
        Bukkit.getConsoleSender().sendMessage(line);
        // Or sender.sendMessage if you are using it inside onCommand
    }
    4. Delimiter:
    The delimiter (currently " | ") can be changed if you want so.
    Simply edit the 'String delimiter' variable at the top of the class.

    Final result:
    [​IMG]

    The column widths are equal to the longest string in the current column for every row.


    GitHub repo: https://github.com/FisheyLP/TableGenerator
     
    Last edited: Feb 28, 2017
  2. Offline

    ArsenArsen

    Cool. Arsen approves.
     
  3. Offline

    OPTIVNERVE

  4. Offline

    FisheyLP

Thread Status:
Not open for further replies.

Share This Page