/* * * ChatUtil * * Helpful utility for pretty printing in chat in the game with different supported functions and levels * */ package jesse.keeblarcraft.Utils; import org.slf4j.Logger; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; public class ChatUtil { //This is a private class only used internally to get ANSI colors private static class ConsoleColor { public static String getColor(CONSOLE_COLOR color) { String ret = ""; switch(color) { case RED: ret = "31"; break; case GREEN: ret = "32"; break; case YELLOW: ret = "33"; break; case BLUE: ret = "34"; break; case MAGENTA: ret = "35"; break; case CYAN: ret = "36"; break; } return ret; } } public static enum CONSOLE_COLOR { RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN; } static int CHATBOX_WIDTH_CHARS = 80; // Maximum length of the textbox in individual characters // Helpful print wrapper function static public void SendPlayerMsg(ServerPlayerEntity player, String text) { player.sendMessage(Text.literal(text)); } /// TODO: Add this back in later under a chat ticket // Prints a table of data in chat // static public void ChatBlock(ServerPlayerEntity player, HashMap> table) { // ///DEBUG // for (Entry> entry : table.entrySet()) { // for (int debug = 0; debug < entry.getValue().size(); debug++) { // System.out.println("KEY: " + entry.getKey().toString() + " VALUE: " + entry.getValue().get(debug).toString()); // } // } // // The user will likely pass in text strings longer than the character limit for num of columns; therefore // // we are required to split these into this finalPrintList structure // HashMap> finalPrintList = new HashMap>(); // int maxColumnWidth = CHATBOX_WIDTH_CHARS / table.size(); // Represents max char allowance per data column // maxColumnWidth -= table.size(); // Represents a separating '|' between each column // // This first behemoth of a loop is to take the given table hashmap and look at // // the Text values & split them // // should their size exceed the maxColumnWidth given for each entry key // System.out.println("Entry data is size " + table.size()); // for (Entry> entry : table.entrySet()) { // // Each text line found cannot be longer than "maxColumnWidth" or else it must // // wrap which splits it // // into two texts; thus adding an additional row that is required for iteration. // // Each split text must // // maintain the same formatting as the root text it is split from // finalPrintList.put(entry.getKey(), new ArrayList()); // Instantiate the key & array // System.out.println("Map size is " + finalPrintList.size()); // System.out.println("Entry value size is " + entry.getValue().size()); // for (Text item : entry.getValue()) { // int numItems = (int) Math.ceil((item.getString().length() / maxColumnWidth)); // int strOffset = numItems; // Represents number of items per string // System.out.println("numItems: " + numItems); // System.out.println("strOffset: " + strOffset); // for (int offset = 0; offset <= numItems; offset++) { /// TODO: might need to be <= // int start = strOffset * offset; // Multiple from start of string to needed point // int end = start + strOffset; // The original start offset + the width spacer // String substr = item.toString().substring(start, end); // Contains the string to be Textified // MutableText newText = Text.literal(substr).setStyle(item.getStyle()); // finalPrintList.get(entry.getKey()).add(newText); // Add back into list // System.out.println("SPLIT DEBUG: " + newText.toString()); // } // } // } // // This loop does the printing of the table in chat // int tempPreventInfiniteLoops = 10; // while (finalPrintList.size() != 0) { // // This is a one time print // MutableText line = Text.literal(""); // for (Entry> entry : finalPrintList.entrySet()) { // if (entry.getValue().size() != 0) { // line.append(entry.getValue().get(0)); // System.out.println("new line is now " + line.toString()); // line.append("|"); // } else { // finalPrintList.remove(entry.getKey()); // Clear the key; as we are done with this column for // // printing // } // player.sendMessage(line); // String debugPrint = line.toString(); // System.out.println("Debug line to be printed: " + debugPrint); // line = Text.literal(""); // break; // } // tempPreventInfiniteLoops--; // if (tempPreventInfiniteLoops <= 0) { // return; // } // } // } // Returns a string with the proper ANSI encoding for the specified CONSOLE_COLOR static public String ColoredString(String msg, CONSOLE_COLOR color) { return "\033[" + ConsoleColor.getColor(color) + "m" + msg + "\033[0m"; } // Takes in a (already initialized) logger object and prints to console static public void LoggerColored(String msg, CONSOLE_COLOR color, Logger logger) { logger.info(ColoredString(msg, color)); } }