Add generic coloring functions

This commit is contained in:
Jkibbels 2024-08-08 20:47:31 -04:00
parent 26d288b39f
commit d283c2f9b3
2 changed files with 44 additions and 2 deletions

View File

@ -17,7 +17,9 @@ import org.slf4j.LoggerFactory;
import jesse.keeblarcraft.Commands.CustomCommandManager; import jesse.keeblarcraft.Commands.CustomCommandManager;
import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_FAILED_EXCEPTION; import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_FAILED_EXCEPTION;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.Setup; import jesse.keeblarcraft.Utils.Setup;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
// import com.mojang.brigadier.Command; // import com.mojang.brigadier.Command;
@ -36,7 +38,7 @@ public class Keeblarcraft implements ModInitializer {
// This code runs as soon as Minecraft is in a mod-load-ready state. // This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized. // However, some things (like resources) may still be uninitialized.
// Proceed with mild caution. // Proceed with mild caution.
LOGGER.info("\033[36m Hello Fabric world! \033[0m"); ChatUtil.LoggerColored("Hello Fabric world!", CONSOLE_COLOR.CYAN, LOGGER);
if (setup != null) { if (setup != null) {
try { try {

View File

@ -8,13 +8,53 @@
package jesse.keeblarcraft.Utils; package jesse.keeblarcraft.Utils;
import org.slf4j.Logger;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text; import net.minecraft.text.Text;
public class ChatUtil { public class ChatUtil {
private static class ConsoleColor {
public static String getColor(CONSOLE_COLOR color) {
String ret = "";
switch(color) {
case CONSOLE_COLOR.RED:
ret = "31";
break;
case CONSOLE_COLOR.BLUE:
ret = "34";
break;
case CONSOLE_COLOR.GREEN:
ret = "32";
break;
case CONSOLE_COLOR.CYAN:
ret = "36";
break;
}
return ret;
}
}
public static enum CONSOLE_COLOR {
RED,
BLUE,
GREEN,
CYAN;
}
// Helpful print wrapper function // Helpful print wrapper function
static public void SendPlayerMsg(ServerPlayerEntity player, String text) { static public void SendPlayerMsg(ServerPlayerEntity player, String text) {
player.sendMessage(Text.literal(text)); player.sendMessage(Text.literal(text));
} }
// 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));
}
} }