2024-07-29 00:13:48 +00:00
package jesse.keeblarcraft.Commands ;
2024-08-03 17:11:33 +00:00
import java.util.UUID ;
2024-07-29 00:13:48 +00:00
import com.mojang.brigadier.arguments.ArgumentType ;
import com.mojang.brigadier.arguments.IntegerArgumentType ;
import com.mojang.brigadier.arguments.StringArgumentType ;
import com.mojang.brigadier.context.CommandContext ;
import com.mojang.datafixers.Products.P1 ;
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-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 {
// IMPORTANT NOTE:
//
// Each player will retain their own private file of notes inside a "notes" directory inside the overall mod config directory
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
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 ) {
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 " ) ;
2024-07-29 00:13:48 +00:00
}
} else {
System . out . println ( " Notes directory already exists. Skipping creation... " ) ; //TODO: Success!
}
}
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 " )
. then ( CommandManager . argument ( " value " , StringArgumentType . greedyString ( ) )
. executes ( context - > PurgeAllNotes ( StringArgumentType . getString ( context , " value " ) , context ) ) ) ) ;
} ) ;
// Command: "/modifynote noteIdHere"
CommandRegistrationCallback . EVENT . register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( CommandManager . literal ( " editnote " )
. then ( CommandManager . argument ( " value " , IntegerArgumentType . integer ( ) )
. executes ( context - > ModifyNote ( IntegerArgumentType . getInteger ( context , " value " ) , context ) ) ) ) ;
} ) ;
// 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 ) ) ;
} ) ;
}
/// COMMAND HANDLERS BELOW
// AddNote
//
// Adds a new note based on the string value provided by the player. The note is labeled in the background based on the portion
// of the story they are currently in as well to help provide filtering methods later on
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 {
System . out . println ( " Only a player can execute this command! " ) ;
}
return ret ;
}
private int DeleteNote ( int value , CommandContext < ServerCommandSource > context ) {
int ret = - 1 ;
return ret ;
}
private int ModifyNote ( int value , CommandContext < ServerCommandSource > context ) {
int ret = - 1 ;
return ret ;
}
private int PurgeAllNotes ( String value , CommandContext < ServerCommandSource > context ) {
int ret = - 1 ;
return ret ;
}
private int ListNotes ( String value , CommandContext < ServerCommandSource > context ) {
int ret = - 1 ;
return ret ;
}
private int OpenNoteGui ( String value , CommandContext < ServerCommandSource > context ) {
int ret = - 1 ;
return ret ;
}
private int FilterForNote ( String value , CommandContext < ServerCommandSource > context ) {
int ret = - 1 ;
return ret ;
}
}