2021-08-11 20:32:42 -06:00
package net.brysonsteck.Resurrection ;
2021-08-19 21:12:47 -06:00
import org.bukkit.Bukkit ;
import org.bukkit.plugin.java.JavaPlugin ;
2021-08-19 21:58:40 -06:00
import java.io.* ;
2021-08-11 20:50:08 -06:00
import java.util.Hashtable ;
2021-08-19 21:12:47 -06:00
import java.util.Locale ;
2021-08-11 20:50:08 -06:00
2021-08-11 20:32:42 -06:00
public class ParseSettings {
2021-08-11 20:50:08 -06:00
Hashtable < String , String > settings = new Hashtable < > ( ) ;
2021-08-19 21:12:47 -06:00
// <setting that failed / does it exist? (true = the value is wrong, false = the setting is missing)>
String failedSetting ;
boolean settingsComplete ;
boolean valuesComplete ;
2021-08-11 20:50:08 -06:00
public ParseSettings ( ) {
2021-08-19 21:12:47 -06:00
try {
2021-08-19 22:40:04 -06:00
File settingsFile = new File ( " plugins/settings.resurrection " ) ;
2021-08-19 21:58:40 -06:00
if ( ! settingsFile . exists ( ) ) {
// create default settings file
FileWriter writer = new FileWriter ( settingsFile ) ;
writer . write ( " # This is the default settings file. All lines starting with a '#' are treated as comments and will be ignored. \ n " +
" # 'resurrection_time' is the amount of time in milliseconds Resurrection will force the player to wait. Default value is 8640000 milliseconds (24 hours). \ n " +
" resurrection_time=86400000 \ n " +
" # 'debug' enables debug messages in the console and players' chat as the plugin runs. The only valid values are 'true' and 'false'. Default value is false. \ n " +
" debug=false " ) ;
writer . close ( ) ;
}
2021-08-19 21:12:47 -06:00
String rawData = " " ;
2021-08-19 22:40:04 -06:00
BufferedReader reader = new BufferedReader ( new FileReader ( " plugins/settings.resurrection " ) ) ;
2021-08-19 21:12:47 -06:00
String line ;
String [ ] setting ;
while ( true ) {
line = reader . readLine ( ) ;
if ( line = = null ) {
break ;
} else if ( ! line . startsWith ( " # " ) ) {
rawData = rawData + line ;
setting = line . split ( " = " ) ;
settings . put ( setting [ 0 ] , setting [ 1 ] ) ;
}
}
if ( ! verifySettings ( ) ) {
System . out . println ( " [Resurrection] There is a syntax issue inside the Settings file: " ) ;
if ( ! settingsComplete ) {
System . out . println ( " [Resurrection] The setting \" " + failedSetting + " \" is not present in the settings file. \ n " +
" [Resurrection] Please double check the settings file to make sure the setting exists and a valid corresponding value is set. \ n " +
2021-08-19 21:58:40 -06:00
" [Resurrection] Example: \" resurrection_time=86400000 \" \ n " +
2021-08-19 21:12:47 -06:00
" [Resurrection] Example: \" debug=false \" " ) ;
} else if ( ! valuesComplete ) {
System . out . println ( " [Resurrection] The setting \" " + failedSetting + " \" contains an invalid or empty value. \ n " +
" [Resurrection] Please double check the settings file to make sure that a valid value is set for this setting. \ n " +
2021-08-19 21:58:40 -06:00
" [Resurrection] Example: \" resurrection_time=86400000 \" \ n " +
2021-08-19 21:12:47 -06:00
" [Resurrection] Example: \" debug=false \" " ) ;
}
System . out . println ( " [Resurrection] This file is crucial to Resurrection. Since the file is not complete, the plugin will now stop. " ) ;
System . exit ( 1 ) ;
}
} catch ( IOException e ) {
2021-08-19 22:40:04 -06:00
System . out . println ( " [Resurrection] There was an issue reading the Settings file: " ) ;
2021-08-19 21:12:47 -06:00
e . printStackTrace ( ) ;
2021-08-19 22:40:04 -06:00
System . out . println ( " [Resurrection] This file is crucial to Resurrection. Since the file is not complete, the plugin will now stop. " ) ;
System . exit ( 1 ) ;
2021-08-19 21:12:47 -06:00
}
}
public boolean verifySettings ( ) {
settingsComplete = false ;
valuesComplete = false ;
if ( ! settings . containsKey ( " resurrection_time " ) ) {
failedSetting = " resurrection_time " ;
return false ;
} else if ( ! settings . containsKey ( " debug " ) ) {
failedSetting = " debug " ;
return false ;
}
settingsComplete = true ;
// is resurrection_time a long?
try {
long time = Long . parseLong ( settings . get ( " resurrection_time " ) ) ;
} catch ( NumberFormatException | NullPointerException e ) {
failedSetting = " resurrection_time " ;
return false ;
}
// is debug a boolean?
if ( settings . get ( " debug " ) = = null ) {
failedSetting = " debug " ;
return false ;
}
if ( settings . get ( " debug " ) . toLowerCase ( ) . contains ( " true " ) & & settings . get ( " debug " ) . toLowerCase ( ) . contains ( " false " ) ) {
failedSetting = " debug " ;
return false ;
}
valuesComplete = true ;
2021-08-11 20:50:08 -06:00
2021-08-19 21:12:47 -06:00
return true ;
2021-08-11 20:50:08 -06:00
}
2021-08-11 20:32:42 -06:00
2021-08-11 20:50:08 -06:00
public String getSetting ( String setting ) {
return settings . get ( setting ) ;
}
2021-08-11 20:32:42 -06:00
}