2022-04-07 02:00:28 +00:00
package dev.plex.command.impl ;
import dev.plex.cache.DataUtils ;
import dev.plex.command.PlexCommand ;
import dev.plex.command.annotation.CommandParameters ;
import dev.plex.command.annotation.CommandPermissions ;
import dev.plex.player.PlexPlayer ;
import dev.plex.punishment.extra.Note ;
import dev.plex.rank.enums.Rank ;
2022-04-07 07:37:31 +00:00
import dev.plex.storage.StorageType ;
2022-04-07 02:00:28 +00:00
import dev.plex.util.PlexUtils ;
2022-04-13 02:22:17 +00:00
import java.time.LocalDateTime ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.List ;
import java.util.concurrent.atomic.AtomicReference ;
2022-04-07 02:00:28 +00:00
import net.kyori.adventure.text.Component ;
import net.kyori.adventure.text.format.NamedTextColor ;
import org.apache.commons.lang3.ArrayUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
import org.jetbrains.annotations.NotNull ;
import org.jetbrains.annotations.Nullable ;
@CommandParameters ( name = " notes " , description = " Manage notes for a player " , usage = " /<command> <player> <list | add <note> | remove <id> | clear> " )
@CommandPermissions ( level = Rank . ADMIN , permission = " plex.notes " )
public class NotesCMD extends PlexCommand
{
@Override
protected Component execute ( @NotNull CommandSender sender , @Nullable Player playerSender , String [ ] args )
{
if ( args . length < 2 )
{
return usage ( ) ;
}
Player player = getNonNullPlayer ( args [ 0 ] ) ;
PlexPlayer plexPlayer = getPlexPlayer ( player ) ;
switch ( args [ 1 ] . toLowerCase ( ) )
{
case " list " :
{
2022-04-07 07:37:31 +00:00
if ( plugin . getStorageType ( ) ! = StorageType . MONGODB )
2022-04-07 02:00:28 +00:00
{
2022-04-07 07:37:31 +00:00
plugin . getSqlNotes ( ) . getNotes ( plexPlayer . getUuid ( ) ) . whenComplete ( ( notes , ex ) - >
{
if ( notes . size ( ) = = 0 )
{
send ( sender , mmString ( " <red>This player has no notes! " ) ) ;
return ;
}
2022-04-07 23:23:38 +00:00
readNotes ( sender , plexPlayer , notes ) ;
2022-04-07 07:37:31 +00:00
} ) ;
2022-04-07 23:23:38 +00:00
}
else
2022-04-07 07:37:31 +00:00
{
List < Note > notes = plexPlayer . getNotes ( ) ;
2022-04-07 03:55:01 +00:00
if ( notes . size ( ) = = 0 )
{
2022-04-07 07:37:31 +00:00
return mmString ( " <red>This player has no notes! " ) ;
2022-04-07 03:55:01 +00:00
}
2022-04-07 23:23:38 +00:00
readNotes ( sender , plexPlayer , notes ) ;
2022-04-07 07:37:31 +00:00
}
2022-04-07 02:00:28 +00:00
return null ;
}
case " add " :
{
if ( args . length < 3 )
{
return usage ( ) ;
}
String content = StringUtils . join ( ArrayUtils . subarray ( args , 2 , args . length ) , " " ) ;
if ( playerSender ! = null )
{
2022-04-07 03:55:01 +00:00
Note note = new Note ( plexPlayer . getUuid ( ) , content , playerSender . getUniqueId ( ) , LocalDateTime . now ( ) ) ;
2022-04-07 02:00:28 +00:00
plexPlayer . getNotes ( ) . add ( note ) ;
2022-04-07 07:37:31 +00:00
if ( plugin . getStorageType ( ) ! = StorageType . MONGODB )
{
plugin . getSqlNotes ( ) . addNote ( note ) ;
2022-04-07 23:23:38 +00:00
}
else
2022-04-07 07:37:31 +00:00
{
DataUtils . update ( plexPlayer ) ;
}
2022-04-07 02:00:28 +00:00
return Component . text ( " Note added. " ) . color ( NamedTextColor . GREEN ) ;
}
}
case " remove " :
{
2022-04-10 22:02:50 +00:00
if ( args . length < 3 )
{
return usage ( ) ;
}
2022-04-07 03:55:01 +00:00
int id ;
try
{
id = Integer . parseInt ( args [ 2 ] ) ;
2022-04-07 23:23:38 +00:00
}
catch ( NumberFormatException ignored )
2022-04-07 03:55:01 +00:00
{
return Component . text ( " Invalid number: " + args [ 2 ] ) . color ( NamedTextColor . RED ) ;
}
2022-04-07 07:37:31 +00:00
if ( plugin . getStorageType ( ) ! = StorageType . MONGODB )
2022-04-07 03:55:01 +00:00
{
2022-04-07 07:37:31 +00:00
plugin . getSqlNotes ( ) . getNotes ( plexPlayer . getUuid ( ) ) . whenComplete ( ( notes , ex ) - >
2022-04-07 03:55:01 +00:00
{
2022-04-10 22:02:50 +00:00
boolean deleted = false ;
2022-04-07 07:37:31 +00:00
for ( Note note : notes )
2022-04-07 03:55:01 +00:00
{
2022-04-07 07:37:31 +00:00
if ( note . getId ( ) = = id )
{
plugin . getSqlNotes ( ) . deleteNote ( id , plexPlayer . getUuid ( ) ) . whenComplete ( ( notes1 , ex1 ) - >
send ( sender , Component . text ( " Removed note with ID: " + id ) . color ( NamedTextColor . GREEN ) ) ) ;
2022-04-10 22:02:50 +00:00
deleted = true ;
2022-04-07 23:23:38 +00:00
}
2022-04-10 22:02:50 +00:00
}
if ( ! deleted )
{
send ( sender , mmString ( " <red>A note with this ID could not be found " ) ) ;
2022-04-07 03:55:01 +00:00
}
2022-04-07 07:37:31 +00:00
plexPlayer . getNotes ( ) . removeIf ( note - > note . getId ( ) = = id ) ;
} ) ;
2022-04-07 23:23:38 +00:00
}
else
2022-04-07 07:37:31 +00:00
{
if ( plexPlayer . getNotes ( ) . removeIf ( note - > note . getId ( ) = = id ) )
{
return Component . text ( " Removed note with ID: " + id ) . color ( NamedTextColor . GREEN ) ;
2022-04-07 03:55:01 +00:00
}
2022-04-07 07:37:31 +00:00
return mmString ( " <red>A note with this ID could not be found " ) ;
}
2022-04-10 22:02:50 +00:00
return null ;
2022-04-07 02:00:28 +00:00
}
case " clear " :
{
2022-04-07 07:37:31 +00:00
if ( plugin . getStorageType ( ) ! = StorageType . MONGODB )
2022-04-07 02:00:28 +00:00
{
2022-04-07 07:37:31 +00:00
plugin . getSqlNotes ( ) . getNotes ( plexPlayer . getUuid ( ) ) . whenComplete ( ( notes , ex ) - >
2022-04-07 03:55:01 +00:00
{
2022-04-07 07:37:31 +00:00
for ( Note note : notes )
{
plugin . getSqlNotes ( ) . deleteNote ( note . getId ( ) , plexPlayer . getUuid ( ) ) ;
}
plexPlayer . getNotes ( ) . clear ( ) ;
send ( sender , Component . text ( " Cleared " + notes . size ( ) + " note(s). " ) . color ( NamedTextColor . GREEN ) ) ;
} ) ;
2022-04-07 23:23:38 +00:00
}
else
{
2022-04-07 07:37:31 +00:00
int count = plexPlayer . getNotes ( ) . size ( ) ;
plexPlayer . getNotes ( ) . clear ( ) ;
DataUtils . update ( plexPlayer ) ;
return Component . text ( " Cleared " + count + " note(s). " ) . color ( NamedTextColor . GREEN ) ;
}
2022-04-07 03:55:01 +00:00
return null ;
2022-04-07 02:00:28 +00:00
}
default :
{
return usage ( ) ;
}
}
}
2022-04-07 23:23:38 +00:00
private void readNotes ( @NotNull CommandSender sender , PlexPlayer plexPlayer , List < Note > notes )
{
AtomicReference < Component > noteList = new AtomicReference < > ( Component . text ( " Player notes for: " + plexPlayer . getName ( ) ) . color ( NamedTextColor . GREEN ) ) ;
for ( Note note : notes )
{
2022-04-10 23:30:21 +00:00
Component noteLine = mmString ( " <gold><!italic> " + note . getId ( ) + " - Written by: " + DataUtils . getPlayer ( note . getWrittenBy ( ) ) . getName ( ) + " on " + PlexUtils . useTimezone ( note . getTimestamp ( ) ) ) ;
2022-04-10 22:27:25 +00:00
noteLine = noteLine . append ( mmString ( " <newline><yellow># " + note . getNote ( ) ) ) ;
2022-04-07 23:23:38 +00:00
noteList . set ( noteList . get ( ) . append ( Component . newline ( ) ) ) ;
noteList . set ( noteList . get ( ) . append ( noteLine ) ) ;
}
send ( sender , noteList . get ( ) ) ;
}
2022-04-07 02:00:28 +00:00
@Override
public @NotNull List < String > tabComplete ( @NotNull CommandSender sender , @NotNull String alias , @NotNull String [ ] args ) throws IllegalArgumentException
{
if ( args . length = = 1 )
{
return PlexUtils . getPlayerNameList ( ) ;
}
if ( args . length = = 2 )
{
return Arrays . asList ( " list " , " add " , " remove " , " clear " ) ;
}
return Collections . emptyList ( ) ;
}
}