2018-07-06 15:30:00 +02:00
package com.eu.habbo.core ;
import com.eu.habbo.Emulator ;
2020-05-03 01:46:07 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-07-06 15:30:00 +02:00
import java.sql.* ;
import java.util.Properties ;
2019-05-26 20:14:53 +02:00
public class TextsManager {
2018-07-08 23:32:00 +02:00
2020-05-03 01:46:07 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( TextsManager . class ) ;
2018-07-06 15:30:00 +02:00
private final Properties texts ;
2019-05-26 20:14:53 +02:00
public TextsManager ( ) {
2018-07-06 15:30:00 +02:00
long millis = System . currentTimeMillis ( ) ;
this . texts = new Properties ( ) ;
2019-05-26 20:14:53 +02:00
try {
2018-07-06 15:30:00 +02:00
this . reload ( ) ;
2020-05-03 01:46:07 +02:00
LOGGER . info ( " Texts Manager -> Loaded! ( " + ( System . currentTimeMillis ( ) - millis ) + " MS) " ) ;
2019-05-26 20:14:53 +02:00
} catch ( Exception e ) {
2018-07-06 15:30:00 +02:00
e . printStackTrace ( ) ;
}
}
2019-05-26 20:14:53 +02:00
public void reload ( ) throws Exception {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; Statement statement = connection . createStatement ( ) ; ResultSet set = statement . executeQuery ( " SELECT * FROM emulator_texts " ) ) {
while ( set . next ( ) ) {
if ( this . texts . containsKey ( set . getString ( " key " ) ) ) {
2018-07-06 15:30:00 +02:00
this . texts . setProperty ( set . getString ( " key " ) , set . getString ( " value " ) ) ;
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
this . texts . put ( set . getString ( " key " ) , set . getString ( " value " ) ) ;
}
}
2019-05-26 20:14:53 +02:00
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
public String getValue ( String key ) {
2019-03-18 02:22:00 +01:00
return this . getValue ( key , " " ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public String getValue ( String key , String defaultValue ) {
2018-07-06 15:30:00 +02:00
if ( ! this . texts . containsKey ( key ) ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Text key not found: {} " , key ) ;
2018-07-06 15:30:00 +02:00
}
return this . texts . getProperty ( key , defaultValue ) ;
}
2019-05-26 20:14:53 +02:00
public boolean getBoolean ( String key ) {
2019-03-18 02:22:00 +01:00
return this . getBoolean ( key , false ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public boolean getBoolean ( String key , Boolean defaultValue ) {
try {
2019-03-18 02:22:00 +01:00
return ( this . getValue ( key , " 0 " ) . equals ( " 1 " ) ) | | ( this . getValue ( key , " false " ) . equals ( " true " ) ) ;
2019-05-26 20:14:53 +02:00
} catch ( Exception e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
return defaultValue ;
}
2019-05-26 20:14:53 +02:00
public int getInt ( String key ) {
2019-03-18 02:22:00 +01:00
return this . getInt ( key , 0 ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public int getInt ( String key , Integer defaultValue ) {
try {
2019-03-18 02:22:00 +01:00
return Integer . parseInt ( this . getValue ( key , defaultValue . toString ( ) ) ) ;
2020-10-07 16:29:36 +02:00
} catch ( NumberFormatException e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
return defaultValue ;
}
2019-05-26 20:14:53 +02:00
public void update ( String key , String value ) {
2018-07-06 15:30:00 +02:00
this . texts . setProperty ( key , value ) ;
}
2019-05-26 20:14:53 +02:00
public void register ( String key , String value ) {
2018-07-06 15:30:00 +02:00
if ( this . texts . getProperty ( key , null ) ! = null )
return ;
2019-05-26 20:14:53 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " INSERT INTO emulator_texts VALUES (?, ?) " ) ) {
2018-07-06 15:30:00 +02:00
statement . setString ( 1 , key ) ;
statement . setString ( 2 , value ) ;
statement . execute ( ) ;
2019-05-26 20:14:53 +02:00
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
this . update ( key , value ) ;
}
}