Make debug colored

This commit is contained in:
Jesse 2024-08-10 19:27:39 +00:00
parent a355f13bb3
commit c3e3212737
8 changed files with 106 additions and 46 deletions

View File

@ -8,6 +8,9 @@
package jesse.keeblarcraft.Commands;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
public class CustomCommandManager {
// Intentionally empty constructor since at object definition time it may not be possible to register commands
public CustomCommandManager() {}
@ -19,7 +22,7 @@ public class CustomCommandManager {
NoteCommands noteCommands = new NoteCommands();
// REGISTER COMMANDS BELOW
System.out.println("REGISTERING CUSTOM COMMAND EXTENSIONS BELOW");
System.out.println(ChatUtil.ColoredString("REGISTERING CUSTOM COMMAND EXTENSIONS BELOW", CONSOLE_COLOR.BLUE));
shortcuts.RegisterShortcutCommands();
noteCommands.RegisterNoteCommands();
}

View File

@ -11,6 +11,7 @@ import com.mojang.datafixers.Products.P1;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.JsonClassObjects.PlayerNote;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_CREATE_EXCEPTION;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
@ -31,15 +32,15 @@ public class NoteCommands {
// Attempt to create the directory
try {
if (notesConfig.CreateDirectory(NOTES_GLOBAL_DIRECTORY) == true) {
System.out.println("Created notes directory successfully!"); //TODO: Success!
System.out.println(ChatUtil.ColoredString("Created notes directory successfully!", CONSOLE_COLOR.BLUE)); //TODO: Success!
} else {
System.out.println("ERROR: Notes directory FAILED to create!! Either the directory already exists or we are missing permissions!"); //TODO: Critical failure
System.out.println(ChatUtil.ColoredString("ERROR: Notes directory FAILED to create!! Either the directory already exists or we are missing permissions!", CONSOLE_COLOR.RED)); //TODO: Critical failure --not specfic enough to mark it as a red or blue
}
} catch (DIRECTORY_CREATE_EXCEPTION e) {
System.out.println("Directory creation failed");
System.out.println(ChatUtil.ColoredString("Directory creation failed", CONSOLE_COLOR.RED));
}
} else {
System.out.println("Notes directory already exists. Skipping creation..."); //TODO: Success!
System.out.println(ChatUtil.ColoredString("Notes directory already exists. Skipping creation...", CONSOLE_COLOR.BLUE)); //TODO: Success!
}
}
@ -104,7 +105,7 @@ public class NoteCommands {
ret = 0;
} else {
System.out.println("Only a player can execute this command!");
System.out.println(ChatUtil.ColoredString("Only a player can execute this command!", CONSOLE_COLOR.RED));
}
return ret;

View File

@ -12,6 +12,7 @@ import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.entity.player.PlayerAbilities;
import net.minecraft.server.command.CommandManager;
@ -96,11 +97,11 @@ public class ShortcutCommands {
}
else {
player.sendMessage(Text.literal("You do not have permissions to run this command!"));
player.sendMessage(Text.literal("\033[31m You do not have permissions to run this command! \033[0m"));
}
}
else {
System.out.println("This command cannot be executed by a non-player entity!");
System.out.println(ChatUtil.ColoredString("This command cannot be executed by a non-player entity!", CONSOLE_COLOR.RED));
}
return retValue;

View File

