From 6d82a8b6bc19d57ea8f7cacab6245d66bed529b1 Mon Sep 17 00:00:00 2001 From: jkibbels Date: Wed, 31 Jul 2024 22:50:08 -0400 Subject: [PATCH] Tested previously committed code and made adjustments to make sure it builds correctly. Finished Setup call to test for read and write privileges --- .../keeblarcraft/Commands/NoteCommands.java | 15 +++++--- .../keeblarcraft/ConfigMgr/ConfigManager.java | 38 +++++++++++++++++-- .../java/jesse/keeblarcraft/Keeblarcraft.java | 1 - .../java/jesse/keeblarcraft/Utils/Setup.java | 36 +++++++++++++----- 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/main/java/jesse/keeblarcraft/Commands/NoteCommands.java b/src/main/java/jesse/keeblarcraft/Commands/NoteCommands.java index e478b8a..33e158f 100644 --- a/src/main/java/jesse/keeblarcraft/Commands/NoteCommands.java +++ b/src/main/java/jesse/keeblarcraft/Commands/NoteCommands.java @@ -8,6 +8,7 @@ import com.mojang.datafixers.Products.P1; import jesse.keeblarcraft.ConfigMgr.ConfigManager; import jesse.keeblarcraft.Utils.ChatUtil; +import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_CREATE_EXCEPTION; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; @@ -19,16 +20,20 @@ public class NoteCommands { // // Each player will retain their own private file of notes inside a "notes" directory inside the overall mod config directory ConfigManager notesConfig = new ConfigManager(); - String NOTES_GLOBAL_DIRECTORY = ""; // The overall "notes" dir inside cfg folder + String NOTES_GLOBAL_DIRECTORY = "notes"; // The overall "notes" dir inside cfg folder public NoteCommands() { // Check if directory exists if (notesConfig.DoesDirectoryExist(NOTES_GLOBAL_DIRECTORY) == false) { // Attempt to create the directory - if (notesConfig.CreateDirectory(NOTES_GLOBAL_DIRECTORY) == true) { - System.out.println("Created notes directory successfully!"); //TODO: Success! - } else { - System.out.println("ERROR: Notes directory FAILED to create!! Permissions missing to create directory!"); //TODO: Critical failure + try { + if (notesConfig.CreateDirectory(NOTES_GLOBAL_DIRECTORY) == true) { + System.out.println("Created notes directory successfully!"); //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 + } + } catch (DIRECTORY_CREATE_EXCEPTION e) { + System.out.println("Directory creation failed"); } } else { System.out.println("Notes directory already exists. Skipping creation..."); //TODO: Success! diff --git a/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java b/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java index b000dc3..b23785f 100644 --- a/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java +++ b/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java @@ -33,13 +33,14 @@ public class ConfigManager { // CreateFile // - // Returns true if file is created, will return false if file cannot be created or already exists + // Returns true if file is created, will return false if file cannot be created (returns true if already exists) public Boolean CreateFile(String fileName) throws FILE_CREATE_EXCEPTION { Boolean ret = false; File file = new File(fileName); // Check 1: Does the file already exist? - ret = !file.exists(); + ret = file.exists(); + System.out.println("Does file exist? " + (ret ? "YES" : "NO")); // Check 2: If the file does not exist, attempt to create it if (ret == false) { @@ -49,6 +50,9 @@ public class ConfigManager { // The file could not be created 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"); } return ret; } @@ -70,6 +74,8 @@ public class ConfigManager { } catch (SecurityException e) { throw new FILE_DELETE_EXCEPTION(); } + } else { + System.out.println("Cannot delete file " + fileName + " because file does not exist"); } return ret; } @@ -80,6 +86,28 @@ public class ConfigManager { public Boolean WriteToFile(String fileName, String data, String mode) { Boolean ret = false; + FileWriter file; + try { + file = new FileWriter(fileName); + switch(mode) { + case "w": + file.write(data); + ret = true; + break; + case "a": + file.append(data); + ret = true; + break; + default: + System.out.println("Invalid mode to WriteToFile!"); + break; + } + + file.close(); + } catch (IOException e) { + System.out.println("Could not open file " + fileName + " to write to it! Possibly permission issue?"); + } + return ret; } @@ -129,10 +157,12 @@ public class ConfigManager { File dir = new File(dirName); try { - if (dir.exists()) { + if ( ! dir.exists()) { ret = dir.mkdirs(); } } catch (Exception e) { + System.out.println("Failed to make directory with name " + dirName); + ret = true; /// TODO: Hack to make Setup fn be fine with prev-created files! Make Setup more robust! throw new DIRECTORY_CREATE_EXCEPTION(); } return ret; @@ -145,7 +175,9 @@ public class ConfigManager { try { ret = dir.delete(); + System.out.println("Deleted directory " + dirName); } catch (Exception e) { + System.out.println("Failed to delete directory " + dirName); throw new DIRECTORY_DELETE_EXCEPTION(); } diff --git a/src/main/java/jesse/keeblarcraft/Keeblarcraft.java b/src/main/java/jesse/keeblarcraft/Keeblarcraft.java index e903d00..b18a000 100644 --- a/src/main/java/jesse/keeblarcraft/Keeblarcraft.java +++ b/src/main/java/jesse/keeblarcraft/Keeblarcraft.java @@ -16,7 +16,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jesse.keeblarcraft.Commands.CustomCommandManager; -import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_DUAL_DEFINITION; import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_FAILED_EXCEPTION; import jesse.keeblarcraft.Utils.Setup; diff --git a/src/main/java/jesse/keeblarcraft/Utils/Setup.java b/src/main/java/jesse/keeblarcraft/Utils/Setup.java index 1ea45c8..f507612 100644 --- a/src/main/java/jesse/keeblarcraft/Utils/Setup.java +++ b/src/main/java/jesse/keeblarcraft/Utils/Setup.java @@ -62,13 +62,12 @@ public final class Setup { // These will be top-level config files above the directories this mod creates private static final List FILE_LIST = new ArrayList() {{ - add("story.json"); // Big config file, determines when players can do certain things at different story levels - add("factions.json"); // General configuration file for factions stuff - add("events.json"); // General configuration file for story events! + add("story/story.json"); // Big config file, determines when players can do certain things at different story levels + add("factions/factions.json"); // General configuration file for factions stuff + add("events/1events.json"); // General configuration file for story events! add("general.json"); // The super general configuration file! (May be removed) }}; - // RunChecks() // // Checks if we are able to create necessary directories and run reading over the file system for the current @@ -80,21 +79,26 @@ public final class Setup { // Create directory check try { has_write = conf.CreateDirectory("test_dir"); + System.out.println("test_dir created? " + (has_write ? "YES" : "NO")); } catch (DIRECTORY_CREATE_EXCEPTION e) { - has_write = false; // No read access + System.out.println("Failed to create test directory or it already exists"); + has_write = false; } // Write to disk then read that data back if (has_write) { try { - has_write = conf.CreateFile("test_dir/note.txt"); - has_write = conf.WriteToFile("test_dir/note.txt", "test_write_read", "w"); + has_write = conf.CreateFile("test_dir/test_note.txt"); + has_write = conf.WriteToFile("test_dir/test_note.txt", "test_write_read", "w"); - List lines = conf.GetFile("test_dir/note.txt"); + List lines = conf.GetFile("test_dir/test_note.txt"); if (lines.size() == 0) { has_read = false; + } else { + has_read = true; } } catch (Exception e) { + System.out.println("Failed to create or write to test dir file"); has_read = false; } } @@ -102,12 +106,15 @@ public final class Setup { // Delete directory if created (it's a temporary dir) if (has_write) { try { + has_write = conf.DeleteFile("test_dir/test_note.txt"); has_write = conf.DeleteDirectory("test_dir"); - } catch (DIRECTORY_DELETE_EXCEPTION e) { + } catch (Exception e) { + System.out.println("Lost access to writing mid-way"); has_write = false; } } + System.out.println("CHECKS DEBUG: Value of has_write: " + has_write + ". Value of has_read: " + has_read); return has_write && has_read; } @@ -125,22 +132,31 @@ 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) + "..."); + } else { + System.out.println("Directory " + conf.DoesDirectoryExist(DIRECTORY_LIST.get(i)) + " already exists. Skipping..."); } } // Create necessary files for (Integer i = 0; i < FILE_LIST.size(); i++) { - if ( ! conf.DoesFileExist(DIRECTORY_LIST.get(i))) { + if ( ! conf.DoesFileExist(FILE_LIST.get(i))) { conf.CreateFile(FILE_LIST.get(i)); + System.out.println("Creating file " + FILE_LIST.get(i) + "..."); + } else { + System.out.println("File " + conf.DoesDirectoryExist(FILE_LIST.get(i)) + " already exists. Skipping..."); } } } 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."); throw new SETUP_FAILED_EXCEPTION(); } + System.out.println("DID SETUP COMPLETE SUCCESSFULLY? " + (ret ? "YES" : "NO")); + return ret; } }