@ -8,7 +8,6 @@ import org.json.JSONObject;
import java.io.File ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Files ;
import java.nio.file.Paths ;
import java.nio.file.Paths ;
@ -19,32 +18,53 @@ import java.util.stream.StreamSupport;
public final class Resources {
public final class Resources {
@Getter
@Getter
private static boolean greetings_and_byebyes_on ;
private static String resourcePath ;
@Getter
private static String configPath ;
@Getter
private static String audioPath ;
private static String tempPath ;
@Getter
private static String activitiesPath ;
@Getter
private static String greetingsPath ;
@Getter
private static String byebyesPath ;
@Getter
private static String sauceConfigPath ;
@Getter
private static String ttsPath ;
@Getter
@Getter
private static int statusUpdate ;
private static String patPngPath ;
@Getter
private static String imagePath ;
@Getter
private static String bonkPngPath ;
@Getter
@Getter
private static long guild_id ;
private static String jda_builder_string ;
@Getter
@Getter
private static String resourcePath , configPath , audioPath , tempPath , activitiesPath , greetingsPath , byebyesPath ,
sauceConfigPath , ttsPath , patPngPath , imagePath , bonkPngPath , jda_builder_string , status_channel ,
path_to_mcstatus , comebacksPath , departsPath , mcserver_config_file ;
private static Long guild_id ;
private static List < String > greetings , byebyes , departs , comebacks ;
@Getter
private static boolean greetings_and_byebyes_on ;
private static List < String > greetings ;
private static List < String > byebyes ;
@Getter
@Getter
private static String [ ] restrict_commands_to_channel , filteredTags ;
private static String [ ] restrict_commands_to_channel ;
@Getter
private static String [ ] filteredTags ;
@Getter
@Getter
private static final Map < String , List < String > > feedDetails = new HashMap < > ( ) ;
private static final Map < String , List < String > > feedDetails = new HashMap < > ( ) ;
private static Properties propertiesFile ;
private static Properties propertiesFile ;
public static boolean init ( String resourcePathArg ) {
public synchronized s tatic boolean init ( String resourcePathArg ) {
boolean isOk = initResources ( resourcePathArg ) ;
boolean isOk = initResources ( resourcePathArg ) ;
if ( isOk ) isOk = initConfig ( ) ;
if ( isOk ) isOk = initConfig ( ) ;
if ( isOk ) isOk = initAudio ( ) ;
if ( isOk ) isOk = initAudio ( ) ;
@ -60,7 +80,6 @@ public final class Resources {
if ( isOk ) isOk = initPatPngPath ( ) ;
if ( isOk ) isOk = initPatPngPath ( ) ;
if ( isOk ) isOk = initImages ( ) ;
if ( isOk ) isOk = initImages ( ) ;
if ( isOk ) isOk = initBonkPngPath ( ) ;
if ( isOk ) isOk = initBonkPngPath ( ) ;
if ( isOk ) isOk = initStatusMessage ( ) ;
if ( isOk ) Logger . logInfo ( "Die Konfigurationen wurden erfolgreich geladen." ) ;
if ( isOk ) Logger . logInfo ( "Die Konfigurationen wurden erfolgreich geladen." ) ;
else Logger . logError ( "Die Konfiguration konnte nicht geladen werden" ) ;
else Logger . logError ( "Die Konfiguration konnte nicht geladen werden" ) ;
@ -92,18 +111,12 @@ public final class Resources {
}
}
private static boolean initAudio ( ) {
private static boolean initAudio ( ) {
return ( audioPath = verifyExists ( resourcePath + "audio/" , File : : isDirectory ) ) ! = null ;
audioPath = verifyExists ( resourcePath + "audio/" , File : : isDirectory ) ;
return audioPath ! = null ;
}
}
public static String getPathToAudioFile ( String name ) {
public static String getPathToAudioFile ( String name ) {
String filePathWithoutExtension = audioPath + name ;
if ( new File ( filePathWithoutExtension + ".opus" ) . isFile ( ) ) {
return filePathWithoutExtension + ".opus" ;
} else if ( new File ( filePathWithoutExtension + ".mp3" ) . isFile ( ) ) {
return filePathWithoutExtension + ".mp3" ;
} else {
return "" ;
}
return audioPath + name + ".opus" ;
}
}
private static boolean initTemp ( ) {
private static boolean initTemp ( ) {
@ -124,14 +137,11 @@ public final class Resources {
return false ;
return false ;
}
}
}
}
public static String getEnsuredTempPath ( ) {
if ( tempPath = = null ) {
initTemp ( ) ;
}
public static String getTempPath ( ) {
File tempDir = new File ( tempPath ) ;
File tempDir = new File ( tempPath ) ;
if ( ! tempDir . isDirectory ( ) )
if ( ! tempDir . mkdir ( ) ) throw new Error ( "Could not make Temp directory" ) ;
if ( ! tempDir . isDirectory ( ) )
if ( ! tempDir . mkdir ( ) ) return null ;
return tempPath ;
return tempPath ;
}
}
@ -164,7 +174,7 @@ public final class Resources {
patPngPath = verifyExists ( resourcePath + "pats/" , File : : isDirectory ) ;
patPngPath = verifyExists ( resourcePath + "pats/" , File : : isDirectory ) ;
return patPngPath ! = null ;
return patPngPath ! = null ;
}
}
private static boolean initBonkPngPath ( ) {
private static boolean initBonkPngPath ( ) {
bonkPngPath = verifyExists ( resourcePath + "bonks/" , File : : isDirectory ) ;
bonkPngPath = verifyExists ( resourcePath + "bonks/" , File : : isDirectory ) ;
return bonkPngPath ! = null ;
return bonkPngPath ! = null ;
@ -189,12 +199,12 @@ public final class Resources {
@SneakyThrows
@SneakyThrows
private static boolean initGreetingsAndByebyes ( ) {
private static boolean initGreetingsAndByebyes ( ) {
greetingsPath = verifyExists ( resourcePath + "greetings.txt" , File : : isFile ) ;
greetingsPath = verifyExists ( resourcePath + "greetings.txt" , File : : isFile ) ;
if ( greetingsPath = = null ) {
if ( greetingsPath = = null ) {
return false ;
return false ;
}
}
byebyesPath = verifyExists ( resourcePath + "byebyes.txt" , File : : isFile ) ;
comebacksPath = verifyExists ( resourcePath + "comebacks.txt" , File : : isFile ) ;
departsPath = verifyExists ( resourcePath + "departs.txt" , File : : isFile ) ;
byebyesPath = verifyExists ( resourcePath + "byebyes.txt" , File : : isFile ) ;
if ( propertiesFile . containsKey ( "greetings_and_byebyes_on" ) ) {
if ( propertiesFile . containsKey ( "greetings_and_byebyes_on" ) ) {
greetings_and_byebyes_on = Boolean . parseBoolean ( propertiesFile . getProperty ( "greetings_and_byebyes_on" ) ) ;
greetings_and_byebyes_on = Boolean . parseBoolean ( propertiesFile . getProperty ( "greetings_and_byebyes_on" ) ) ;
@ -203,8 +213,6 @@ public final class Resources {
greetings = Files . readAllLines ( Paths . get ( greetingsPath ) ) ;
greetings = Files . readAllLines ( Paths . get ( greetingsPath ) ) ;
byebyes = Files . readAllLines ( Paths . get ( byebyesPath ) ) ;
byebyes = Files . readAllLines ( Paths . get ( byebyesPath ) ) ;
comebacks = Files . readAllLines ( Paths . get ( comebacksPath ) ) ;
departs = Files . readAllLines ( Paths . get ( departsPath ) ) ;
return true ;
return true ;
}
}
@ -216,10 +224,6 @@ public final class Resources {
return greetings_and_byebyes_on ? String . format ( getRandomFrom ( byebyes ) , name ) : null ;
return greetings_and_byebyes_on ? String . format ( getRandomFrom ( byebyes ) , name ) : null ;
}
}
public static String getRandomAfk ( String name , boolean didComeBack ) {
return greetings_and_byebyes_on ? String . format ( getRandomFrom ( didComeBack ? comebacks : departs ) , name ) : null ;
}
private static String getRandomFrom ( List < String > pool ) {
private static String getRandomFrom ( List < String > pool ) {
return pool . get ( YoshiBot . getInstance ( ) . getRandom ( ) . nextInt ( pool . size ( ) ) ) ;
return pool . get ( YoshiBot . getInstance ( ) . getRandom ( ) . nextInt ( pool . size ( ) ) ) ;
}
}
@ -278,7 +282,7 @@ public final class Resources {
private static String verifyExists ( String filename , Function < File , Boolean > checkIsValidFile ) {
private static String verifyExists ( String filename , Function < File , Boolean > checkIsValidFile ) {
String [ ] split = filename . split ( "/" ) ;
String [ ] split = filename . split ( "/" ) ;
Logger . logDebug ( String . format ( "Versuche %s zu finden." , split [ split . length - 1 ] ) ) ;
Logger . logInfo ( String . format ( "Versuche %s zu finden." , split [ split . length - 1 ] ) ) ;
if ( checkIsValidFile . apply ( new File ( filename ) ) ) {
if ( checkIsValidFile . apply ( new File ( filename ) ) ) {
return filename ;
return filename ;
} else {
} else {
@ -287,31 +291,4 @@ public final class Resources {
}
}
}
}
private static boolean initStatusMessage ( ) {
if ( propertiesFile . containsKey ( "path_to_mcstatus" ) & &
propertiesFile . containsKey ( "status_channel" ) & &
propertiesFile . containsKey ( "path_to_status_json" ) ) {
status_channel = propertiesFile . getProperty ( "status_channel" ) ;
path_to_mcstatus = propertiesFile . getProperty ( "path_to_mcstatus" ) ;
mcserver_config_file = propertiesFile . getProperty ( "path_to_status_json" ) ;
try {
statusUpdate = Integer . parseInt ( propertiesFile . getProperty ( "status_update" ) ) ;
} catch ( NumberFormatException e ) {
return false ;
}
return true ;
} else return false ;
}
public static String getProperty ( String key ) {
return propertiesFile . getProperty ( key ) ;
}
@SneakyThrows
public synchronized static void setProperty ( String key , String value ) {
propertiesFile . setProperty ( key , value ) ;
propertiesFile . store ( new FileOutputStream ( configPath ) , "" ) ;
}
}
}