@ -29,6 +29,8 @@ import org.apache.commons.io.FileUtils;
import java.util.ArrayList;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
// Import all custom exceptions
import jesse.keeblarcraft.Utils.CustomExceptions.*;
@ -46,7 +48,7 @@ public class ConfigManager {
// Check 1: Does the file already exist?
ret = file.exists();
System.out.println("Does file exist? " + (ret ? "YES" : "NO"));
System.out.println(ChatUtil.ColoredString("Does file exist? ", CONSOLE_COLOR.BLUE) + (ret ? ChatUtil.ColoredString("YES", CONSOLE_COLOR.YELLOW) : ChatUtil.ColoredString("NO", CONSOLE_COLOR.YELLOW)));
// Check 2: If the file does not exist, attempt to create it
if (ret == false) {
@ -57,8 +59,8 @@ public class ConfigManager {
throw new FILE_CREATE_EXCEPTION();
}
} else {
ret = true; // This might be a hot fix, but technically the file already exists so would this be true or false?
System.out.println("File (name:" + fileName + ") was determined to already exist. Exiting out");
ret = true; // This might be a hot fix, but technically the file already exists so would this be true or false? --?this statement is wild?
System.out.println(ChatUtil.ColoredString("File (name: ", CONSOLE_COLOR.BLUE) + ChatUtil.ColoredString(fileName, CONSOLE_COLOR.YELLOW) + ChatUtil.ColoredString(" was determined to already exist. Exiting out", CONSOLE_COLOR.BLUE));
}
return ret;
}
@ -81,7 +83,7 @@ public class ConfigManager {
throw new FILE_DELETE_EXCEPTION();
}
} else {
System.out.println("Cannot delete file " + fileName + " because file does not exist");
System.out.println(ChatUtil.ColoredString("cannot delete file ", CONSOLE_COLOR.RED) + ChatUtil.ColoredString(fileName, CONSOLE_COLOR.YELLOW) + ChatUtil.ColoredString(" because file does not exist", CONSOLE_COLOR.BLUE));
}
return ret;
}
@ -105,13 +107,13 @@ public class ConfigManager {
ret = true;
break;
default:
System.out.println("Invalid mode to WriteToFile!");
System.out.println(ChatUtil.ColoredString("Invalid mode to WriteToFile!", CONSOLE_COLOR.RED));
break;
}
file.close();
} catch (IOException e) {
System.out.println("Could not open file " + fileName + " to write to it! Possibly permission issue?");
System.out.println(ChatUtil.ColoredString("Could not open file ", CONSOLE_COLOR.RED) + ChatUtil.ColoredString(fileName, CONSOLE_COLOR.YELLOW) + ChatUtil.ColoredString(" to write to it! Possibly permission issue?", CONSOLE_COLOR.RED));
}
return ret;
@ -135,7 +137,7 @@ public class ConfigManager {
writer.flush();
writer.close();
} catch (JsonIOException | IOException e) {
System.out.println("Could not successfully write to json file");
System.out.println(ChatUtil.ColoredString("Could not successfully write to json file", CONSOLE_COLOR.RED));
throw new FILE_WRITE_EXCEPTION();
}
}
@ -153,16 +155,16 @@ public class ConfigManager {
File file = new File(fileName);
ret = FileUtils.readFileToString(file, "UTF-8");
} catch (NullPointerException e) {
System.out.println("nullptr exception");
System.out.println(ChatUtil.ColoredString("nullptr exception", CONSOLE_COLOR.RED));
throw new JsonSyntaxException("");
} catch (FileNotFoundException e) {
System.out.println("file not found");
System.out.println(ChatUtil.ColoredString("file not found", CONSOLE_COLOR.RED));
throw new JsonSyntaxException("");
} catch (java.nio.charset.UnsupportedCharsetException e) {
System.out.println("charset issue");
System.out.println(ChatUtil.ColoredString("charset issue", CONSOLE_COLOR.RED));
throw new JsonSyntaxException("");
} catch (IOException e) {
System.out.println("io exception");
System.out.println(ChatUtil.ColoredString("io exception", CONSOLE_COLOR.RED));
throw new JsonSyntaxException("");
}
@ -191,7 +193,7 @@ public class ConfigManager {
ret = dir.mkdirs();
}
} catch (Exception e) {
System.out.println("Failed to make directory with name " + dirName);
System.out.println(ChatUtil.ColoredString("Failed to make directory with name: ", CONSOLE_COLOR.RED) + ChatUtil.ColoredString(dirName, CONSOLE_COLOR.YELLOW));
ret = true; /// TODO: Hack to make Setup fn be fine with prev-created files! Make Setup more robust!
throw new DIRECTORY_CREATE_EXCEPTION();
}
@ -205,9 +207,9 @@ public class ConfigManager {
try {
ret = dir.delete();
System.out.println("Deleted directory " + dirName);
System.out.println(ChatUtil.ColoredString("Deleted directory ", CONSOLE_COLOR.GREEN) + ChatUtil.ColoredString(dirName, CONSOLE_COLOR.YELLOW));
} catch (Exception e) {
System.out.println("Failed to delete directory " + dirName);
System.out.println(ChatUtil.ColoredString("Failed to delete directory: ", CONSOLE_COLOR.RED) + ChatUtil.ColoredString(dirName, CONSOLE_COLOR.YELLOW));
throw new DIRECTORY_DELETE_EXCEPTION();
}

View File

@ -12,6 +12,8 @@ import java.util.HashMap;
import java.util.Map.Entry;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
import jesse.keeblarcraft.Utils.CustomExceptions.FILE_WRITE_EXCEPTION;
public class PlayerNote {
@ -91,21 +93,21 @@ public class PlayerNote {
// In the event the above code failed out, this means a new file has to be created for the player's uuid
if (!existingFile)
{
System.out.println("Trying to create new file");
System.out.println(ChatUtil.ColoredString("Trying to create new file", CONSOLE_COLOR.BLUE));
try {
thisNote.uuid = uuid;
FlashConfig();
} catch (Exception e) {
System.out.println("Could not write to file");
System.out.println(ChatUtil.ColoredString("Could not write to file", CONSOLE_COLOR.RED));
}
} else {
System.out.println("Moving on");
System.out.println(ChatUtil.ColoredString("Moving on", CONSOLE_COLOR.BLUE));
}
// It's possible the above code will return a blank class if a file doesn't exist. This will make
// a new file with this players uuid
if ("".equals(thisNote.uuid)) {
System.out.println("Assigning new config file for this uuid. No previous existing");
System.out.println(ChatUtil.ColoredString("Assigning new config file for this uuid. No previous existing", CONSOLE_COLOR.BLUE));
thisNote.uuid = uuid;
}
}
@ -142,7 +144,7 @@ public class PlayerNote {
try {
config.WriteToJsonFile("notes/" + thisNote.uuid.toString() + ".json", thisNote);
} catch (FILE_WRITE_EXCEPTION e) {
System.out.println("Could not flash notes configuration file");
System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED));
}
}
}

