2024-07-29 00:13:48 +00:00
package jesse.keeblarcraft.Commands ;
import com.mojang.brigadier.arguments.IntegerArgumentType ;
import com.mojang.brigadier.arguments.StringArgumentType ;
import com.mojang.brigadier.context.CommandContext ;
import jesse.keeblarcraft.ConfigMgr.ConfigManager ;
2024-08-03 17:11:33 +00:00
import jesse.keeblarcraft.JsonClassObjects.PlayerNote ;
2024-07-29 00:13:48 +00:00
import jesse.keeblarcraft.Utils.ChatUtil ;
2024-08-10 19:27:39 +00:00
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR ;
2024-08-01 02:50:08 +00:00
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_CREATE_EXCEPTION ;
2024-07-29 00:13:48 +00:00
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback ;
import net.minecraft.server.command.CommandManager ;
import net.minecraft.server.command.ServerCommandSource ;
import net.minecraft.server.network.ServerPlayerEntity ;
public class NoteCommands {
2024-08-10 19:19:42 +00:00
/// Class Variables
2024-07-29 00:13:48 +00:00
ConfigManager notesConfig = new ConfigManager ( ) ;
2024-08-01 02:50:08 +00:00
String NOTES_GLOBAL_DIRECTORY = " notes " ; // The overall "notes" dir inside cfg folder
2024-07-29 00:13:48 +00:00
2024-08-10 19:19:42 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn NoteCommands
///
/// @brief This classes non-trivial constructor. Ensures creation
// of notes directory exists before commands can be ran
/////////////////////////////////////////////////////////////////////////////
2024-07-29 00:13:48 +00:00
public NoteCommands ( ) {
// Check if directory exists
if ( notesConfig . DoesDirectoryExist ( NOTES_GLOBAL_DIRECTORY ) = = false ) {
// Attempt to create the directory
2024-08-01 02:50:08 +00:00
try {
if ( notesConfig . CreateDirectory ( NOTES_GLOBAL_DIRECTORY ) = = true ) {
2024-08-10 19:27:39 +00:00
System . out . println ( ChatUtil . ColoredString ( " Created notes directory successfully! " , CONSOLE_COLOR . BLUE ) ) ; //TODO: Success!
2024-08-01 02:50:08 +00:00
} else {
2024-08-10 19:27:39 +00:00
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
2024-08-01 02:50:08 +00:00
}
} catch ( DIRECTORY_CREATE_EXCEPTION e ) {
2024-08-10 19:27:39 +00:00
System . out . println ( ChatUtil . ColoredString ( " Directory creation failed " , CONSOLE_COLOR . RED ) ) ;
2024-07-29 00:13:48 +00:00
}
} else {
2024-08-10 19:27:39 +00:00
System . out . println ( ChatUtil . ColoredString ( " Notes directory already exists. Skipping creation... " , CONSOLE_COLOR . BLUE ) ) ; //TODO: Success!
2024-07-29 00:13:48 +00:00
}
}
2024-08-10 19:19:42 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn RegisterNoteCommands
///
/// @brief Registers all the commands for this class
/////////////////////////////////////////////////////////////////////////////
2024-07-29 00:13:48 +00:00
public void RegisterNoteCommands ( ) {
// Command: "/addnote note goes here"
CommandRegistrationCallback . EVENT . register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( CommandManager . literal ( " addnote " )
. then ( CommandManager . argument ( " value " , StringArgumentType . greedyString ( ) )
. executes ( context - > AddNote ( StringArgumentType . getString ( context , " value " ) , context ) ) ) ) ;
} ) ;
// Command: "/delnote noteIdHere"
// CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
// dispatcher.register(CommandManager.literal("delnote")
// .then(CommandManager.argument("value", StringArgumentType.greedyString())
// .executes(context -> AddNote(StringArgumentType.getString(context, "value"), context))));
// });
// Command: "/purgenotes"
CommandRegistrationCallback . EVENT . register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( CommandManager . literal ( " purgenotes " )
2024-08-10 19:19:42 +00:00
. executes ( context - > PurgeAllNotes ( context ) ) ) ;
2024-07-29 00:13:48 +00:00
} ) ;
2024-08-10 19:19:42 +00:00
// Command: "/modifynote noteIdHere new_note_string_here"
// Alises: "/editnote"
2024-07-29 00:13:48 +00:00
CommandRegistrationCallback . EVENT . register ( ( dispatcher , registryAccess , environment ) - > {
2024-08-10 19:19:42 +00:00
final var mNote = dispatcher . register ( CommandManager . literal ( " editnote " )
. then ( CommandManager . argument ( " note_id " , IntegerArgumentType . integer ( ) )
. then ( CommandManager . argument ( " new_note " , StringArgumentType . string ( ) )
. executes ( context - > ModifyNote (
IntegerArgumentType . getInteger ( context , " note_id " ) ,
StringArgumentType . getString ( context , " new_note " ) ,
context ) ) ) ) ) ;
dispatcher . register ( CommandManager . literal ( " editnote " ) . redirect ( mNote ) ) ;
2024-07-29 00:13:48 +00:00
} ) ;
// Command Root: "/delnote noteIdHere"
// Aliases: "/rmnote", "/deletenote"
CommandRegistrationCallback . EVENT . register ( ( dispatcher , registryAccess , environment ) - > {
final var rootDeleteCmd = dispatcher . register ( CommandManager . literal ( " delnote " )
. then ( CommandManager . argument ( " value " , IntegerArgumentType . integer ( ) )
. executes ( context - > DeleteNote ( IntegerArgumentType . getInteger ( context , " value " ) , context ) ) ) ) ;
// Alias redirects
dispatcher . register ( CommandManager . literal ( " rmnote " ) . redirect ( rootDeleteCmd ) ) ;
dispatcher . register ( CommandManager . literal ( " deletenote " ) . redirect ( rootDeleteCmd ) ) ;
} ) ;
2024-08-10 19:19:42 +00:00
// Command Root: "/notegui"
CommandRegistrationCallback . EVENT . register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( CommandManager . literal ( " notegui " )
. executes ( context - > { OpenNoteGui ( context ) ;
return 0 ;
} ) ) ;
} ) ;
2024-07-29 00:13:48 +00:00
2024-08-10 19:19:42 +00:00
// Command Root: "/notelist"
// Aliases: "/listnotes"
CommandRegistrationCallback . EVENT . register ( ( dispatcher , registryAccess , environment ) - > {
final var rootListNotes = dispatcher . register ( CommandManager . literal ( " notelist " )
. executes ( context - > { ListNotes ( context ) ;
return 0 ;
} ) ) ;
dispatcher . register ( CommandManager . literal ( " listnotes " ) . redirect ( rootListNotes ) ) ;
} ) ;
}
/////////////////////////////////////////////////////////////////////////////
/// @fn AddNote
///
/// @brief Adds a new note to the players notebook
///
/// @arg[in] value is the new note to be added
///
/// @arg[in] context is the context of the ServerCommandSource object
/// the command was run with
///
/// @return 0 if success, -1 if not
/////////////////////////////////////////////////////////////////////////////
2024-08-03 17:11:33 +00:00
private Integer AddNote ( String value , CommandContext < ServerCommandSource > context ) {
Integer ret = - 1 ;
2024-07-29 00:13:48 +00:00
if ( context . getSource ( ) . isExecutedByPlayer ( ) ) {
ServerPlayerEntity player = context . getSource ( ) . getPlayer ( ) ;
2024-08-03 17:11:33 +00:00
// Note mgmt
PlayerNote note = new PlayerNote ( player . getUuidAsString ( ) ) ;
note . AddNote ( value , 1 , 1 , 1 , 1 ) ;
2024-07-29 00:13:48 +00:00
ChatUtil . SendPlayerMsg ( player , " New note logged to entry! View notes any time with /notegui " ) ;
2024-08-03 17:11:33 +00:00
ret = 0 ;
2024-07-29 00:13:48 +00:00
} else {
2024-08-10 19:27:39 +00:00
System . out . println ( ChatUtil . ColoredString ( " Only a player can execute this command! " , CONSOLE_COLOR . RED ) ) ;
2024-07-29 00:13:48 +00:00
}
return ret ;
}
2024-08-10 19:19:42 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn DeleteNote
///
/// @brief Deletes a note by id
///
/// @arg[in] value is the integer ID of the note to be deleted
///
/// @arg[in] context is the context of the ServerCommandSource object
/// the command was run with
///
/// @return 0 if success, -1 if not
/////////////////////////////////////////////////////////////////////////////
2024-07-29 00:13:48 +00:00
private int DeleteNote ( int value , CommandContext < ServerCommandSource > context ) {
int ret = - 1 ;
2024-08-10 19:19:42 +00:00
if ( context . getSource ( ) . isExecutedByPlayer ( ) ) {
ServerPlayerEntity player = context . getSource ( ) . getPlayer ( ) ;
2024-07-29 00:13:48 +00:00
2024-08-10 19:19:42 +00:00
PlayerNote note = new PlayerNote ( player . getUuidAsString ( ) ) ;
ChatUtil . SendPlayerMsg ( player , " Deleted note entry. View notes any time with /notegui " ) ;
ret = 0 ;
note . DeleteNote ( value ) ;
} else {
System . out . println ( " Only a player can execute this command! " ) ;
}
2024-07-29 00:13:48 +00:00
return ret ;
}
2024-08-10 19:19:42 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn ModifyNote
///
/// @brief Modifies a single note by id value
///
/// @arg[in] value is the integer ID of the note to be modified
///
/// @arg[in] newNote is the new version of the edited note
///
/// @arg[in] context is the context of the ServerCommandSource object
/// the command was run with
///
/// @return 0 if success, -1 if not
/////////////////////////////////////////////////////////////////////////////
private int ModifyNote ( Integer value , String newNote , CommandContext < ServerCommandSource > context ) {
2024-07-29 00:13:48 +00:00
int ret = - 1 ;
2024-08-10 19:19:42 +00:00
if ( context . getSource ( ) . isExecutedByPlayer ( ) & & value > 0 ) {
ServerPlayerEntity player = context . getSource ( ) . getPlayer ( ) ;
PlayerNote note = new PlayerNote ( player . getUuidAsString ( ) ) ;
long time = context . getSource ( ) . getWorld ( ) . getTime ( ) ;
// long day = ; ///TODO: Docs lack this for some reason? Add in future
long epochTime = System . currentTimeMillis ( ) ;
long storyChapter = - 1 ; // Intentional garbage until story is fleshed out later (TODO)
long storyPart = - 1 ; // Intentional garbage until story is fleshed out later (TODO)
note . ModifyNote ( value , newNote , epochTime , storyChapter , storyPart ) ;
ret = 0 ;
}
2024-07-29 00:13:48 +00:00
return ret ;
}
2024-08-10 19:19:42 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn PurgeAllNotes
///
/// @brief Removes all notes from a players note file
///
/// @arg[in] context is the context of the ServerCommandSource object
/// the command was run with
///
/// @return 0 if success, -1 if not
/////////////////////////////////////////////////////////////////////////////
private int PurgeAllNotes ( CommandContext < ServerCommandSource > context ) {
2024-07-29 00:13:48 +00:00
int ret = - 1 ;
2024-08-10 19:19:42 +00:00
if ( context . getSource ( ) . isExecutedByPlayer ( ) ) {
ServerPlayerEntity player = context . getSource ( ) . getPlayer ( ) ;
PlayerNote note = new PlayerNote ( player . getUuidAsString ( ) ) ;
note . PurgeAllNotes ( ) ;
ChatUtil . SendPlayerMsg ( player , " Purged all notes. View notes any time with /notegui " ) ;
ret = 0 ;
} else {
System . out . println ( " Only a player can execute this command! " ) ;
}
2024-07-29 00:13:48 +00:00
return ret ;
}
2024-08-10 19:19:42 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn ListNotes
///
/// @brief Lists notes in pages in the players active chat
///
/// @arg[in] context is the context of the ServerCommandSource object
/// the command was run with
///
/// @return 0 if success, -1 if not
/////////////////////////////////////////////////////////////////////////////
private int ListNotes ( CommandContext < ServerCommandSource > context ) {
2024-07-29 00:13:48 +00:00
int ret = - 1 ;
2024-08-10 19:19:42 +00:00
if ( context . getSource ( ) . isExecutedByPlayer ( ) ) {
ServerPlayerEntity player = context . getSource ( ) . getPlayer ( ) ;
PlayerNote notes = new PlayerNote ( player . getUuidAsString ( ) ) ;
ChatUtil . SendPlayerMsg ( player , " Listing all notes... " ) ;
for ( int i = 0 ; i < = notes . GetNotebookSize ( ) ; i + + ) {
String individualNote = notes . GetNoteString ( i ) ;
if ( individualNote ! = " " ) {
ChatUtil . SendPlayerMsg ( player , " Note " + i + " : " + individualNote ) ;
}
}
ret = 0 ;
} else {
System . out . println ( " Only a player can execute this command! " ) ;
}
2024-07-29 00:13:48 +00:00
return ret ;
}
2024-08-10 19:19:42 +00:00
///TODO: Blocked until GUI manager is available
/////////////////////////////////////////////////////////////////////////////
/// @fn OpenNoteGui
///
/// @brief Opens up the graphical display of the note manager
///
/// @arg[in] context is the context of the ServerCommandSource object
/// the command was run with
///
/// @return 0 if success, -1 if not
/////////////////////////////////////////////////////////////////////////////
private int OpenNoteGui ( CommandContext < ServerCommandSource > context ) {
2024-07-29 00:13:48 +00:00
int ret = - 1 ;
2024-08-10 19:19:42 +00:00
if ( context . getSource ( ) . isExecutedByPlayer ( ) ) {
ServerPlayerEntity player = context . getSource ( ) . getPlayer ( ) ;
ret = 0 ;
} else {
System . out . println ( " Only a player can execute this command! " ) ;
}
2024-07-29 00:13:48 +00:00
return ret ;
}
}