Tested previously committed code and made adjustments to make sure it builds correctly. Finished Setup call to test for read and write privileges

This commit is contained in:
jkibbels 2024-07-31 22:50:08 -04:00
parent edd33c05fa
commit 6d82a8b6bc
4 changed files with 71 additions and 19 deletions

View File

@ -8,6 +8,7 @@ import com.mojang.datafixers.Products.P1;
import jesse.keeblarcraft.ConfigMgr.ConfigManager; import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.Utils.ChatUtil; import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_CREATE_EXCEPTION;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource; 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 // Each player will retain their own private file of notes inside a "notes" directory inside the overall mod config directory
ConfigManager notesConfig = new ConfigManager(); 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() { public NoteCommands() {
// Check if directory exists // Check if directory exists
if (notesConfig.DoesDirectoryExist(NOTES_GLOBAL_DIRECTORY) == false) { if (notesConfig.DoesDirectoryExist(NOTES_GLOBAL_DIRECTORY) == false) {
// Attempt to create the directory // Attempt to create the directory
if (notesConfig.CreateDirectory(NOTES_GLOBAL_DIRECTORY) == true) { try {
System.out.println("Created notes directory successfully!"); //TODO: Success! if (notesConfig.CreateDirectory(NOTES_GLOBAL_DIRECTORY) == true) {
} else { System.out.println("Created notes directory successfully!"); //TODO: Success!
System.out.println("ERROR: Notes directory FAILED to create!! Permissions missing to create directory!"); //TODO: Critical failure } 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 { } else {
System.out.println("Notes directory already exists. Skipping creation..."); //TODO: Success! System.out.println("Notes directory already exists. Skipping creation..."); //TODO: Success!

View File

@ -33,13 +33,14 @@ public class ConfigManager {
// CreateFile // 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 { public Boolean CreateFile(String fileName) throws FILE_CREATE_EXCEPTION {
Boolean ret = false; Boolean ret = false;
File file = new File(fileName); File file = new File(fileName);
// Check 1: Does the file already exist? // 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 // Check 2: If the file does not exist, attempt to create it
if (ret == false) { if (ret == false) {
@ -49,6 +50,9 @@ public class ConfigManager {
// The file could not be created // The file could not be created
throw new FILE_CREATE_EXCEPTION(); 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; return ret;
} }
@ -70,6 +74,8 @@ public class ConfigManager {
} catch (SecurityException e) { } catch (SecurityException e) {
throw new FILE_DELETE_EXCEPTION(); throw new FILE_DELETE_EXCEPTION();
} }
} else {
System.out.println("Cannot delete file " + fileName + " because file does not exist");
} }
return ret; return ret;
} }
@ -80,6 +86,28 @@ public class ConfigManager {
public Boolean WriteToFile(String fileName, String data, String mode) { public Boolean WriteToFile(String fileName, String data, String mode) {
Boolean ret = false; 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; return ret;
} }
@ -129,10 +157,12 @@ public class ConfigManager {
File dir = new File(dirName); File dir = new File(dirName);
try { try {
if (dir.exists()) { if ( ! dir.exists()) {
ret = dir.mkdirs(); ret = dir.mkdirs();
} }
} catch (Exception e) { } 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(); throw new DIRECTORY_CREATE_EXCEPTION();
} }
return ret; return ret;
@ -145,7 +175,9 @@ public class ConfigManager {
try { try {
ret = dir.delete(); ret = dir.delete();
System.out.println("Deleted directory " + dirName);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Failed to delete directory " + dirName);
throw new DIRECTORY_DELETE_EXCEPTION(); throw new DIRECTORY_DELETE_EXCEPTION();
} }

View File

@ -16,7 +16,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jesse.keeblarcraft.Commands.CustomCommandManager; 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.CustomExceptions.SETUP_FAILED_EXCEPTION;
import jesse.keeblarcraft.Utils.Setup; import jesse.keeblarcraft.Utils.Setup;

View File

@ -62,13 +62,12 @@ public final class Setup {
// These will be top-level config files above the directories this mod creates // These will be top-level config files above the directories this mod creates
private static final List<String> FILE_LIST = new ArrayList<String>() {{ private static final List<String> FILE_LIST = new ArrayList<String>() {{
add("story.json"); // Big config file, determines when players can do certain things at different story levels add("story/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("factions/factions.json"); // General configuration file for factions stuff
add("events.json"); // General configuration file for story events! add("events/1events.json"); // General configuration file for story events!
add("general.json"); // The super general configuration file! (May be removed) add("general.json"); // The super general configuration file! (May be removed)
}}; }};
// RunChecks() // RunChecks()
// //
// Checks if we are able to create necessary directories and run reading over the file system for the current // 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 // Create directory check
try { try {
has_write = conf.CreateDirectory("test_dir"); has_write = conf.CreateDirectory("test_dir");
System.out.println("test_dir created? " + (has_write ? "YES" : "NO"));
} catch (DIRECTORY_CREATE_EXCEPTION e) { } 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 // Write to disk then read that data back
if (has_write) { if (has_write) {
try { try {
has_write = conf.CreateFile("test_dir/note.txt"); has_write = conf.CreateFile("test_dir/test_note.txt");
has_write = conf.WriteToFile("test_dir/note.txt", "test_write_read", "w"); has_write = conf.WriteToFile("test_dir/test_note.txt", "test_write_read", "w");
List<String> lines = conf.GetFile("test_dir/note.txt"); List<String> lines = conf.GetFile("test_dir/test_note.txt");
if (lines.size() == 0) { if (lines.size() == 0) {
has_read = false; has_read = false;
} else {
has_read = true;
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("Failed to create or write to test dir file");
has_read = false; has_read = false;
} }
} }
@ -102,12 +106,15 @@ public final class Setup {
// Delete directory if created (it's a temporary dir) // Delete directory if created (it's a temporary dir)
if (has_write) { if (has_write) {
try { try {
has_write = conf.DeleteFile("test_dir/test_note.txt");
has_write = conf.DeleteDirectory("test_dir"); 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; 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; return has_write && has_read;
} }
@ -125,22 +132,31 @@ public final class Setup {
for (Integer i = 0; i < DIRECTORY_LIST.size(); i++) { for (Integer i = 0; i < DIRECTORY_LIST.size(); i++) {
if ( ! conf.DoesDirectoryExist(DIRECTORY_LIST.get(i))) { if ( ! conf.DoesDirectoryExist(DIRECTORY_LIST.get(i))) {
conf.CreateDirectory(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 // Create necessary files
for (Integer i = 0; i < FILE_LIST.size(); i++) { 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)); 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) { } catch (Exception e) {
throw new SETUP_FAILED_EXCEPTION(); throw new SETUP_FAILED_EXCEPTION();
} }
} else { } 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(); throw new SETUP_FAILED_EXCEPTION();
} }
System.out.println("DID SETUP COMPLETE SUCCESSFULLY? " + (ret ? "YES" : "NO"));
return ret; return ret;
} }
} }