View File

@ -17,7 +17,9 @@ import org.slf4j.LoggerFactory;
import jesse.keeblarcraft.Commands.CustomCommandManager;
import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_FAILED_EXCEPTION;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.Setup;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
// import com.mojang.brigadier.Command;
@ -36,25 +38,25 @@ public class Keeblarcraft implements ModInitializer {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
LOGGER.info("Hello Fabric world!");
ChatUtil.LoggerColored("Hello Fabric world!", CONSOLE_COLOR.CYAN, LOGGER);
if (setup != null) {
try {
// Run setup. If setup fails; it throws SETUP_FAILED_EXCEPTION
LOGGER.info("Running setup stage");
LOGGER.info("\033[34m Running setup stage \033[0m");
setup.RunSetup();
// Run command registrations from the command manager
LOGGER.info("Running command registration");
LOGGER.info("\033[34m Running command registration \033[0m");
cmdMgr.RegisterCustomCommands();
} catch (SETUP_FAILED_EXCEPTION e) {
System.out.println("ERROR. Setup failed to initialize environment. Mod likely does not have read/write permissions inside area. Mod will now close out.");
System.out.println(ChatUtil.ColoredString("ERROR. Setup failed to initialize environment. Mod likely does not have read/write permissions inside area. Mod will now close out.", CONSOLE_COLOR.RED));
e.printStackTrace();
}
} else {
// Program exit. Dual definition of setup somehow happened!
System.out.println("Dual definition of singleton attempted! Out of order initialization? How did this even happen?");
System.out.println(ChatUtil.ColoredString("Dual definition of singleton attempted! Out of order initialization? How did this even happen?", CONSOLE_COLOR.RED));
}
}
}

View File

@ -8,13 +8,61 @@
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 CONSOLE_COLOR.RED:
ret = "31";
break;
case CONSOLE_COLOR.GREEN:
ret = "32";
break;
case CONSOLE_COLOR.YELLOW:
ret = "33";
break;
case CONSOLE_COLOR.BLUE:
ret = "34";
break;
case CONSOLE_COLOR.MAGENTA:
ret = "35";
break;
case CONSOLE_COLOR.CYAN:
ret = "36";
break;
}
return ret;
}
}
public static enum CONSOLE_COLOR {
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN;
}
// Helpful print wrapper function
static public void SendPlayerMsg(ServerPlayerEntity player, String 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));
}
}

View File

