2021-01-10 02:09:40 +00:00
package me.rigamortis.seppuku.impl.command ;
import com.mojang.realmsclient.gui.ChatFormatting ;
import me.rigamortis.seppuku.Seppuku ;
import me.rigamortis.seppuku.api.command.Command ;
2021-01-10 02:11:14 +00:00
import me.rigamortis.seppuku.impl.module.world.NoteBotModule ;
2021-01-10 02:09:40 +00:00
import java.io.File ;
/ * *
* @author noil
* /
public final class PlayCommand extends Command {
2021-01-13 04:41:26 +00:00
private final File directory ;
2021-01-10 02:09:40 +00:00
public PlayCommand ( ) {
2023-05-20 21:01:41 +00:00
super ( " Play " , new String [ ] { " playsong " , " begin " } , " Plays a song file from your /Seppuku/Config/<current config>/Songs/ directory. " , " .play <midi file name> " ) ;
2021-01-10 02:09:40 +00:00
this . directory = new File ( Seppuku . INSTANCE . getConfigManager ( ) . getConfigDir ( ) , " Songs " ) ;
if ( ! directory . exists ( ) ) {
directory . mkdirs ( ) ;
}
}
@Override
public void exec ( String input ) {
if ( ! this . clamp ( input , 2 , 2 ) ) {
this . printUsage ( ) ;
2023-05-20 21:01:41 +00:00
if ( ! this . directory . exists ( ) )
return ;
final StringBuilder listedFilesBuilder = new StringBuilder ( ) ;
final String [ ] fileList = this . directory . list ( ) ;
if ( fileList = = null )
return ;
if ( fileList . length = = 0 | | fileList . length > 100 )
return ;
for ( int i = 0 ; i < fileList . length ; i + + ) {
String s = fileList [ i ] . replaceAll ( " .midi " , " " ) . replaceAll ( " .mid " , " " ) ;
listedFilesBuilder . append ( ChatFormatting . GREEN ) . append ( s ) ;
if ( i ! = fileList . length - 1 )
listedFilesBuilder . append ( ChatFormatting . GRAY ) . append ( " , " ) ;
}
Seppuku . INSTANCE . logChat ( ChatFormatting . GRAY + listedFilesBuilder . toString ( ) ) ;
2021-01-10 02:09:40 +00:00
return ;
}
final String [ ] split = input . split ( " " ) ;
2021-01-10 02:11:14 +00:00
final NoteBotModule notebotModule = ( NoteBotModule ) Seppuku . INSTANCE . getModuleManager ( ) . find ( NoteBotModule . class ) ;
2021-01-10 02:09:40 +00:00
if ( notebotModule ! = null ) {
try {
2021-01-13 04:41:26 +00:00
// check for .mid or .midi
if ( split [ 1 ] . contains ( " .mid " ) )
split [ 1 ] = split [ 1 ] . replaceAll ( " .mid " , " " ) ;
if ( split [ 1 ] . contains ( " .midi " ) )
split [ 1 ] = split [ 1 ] . replaceAll ( " .midi " , " " ) ;
File midiFile = new File ( directory , split [ 1 ] + " .mid " ) ;
if ( ! midiFile . exists ( ) ) {
midiFile = new File ( directory , split [ 1 ] + " .midi " ) ;
}
// now check if the midi file exists
if ( midiFile . exists ( ) ) {
2021-01-10 02:09:40 +00:00
notebotModule . getState ( ) . setEnumValue ( " PLAYING " ) ;
2021-01-13 04:41:26 +00:00
notebotModule . getNotePlayer ( ) . begin ( midiFile , notebotModule ) ;
2021-01-13 05:14:47 +00:00
Seppuku . INSTANCE . logChat ( " Playing ' " + ChatFormatting . YELLOW + midiFile . getName ( ) + ChatFormatting . RESET + " ' " ) ;
2021-01-10 02:09:40 +00:00
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}
}