Added start of frame work for converting json objects back to java via Notes

This commit is contained in:
jkibbels 2024-08-01 00:03:46 -04:00
parent 6d82a8b6bc
commit 3507b7e69a
4 changed files with 84 additions and 31 deletions

View File

@ -8,31 +8,12 @@
package jesse.keeblarcraft.Commands;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
public class CustomCommandManager {
// Intentionally empty constructor since at object definition time it may not be possible to register commands
public CustomCommandManager() {}
// Registers all commands for the mod
// Registers all custom command classes here
public void RegisterCustomCommands() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("amogus").executes(ctx -> {
ctx.getSource().sendFeedback(() -> Text.literal("AMOGOOOOOO"), false);
return 0;
}));
});
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("cbtest").executes(ctx -> {
ctx.getSource().sendFeedback(() -> TestCallback(ctx.getSource()), false);
return 0;
}));
});
// CUSTOM COMMAND CLASS OBJECTS BELOW
ShortcutCommands shortcuts = new ShortcutCommands();
NoteCommands noteCommands = new NoteCommands();
@ -41,11 +22,5 @@ public class CustomCommandManager {
System.out.println("REGISTERING CUSTOM COMMAND EXTENSIONS BELOW");
shortcuts.RegisterShortcutCommands();
noteCommands.RegisterNoteCommands();
}
public Text TestCallback(ServerCommandSource ctx) {
String stuff = ctx.getName();
return Text.literal("Callback called from " + stuff);
}
}

View File

@ -133,8 +133,22 @@ public class ConfigManager {
// GetJsonStringFromFile
//
// Retrieves a json formatted string from the file based on key. Returns empty string if not found
public String GetJsonStringFromFile(String fileName, String key) {
String ret = "";
public Object GetJsonObjectFromFile(String key, String fileName, Object jsonObject) {
Object ret = jsonObject.getClass();
Gson gson = new Gson();
// Step 1: Get file as 1 constant string
try {
File file = new File(fileName);
if (file.exists()) {
String str = file.toString();
ret = gson.fromJson(str, jsonObject.getClass());
} else {
System.out.println("File does not exist. Cannot convert to json");
}
} catch (Exception e) {
System.out.println("Something went wrong when converting json file object to object");
}
return ret;
}

View File

@ -0,0 +1,59 @@
/*
*
* Note.java
*
* Class defines what will be in a configuration file for a user note. All metadata is in this class when read from file
*/
package jesse.keeblarcraft.JsonClassObjects;
import java.util.HashMap;
import java.util.Map.Entry;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
public class PlayerNote {
// Internal class structure that defines a blank note. It represents the overall loaded json object
public class NoteFile {
public String uuid;
// Integer = note id
// String = note
public HashMap<Integer, String> noteMap = new HashMap<Integer, String>();
}
NoteFile thisNote = new NoteFile();
ConfigManager config = new ConfigManager();
public PlayerNote(String uuid) {
/// TODO: Below code needs to be tested first
thisNote = (NoteFile) config.GetJsonObjectFromFile(uuid, uuid, NoteFile.class);
}
public void AddNote(String newNote) {
Integer noteKey = thisNote.noteMap.size() + 1;
thisNote.noteMap.put(noteKey, newNote);
}
public void DeleteNote(Integer noteId) {
thisNote.noteMap.remove(noteId);
}
public void ModifyNote(Integer noteId, String newNote) {
thisNote.noteMap.put(noteId, newNote);
}
// Find the key of a note if it exists (O(n) search time)
// Returns -1 on failure to find note key
public Integer GetNoteKey(String note) {
Integer ret = -1;
for (Entry<Integer, String> entry : thisNote.noteMap.entrySet()) {
if (entry.getValue().equals(note)) {
ret = entry.getKey();
}
}
return ret;
}
}

View File

@ -36,20 +36,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!");
cmdMgr.RegisterCustomCommands();
if (setup != null) {
try {
// Run setup. If setup fails; it throws SETUP_FAILED_EXCEPTION
LOGGER.info("Running setup stage");
setup.RunSetup();
// Run command registrations from the command manager
LOGGER.info("Running command registration");
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.");
e.printStackTrace();
}
} else {
// Program exit. Dual definition of setup somehow happened!
System.out.println("Dual definition of singleton attempted! Out of order initialization?");
System.out.println("Dual definition of singleton attempted! Out of order initialization? How did this even happen?");
}
}
}