@ -18,6 +18,7 @@ import java.util.List;
import java.util.ArrayList;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_CREATE_EXCEPTION;
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_DELETE_EXCEPTION;
import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_FAILED_EXCEPTION;
@ -35,7 +36,7 @@ public final class Setup {
private static Setup static_inst;
public Setup() {
System.out.println("Running system setup and checks...");
System.out.println(ChatUtil.ColoredString("Running system setup and checks...", CONSOLE_COLOR.BLUE));
}
// Returns the singleton object
@ -79,9 +80,9 @@ public final class Setup {
// Create directory check
try {
has_write = conf.CreateDirectory("test_dir");
System.out.println("test_dir created? " + (has_write ? "YES" : "NO"));
System.out.println(ChatUtil.ColoredString("test_dir created! has write: ", CONSOLE_COLOR.GREEN) + (has_write ? ChatUtil.ColoredString("YES", CONSOLE_COLOR.YELLOW) : ChatUtil.ColoredString("NO", CONSOLE_COLOR.YELLOW)));
} catch (DIRECTORY_CREATE_EXCEPTION e) {
System.out.println("Failed to create test directory or it already exists");
System.out.println(ChatUtil.ColoredString("Failed to create test directory or it already exists", CONSOLE_COLOR.MAGENTA));
has_write = false;
}
@ -98,7 +99,7 @@ public final class Setup {
has_read = true;
}
} catch (Exception e) {
System.out.println("Failed to create or write to test dir file");
System.out.println(ChatUtil.ColoredString("Failed to create or write to test dir file ", CONSOLE_COLOR.RED));
has_read = false;
}
}
@ -109,12 +110,12 @@ public final class Setup {
has_write = conf.DeleteFile("test_dir/test_note.txt");
has_write = conf.DeleteDirectory("test_dir");
} catch (Exception e) {
System.out.println("Lost access to writing mid-way");
System.out.println(ChatUtil.ColoredString("Lost access to writing mid-way", CONSOLE_COLOR.RED));
has_write = false;
}
}
System.out.println("CHECKS DEBUG: Value of has_write: " + has_write + ". Value of has_read: " + has_read);
//need to be able to take in raw booleans for coloredstrings functions
System.out.println(ChatUtil.ColoredString("CHECKS DEBUG: Value of has_write: ", CONSOLE_COLOR.BLUE) + ChatUtil.ColoredString(has_write.toString(), CONSOLE_COLOR.YELLOW) + ChatUtil.ColoredString(". Value of has_read: ", CONSOLE_COLOR.BLUE) + ChatUtil.ColoredString(has_read.toString(), CONSOLE_COLOR.YELLOW));
return has_write && has_read;
}
@ -132,9 +133,9 @@ public final class Setup {
for (Integer i = 0; i < DIRECTORY_LIST.size(); i++) {
if ( ! conf.DoesDirectoryExist(DIRECTORY_LIST.get(i))) {
conf.CreateDirectory(DIRECTORY_LIST.get(i));
System.out.println("Creating directory " + DIRECTORY_LIST.get(i) + "...");
System.out.println(ChatUtil.ColoredString("Creating directory ", CONSOLE_COLOR.GREEN) + ChatUtil.ColoredString(DIRECTORY_LIST.get(i), CONSOLE_COLOR.YELLOW) + ChatUtil.ColoredString("...", CONSOLE_COLOR.GREEN));
} else {
System.out.println("Directory " + conf.DoesDirectoryExist(DIRECTORY_LIST.get(i)) + " already exists. Skipping...");
System.out.println(ChatUtil.ColoredString("Directory ", CONSOLE_COLOR.BLUE) + conf.DoesDirectoryExist(DIRECTORY_LIST.get(i)) + ChatUtil.ColoredString(" already exists. Skipping... ", CONSOLE_COLOR.BLUE));
}
}
@ -142,20 +143,20 @@ public final class Setup {
for (Integer i = 0; i < FILE_LIST.size(); i++) {
if ( ! conf.DoesFileExist(FILE_LIST.get(i))) {
conf.CreateFile(FILE_LIST.get(i));
System.out.println("Creating file " + FILE_LIST.get(i) + "...");
System.out.println(ChatUtil.ColoredString("Creating file ", CONSOLE_COLOR.GREEN) + ChatUtil.ColoredString(FILE_LIST.get(i), CONSOLE_COLOR.YELLOW) + ChatUtil.ColoredString("...", CONSOLE_COLOR.GREEN));
} else {
System.out.println("File " + conf.DoesDirectoryExist(FILE_LIST.get(i)) + " already exists. Skipping...");
System.out.println(ChatUtil.ColoredString("File ", CONSOLE_COLOR.BLUE) + conf.DoesDirectoryExist(FILE_LIST.get(i)) + ChatUtil.ColoredString(" already exists. Skipping...", CONSOLE_COLOR.BLUE));
}
}
} catch (Exception e) {
throw new SETUP_FAILED_EXCEPTION();
}
} else {
System.out.println("RunChecks() failed in its process. This mod has deemed it does not have read or write privileges in its hosted area and will now exit.");
System.out.println(ChatUtil.ColoredString("RunChecks() failed in its process. This mod has deemed it does not have read or write privileges in its hosted area and will now exit.", CONSOLE_COLOR.RED));
throw new SETUP_FAILED_EXCEPTION();
}
System.out.println("DID SETUP COMPLETE SUCCESSFULLY? " + (ret ? "YES" : "NO"));
System.out.println(ChatUtil.ColoredString("DID SETUP COMPLETE SUCCESSFULLY? ", CONSOLE_COLOR.YELLOW) + (ret ? ChatUtil.ColoredString("YES", CONSOLE_COLOR.YELLOW) : ChatUtil.ColoredString("NO", CONSOLE_COLOR.YELLOW)));
return ret;
}