the_big_one/src/main/java/jesse/keeblarcraft/JsonClassObjects/PlayerNote.java
2024-08-10 19:27:39 +00:00

151 lines
5.5 KiB
Java

/*
*
* 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.math.BigInteger;
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 {
// Internal class structure that defines a blank note. It represents the overall loaded json object
private class NoteMetadata {
public NoteMetadata(String note, long id, long mcday, long sysTime, long chapter, long part) {
this.note = note;
this.noteId = id;
this.minecraftDay = mcday;
this.systemTime = sysTime;
this.storyChapter = chapter;
this.storyPart = part;
}
String note; // The note itself
long noteId; // Copied in from the file name, noteId
long minecraftDay; // The minecraft day the note was taken on
long systemTime; // The current system time of the server
long storyChapter; // The chapter of the story the player is in
long storyPart; // Every event in a story is one part
}
public class NoteFile {
// Players uuid is the name of the file
String uuid;
// Contents of file
/*
* Example:
* player_uuid_here:
* {
* "1":
* {
* "note": "this is the players first note";
* "noteId": "1";
* "minecraftDay": "443";
* "systemTime": "4849892839823";
* "storyChapter": "3";
* "storyPart": "2";
* }
* "2":
* {
* Etc.
* }
* }
*/
public HashMap<Integer, NoteMetadata> noteMap = new HashMap<Integer, NoteMetadata>();
}
NoteFile thisNote = new NoteFile();
ConfigManager config = new ConfigManager();
class TestClass {
String testString;
}
// PlayerNote
//
// String uuid - The uuid of the player (used in searching for existing notes file and writing to it)
public PlayerNote(String uuid) {
// DEVELOPER NOTE:
// If you are testing this part of the code, please be reminded that anonymous testing starts a new
// player instance everytime you launch. This means the UUID CAN CHANGE when you launch the
// game! This is not an issue with proper registered accounts in production
Boolean existingFile = false;
try {
thisNote = config.GetJsonObjectFromFile("notes/" + uuid + ".json", NoteFile.class);
existingFile = true;
} catch (Exception e) {
// Do nothing. This means the file does not exist
}
// 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(ChatUtil.ColoredString("Trying to create new file", CONSOLE_COLOR.BLUE));
try {
thisNote.uuid = uuid;
FlashConfig();
} catch (Exception e) {
System.out.println(ChatUtil.ColoredString("Could not write to file", CONSOLE_COLOR.RED));
}
} else {
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(ChatUtil.ColoredString("Assigning new config file for this uuid. No previous existing", CONSOLE_COLOR.BLUE));
thisNote.uuid = uuid;
}
}
public void AddNote(String newNote, long minecraftDay, long systemTime, long storyChapter, long storyPart) {
Integer noteKey = thisNote.noteMap.size() + 1;
thisNote.noteMap.put(noteKey, new NoteMetadata(newNote, noteKey, minecraftDay, systemTime, storyChapter, storyPart));
FlashConfig(); ///TODO: This might be really unnecessary and may only be required on clean up as opposed to everytime the command is run
}
public void DeleteNote(Integer noteId) {
thisNote.noteMap.remove(noteId);
}
public void ModifyNote(Integer noteId, String newNote, long minecraftDay, long systemTime, long storyChapter, long storyPart) {
thisNote.noteMap.put(noteId, new NoteMetadata(newNote, noteId, minecraftDay, systemTime, storyChapter, storyPart));
}
// 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 currentNote) {
Integer ret = -1;
for (Entry<Integer, NoteMetadata> entry : thisNote.noteMap.entrySet()) {
if (entry.getValue().note.equals(currentNote)) {
ret = entry.getKey();
}
}
return ret;
}
public void FlashConfig() {
try {
config.WriteToJsonFile("notes/" + thisNote.uuid.toString() + ".json", thisNote);
} catch (FILE_WRITE_EXCEPTION e) {
System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED));
}
}
}